1:
queue and topic two kinds of magic. queue is "the queue of all the listener spending a total of 1"; topic is "that all subscribers will consume 1"
//连接配置
private String userName = "admin"; private String password = "admin"; private String url = "tcp://192.168.8.65:61616"; private String queueName = "Qtest_queue"; private String topicName = "Ttest_topic";
1.1 queue message queue
1.1.1 Producers
/ ** * Queue Mode: Manufacturer * / @Test public void queueProduce () throws a JMSException { // # create the connection factory. 1 ActiveMQConnectionFactory Factory = new new ActiveMQConnectionFactory (the userName, password, URL); // # 2 obtained from the plant connected connectino Connection = Connection factory.createConnection (); // # 3 start access connection.start (); // # 4 create a session the session the session the session = connection.createSession ( false , Session.AUTO_ACKNOWLEDGE); // Session # 5 is created by the session destination DISTINCT (Queue / Topic) Queue Queue = session.createQueue (queueName); // # session session to create producer. 6 Produce the MessageProducer Producer = session.createProducer (Queue); // # downtime. 7 save the message producer.setDeliveryMode (DeliveryMode.PERSISTENT); // #. 8 production message, sent to the queue for ( int I =. 1; I <= 10; I ++ ) { String MSG = "first" + i + "th MSG" ; message TextMessage = session.createTextMessage (MSG); // #. 9 sends a message to the MQ producer.send (message); } // # 10 close the resource producer.close(); session.close(); connection.close(); }
1.1.2 Consumers
1.1.2.1 receive mode
/ ** * Queue mode: Consumers * Receive mode: Only once consumption (Code requires the while loop query) * / @Test public void queueReceiveConsumer () throws JMSException { // # 1 creates a connection factory ActiveMQConnectionFactory = Factory's new new ActiveMQConnectionFactory ( the userName, password, URL); // # 2 obtained from the plant connected connectino connection connection = factory.createConnection (); // # access start. 3 connection.start (); // #. 4 to create a session the session the session connection.createSession the session = ( false, Session.AUTO_ACKNOWLEDGE); // # 5 create a destination distinct (Queue / Topic) by a session session Queue Queue = session.createQueue (queueName); // # 6 session session to create a consumer Produce MessageConsumer Consumer = session.createConsumer (Queue ); // receive synchronous blocking the way: not receiving messages have been waiting for the while ( to true ) { the message the message = consumer.receive (); // no parameters will be waiting IF (the message =! null && the message instanceof TextMessage) { TextMessage textMessage = (TextMessage) Message; System.out.println ("Consumed ::" + textMessage); } the else { BREAK ; } } // # 10 closed resource consumer.close (); Session.close (); Connection.close (); }
1.1.2.2listen listening mode
/ ** * Queue Mode: Consumer * the Listen mode * / @Test public void queueListenConsumer () throws a JMSException { // # create the connection factory. 1 ActiveMQConnectionFactory Factory = new new ActiveMQConnectionFactory (the userName, password, URL); // # 2 from the plant GETTING cONNECTED connectino connection connection = factory.createConnection (); // # 3 start access connection.start (); // # 4 create a session the session the session the session = connection.createSession ( false , Session.AUTO_ACKNOWLEDGE); // # 5 created by a session session destination DISTINCT (Queue / Topic) Queue Queue = session.createQueue (queueName); // # 6 session session to create a consumer Produce MessageConsumer Consumer = session.createConsumer (Queue); // listen for ways to get message consumer.setMessageListener (Message -> { IF (Message =! null && Message the instanceof TextMessage) { TextMessage textMessage = (TextMessage) Message; System.out.println ( "consume ::" + textMessage); } }); the try { System.in.read (); // ensure console immortal: not avoid listening to the message, when closing the console } the catch (IOException E) { e.printStackTrace (); } // # 10 close the resource consumer.close ( ); Session.close (); Connection.close (); }
1.2 topic mode
1.2.1 Producers
@Test public void topicProducer () throws a JMSException { // # create the connection factory. 1 ActiveMQConnectionFactory Factory = new new ActiveMQConnectionFactory (the userName, password, URL); // # 2 from the factory to obtain a connection connectino Connection Connection = factory.createConnection (); // # 3 start access connection.start (); // # 4 create a session the session the session the session = connection.createSession ( false , Session.AUTO_ACKNOWLEDGE); // # 5 create a destination distinct (Queue / Topic) by a session the session Topic Topic =session.createTopic (topicName); // # session session to create producer. 6 Produce the MessageProducer Producer = session.createProducer (Topic); // # saving downtime. 7 message producer.setDeliveryMode (DeliveryMode.PERSISTENT); // # message production. 8 , sent to the queue for ( int I =. 1; I <= 10; I ++ ) { String MSG = "first" + i + "th MSG" ; TextMessage Message = session.createTextMessage (MSG); message.setStringProperty ( "Top" , "VIP"); // set the message properties, strengthen the identification of the message (Comsumer some messages can be screened, focusing on) // # 9 send a message to the MQ producer.send(message); } // #10 关闭资源 producer.close(); session.close(); connection.close(); }
1.2.2 Consumers
1.2.2.1 receive mode
// plurality of consumer testing mode topic
@Test public void topicReceiveConsumer1 () throws a JMSException { // # create the connection factory. 1 ActiveMQConnectionFactory Factory = new new ActiveMQConnectionFactory (the userName, password, URL); // # 2 obtained from the plant connected connectino Connection Connection = factory.createConnection (); // # 3 start access connection.start (); // # 4 create session session the session session = connection.createSession ( false , Session.AUTO_ACKNOWLEDGE); // # 5 created by the destination session session distinct (Queue / Topic) Topic topic = session.createTopic(topicName); // #6 会话session创建生产者Produce MessageConsumer consumer = session.createConsumer(topic); while (true) { Message receive = consumer.receive(); if (receive != null && receive instanceof TextMessage) { TextMessage message = (TextMessage) receive; System.out.println("topicConsumer1收到topic消息" + message.getText()); } } // #10 关闭资源 // consumer.close(); // session.close(); // connection.close(); }
1.2.2.2 listen listening mode
@Test public void topicListenConsumer2 () throws a JMSException, IOException { // # create the connection factory. 1 ActiveMQConnectionFactory Factory = new new ActiveMQConnectionFactory (the userName, password, URL); // # 2 obtained from the plant connected connectino Connection Connection = factory.createConnection (); // # 3 start access connection.start (); // # 4 create a session the session the session the session = connection.createSession ( false , Session.AUTO_ACKNOWLEDGE); // # 5 create a destination distinct (Queue / Topic) by a session the session Topic topic = session.createTopic(topicName); // #6 会话session创建生产者Produce MessageConsumer consumer = session.createConsumer(topic); consumer.setMessageListener(message -> { if (message != null && message instanceof TextMessage) { TextMessage msg = (TextMessage) message; try { System.out.println("topicListenConsumer2收到topic消息" + msg.getText()); } catch (JMSException e) { e.printStackTrace(); } } }); System.in.read(); // #10 关闭资源 // consumer.close(); // session.close(); // connection.close(); }