ACTIVEMQ (Point-to-point & Publish/Subscribe communication mode) and (persistence mode)

Source: Internet
Author: User
Tags amq

The persistence of ACTIVEMQ

Message persistence should be a good way for reliable messaging, with message persistence, even if the sender and recipient are not both online or the message center is down after the sender sends the message.
Messages can still be sent out after the message center is restarted, and if the persistence and reliablemessaging are combined it should be a good guarantee of reliable delivery of the message.
The message persistence principle is simple, that is, after the sender sends the message, the message center first stores the message to the local data file, the memory database or the remote database, etc.
The message is then attempted to be sent to the receiver, the message is deleted from the store, and the attempt is unsuccessful. After the message center is started, first check the established storage location, if you have not sent a successful message, you need to send the message.

ACTIVEMQ Persistence mode: AMQ, kahadb, JDBC, LevelDB.

1, AMQ

ACTIVEMQ and Messages communication methods

The above mentioned JMS communication methods are divided into point-to-point communication and publish/Subscribe mode 
1) Point-to-point mode (point-to-point)
The point-to-point message is mainly based on the message queue,sender,reciever, the messages Queue storage, the Sneder sends the message, and the receive receives the message.
The specific point is that sender client sends a message queue, and receiver Cliernt receives messages from the queue and "sends messages accepted" to Quere, confirming that the message is received.
The message sending client has no time dependency on the receiving client, and the sending client can send information to the queue at any time without needing to know whether the receiving client is running
2) How to publish/subscribe (publish/subscriber Messaging)
How the Publish/subscribe method is used for multi-receive clients. As a way to publish a subscription, there may be multiple receive clients, and there is a time-dependent dependency on the receiving client and the sending client.
A receiving end can only receive information that he sends after the client is created. As a subscriber, there are two ways to receive messages, the destination receive method, and the OnMessage method that implements the message listener interface.

Examples of each are as follows:

1) Point-to-point: As in the previous article

Sender class:

Import javax.jms.Connection;  Import Javax.jms.ConnectionFactory;  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 class Sender {private static final int send_number = 2000; public static void Main (string[] args) {//ConnectionFactory: Connection Factory, JMS uses it to create a connection ConnectionFactory connection         Factory;          CONNECTION:JMS client-to-JMS provider connection Connection Connection = null;         Session: A thread that sends or receives a message session session;         Destination: The destination of the message, to whom the message is sent.         Destination Destination;          MessageProducer: Message sender MessageProducer producer;          TextMessage message;              Constructs the ConnectionFactory instance object, which is implemented here with activemq connectionfactory = new Activemqconnectionfactory (  Activemqconnection.default_user, Activemqconnection.default_password, "tcp://localhost:616         16 ");             try{//Construction Gets connection object from factory connection = Connectionfactory.createconnection ();             Start Connection.start ();             Get operation Connection session = Connection.createsession (false, Session.auto_acknowledge);             Get Session,firstqueue is a server of queue destination = Session.createqueue ("Firstqueue");             Get the message Generator "sender" producer = Session.createproducer (destination);             Set non-persistent producer.setdeliverymode (deliverymode.non_persistent);             Construct Message SendMessage (session, producer);             Session.commit ();         Connection.close ();         } catch (Exception e) {e.printstacktrace ();                }finally{if (null! = connection) {try {connection.close (); } CATCH (jmsexception e) {//TODO Auto-generatedcatch block E.printstacktrace (); }}}} public static void SendMessage (Session session, MessageProducer Produc ER) throws exception{for (int i=1; i<=send_number; i++) {TextMessage message = Session.createtextmes             Sage ("ACTIVEMQ send Message" +i);             SYSTEM.OUT.PRINTLN ("Send message: Message sent by ACTIVEMQ" +i);         Producer.send (message);  }      }  }

 receiver class:

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 Receiver {public static void main (string[] args) {//ConnectionFactory: Connection Factory, JMS uses it to create a connection          ConnectionFactory ConnectionFactory;          CONNECTION:JMS client-to-JMS provider connection Connection Connection = null;          Session: A thread that sends or receives a message session session;          Destination: The destination of the message, to whom the message is sent.          Destination Destination;          Consumer, message recipient Messageconsumer consumer; ConnectionFactory = Newactivemqconnectionfactory (Activemqconnection.default_user, Activ          Emqconnection.default_password, "tcp://localhost:61616"); try {//Get Connection object connection =cOnnectionfactory.createconnection ();              Start Connection.start ();              Get operation Connection session = Connection.createsession (false, Session.auto_acknowledge);              Create Queue Destination = Session.createqueue ("Firstqueue");                      Consumer =session.createconsumer (destination); while (true) {//sets the time at which the receiver receives the message, for testing purposes, this is set to 100s textmessagemessage = (textmessage) consumer.recei                ve (100000);                if (null! = message) {SYSTEM.OUT.PRINTLN ("received message" +message.gettext ());              }else break;          }}catch (Exception e) {e.printstacktrace ();              }finally {try {if (null! = connection) connection.close ();   } catch (Throwable ignore) {}}}}

Run to test:

ACTIVEMQ (Point-to-point & Publish/Subscribe communication mode) and (persistence mode)

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.