ActiveMQ消息队列之java消息生产与消费
2017-08-31 21:31
447 查看
话题生产者:
话题生产池:
话题消费者:
话题消费池:
队列生产者:
队列生产池:
队列消费者:
队列消费池:
package cn.itcast_03_mq.topic; import java.util.Random; import javax.jms.JMSException; public class ProducerTest { /** * @param args */ public static void main(String[] args) throws JMSException, Exception { ProducerTool producer = new ProducerTool(); Random random = new Random(); for(int i=0;i<20;i++){ Thread.sleep(random.nextInt(10)*1000); producer.produceMessage("Hello, world!--"+i); producer.close(); } } }
话题生产池:
package cn.itcast_03_mq.topic; import javax.jms.Connection; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; public class ProducerTool { private String user = ActiveMQConnection.DEFAULT_USER; private String password = ActiveMQConnection.DEFAULT_PASSWORD; private String url = ActiveMQConnection.DEFAULT_BROKER_URL; private String subject = "mytopic"; private Destination destination = null; private Connection connection = null; private Session session = null; private MessageProducer producer = null; // 初始化 private void initialize() throws JMSException, Exception { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( user, password, url); connection = connectionFactory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); destination = session.createTopic(subject); producer = session.createProducer(destination); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); } // 发送消息 public void produceMessage(String message) throws JMSException, Exception { initialize(); TextMessage msg = session.createTextMessage(message); connection.start(); System.out.println("Producer:->Sending message: " + message); producer.send(msg); System.out.println("Producer:->Message sent complete!"); } // 关闭连接 public void close() throws JMSException { System.out.println("Producer:->Closing connection"); if (producer != null) producer.close(); if (session != null) session.close(); if (connection != null) connection.close(); } }
话题消费者:
package cn.itcast_03_mq.topic; import javax.jms.JMSException; public class ConsumerTest implements Runnable { static Thread t1 = null; /** * @param args * @throws InterruptedException * @throws InterruptedException * @throws JMSException * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { t1 = new Thread(new ConsumerTest()); t1.setDaemon(false); t1.start(); /** * 如果发生异常,则重启consumer */ /*while (true) { System.out.println(t1.isAlive()); if (!t1.isAlive()) { t1 = new Thread(new ConsumerTest()); t1.start(); System.out.println("重新启动"); } Thread.sleep(5000); }*/ // 延时500毫秒之后停止接受消息 // Thread.sleep(500); // consumer.close(); } public void run() { try { ConsumerTool consumer = new ConsumerTool(); consumer.consumeMessage(); while (ConsumerTool.isconnection) { } } catch (Exception e) { } } }
话题消费池:
package cn.itcast_03_mq.topic; import javax.jms.Connection; import javax.jms.Destination; import 4000 javax.jms.ExceptionListener; import javax.jms.JMSException; import javax.jms.MessageConsumer; import javax.jms.Session; import javax.jms.MessageListener; import javax.jms.Message; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; public class ConsumerTool implements MessageListener,ExceptionListener { private String user = ActiveMQConnection.DEFAULT_USER; private String password = ActiveMQConnection.DEFAULT_PASSWORD; private String url =ActiveMQConnection.DEFAULT_BROKER_URL; private String subject = "mytopic"; private Destination destination = null; private Connection connection = null; private Session session = null; private MessageConsumer consumer = null; public static Boolean isconnection=false; // 初始化 private void initialize() throws JMSException, Exception { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( user, password, url); connection = connectionFactory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); destination = session.createTopic(subject); consumer = session.createConsumer(destination); } // 消费消息 public void consumeMessage() throws JMSException, Exception { initialize(); connection.start(); consumer.setMessageListener(this); connection.setExceptionListener(this); isconnection=true; System.out.println("Consumer:->Begin listening..."); // 开始监听 // Message message = consumer.receive(); } // 关闭连接 public void close() throws JMSException { System.out.println("Consumer:->Closing connection"); if (consumer != null) consumer.close(); if (session != null) session.close(); if (connection != null) connection.close(); } // 消息处理函数 public void onMessage(Message message) { try { if (message instanceof TextMessage) { TextMessage txtMsg = (TextMessage) message; String msg = txtMsg.getText(); System.out.println("Consumer:->Received: " + msg); } else { System.out.println("Consumer:->Received: " + message); } } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void onException(JMSException arg0) { isconnection=false; } }
队列生产者:
package cn.itcast_03_mq.queue; import javax.jms.JMSException; public class ProducerTest { /** * @param args * @throws Exception * @throws JMSException */ public static void main(String[] args) throws JMSException, Exception{ ProducerTool producer = new ProducerTool(); producer.produceMessage("Hello, world!"); producer.close(); } }
队列生产池:
package cn.itcast_03_mq.queue; import javax.jms.Connection; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; public class ProducerTool { private String user = ActiveMQConnection.DEFAULT_USER; private String password = ActiveMQConnection.DEFAULT_PASSWORD; private String url = ActiveMQConnection.DEFAULT_BROKER_URL; private String subject = "myqueue"; private Destination destination = null; private Connection connection = null; private Session session = null; private MessageProducer producer = null; // 初始化 private void initialize() throws JMSException, Exception { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( user, password, url); connection = connectionFactory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); destination = session.createQueue(subject); producer = session.createProducer(destination); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); } // 发送消息 public void produceMessage(String message) throws JMSException, Exception { initialize(); TextMessage msg = session.createTextMessage(message); connection.start(); System.out.println("Producer:->Sending message: " + message); producer.send(msg); System.out.println("Producer:->Message sent complete!"); } // 关闭连接 public void close() throws JMSException { System.out.println("Producer:->Closing connection"); if (producer != null) producer.close(); if (session != null) session.close(); if (connection != null) connection.close(); } }
队列消费者:
package cn.itcast_03_mq.queue; public class ConsumerTest implements Runnable { static Thread t1 = null; public static void main(String[] args) throws InterruptedException { t1 = new Thread(new ConsumerTest()); t1.start(); while (true) { System.out.println(t1.isAlive()); if (!t1.isAlive()) { t1 = new Thread(new ConsumerTest()); t1.start(); System.out.println("重新启动"); } Thread.sleep(5000); } // 延时500毫秒之后停止接受消息 // Thread.sleep(500); // consumer.close(); } public void run() { try { ConsumerTool consumer = new ConsumerTool(); consumer.consumeMessage(); while (ConsumerTool.isconnection) { //System.out.println(123); } } catch (Exception e) { } } }
队列消费池:
package cn.itcast_03_mq.queue; import javax.jms.Connection; import javax.jms.Destination; import javax.jms.ExceptionListener; import javax.jms.JMSException; import javax.jms.MessageConsumer; import javax.jms.Session; import javax.jms.MessageListener; import javax.jms.Message; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; public class ConsumerTool implements MessageListener,ExceptionListener { private String user = ActiveMQConnection.DEFAULT_USER; private String password = ActiveMQConnection.DEFAULT_PASSWORD; private String url = ActiveMQConnection.DEFAULT_BROKER_URL; private String subject = "myqueue"; private Destination destination = null; private Connection connection = null; private Session session = null; private MessageConsumer consumer = null; private ActiveMQConnectionFactory connectionFactory=null; public static Boolean isconnection=false; // 初始化 private void initialize() throws JMSException { connectionFactory= new ActiveMQConnectionFactory( user, password, url); connection = connectionFactory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); destination = session.createQueue(subject); consumer = session.createConsumer(destination); } // 消费消息 public void consumeMessage() throws JMSException { initialize(); connection.start(); consumer.setMessageListener(this); connection.setExceptionListener(this); System.out.println("Consumer:->Begin listening..."); isconnection=true; // 开始监听 Message message = consumer.receive(); System.out.println(message.getJMSMessageID()); } // 关闭连接 public void close() throws JMSException { System.out.println("Consumer:->Closing connection"); if (consumer != null) consumer.close(); if (session != null) session.close(); if (connection != null) connection.close(); } // 消息处理函数 public void onMessage(Message message) { try { if (message instanceof TextMessage) { TextMessage txtMsg = (TextMessage) message; String msg = txtMsg.getText(); System.out.println("Consumer:->Received: " + msg); } else { System.out.println("Consumer:->Received: " + message); } } catch (JMSException e) { e.printStackTrace(); } } public void onException(JMSException arg0){ isconnection=false; } }
相关文章推荐
- Java消息队列--ActiveMq 实战
- ActiveMQ使用线程池实现消息的生产与消费 .
- java消息队列ActiveMQ之Producter
- activemq消息的产生与消费 Java
- Activemq 消息发送、接收java代码实现队列模式
- 初次尝试ActiveMQ,实现简单的消息生产和消息消费
- Java消息队列--ActiveMq 实战
- Java消息队列--ActiveMQ 实战
- 初次尝试ActiveMQ,实现简单的消息生产和消息消费
- Java消息队列--ActiveMq 实战
- Java activemq消息队列入门学习
- java消息队列ActiveMQ的简单使用
- Java消息队列总结只需一篇解决ActiveMQ、RabbitMQ、ZeroMQ、Kafka
- ActiveMQ五种消息类型生产消费
- Kafka消息生产消费的一个java小案例(伪分布)
- java代码主动消费消息ActiveMQ消息{StringMessage}
- Java消息队列-Spring整合ActiveMq
- ActiveMQ使用线程池实现消息的生产与消费
- 使用Lock与ReentrantLock模拟消息队列阻塞,生产与消费问题模拟
- Java消息队列--ActiveMq 实战