The technical principles of the "Jakarta Messaging API" framework in the Java class library
The Jakarta Messaging API (JMS) in the Java Class Library is a standard API used to transfer asynchronous messages between distributed applications.It is based on the Java EE platform and provides a flexible and powerful way to achieve asynchronous communication, thereby supporting reliable message transmission.
The technical principles of the Jakarta Messaging API framework can be summarized as follows:
1. Message model: Jakarta Messaging API is based on the release-subscription model and point-to-point model.Release-Subscribe model allows message publishers to send the message to one or more subscribers, while the point-to-point model uses a queue to send the message to a receiver.
2. Connect the factory: The application uses the connection factory to create a connection between the factory through the Jakarta Messaging API.Connecting the factory is responsible for creating connection and sessions, as well as the sending and receiving of management messages.
Below is a sample code that uses Jakarta Messaging API to create connecting factories:
import javax.jms.ConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
public class ConnectionFactoryExample {
public static void main(String[] args) throws Exception {
// Create ActiveMQ connection factory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// Use the connection factory to create a connection
Connection connection = connectionFactory.createConnection();
// Other operations ...
}
}
3. Session: The connection created by the factory can be further used to create a session.The session is a context between the producer and consumers of the message, which is used to send and receive messages.There are two types of sessions available: non -transactional sessions and transactional sessions.
Below is an example code that uses Jakarta Messaging API:
import javax.jms.Session;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
public class SessionExample {
public static void main(String[] args) throws Exception {
// Create ActiveMQ connection factory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// Use the connection factory to create a connection
Connection connection = connectionFactory.createConnection();
// Create the meeting
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Other operations ...
}
}
4. Destination: In Jakarta Messaging API, the destination of the message can be a queue or theme.The queue is used for point-to-point communication, and the theme is used to publish-subscription communication.
Below is a sample code that uses Jakarta Messaging API to create a queue destination:
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
public class QueueDestinationExample {
public static void main(String[] args) throws Exception {
// Create ActiveMQ connection factory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// Use the connection factory to create a connection
Connection connection = connectionFactory.createConnection();
// Create the meeting
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create queue destination
Queue queue = session.createQueue("myQueue");
// Other operations ...
}
}
5. Message producers and consumers: Message producers and consumers created by session can send and receive messages between destinations.Message producers are responsible for sending messages, and the message consumers are responsible for receiving the message.
Below is a sample code for creating message producers and consumers using Jakarta Messaging API:
import javax.jms.MessageProducer;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
public class ProducerConsumerExample {
public static void main(String[] args) throws Exception {
// Create ActiveMQ connection factory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// Use the connection factory to create a connection
Connection connection = connectionFactory.createConnection();
// Create the meeting
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create queue destination
Queue queue = session.createQueue("myQueue");
// Create message producers
MessageProducer producer = session.createProducer(queue);
// Create message Consumers
MessageConsumer consumer = session.createConsumer(queue);
// Other operations ...
}
}
Summary: The Jakarta Messaging API framework provides a convenient way to achieve asynchronous message transmission.By connecting the factory to create a connection and session, you can use the destination to communicate between message producers and consumers.Using Jakarta Messaging API, you can easily achieve reliable distributed applications.