JMS_使用ActiveMQ实现消息的发送和接收
来源:互联网 发布:纸箱计算公式软件 编辑:程序博客网 时间:2024/04/26 10:22
一、说明
示例中使用activeMQ-all的jar包实现jms消息的发送和接收,发送端和接收端不依赖与服务器,如果想了解使用服务器配置的方式请参照之前的jsm和MDB的文章。
注意topic消息和queue消息的区别。
二、配置
1、ActiveMQ配置
activemq-web-console的配置不再重复。
2、Maven配置
<dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>5.5.0</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-nop</artifactId><version>1.6.2</version></dependency>
三、发送方和接收方的代码
1、发送方
public class AMQProducer {public static void main(String[] args) throws Exception {MessageProducer producer = null;Connection connection = null;ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");connection = factory.createConnection();ActiveMQSession session = (ActiveMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);Queue queue = session.createQueue("myQueue2");producer = session.createProducer(queue);// Topic topic = session.createTopic("myTopic2");// producer = session.createProducer(topic);TextMessage textMessage = session.createTextMessage();textMessage.setText("Hello AMQ2");producer.send(textMessage);producer.close();connection.close();}}
2、接收方
public class AMQConsumer {public static void main(String[] args) throws Exception {ActiveMQConnectionFactory connFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");Connection conn = connFactory.createConnection();conn.start();Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);Queue queue = session.createQueue("myQueue2");MessageConsumer consumer = session.createConsumer(queue);// Topic topic = session.createTopic("myTopic2");// MessageConsumer consumer = session.createConsumer(topic);TextMessage message = (TextMessage) consumer.receive();String msg = message.getText();System.out.println(msg);consumer.close();conn.close();}}
- JMS_使用ActiveMQ实现消息的发送和接收
- ActiveMQ 消息的发送和接收
- 使用ActiveMQ 发送/接收消息
- ActiveMQ消息发送和接收
- ActiveMQ消息发送和接收
- ActiveMQ消息发送和接收
- activemq使用JMS发送消息和接收消息
- ActiveMQ 发送消息和接收消息 Demo
- ActiveMQ(一):ActiveMQ入门案例 发送消息和接收消息
- JAVA ActiveMQ消息发送和接收
- ActiveMQ 整合 Spring 发送和接收消息
- java activeMQ消息的发送与接收
- Activemq消息的发送与接收
- ActiveMQ使用笔记(三)ActiveMQ消息发送与接收
- ActiveMQ使用笔记(三)ActiveMQ消息发送与接收
- 使用ActiveMQ 发送/接收消息(没用到框架)
- jms+spring+activemq配置(发送和接收消息)
- jms+spring+activemq配置(发送和接收消息)
- checkbox全选和反选最简洁的代码
- PHP学习#Yii扩展的使用
- powerdesigner工具面板没了
- 开博第一天
- hdu 1251 统计难题 (字典树)
- JMS_使用ActiveMQ实现消息的发送和接收
- Flex 文字滚动
- Eclipse快捷键大全
- 当鼠标滑过文本框自动选中输入框内容JS代码
- hdu 1003 hdu1231 简单dp最大子串和
- JS常用正则表达式验证
- iOS开发-数据储存Core Data(一)
- 从大数据中挖掘什么?
- 我的sublime text 2 linux下安装