ActiveMq中Session的事务与消息过期

ActiveMQ有支持两种事务,

  • JMS transactions - the commit() / rollback() methods on a Session (which is like doing commit() / rollback() on a JDBC connection)
  • XA Transactions - where the XASession acts as an XAResource by communicating with the Message Broker, rather like a JDBC Connection takes place in an XA transaction by communicating with the database.

在支持事务的session中,producer发送message时在message中带有transaction ID。broker收到message后判断是否有transaction ID,如果有就把message保存在transaction store中,等待commit或者rollback消息。所以ActiveMq的事务是针对broker而不是producer的,不管session是否commit,broker都会收到message。

如果producer发送模式选择了persistent,那么message过期后会进入死亡队列。在message进入死亡队列之前,ActiveMQ会删除message中的transaction ID,这样过期的message就不在事务中了,不会保存在transaction store中,会直接进入死亡队列。具体删除transaction ID的地方是在

org.apache.activemq.util.BrokerSupport的doResend,将transaction ID保存在了originalTransactionID中,删除了transaction ID

public static void doResend(final ConnectionContext context, Message originalMessage, ActiveMQDestination deadLetterDestination, boolean copy) throws Exception {         
        Message message = copy ? originalMessage.copy() : originalMessage;  
        message.setOriginalDestination(message.getDestination());  
        <strong>message.setOriginalTransactionId(message.getTransactionId());</strong>  
        message.setDestination(deadLetterDestination);  
        <strong>message.setTransactionId(null);</strong>  
        message.setMemoryUsage(null);  
        message.setRedeliveryCounter(0);  
        boolean originalFlowControl = context.isProducerFlowControl();  
        try {  
            context.setProducerFlowControl(false);  
            ProducerInfo info = new ProducerInfo();  
            ProducerState state = new ProducerState(info);  
            ProducerBrokerExchange producerExchange = new ProducerBrokerExchange();  
            producerExchange.setProducerState(state);  
            producerExchange.setMutable(true);  
            producerExchange.setConnectionContext(context);  
            context.getBroker().send(producerExchange, message);  
        } finally {  
            context.setProducerFlowControl(originalFlowControl);  
        }  

 

转载于:https://my.oschina.net/u/2499632/blog/995640

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值