How to handle the durability of the Java Ee JMS API in the Java Class Library
The Java Ee JMS API provides a flexible and powerful processing mechanism for the durability of the message.The durability refers to the ability of the message to be saved and being transmitted when the receiver is transmitted again even when the receiver is offline or unavailable during the transmission of the message.The JMS API provides two types of message transmission modes: durable messages and non -persistent messages.
The characteristics of persistent messages are that the message will be stored in the JMS Provider even until the receiver is re -launched even in the JMS Provider.When the receiver is re -launched, the message is passed to it.This model is suitable for important business messages to ensure the reliability transmission of messages.Below is an example code that uses Java Ee JMS API to send persistent messages:
import javax.jms.*;
public class JMSProducer {
private ConnectionFactory connectionFactory;
private Destination destination;
public JMSProducer(ConnectionFactory connectionFactory, Destination destination) {
this.connectionFactory = connectionFactory;
this.destination = destination;
}
public void sendMessage(String message) {
try (Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination)) {
TextMessage textMessage = session.createTextMessage(message);
Producer.SetDeliveryMode (DeliveryMode.persistent); // Set the durability of messages
producer.send(textMessage);
} catch (JMSException e) {
// Treatment JMS abnormalities
}
}
}
In the above code, use the `MessageProducer` to create a` TextMessage` message, and set the message to persistent messages with the method of `SetDeliveryMode ()`.Then send messages to the target `Destination` by the method of` Producer.send () `.
The code for receiving persistent messages is shown below:
import javax.jms.*;
public class JMSConsumer {
private ConnectionFactory connectionFactory;
private Destination destination;
public JMSConsumer(ConnectionFactory connectionFactory, Destination destination) {
this.connectionFactory = connectionFactory;
this.destination = destination;
}
public void receiveMessage() {
try (Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(destination)) {
connection.start();
Message message = consumer.receive();
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message: " + textMessage.getText());
}
} catch (JMSException e) {
// Treatment JMS abnormalities
}
}
}
In the above code, you first need to call the method to start the connection, and then receive the message through the method of the `consumer.Receive ()` method.If the received message is the `TextMessage" type, you can get the content of the message through the method of `Gettext ()`.
Through the above example code, we can see that using Java Ee JMS API to process persistence messages is very simple. Just use the `SetDeliveryMode ()` method to set the message to persistence. JMS Provider will be responsible for saving and transmission of messages.This provides convenience for us to develop high -reliability and scalability applications.