问题: 路由和事务一起使用时,会有问题,在路由的代码里,希望去掉设置的事务的属性,但发现设置的属性不起作用。
在测试的时候,发现,如果修改为如下代码,属性的设置是起作用的:
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); 如果将该行代码去掉,则对属性的修改还是无效的。
在ActiveMQ的BrokerFilter中,如果调用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可见了