ActiveMQ Introductory Example

Source: Internet
Author: User

A: Create a new MAVEN project and join the following dependencies

<dependency><groupid>org.apache.activemq</groupid><artifactid>activemq-all</ Artifactid><version>5.11.1</version></dependency>

Two:

Server

Package Com.lala.mq;import Java.util.concurrent.timeunit;import Org.apache.activemq.broker.brokerservice;public Class Server {public static void main (string[] args) throws Exception{brokerservice broker = new Brokerservice (); broker.ad Dconnector ("tcp://127.0.0.1:55551"); Broker.setusejmx (false); Broker.start (); TimeUnit.SECONDS.sleep (9999);}}


Producer

Package Com.lala.mq;import Javax.jms.connection;import Javax.jms.connectionfactory;import Javax.jms.messageproducer;import Javax.jms.queue;import Javax.jms.session;import Javax.jms.TextMessage;import Org.apache.activemq.activemqconnectionfactory;public class Myproducer{public static void Main (string[] args) throws Exception{connectionfactory CF = new Activemqconnectionfactory ("tcp://127.0.0.1:55551"); Connection conn = Cf.createconnection (); Conn.start (); Session session = Conn.createsession (false, Session.auto_acknowledge); Queue queue = Session.createqueue ("CIB"); MessageProducer producer = Session.createproducer (queue); TextMessage TM = null;for (int i=1;i<=5;i++) {TM = Session.createtextmessage ("This is test message and MsgId =" + i);p ro Ducer.send (tm);} Session.close (); Conn.close ();}}


Consumer

Package Com.lala.mq;import Javax.jms.connection;import Javax.jms.connectionfactory;import Javax.jms.messageconsumer;import Javax.jms.queue;import Javax.jms.session;import Javax.jms.TextMessage;import Org.apache.activemq.activemqconnectionfactory;public class Myconsumer {public static void main (string[] args) throws Exception{connectionfactory CF = new Activemqconnectionfactory ("tcp://127.0.0.1:55551"); Connection conn = Cf.createconnection (); Conn.start (); Session session = Conn.createsession (false, Session.auto_acknowledge); Queue queue = Session.createqueue ("CIB"); Messageconsumer consumer = session.createconsumer (queue), Consumer.setmessagelistener ((message), {TextMessage TM = (TextMessage) message;try {System.out.println ("received message:" + tm.gettext ());} catch (Exception e) {e.printstacktrace ();}});}}


Three: Run

Run First: Server

Re-run: Consumer

Last run: Producer

You can see the output


At last

Note: The above practices do not require prior download and installation of the ACTIVEMQ service


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ActiveMQ Introductory Example

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.