JMS API Framework Technical Principles Details: Application Practice in the Java Class Library
JMS API (Java Messaging Service) is a standard Java API for distributed applications to send messages between distributed applications.It provides a reliable way of transmission that allows different applications to communicate asynchronous.This article will explain the technical principles of the JMS API framework in detail and provide some Java code examples to illustrate its application practice in the Java class library.
The JMS API provides two types of message transmission models: point-to-point and Publish/Subscripe.In the point -to -point model, the message sender sends the message to a specific queue, and the message receiver receives and processes the message from the queue.This model is suitable for a message that there is only one receiver.In the release/subscription model, the message sender publishes the message to a theme, and multiple subscribers can subscribe to the theme and receive the message.This model is suitable for a message that there can be multiple receivers.
The following is the main component of the JMS API framework:
1. ConnectionFactory: Used to create a connection with JMS providers.
2. Connection (connection): Indicates a activity connection with JMS providers, and provides functions such as the creation and closing of sessions.
3. SESSION: It is used to send and receive messages, and provide functions such as transactions and confirmation models.One session can have multiple producers and consumers.
4. Destination (destination): The location of the message sent by the message sender, or the position of the message receiver received the message.The destination can be queue or topic.
5. MESSAGEPRODUCER: Used to create and send messages to destination.
6. MessageConsumer
Next, we will explain the application practice of JMS API through several practical application scenarios.
1. Send message to the queue:
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class QueueSender {
public static void main(String[] args) {
try {
// Create a connection factory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// Create a connection
Connection connection = connectionFactory.createConnection();
// Start the connection
connection.start();
// Create the meeting
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create queue
Queue queue = session.createQueue("testQueue");
// Create message producers
MessageProducer producer = session.createProducer(queue);
// Create messages
TextMessage message = session.createTextMessage("Hello, JMS!");
// Send a message
producer.send(message);
// Turn off the connection
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
2. Receive messages from the queue:
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class QueueReceiver {
public static void main(String[] args) {
try {
// Create a connection factory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// Create a connection
Connection connection = connectionFactory.createConnection();
// Start the connection
connection.start();
// Create the meeting
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create queue
Queue queue = session.createQueue("testQueue");
// Create message Consumers
MessageConsumer consumer = session.createConsumer(queue);
// Receive messages
consumer.setMessageListener((Message message) -> {
try {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message: " + textMessage.getText());
}
} catch (JMSException e) {
e.printStackTrace();
}
});
} catch (JMSException e) {
e.printStackTrace();
}
}
}
Through the above example code, we can see the use of JMS API: Create a connection factory, create connection, start connection, create a meeting, create a destination (queue or theme), create producers or consumers, and send or receive messages.These steps constitute the core concepts and practical methods of the JMS API framework.
It is hoped that this article will help understand the technical principles of the JMS API framework and the application practice in the Java class library.