ActiveMQ开发实例3
来源:互联网 发布:旋转矩阵 编辑:程序博客网 时间:2024/06/08 08:44
MessagePublisher类
package activemq.yang; import java.util.Date;import javax.jms.Connection;import javax.jms.DeliveryMode;import javax.jms.JMSException;import javax.jms.MessageProducer;import javax.jms.Session;import javax.jms.Topic;import org.apache.activemq.ActiveMQConnectionFactory; public class MessagePublisher implements Runnable { private String url; private String user; private String password; private String topicName; public MessagePublisher(String topicName, String url, String user, String password) { this.url = url; this.user = user; this.password = password; this.topicName = topicName; } @Override public void run() { try { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url); Connection connection = connectionFactory.createConnection(); connection.start(); System.out.println(Thread.currentThread().getName() + "开启"); //创建Topic //Topic topic = new ActiveMQTopic(this.topicName); Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic(this.topicName); MessageProducer sendPublisher = session.createProducer(topic); sendPublisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT); for(int i = 0; i < 5; i++){ String text = new Date() + " 现在发送是第" + (i+1) + "条消息"; sendPublisher.send(session.createTextMessage(text)); System.out.println("send message :" + text);// Thread.sleep(1000); } sendPublisher.close(); session.close(); connection.close(); // System.out.println("发布消息线程结束******************************"); } catch (JMSException e) { e.printStackTrace(); } } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getTopic() { return topicName; } }
MessageSubscriber类
package activemq.yang;import javax.jms.Connection;import javax.jms.JMSException;import javax.jms.MessageConsumer;import javax.jms.Session;import javax.jms.Topic; import org.apache.activemq.ActiveMQConnectionFactory; public class MessageSubscriber implements Runnable { private String url; private String user; private String password; private String topicName; public MessageSubscriber(String topicName, String url, String user, String password) { this.url = url; this.user = user; this.password = password; this.topicName = topicName; } @Override public void run() { try { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url); Connection connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 创建Topic Topic topic = session.createTopic(this.topicName); MessageConsumer subscriber = session.createConsumer(topic); subscriber.setMessageListener(new TextListener()); connection.start(); System.out.println(Thread.currentThread().getName() + "开启"); // subscriber.close();// session.close();// connection.close(); } catch (JMSException e) { e.printStackTrace(); } } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getTopic() { return topicName; }}
TextListener类,用来监听
package activemq.yang;import javax.jms.*;/** * Text消息监听 * * @author XXXX<br> * */public class TextListener implements MessageListener { /** * Casts the message to a TextMessage and displays its text. * * @param message * the incoming message */ public void onMessage(Message message) { TextMessage msg = null; try { if (message instanceof TextMessage) { msg = (TextMessage) message; System.out.println(Thread.currentThread().getName() +"Recevie message: " + msg.getText()); } else { System.out.println("Message of wrong type: " + message.getClass().getName()); } } catch (JMSException e) { System.out.println("JMSException in onMessage(): " + e.toString()); } catch (Throwable t) { System.out.println("Exception in onMessage():" + t.getMessage()); } }}
测试类MyActiveMQDemo
package activemq.yang;import javax.jms.JMSException;public class MyActiveMQDemo { public static void main(String[] args) throws InterruptedException, JMSException { String url = "tcp://localhost:61616"; String user = null; String password = null; String topic = "TestTopic"; new Thread(new MessagePublisher(topic,url,user,password), "Publish").start(); new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber1").start(); new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber2").start();// new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber3").start();// new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber4").start();// new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber5").start(); }}执行程序,结果如下:
Publish开启Name-Subscriber1开启Name-Subscriber2开启send message :Fri Oct 25 13:11:43 CST 2013 现在发送是第1条消息send message :Fri Oct 25 13:11:43 CST 2013 现在发送是第2条消息send message :Fri Oct 25 13:11:43 CST 2013 现在发送是第3条消息send message :Fri Oct 25 13:11:43 CST 2013 现在发送是第4条消息send message :Fri Oct 25 13:11:43 CST 2013 现在发送是第5条消息ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第1条消息ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第1条消息ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第2条消息ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第2条消息ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第3条消息ActiveMQ Session Task-2 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第3条消息ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第4条消息ActiveMQ Session Task-2 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第4条消息ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第5条消息ActiveMQ Session Task-2 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第5条消息监听显示:
- ActiveMQ开发实例3
- ActiveMQ开发实例
- ActiveMQ开发实例2
- ActiveMQ开发实例4 查看队列消息
- activeMq Windows环境部署及实例开发
- ActiveMQ 实例
- ActiveMQ实例
- activeMq 实例
- ActiveMQ实例
- ActiveMQ实例
- Activemq实例
- ActiveMQ实例
- ActiveMQ入门实例 - ActiveMQ教程
- ActiveMQ入门实例 - ActiveMQ教程
- ActiveMQ开发
- ActiveMQ入门实例
- ActiveMQ简述及实例
- ActiveMQ简述及实例
- Android emulator failed to allocate memory 8
- 二分搜索总结
- 四极管:android4.0 实现U盘挂载PC (一)
- 指针数组和数组指针
- du命令
- ActiveMQ开发实例3
- keyword in Jpa
- Java程序员面试中的多线程问题
- Google是如何逐步牢牢控制开源系统Android的?
- NSString 与C++ string字符串的互转
- iNotes 8.5.3以及9.0 Social Edition中客户化相关内容的更新
- 自定义ajax支持跨域组件封装
- office_visio_pro_2007软件以及秘钥
- ubuntu 关闭防火墙