1. 在http://activemq.apache.org/下载ActiveMQ
2. 解压压缩包,执行bin目录下的ActiveMQ启动脚本。启动ActiveMQ后,可登录到http://localhost:8161/admin/进行一些管理操作。
3. 创建一个新的工程,导入ActiveMQ相关的jar包。
4. 消息发送:
package com.huey.dream.producer;
import java.util.Date;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
*
* @author huey
* @version 1.0
* @created 2014-12-9
*/
public class MsgSender {
public static void main(String[] args) {
// 创建连接工厂实例
ConnectionFactory connFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
Connection conn = null;
try {
// 取得连接对象实例
conn = connFactory.createConnection();
// 启动连接
conn.start();
// 创建会话对象实例
Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
// 创建消息目的地
Destination destination = session.createQueue("helloQueue");
// 创建消息生产者
MessageProducer msgProducer = session.createProducer(destination);
// 设置非持久化
msgProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
for (int i = 1; i <= 5; i++) {
Date time = new Date();
// 创建消息对象实例message = session.createMapMessage().
Message textMsg = session.createTextMessage("Hello,这是第" + i + "个消息。");
// 发送消息
msgProducer.send(textMsg);
// 会话提交
session.commit();
System.out.println("第" + i + "个消息的发送时间: " + time);
Thread.sleep(3 * 1000);
}
} catch (JMSException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
}
5. 消息接收:
package com.huey.dream.receiver;
import java.util.Date;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
*
* @author huey
* @version 1.0
* @created 2014-12-9
*/
public class MsgReceiver {
public static void main(String[] args) {
// 创建连接工厂实例
ConnectionFactory connFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
Connection conn = null;
try {
// 取得连接对象实例
conn = connFactory.createConnection();
// 启动连接
conn.start();
// 创建会话对象实例
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建消息目的地
Destination destination = session.createQueue("helloQueue");
// 创建消息消费者
MessageConsumer msgConsumer = session.createConsumer(destination);
System.out.println("等待消息...");
while(true) {
// 取消息
TextMessage textMsg = (TextMessage)msgConsumer.receive(40 * 1000);
if (textMsg != null) {
System.out.println(textMsg.getText() + " 接收时间:" + new Date());
} else {
System.out.println("没有消息了。");
break;
}
}
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
}