Second, the use of Java ACTIVEMQ

Source: Internet
Author: User

This article is based on the previous article: ACTIVEMQ service installation, will show how to use ACTIVEMQ;

First, Introduction

In the previous article, we learned how to create and start a ACTIVEMQ messaging service. So how do we go about using this service? In fact, we need to learn how to write the client code.

1) ACTIVEMQ clients need to introduce dependent support:

<dependency>        <groupId>org.apache.activemq</groupId>        <artifactid>activemq-all </artifactId>        <version>5.15.3</version></dependency>

2) ACTIVEMQ is implemented according to the JMS specification, and the JMS specification defines its schema model, which we can view in the document: https://docs.oracle.com/javaee/6/tutorial/doc/bnceh.html

Based on the build model provided by the documentation, we can list the process of client code writing as follows:

2-1. Create ConnectionFactory

2-2. Create connection

2-3. Create session

2-4. Create producer or consumers

2-5. Send messages or listen to messages

In fact, we can simply understand that: connect the client to the server, then send the message or listen to the message

Second, point-to-point/publish Subscription

Before we go into the sample code, let's look at two concepts

1) Point-to-point: As the name implies, is actually a one-to-two message sent, each message can only have a consumer;

2) Publish a subscription: that is, a message published, you can have multiple consumers to spend, a one -to-many relationship;

Three, sample code

The following is the example code for the point-pair approach:

Producers

 Public classActivemqproducer {StaticString Brokerurl = "tcp://127.0.0.1:61616"; StaticString userName = "admin"; StaticString Password = "Admin";  Public Static voidMain (string[] args)throwsjmsexception {//Create a connection factoryConnectionFactory ConnectionFactory =Newactivemqconnectionfactory (userName, password, brokerurl); //Create a connection connectionConnection Connection =connectionfactory.createconnection (); //Create session, open transaction, automatic message confirmationSession session = Connection.createsession (true, Session.auto_acknowledge); //Create a message bodyMessage message = Session.createtextmessage ("Hello JMS"); //Create a queueDestination Destination = Session.createqueue ("queue1"); //Create a producerMessageProducer MessageProducer =session.createproducer (destination); //Send Messagemessageproducer.send (message); //Transaction CommitSession.commit (); //Close ConnectionConnection.close (); System.out.println ("Send Complete ..."); }}

Consumers

 Public classActivemqreceiver {StaticString Brokerurl = "tcp://127.0.0.1:61616"; StaticString userName = "admin"; StaticString Password = "Admin";Static voidCreatereceiver ()throwsjmsexception {//Create a connection factoryConnectionFactory ConnectionFactory =Newactivemqconnectionfactory (userName, password, brokerurl); //Create a connection connectionConnection Connection =connectionfactory.createconnection ();        Connection.start (); //Create session, open transaction, automatic message confirmationSession session = Connection.createsession (false, Session.auto_acknowledge); //Create a queueDestination Destination = Session.createqueue ("queue1"); //Create a producerMessageconsumer Messageconsumer =Session.createconsumer (destination); Messageconsumer.setmessagelistener (NewReceivelistener ()); //Accept Message//Message message = messageconsumer.receive (1000); //Close Connection//connection.close ();SYSTEM.OUT.PRINTLN ("Receive listening ..."); }     Public Static voidMain (string[] args) {Try{createreceiver ();        Createreceiver (); } Catch(jmsexception e) {e.printstacktrace (); }    }}classReceivelistenerImplementsmessagelistener{@Override Public voidonMessage (Message message) {Try{textmessage TextMessage=(textmessage) message;        System.out.println (Textmessage.gettext ()); } Catch(jmsexception e) {e.printstacktrace (); }    }}

Note: Here is the way to implement MessageListener to listen to messages , such as the Code comment section, we can also use the call method to receive , but the recommended way to use the listener

Reference Documentation:

JMS Document: Https://docs.oracle.com/javaee/6/tutorial/doc/bncdx.html#bnceb

Api:http://tool.oschina.net/apidocs/apidoc?api=javaee6 of Java EE JMS

ACTIVEMQ Document: http://activemq.apache.org/getting-started.html

ACTIVEMQ's API Documentation: http://activemq.apache.org/maven/apidocs/index.html

Second, the use of Java ACTIVEMQ

Related Article

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.