ActiveMQ入门:点对点模型
来源:互联网 发布:国外代购淘宝 编辑:程序博客网 时间:2024/05/29 15:36
消息生产者:
import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;public class Producer { private static final String USERNAME= ActiveMQConnection.DEFAULT_USER; //默认连接用户名 private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;//默认连接密码 private static final String BROKER_URL=ActiveMQConnection.DEFAULT_BROKER_URL;//默认连接地址 private static final int SENDNUM=10; //发送的消息数量 public static void main(String[] args) { ConnectionFactory connectionFactory; Connection connection=null; Session session; //接收或发送消息 Destination destination; //消息的目的地 MessageProducer messageProducer; //消息生产者 try { connectionFactory=new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKER_URL); connection=connectionFactory.createConnection(); //通过连接工厂获取连接 connection.start(); //启动连接 //第一个参数表示是否开启事务,第二个表示确认事务的方式 session=connection.createSession(true,Session.AUTO_ACKNOWLEDGE); //创建Session destination=session.createQueue("FirstQueue"); //创建消息队列 messageProducer=session.createProducer(destination);//创建消息生产者 sendMessage(session,messageProducer); //发送消息 session.commit(); //由于开启了事务,所以要提交事务 } catch (JMSException e) { e.printStackTrace(); } finally { if (connection!=null){ try { connection.close(); } catch (JMSException e) { e.printStackTrace(); } } } } //发送消息 private static void sendMessage(Session session,MessageProducer messageProducer){ try { for (int i = 0; i < SENDNUM; i++) { TextMessage message=session.createTextMessage("ActiveMQ 发送的消息"+i); System.out.println("发送的消息 : ActiveMQ 发送的消息"+i); messageProducer.send(message); //发送消息 } } catch (JMSException e){ e.printStackTrace(); } }}
消息消费者:
import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;public class Customer { private static final String USERNAME= ActiveMQConnection.DEFAULT_USER; //默认连接用户名 private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;//默认连接密码 private static final String BROKER_URL=ActiveMQConnection.DEFAULT_BROKER_URL;//默认连接地址 public static void main(String[] args) { ConnectionFactory connectionFactory; Connection connection=null; Session session; //接收或发送消息 Destination destination; //消息的目的地 MessageConsumer messageConsumer; //消息消费者 try { connectionFactory=new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKER_URL); connection=connectionFactory.createConnection(); connection.start(); session=connection.createSession(false,Session.AUTO_ACKNOWLEDGE); destination=session.createQueue("FirstQueue"); //创建连接的消息队列 messageConsumer=session.createConsumer(destination); /*while (true){ TextMessage textMessage= (TextMessage) messageConsumer.receive(1000); if (textMessage!=null){ System.out.println("收到的消息:"+textMessage.getText()); } else { break; } }*/ messageConsumer.setMessageListener(new Listener()); //注册消息监听 } catch (JMSException e){ e.printStackTrace(); } }}
监听器:
import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;public class Listener implements MessageListener{ //消息监听 @Override public void onMessage(Message message) { try { System.out.println("收到的消息: "+((TextMessage)message).getText()); } catch (JMSException e) { e.printStackTrace(); } }}
0 0
- ActiveMQ入门:点对点模型
- ActiveMQ 点对点消息传送模型
- (二)ActiveMQ 点对点系统模型
- jms activeMQ消息点对点通信入门实例
- ActiveMQ 实现点对点的消息模型小Demo
- ActiveMQ 点对点(p2p)
- activeMq的点对点发送
- ActiveMQ 点对点消息实现
- Activemq点对点模式
- activemq点对点模式
- ActiveMQ点对点消息实现
- ActiveMQ 点对点模式
- JMS入门(三)点对点消息传送模型示例
- JMS之activeMQ--点对点通信
- activemq 点对点demo [代码参考]
- ActiveMQ点对点及topic使用
- ActiveMQ点对点模式(PTP)
- 点对点的即时通讯模型
- 使用VS自带Dumpbin判断动态链接库.dll和应用程序.exe是32位还是64位
- opencv入门之数米粒
- 比较字符串大小
- 南阳理工ACM 题目22 素数求和问题
- 数据分析在实际项目上的应用
- ActiveMQ入门:点对点模型
- 第一篇博文来测试csdn的markdown编辑器
- 树莓派3--安装ffmpeg
- “Tensorflow+OpenCV“容器进行CNN数字识别训练
- pat 1118. Birds in Forest
- 带你深入理解传递参数
- HNOI2017滚粗记
- MongoDB系列(二):C#应用
- Spring PlaceHolder实现原理解析