Simple Example of ActiveMQ receiving and sending messages

Source: Internet
Author: User

ActiveMQ is a simple instance of ActiveMQ-ActiveMQ receiving + message sending JMS message framework-ActiveMQ recently used ActiveMQ because of the company's project requirements. There are not many online examples, and some of them are incomplete. After several days of study, I found a suitable solution on the Internet. My IT resource library http://itlib.tk/producertool.javais used for message sending: java code package homework; 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 clas S ProducerTool {private String user = ActiveMQConnection. DEFAULT_USER; private String password = ActiveMQConnection. DEFAULT_PASSWORD; private String url = ActiveMQConnection. DEFAULT_BROKER_URL; private String subject = "TOOL. DEFAULT "; private Destination destination = null; private Connection connection = null; private Session session = null; private MessageProducer producer = null; // initialize privat E 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 (DeliveryMod E. NON_PERSISTENT);} // send the 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! ");} // Close the connection 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 () ;}} ConsumerTool. java is used to receive messages. I use a message listening mechanism and need to implement the MessageListener interface. This interface has an onMessage method, when a message is received, this function is automatically called to process the message. Java code package homework; import javax. jms. connection; import javax. jms. destination; 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 implemen Ts MessageListener {private String user = ActiveMQConnection. DEFAULT_USER; private String password = ActiveMQConnection. DEFAULT_PASSWORD; private String url = ActiveMQConnection. DEFAULT_BROKER_URL; private String subject = "TOOL. DEFAULT "; private Destination destination = null; private Connection connection = null; private Session session = null; private MessageConsumer consumer = null; // initialize pr Ivate 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); consumer = session. createConsumer (destination);} // consume the message public void consumeMes Sage () throws JMSException, Exception {initialize (); connection. start (); System. out. println ("Consumer:-> Begin listening... "); // start listening for consumer. setMessageListener (this); // Message message = consumer. receive ();} // close the connection 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 () ;}// Message processing function public void onMessage (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 ();}} If You Want To actively accept the message, instead of using the message listener, set consumer. setMessageListener (this) is changed to Message message = consumer. receive (), manually call the receive method of MessageConsumer. Below is the Test class. java: java code package homework; import javax. jms. JMSException; public class Test {/*** @ param args */public static void main (String [] args) throws JMSException, exception {// TODO Auto-generated method stub ConsumerTool consumer = new ConsumerTool (); ProducerTool producer = new ProducerTool (); // start to listen to consumer. consumeMessage (); // The message Thread is sent 500 milliseconds later. sleep (500); producer. produceMess Age ("Hello, world! "); Producer. close (); // stop receiving message Thread after 500 milliseconds of delay. sleep (500); consumer. close () ;}} a simple ActiveMQ instance-ActiveMQ receives and sends messages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.