JMS Middleware -- activemq

Source: Internet
Author: User

Message Communication between Java systems is mostly based on rmi rpc and JMS rpc. Both of these two message transmission modes can communicate with each other, but in my opinion, there is a big difference between the two. First, RMI is synchronous transmission, while JMS is asynchronous transmission. The Use Cases of the two are also very different. In the system integration platform project, I have the opportunity to gain a deeper understanding of these two messaging mechanisms. Data transmission between the basic system and the examination system we use to publish EJB as WebService, and then connect the client and WebService through ESB, in essence, this method is the mutual call between ejbs. It is an Rmi message communication method, the most obvious feature is that after we execute a remote call, we have to wait until all the data is transmitted by WebService before relevant operations can be performed (of course, this method must also be selected based on the business, because all the subsequent operations are based on returned data), while JMS provides message service through a third-party broker, which is equivalent to a post office, when a client sends a message, it only sends the message content to the broker and notifies the broker to send the message to the address. The rest is done by the broker. The client does not have to wait and can perform any other operations, the main responsibility of the broker is to ensure message delivery. We can only listen to the broker to receive messages. After comparison, I believe that you have a preliminary understanding of the two methods. Next we will focus on activemq, a message-oriented middleware based on JMS.

1. First, we need to build the activemq server, which is equivalent to the broker we mentioned earlier. The building process is very simple and can be found on the official website.

2. Create a queue and then create the sender Test Code. The Code is as follows:

Public static void main (string [] ARGs) {// connectionfactory: Connection factory, which is used by JMS to create a connection connectionfactory; // connection: connection connection from the JMS client to the JMS provider = NULL; // session: the session of the thread that sends or receives the message; // destination: the Message destination; to which the message is sent. destination destination; // messageproducer: messageproducer; // textmessage; // construct the connectionfactory instance object. activemq is used here. Jar connectionfactory = new activemqconnectionfactory (activemqconnection. default_user, activemqconnection. default_password, "TCP: // localhost: 61616"); try {// construct the connection object connection = connectionfactory from the factory. createconnection (); // start connection. start (); // get the operation connection session = connection. createsession (Boolean. true, session. auto_acknowledge); // obtains the xingbo parameter value of the session. xu-queue is a server's queue, which must be configured on the activemq Console Set // Destination = session. createqueue ("firstqueue"); // Destination = session. createtopic ("secondtopic"); Destination = session. createqueue ("myqueue"); // The producer [Sender] producer = session. createproducer (destination); // The setting is not persistent. Learn here and decide the producer according to the project. setdeliverymode (deliverymode. non_persistent); // construct a message. The message is written to an end here. The project is a parameter, or the method used to obtain sendmessage (Session, producer); Session. commit ();} catch (exception e) {e. PRI Ntstacktrace ();} finally {try {If (null! = Connection) connection. close () ;}catch (throwable ignore) {}} public static void sendmessage (session, messageproducer producer) throws exception {textmessage message = session. createtextmessage ("Hello Changshou"); // sends the message to the destination system. out. println ("Send message:" + message. gettext (); producer. send (Message );}


Execute this method. The output result of the sender is as follows:

3. Create the acceptor Test Code as follows:

Package COM. xuwei. activemq; import javax. JMS. connection; import javax. JMS. connectionfactory; import javax. JMS. destination; import javax. JMS. messageconsumer; import javax. JMS. session; import javax. JMS. textmessage; import Org. apache. activemq. activemqconnection; import Org. apache. activemq. activemqconnectionfactory; public class er {public static void main (string [] ARGs) {// connectionfactory: Connection factory, JMS Use it to create a connection connectionfactory; // connection: Connection connection from the JMS client to the JMS provider = NULL; // session: A session for sending or receiving messages; // destination: the destination of the message. destination destination; // consumer, Message Receiver messageconsumer consumer; connectionfactory = new activemqconnectionfactory (activemqconnection. default_user, activemqconnection. default_password, "TCP: // localhost: 61616"); tr Y {// construct the connection object connection = connectionfactory from the factory. createconnection (); // start connection. start (); // get the operation connection session = connection. createsession (Boolean. false, session. auto_acknowledge); // obtains the xingbo parameter value of the session. xu-queue is a server's queue, which must be configured on the activemq console // Destination = session. createqueue ("firstqueue"); // Destination = session. createtopic ("secondtopic"); Destination = session. createqueue ("myqueue"); c Onsumer = session. createconsumer (destination); While (true) {// set the time for the receiver to receive the message. To facilitate the test, who set this parameter to 100 s textmessage message = (textmessage) consumer. receive (100000); If (null! = Message) {system. out. println ("Receive message" + message. gettext () ;}else {break ;}} catch (exception e) {e. printstacktrace ();} finally {try {If (null! = Connection) connection. Close () ;}catch (throwable ignore ){}}}}

The output result of the acceptor is as follows:

The results show that there is no problem between sending and receiving messages, but here we should be clear about the difference between the message queue and the message topic. When we create a message queue, when a message is sent to a message queue, there may be multiple recipients of the message, when the message is received by any one of them, the message is consumed and no one else will receive the message, when we create a topic, the message is also sent to this topic, and all the recipients will receive the message and make their own Processing Based on the message, in principle, it is similar to QQ's single-person message transmission and discussion groups. The verification code for these two differences is also very simple. As long as the sender creates a queue and then creates two receivers, execute the sender's sender method and receiver's receiving method respectively. If only one receiver can receive the message, the verification is successful. The verification of the topic is the same. The sender creates a topic, then create two recipients and execute their respective methods. If both recipients receive messages, the verification of the topic is successful. If you are interested, you can try it yourself.

JMS Middleware -- activemq

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.