JMS learning five (ActiveMQ local transaction)

1. ActiveMQ's local transaction

 

        In a JMS client, the sending and receiving of messages can be combined using local transactions. The JMS Session interface provides commit and rollback methods. Transaction commit means that all messages produced are sent and all messages consumed are acknowledged; transaction rollback means that all messages produced are destroyed and all messages consumed are recovered and resubmitted unless they have expired. Transactional sessions are always involved in transaction processing. Once the commit or rollback method is called, one transaction ends and another is started. Closing a transactional session will roll back the transactions within it. It is important to note that if you use the request/reply mechanism, i.e. send a message and expect to wait in the same transaction to receive a reply to that message, the program will be suspended because the send operation will not actually execute until the transaction commits. Another thing to note is that the production and consumption of messages cannot be included in the same transaction.

       When the send operation is performed in the transaction state, the message is not actually delivered to the middleware, but only after the session.commit operation is performed, the message will be sent to the middleware, and then forwarded to the appropriate consumer for processing. If the rollback operation is called, it indicates that all messages sent during the current transaction period are canceled.

 

2. About the usage of ActiveMQ local transactions

public class Sender {  
  
    public static void main(String[] args) throws Exception {  
  
        // 1. To create a ConnectionFactory factory object, you need to fill in the username, password, and connection address  
         // only use the default. The port number is "tcp: // localhost:61616"   
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(  
                 "zhangsan", // ActiveMQConnectionFactory.DEFAULT_USER,   
                "123", // ActiveMQConnectionFactory.DEFAULT_PASSWORD,   
                "tcp://localhost:61616" );  
         / / 2. Create a Connection connection through the ConnectionFactory factory object  
         // and call the start method of Connection to open the connection, Connection is not open by default   
        Connection connection = connectionFactory.createConnection();  
        connection.start();  
  
        // 3. Create a Session session (context environment object) through the Connection object,  
         // Parameter 1, indicating whether to open the transaction  
         // Parameter 2, indicating the sign-in mode, generally used are automatic sign-in and client's own confirmation sign-in  
  
        // The first parameter is set to true, which means to open the transaction  
         // After opening the transaction, remember to manually submit the transaction   
  
        Session session = connection.createSession(Boolean.TRUE,  
                Session.AUTO_ACKNOWLEDGE);  
  
        // 4. Create a Destination object through Session, which refers to an object used by a client to specify the destination of the production message and the source of the consumption message.  
        // In PTP mode, Destination refers to Queue  
         // In publish-subscribe mode, Destination refers to Topic   
        Destination destination = session.createQueue("queue1" );  
  
        // 5. Use Session to create the producer or consumer of the message object   
        MessageProducer messageProducer = session.createProducer(destination);  
         // 6. If it is, the producer, use the setDeliverMode method of MessageProducer to set the persistence and non-persistence of the message Change messageProducer.setDeliveryMode   
        (DeliveryMode.NON_PERSISTENT);  
         // 7. Finally use the JMS standard TextMessage form to create data (through the Session object)  
         // And use MessageProducer's send method to send data   
        for ( int i = 0; i < 5; i++ ) {  
            TextMessage textMessage = session.createTextMessage();  
            textMessage.setText( "I am a message" + i);  
            messageProducer.send(textMessage);  
        }  
  
        // Manually commit the opened transaction   
        session.commit();  
  
        // Release the connection   
        if (connection != null ) {  
            connection.close();  
        }  
    }  
}  

 

2.1 Start a transaction

// 3. Create a Session session (context environment object) through the Connection object,  
         // Parameter 1, indicating whether to open the transaction  
         // Parameter 2, indicating the sign-in mode, generally used are automatic sign-in and client's own confirmation sign-in  
  
        // The first parameter is set to true, which means to open the transaction  
         // After opening the transaction, remember to manually submit the transaction   
  
        Session session = connection.createSession(Boolean.TRUE,  
                Session.AUTO_ACKNOWLEDGE);  

 

 2.2 Commit transaction

// 7. Finally, use the JMS standard TextMessage form to create data (through the Session object)  
         // and use the send method of MessageProducer to send data   
        for ( int i = 0; i < 5; i++ ) {  
            TextMessage textMessage = session.createTextMessage();  
            textMessage.setText( "I am a message" + i);  
            messageProducer.send(textMessage);  
        }  
  
        // Manually commit the opened transaction   
        session.commit();  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325117174&siteId=291194637