深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
深入浅出JMS(四)--Spring和ActiveMQ整合的完整实例
已经写的很详细了。
activemq最新版5.15.3下载地址 : http://activemq.apache.org/activemq-5153-release.html
注意:如果要与spring整合,使用5.11.2版本,因为之后的版本自带spring接口,但是没有实现接口,会导致jar包冲突!!!错误如下:
在windows/linux(需安装好jdk)下,解压缩后,启动bin目录下的activemq,
在浏览器中输入:http://localhost:8161/
即可访问后台。
点击链接,输入账号密码,都是admin/admin,进入欢迎页
代码测试:
/**
* 点到点形式发送消息
*/
@Test
public void testQueueProduce() throws JMSException {
//1.创建一个连接工厂对象,需要指定服务的ip及端口
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.1.106:61616");
//2.使用工厂对象创建一个Connection对象
Connection connection = factory.createConnection();
//3.开启连接,调用Connection对象的start方法。
connection.start();
//4.创建一个Session对象
//第一个参数,是否开启事务,一般不开启事务,如果true开启事务,第二个参数无意义
//第二个参数,应答模式,自动应答或手动应答。一般自动应答
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//5.使用Session对象创建一个Destination对象,两种形式queue,topic,现在使用queue
Queue queue = session.createQueue("test-queue");
//6.使用session对象创建一个Producer对象。
MessageProducer producer = session.createProducer(queue);
//7.创建一个Message对象,可以使用TextMessage。
// TextMessage textMessage = new ActiveMQTextMessage();
// textMessage.setText("Hello Activemq !");
TextMessage textMessage = session.createTextMessage("Hello Activemq !");
//8.发送消息
producer.send(textMessage);
//9.关闭资源
producer.close();
session.close();
connection.close();
}
@Test
public void testQueueConsumer() throws JMSException, IOException {
//1.创建一个连接工厂对象,需要指定服务的ip及端口
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.1.106:61616");
//2.使用工厂对象创建一个Connection对象
Connection connection = factory.createConnection();
//3.开启连接,调用Connection对象的start方法。
connection.start();
//4.创建一个Session对象
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//5.使用Session对象创建一个Destination对象,两种形式queue,topic,现在使用queue
Queue queue = session.createQueue("test-queue");
//6.使用session对象创建一个consumer对象。
MessageConsumer consumer = session.createConsumer(queue);
//7.创建消息监听器,接受消息
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
//打印
TextMessage textMessage = (TextMessage) message;
String text;
try {
text = textMessage.getText();
System.out.println(text);
} catch (JMSException e) {
e.printStackTrace();
}
}
});
//8.等待接受消息
System.in.read();
//9.释放资源
consumer.close();
session.close();
connection.close();
}
![](https://i-blog.csdnimg.cn/blog_migrate/4791f6a5e64669daed5aee4c33ab894d.png)
@Test
public void testTopicProducer() throws JMSException {
//连接工厂
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.1.106:61616");
//创建连接
Connection connection = factory.createConnection();
//开启连接
connection.start();
//创建session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//使用session对象创建Destination对象,有queue和topic,现在使用topic
Topic topic = session.createTopic("test-topic");
//使用session创建生产者producer
MessageProducer producer = session.createProducer(topic);
//创建message
TextMessage textMessage = session.createTextMessage("hello activemq !");
//发送
producer.send(textMessage);
//关闭资源
producer.close();
session.close();
connection.close();
}
@Test
public void testTopicConsumer() throws JMSException, IOException {
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.1.106:61616");
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("test-topic");
MessageConsumer consumer = session.createConsumer(topic);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
String text;
try {
text = textMessage.getText();
System.out.println(text);
} catch (JMSException e) {
e.printStackTrace();
}
}
});
System.in.read();
consumer.close();
session.close();
connection.close();
}