在ActiveMQ的BrokerFilter中设置属性

问题: 路由和事务一起使用时,会有问题,在路由的代码里,希望去掉设置的事务的属性,但发现设置的属性不起作用。

在测试的时候,发现,如果修改为如下代码,属性的设置是起作用的:

 

 

public boolean doWork(Message msg) {

            if(log.isInfoEnabled()){

                log.info("Receive a message :" + msg);

            }

           

            boolean result = true;

            NapoliExchange ne = null;

            ActiveMQMessage oldMsg = ((ActiveMQMessage)msg);

            try {

                Map<String, Object> props = new HashMap<String, Object>(oldMsg.getProperties());

                props.remove("NAPOLI_MSG_PRO_KEY_TX_STATE");

                props.remove("NAPOLI_MSG_PRO_KEY_TX_ID");

                oldMsg.setProperties(props);

                oldMsg.setMarshalledProperties(null);

                ne = new NapoliExchange(endpoint.getCamelContext(),oldMsg);

                processor.process(ne);

            } catch (NapoliClientException e) {

                result = false;

 

 

 

 其中,很重要的一行代码是

oldMsg.setMarshalledProperties(null); 如果将该行代码去掉,则对属性的修改还是无效的。

 

ActiveMQBrokerFilter中,如果调用setProperty设置了属性,在consumer中收到后是没有的,而设置其属性,如setGroupId等,则有效。

查看ActiveMQ的源码,发现传输中属性和setProperties调用的不是一个对象,前者(marshalledProperties)是用于传输的,后者(properties)是读取和设置的:

public Map<String, Object> getProperties() throws IOException { if (properties == null) { if (marshalledProperties == null) { return Collections.EMPTY_MAP; } // 如果你先读取过属性,那么这里properties,marshalledProperties就都不为空了 properties = unmarsallProperties(marshalledProperties); } return Collections.unmodifiableMap(properties); } public void setProperty(String name, Object value) throws IOException { lazyCreateProperties(); properties.put(name, value); } protected void lazyCreateProperties() throws IOException { if (properties == null) { if (marshalledProperties == null) { properties = new HashMap<String, Object>(); } else { // 既然properties不为空,这句话永远不会被调用到(只要你先读取过属性) properties = unmarsallProperties(marshalledProperties); marshalledProperties = null; } } } public void beforeMarshall(WireFormat wireFormat) throws IOException { // Need to marshal the properties. if (marshalledProperties == null && properties != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream os = new DataOutputStream(baos); MarshallingSupport.marshalPrimitiveMap(properties, os); os.close(); marshalledProperties = baos.toByteSequence(); } }

 

  

 

这就导致ActiveMQ不知道你修改过属性,因此到达consumer之后不会生效。

 

所以,在设置属性后,强制将unmarsallProperties属性设置为空,在使用时,会调用beforeMarshall,会将properties 的值在marshalledProperties中重新设置,所以,对属性的修改就对consumer可见了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值