activemq transaction messages

Most message-oriented middleware supports transactional messaging, activemq no exception.

Definition and ACID properties on matters not repeat them here.


Contrast Mysql databases,

Mysql concept of affairs,

Activemq also has the concept of transaction

Here that are local affairs, rocketMq also supports distributed transactions


java jdbc developed to regulate access to the database

same

java is also jms (java message services) to regulate access to messaging middleware, activemq specification is fully supported jms1.1


That java to implement the transaction by mysql,

Probably nothing less than this:

 public static void main(String[] args) {
        Connection conn =getConnection();
        try {
            conn.setAutoCommit(false);
            insertUser(conn);
            insertAddress(conn);
            conn.commit();
        } catch (SQLException e) {
            System.out.println("************事务处理出现异常***********");
            e.printStackTrace();
            try {
                conn.rollback();
                System.out.println("*********事务回滚成功***********");
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }finally {
                try {
                    conn.close();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
    }

Then we connect the text: the Spring integration Activemq

Activemq take a look at how to implement transactional messaging


    @Test
    public void p2pSender() {
        //获取连接工厂
        ConnectionFactory connectionFactory = jmsQueueTemplate.getConnectionFactory();
        Session session = null;
        try {
            Connection connection = connectionFactory.createConnection();
            // 参数一:是否开启消息事务
            session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);

            MessageProducer producer = session.createProducer(session.createQueue("test.trsaction"));
            //发送10条消息,开启事务后,要么一起成功,要么一起失败
            for (int i = 0; i < 10; i++) {
                if (i == 4) {
                    //出现异常
//                    throw new RuntimeException("i cannot equals 4");
                }
                TextMessage textMessage = session.createTextMessage("消息-----" + i);
                producer.send(textMessage);
            }
            //开启事务后,需要手动提交
            session.commit();
        } catch (JMSException e) {
            e.printStackTrace();
            //消息事务回滚
            try {
                session.rollback();
            } catch (JMSException ex) {
                ex.printStackTrace();
            }
        }
    }

The figure we manually created an exception.

When no exceptions: 10 messages can be successfully transmitted

image-20200130211242542

We abnormalities that line open

Perform again:

Can be found from the console, the information was not sent out.

image-20200130211348932

That is the effect of the transaction have now.


Also to be mentioned are:

After integration into the spring, should be entrusted spring transaction management.

For Mysql, we configured DatasourceTransactionManager,

Use @transactional annotations can be.

For activemq is the same, but the use is JmsTransactionManager.


Also note that this distributed transaction, mysql and activemq the same as the resource manager (RM), have achieved XA protocol, activemq this with much, not entirely supported, not repeat them here.

Guess you like

Origin www.cnblogs.com/heliusKing/p/12244047.html