Introduction to Java Ee JMS API in Java Library
Java Ee JMS API (Java Message Service Application Programming Interface) is a standard API in the Java platform enterprise version (Java Ee) for creation, sending and receiving messages.The JMS API provides a method of transmitting messages between distributed applications to achieve reliable and efficient asynchronous communication.
JMS API -based MESSAGE Oriented Middleware (MOM) model (MESSAGE OrienteD Middleware (MOM) model provides two message transmission mode (point -to -point and release/subscription) to meet different types of application needs.In the JMS model, the application can act as a message producer, send the message to the message queue or theme, and then other applications can receive and process these messages as message consumers.
The core components of the Java Ee JMS API include the following parts:
1. ConnectionFactory: The factory used to create a factories connecting objects.The connection object represents the communication link between the application and the message transmitter.
2. Connection: Represents the actual connection between the message transmitter.
3. SESSION: Used to create a context environment for producers and consumer objects.A session can span multiple message producers and consumers.
4. Destination: indicate the ultimate goal of the message.It can be queue or topic.Message producers send the message to the destination, and the message consumers receive the message from the destination.
5. MESSAGEPRODUCER: It is used to create and send messages to objects.
6. MESSAGECONSUMER: Objects used to create and receive messages.
The following is a simple example, demonstrating how to send and receive messages with Java Ee JMS API:
1. Create a connection factory and create a connection:
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = factory.createConnection();
connection.start();
2. Create a session object and destination:
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("myQueue");
3. Create producers and send messages:
MessageProducer producer = session.createProducer(destination);
TextMessage message = session.createTextMessage("Hello, JMS!");
producer.send(message);
4. Create consumers and receive messages:
MessageConsumer consumer = session.createConsumer(destination);
Message receivedMessage = consumer.receive();
if (receivedMessage instanceof TextMessage) {
TextMessage textMessage = (TextMessage) receivedMessage;
System.out.println("Received message: " + textMessage.getText());
}
5. Close connection and session:
session.close();
connection.close();
By using the Java Ee JMS API, developers can realize a reliable and flexible message transmission mechanism, so that enterprise applications can efficiently communicate asynchronous communication.Using JMS can effectively decouple the application component to improve the scalability and maintenance of the system.