A profound understanding of JMS (Java Message Service) and jmsmessage

Source: Internet
Author: User

A profound understanding of JMS (Java Message Service) and jmsmessage

JMS has been available for a long time. This is especially true on the Internet, but most of the summary is not comprehensive and not specific. Based on the existing learning resources, I Will resummarize it:

JMS is called Java Message Service (Java Message Service). It is one of the J2EE technical specifications (it belongs to the technical specifications related to Message-oriented middleware (MOM) on the Java platform ), it is used to access the message system (send messages to the message system or receive messages to the message system), and finally realize message interaction between different application systems. Hehao, when you read this, I am afraid you will be confused-what is its mother's "message system "? We know that different systems can access the same relational database through the JDBC interface, so we can access the same "message system" through different systems through the JMS interface ", to put it bluntly, the status of the "Message System" is equivalent to the database mentioned above: A relational database is a software running on a computer or Server (such as Oracle, MySQL, or SQL Server ); the messaging system is also a software running on a computer or server (such as Apache ActiveMQ or IBM WebSphere MQ ). The above sentence not only points out what is the "message system", but also explains what is the role of JMS--JMS is very similar to JDBC: JDBC provides a set of public interfaces for accessing various relational databases (these interfaces are implemented by companies that develop database software, and only need to use their jar packages for development ), while JMS provides a set of interfaces for accessing different messaging systems (these interfaces are implemented by companies that develop messaging system software by using their jar packages ).
An application using JMS is called a JMS client. The "message system" that stores and transmits messages is called a JMS Provider, a jms application is composed of "multiple JMS clients" (the JMS client sending messages is called a producer), while the JMS client receiving messages is called a consumer ); the same JMS client can be either a producer or a consumer) and a service system composed of a JMS Provider. JMS allows us to send messages from one application to another through the message sending and receiving service (that is, the JMS Provider, also known as the message intermediary program or Message Server.
The programming process of JMS is very simple. In summary, application A (Message producer) sends A message to A certain Destination (Destination) of the Message Server (for example, ActiveMQ), and then the Message Server (for example: activeMQ) forwards messages to application B (Message consumer ). Because application A and application B do not have A direct code connection, the two implementations are similar, for example:


And the text above briefly introduces the JMS programming process. The following is a detailed introduction based on the Apache JMS software (ActiveMQ:

Application:

Package com. ghj. packageoftest; import javax. jms. *; import org. apache. activemq. activeMQConnectionFactory;/*** message Producer ** @ author Gao huanjie */public class Producer {public static void main (String [] args) {try {ConnectionFactory connectionFactory = new ActiveMQConnectionFactory ("admin", "admin", "tcp: // localhost: 61616"); Connection connection = connectionFactory. createConnection (); connection. start (); Sess Ion session = connection. createSession (false, Session. AUTO_ACKNOWLEDGE); Destination destination = session. createQueue ("FirstQueue"); // The method parameter is used to indicate the location name MessageProducer producer = session. createProducer (destination); Message message = session. createTextMessage ("you are the good news user. I am the message PRODUCED BY THE MESSAGE producer !!! "); Producer. send (message); if (connection! = Null) {connection. close () ;}} catch (Exception e) {e. printStackTrace ();}}}

Programming process description:

1. ConnectionFactory: ActiveMQConnectionFactory (the first parameter of the ActiveMQConnectionFactory constructor) is the account of the Message Server. The default value is admin; the second parameter is the password of the Message Server. The default value is admin. The third parameter is the URL of the Message Server, the creation of the connection factory instance laid the foundation for JMS to use this instance to create a connection with the message server.

2. Connection (Connection interface): Creates a Connection interface between the JMS client and the message server through the Connection factory implementation class instance, the creation of this instance lays the foundation for starting the connection between the JMS client and the Message Server and creating Session interface implementation class instances.

3,

4,

5,

6,

7,

8,

Application B:

Package com. ghj. packageoftest; import javax. jms. *; import org. apache. activemq. activeMQConnectionFactory;/*** message Consumer ** @ author Gao huanjie */public class Consumer {public static void main (String [] args) {try {ConnectionFactory connectionFactory = new ActiveMQConnectionFactory ("admin", "admin", "tcp: // localhost: 61616"); Connection connection = connectionFactory. createConnection (); connection. start (); Sess Ion session = connection. createSession (false, Session. AUTO_ACKNOWLEDGE); Destination destination = session. createQueue ("FirstQueue"); // The method parameter is used to indicate the location name MessageConsumer messageConsumer = session. createConsumer (destination); messageConsumer. setMessageListener (new MessageListener () {public void onMessage (Message message) {try {System. out. println ("received message content:" + (TextMessage) message ). getText ();} ca Tch (JMSException e) {e. printStackTrace () ;}}); // if (connection! = Null) {// unregister the if statement block and wait until the message server receives the message. // connection. close (); //} catch (Exception e) {e. printStackTrace ();}}}
Programming process description:

1,

2,

3,

4,

5,

6,

7,

8,


Download Demo at 0]

Not complete .......



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.