I used to write their own online mall project of ActiveMQ, although compared to compared to RabbitMQ, kafka, RocketMQ etc., ActiveMQ performance may not be the best choice, but the message queue is actually not very principle difference here to learned about the message knowledge of the queue to sum, combined with their own interview about this problem encountered do a finishing move for the later autumn to prepare to find a job. This is a mainly introduce JMS, ActiveMQ installation and common interfaces both queue mode, how to integrate Spring project, summarizing the interview.
Java Message Service, Java Message Service Application Program Interface, API is a Java platform on message-oriented middleware (MOM) for transmitting messages between two programs, or distributed system, asynchronous communication, JMS is a specific platform-independent API, MOM vast majority of providers offer support for JMS. This is a more detailed definition of JMS, and more intuitive to say, JMS is a set of messaging services API, that is only JMS interface, the implementation class to do various manufacturers MOM.
JMS usage scenarios, applications deployed in Beijing A, B application deployed in Shanghai, whenever an event is triggered after A, B to get some information about A, B may also have wanted to get a lot of information A. In this case, Java provides the best solution -JMS. The same applies to JMS-based application events, such as chat service, he needed a release event mechanism to send message to all clients connected to the server. Different JMS and RMI, the recipient does not need online. That is, the server has finished sending a message that this event will have nothing to do with him.
JMS advantages:
Asynchronous, JMS inherently asynchronous, when the client gets the message and does not need to take the initiative to send a request, a message is automatically sent to the client available.
Reliable, JMS ensure the message is delivered only once. We have encountered problems create duplicate messages, JMS can help you avoid this problem, but not eliminate, the need MOM manufacturers to do more comprehensive mechanism to improve.
JMS common concepts:
Provider / MessageProvider: producer
Consumer / MessageConsumer: Consumer
PTP: Point To Point, point to point communication message model
Pub / Sub: Publish / Subscribe, publish-subscribe messaging model
Queue: Queue, one target type, and binding PTP
Topic: topic, type one of the goals, and Pub / Sub combination
ConnectionFactory: connection factories, JMS create a connection with it
Connnection: JMS Client connected to the JMS Provider
Destination: message destination, created by Session
Session: Session, created by Connection, essentially sending a message thread to accept, therefore producers, consumers are created Session
ActiveMQ Profile
Apache ActiveMQ is produced, most popular, strong ability to open source message bus. ActiveMQ is a fully supported JMS1.1 and J2EE1.4 standard JMS Provider implementation has been JMS above simple introduction.
ActiveMQ features:
Languages and write client-side protocol, languages including Java, C, C ++, C #, Ruby, Perl, Python, PHP, protocols include OpenWire, Stomp, REST, WS Notification, XMPP, AMQP
J2EE1.4 specification and fully support JMS1.1
Support for Spring, so that ActiveMQ is integrated into Spring which is very convenient
Support for multiple transport protocols: in-VM, TCP, SSL, NIO, UDP, JGroups, JXTA
JDBC support provided by high-speed messages and journal persistence
Ensure high-performance clusters from the design, client - server point
Support Ajax
Integration and support of the Axis
ActiveMQ message form
Point to point, which is a province of a car by pressing one consumer
After the publish / subscribe model, a message is generated and transmitted producer, the consumer may be received by a plurality of
JMS defines five different format of the message body and message information call, allows you to send and receive data in a number of different forms:
/ ** * Manufacturer mode the ActiveMQ queue * @throws a JMSException * / @Test public void testQueueProducer () throws a JMSException { //. 1. Create a connection factory object, need to specify the IP and port / service port for the message 61616 the ConnectionFactory The connectionFactory new new = ActiveMQConnectionFactory ( "TCP: //192.168.25.128: 61616"); . // 2 are connected using the connection factory to create connection connection connectionFactory.createConnection = (); .. 3 // open connection connection.start (); //. 4. create a session, // the first parameter is whether ActiveMQ open transactions, generally do not use the transaction // if open transaction, the second parameter is automatically ignored, do not open the transaction, the second parameter indicates the answer mode messages, automatic answer , manual answer Session = connection.createSession the session (to false, Session.AUTO_ACKNOWLEDGE); .. 5 // use the Session object to create a Destination objects, topic or queue = Session.createQueue Queue Queue ( "Test-Queue"); // create a producer. 6 to use the Session object. The MessageProducer Producer = session.createProducer (Queue); .. 7 // Create a TextMessage object TextMessage textMessage = new ActiveMQTextMessage ( ); textMessage.setText ( "Hello!"); // send message. 8. producer.send (textMessage); // Close the resource. 9. producer.close (); Session.close (); Connection.close (); }
It can be seen that an object passing between producers and consumers, consists of three main parts: header + message attributes + message body. Of course, the creation process can also be persistent selection configuration message.
/ ** * ActiveMQ consumer queue mode * @throws JMSException * / @Test public void testQueueConsumer () throws JMSException, IOException { // 1. Create a connection factory object, you need to specify the IP and port / messaging port 61616 ConnectionFactory connectionFactory ActiveMQConnectionFactory new new = ( "TCP: //192.168.25.128: 61616"); . // 2 are connected using the connection factory to create connection connection connectionFactory.createConnection = (); .. 3 // open connection connection.start (); // 4. create a session, // the first parameter is whether ActiveMQ open transactions, generally do not use the transaction // if open transaction, the second parameter is automatically ignored, do not open the transaction, the second parameter indicates the answer mode message, automatic answer, manual answer Session = connection.createSession the session (to false, Session.AUTO_ACKNOWLEDGE); .. 5 // use the Session object to create a Destination objects, topic or queue = Session.createQueue Queue Queue ( "Test-Queue"); // create a consumer to use. 6 Session object. A MessageConsumer Consumer = session.createConsumer (Queue); .. 7 // received message consumer.setMessageListener (new MessageListener () { @Override public void the onMessage (the Message Message) { TextMessage textMessage = (TextMessage) Message; String = null text; the try { text textMessage.getText = (); } the catch (a JMSException E) { e.printStackTrace (); } the System. Out.println (text); } }); // Close the resource. 8. System.in.read (); consumer.close(); session.close(); connection.close(); }
Comment basically explain the basic process, only to note that when receiving a message, we first check setMessageListener this method, its interface is defined as follows: