What is JMS, a ghost?
1, Baidu Encyclopedia explanation: JMS is the Java Message Service Application interface, is a Java platform for message-oriented middleware (MOM) API, used to send messages between two applications, or distributed systems for asynchronous communication. The Java Messaging Service is a platform-agnostic API, and the vast majority of MOM providers support JMS.
2. JMS is a set of message service APIs, the interface specification, which is JMS for the JDBC messaging service of the database.
Second, why to learn, using JMS
1. In Java, if the two applications do not understand each other, even if the two programs may be deployed on different continents, then how to send messages between them? For example, an application A is deployed in India, another application is deployed in the United States, and then whenever a triggers something, b wants to get some update information from a. Of course, it is also possible that more than one B is interested in the update of a, and there may be n B-like applications that want to get updated information from a. In this case, Java provides the best solution-JMS, which perfectly solves the problem discussed above. JMS also applies to event-based applications, such as chat services, which require a publishing event mechanism to send messages to all clients connected to the server. Unlike RMI, the recipient does not need to be online when sending a message. The server sends the message and then it does not, and when the client is online, it is guaranteed to receive the message sent by the server. This is a very powerful solution that can handle many of the world's most common problems today.
Iii. What are the advantages of JMS?
1, asynchronous: JMS is inherently asynchronous, when the client obtains the message, does not need to actively send the request, the message is automatically sent to the available clients.
2. Reliable: JMS guarantee messages are delivered only once. Everyone has encountered a duplicate creation message problem, and JMS can help you avoid the problem, but instead of eliminating it, it is possible to repeat it in some bad environments.
Iv. JMS Message Delivery model
Before the JMS API appeared, most products used a "point-to-point" and "Publish/subscribe" In any way for message communication. JMS defines the specifications for these two message-sending models, which are independent of each other. Any JMS provider can implement one or both of these models, which is their own choice. The JMS specification provides a common interface to ensure that the programs we write based on the JMS API are suitable for any model. Let's look at both of these messaging models in more detail;
1. Peer-to-peer messaging model
In a point-to-point messaging model, applications consist of message queues, senders, and receivers. Each message is sent to a special message queue that holds all messages sent to it (in addition to messages consumed by the receiver and expired).
The point-to-point message model has the following characteristics:
(1), each message only one recipient (test it yourself, can have multiple recipients, but when there are multiple recipients, each receiver can only get a few random information)
(2), Message senders and message recipients do not have time dependencies.
(3), when the message sender sends a message, whether the recipient program is not running, can obtain the message;
(4), when the recipient receives the message, will send the confirmation to receive the notice (acknowledgement).
(5), point-to-point message Model diagram:
2. Publish/Subscribe Message delivery Model
In the Publish/Subscribe message model, the Publisher publishes a message that is passed through topic to all clients. In this model, publishers and Subscribers do not know each other, are anonymous, and can dynamically publish and subscribe to topic. Topic is primarily used to save and deliver messages, and the message is persisted until the message is passed to the client.
The Publish/Subscribe message model has the following characteristics:
(1), a message can be passed to multiple subscribers
(2), publishers and Subscribers have time dependencies, only when the client creates a subscription to accept the message, and the subscriber needs to remain active to receive the message.
(3), in order to mitigate such strict time dependencies, JMS allows subscribers to create a persistent subscription. This way, even if the Subscriber is not activated (running), it can receive the message from the publisher.
(4), publish/Subscribe message Model Diagram:
V. Receive messages
In JMS, the receipt of a message can be used in the following two ways:
1. Synchronous mode: The message Subscriber calls the receive () method when the message is received synchronously. In receive (), the message does not arrive or until the specified time is reached, and the method blocks until the message is available.
(1), Destination is queue
Consumer = session.createconsumer (queue); Message message = Consumer.receive (); Synchronous receive
(2), Destination is destination
Consumer = session.createconsumer (destination);//synchronously accepts information if it has not yet been acquired and then blocks until the message message messages = Consumer.receive () is received;
2. Asynchronous mode: When receiving a message asynchronously, a message subscriber registers a message listener, similar to an event listener, that the JMS service provider will deliver by invoking the listener's OnMessage (), as long as the message arrives.
The asynchronous receive is using the Listening method:
Consumer.setmessagelistener (New MessageListener () {@Overridepublic void onMessage (Message message) {TextMessage TextMessage = (textmessage) message;try {String value = Textmessage.gettext (); System.out.println ("Value:" +value);} catch (JMSException e) {//TODO auto-generated catch Blocke.printstacktrace ();}});
Vi. JMS Programming Interface
The JMS application consists of the following basic modules:
1. Management object (administered objects)-Connection factory (Connection factories) and destination (Destination)
2. Connection object (Connections)
3. Session (Sessions)
4. Message producer (MSG producers)
5. Message consumer (MSG consumers)
6. Message Listener (msg Listeners)
7. JMS Management Objects
The management object (administered objects) is a preconfigured JMS object that is created by the system administrator for clients using JMS, with two managed objects:
Connection Factory (ConnectionFactory)
Destination (Destination)
These two management objects are created by the JMS system administrator by using the Application Server Management console, which is stored in the application server's Jndi namespace or the Jndi registry.
8. Connection Factory (ConnectionFactory)
The client connects to the JMS service provider using a connection factory object, which creates a connection between the JMS service provider and the client. The JMS client, such as the sender or recipient, searches for and obtains the connection in the Jndi namespace. With this connection, the client communicates with the destination, sending/receiving messages to the queue or topic. Let's use an example to understand how to send a message:
9. Destination (Destination)
Destination indicates the destination at which the message was sent and the source from which the client received the message. JMS uses two destinations, queues, and topics.
10. JMS Connection
The connection object encapsulates the virtual connection to the JMS provider, and if we have a ConnectionFactory object, you can use it to create a connection.
Link Factory activemqconnectionfactory connectionfactory = null;//linked object Connection connection = Null;connectionfactory = new Activemqconnectionfactory ("admin", "admin", "tcp://192.168.1.111:61616"); connection = Connectionfactory.createconnection (); Connection.start ();
11. JMS Sessions (session)
Session is a single-threaded context for producing and consuming messages that can be created by message producers and message consumers.
The session object implements the session interface, which we can use to create the session after the connection is created.
Whether the first parameter turns on transaction true on, false does not turn on transaction, if turn on remember manual commit//parameter two, indicate the sign mode, general use has auto sign and client confirm sign for session = Connection.createsession ( True, Session.auto_acknowledge);
12. JMS Message Producers
The message producer is created by the session and is used to send messages toward the destination. The producer realizes the MessageProducer interface, we can create the producer for the destination, the queue or the topic;
(1), queuing (queue)
Queue Queue = Session.createqueue ("Test_queue");//create message for queue producer MessageProducer producer = Session.createproducer (queue);
(2), Destination (Destination)
Destination Destination = Session.createqueue ("Test-queue");//create message for destination producer MessageProducer producer = Session.createproducer (destination);
(1) (2) These two methods can only be used in the point-to-point message model
(3), topic (TOPIC)
Create topic Topic Topic = session.createtopic ("Mytopic.messages"); Create a message for the topic producer MessageProducer producer = session.createproducer (topic);
(3), can only be used on the Publish/Subscribe message model.
After you have created the message producer, you can use the Send method to send the message:
Create message TextMessage msg = Session.createtextmessage () message.settext ("Test queue message" +i);//Send Message to Destination Producer.send ( message);
13. JMS Message Consumers
The message consumer is created by the session to receive messages sent by the destination. The consumer realizes the Messageconsumer interface, we can create the consumer for the destination, the queue or the topic;
(1), queuing (queue)
Queue queue = Session.createqueue ("Test_queue"); Queue (destination, where consumers consume messages) Messageconsumer consumer = session.createconsumer (queue); Message Consumers
(2), Destination (Destination)
Message destination Destination Destination = Session.createqueue ("Test-queue");//message consumer Messageconsumer consumer = Session.createconsumer (destination);
(3), topic (TOPIC)
Create topic Topic Topic = session.createtopic ("Mytopic.messages"); Create a consumer for the topic
14. JMS Message Listener (This is the way the message is received asynchronously)
The JMS message listener is the default event handler for the message, and he implements the MessageListener interface, which contains a OnMessage method in which you need to define the specific action after the message is reached. By calling the Setmessagelistener method we define a message listener for the specified consumer.
(1), TextMessage
Consumer.setmessagelistener (New MessageListener () {@Overridepublic void onMessage (Message message) {TextMessage TextMessage = (textmessage) message;try {String value = Textmessage.gettext (); System.out.println ("Value:" +value);} catch (JMSException e) {//TODO auto-generated catch Blocke.printstacktrace ();}});
(2), objectmessage
Consumer.setmessagelistener (New MessageListener () {@Overridepublic void onMessage (Message message) {try {Testbean Tbean = (Testbean) ((objectmessage) message). GetObject (); System.out.println ("Tbean:" +tbean); if (null! = message) {SYSTEM.OUT.PRINTLN ("received message 1:" +tbean.getname ());}} catch (JMSException e) {//TODO auto-generated catch Blocke.printstacktrace ();}});
VII. JMS Message structure
JMS clients use JMS messages to communicate with the system, and JMS messages are composed of three parts, although they are simple in format but very flexible:
1. Message header
The JMS message header pre-defines several fields for identifying and sending messages between the client and the JMS provider, with the precompiled header as follows:
–jmsdestination
–jmsdeliverymode
–jmsmessageid
–jmstimestamp
–jmscorrelationid
–jmsreplyto
–jmsredelivered
–jmstype
–jmsexpiration
–jmspriority
2. Message Properties
We can set custom properties for messages that are primarily provided to the application. Message properties are useful for implementing message filtering, and the JMS API defines a number of standard properties that a JMS service provider can selectively provide in some standard properties.
3. Message body
In the message body, the JMS API defines five types of message formats, allowing us to send and receive messages in different forms and to provide compatibility with existing message formats. The different message types are as follows:
Text message:javax.jms.TextMessage that represents a text object.
An object message:javax.jms.ObjectMessage that represents a Java object.
Bytes Message:javax.jms.BytesMessage, which represents the byte data.
Stream Message:javax.jms.StreamMessage, which represents the Java raw value data stream.
Map Message:javax.jms.MapMessage, which represents a key-value pair.
Reference article: Http://www.cnblogs.com/chenpi/p/5559349.html#_label6
http://howtodoinjava.com/jms/jms-java-message-service-tutorial/
JMS (Java Messaging service) learns a