Linux中怎么用cat命令创建文件并写入数据
252
2022-11-21
ActiveMQ Java使用
使用Java实现producer-consumer 生产者程序Producer.java public class Producer { private static final String BROKER_URL=ActiveMQConnection.DEFAULT_BROKER_URL; private static final String SUBJECT="waylau-queue"; public static final void main(String[] args)throws JMSException{ //初始化连接工厂 ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(BROKER_URL); //获得连接 Connection conn=connectionFactory.createConnection(); //启动连接 conn.start(); //创建Session,第一个参数表示会话是否在事务中执行,第二个参数设定会话的应答模式 Session session=conn.createSession(false,Session.AUTO_ACKNOWLEDGE); Destination dest=sessin.createQueue(SUBJECT); session.createTopic("TOPIC"); MessageProducer producer=session.createProducer(dest); for(int i=0; i<100; i++){ TextMessage message=session.createTextMessage("Welcome to waylua.com"+1); //发送消息 producer.send(message); } conn.close(); } } 消费者程序 consumer.java public class Consumer implements MessageListener{ private static final String BROKER_URL=ActiveMQConnection.DEFAULT_BROKER_URL; private static final String SUBJECT="waylau-queue"; public static void main(String[] args) throws JMSException{ //初始化ConnectionFactory ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(BROKER_URL); //创建MQ连接 Connection conn=connectionFactory.createConnection(); conn.start(); Session session=conn.createSession(false,Session.AUTO_ACKNOWLEDGE); Destination dest=session.createQueue(SUBJECT); MessageConsumer consumer=session.createConsumer(dest); Consumer me=new Consumer(); consumer.setMessageListener(me); } @Override public void onMessage(Message message){ TextMessage txtMessage=(TextMessage)message; } }
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~