简体   繁体   English

Spring Integration JMS通道设置并发消费者导致数据损坏

[英]spring integration jms channel setting concurrent-consumers is causing data corruption

I'm using spring integration jms channel to consume messages from the queue and process it. 我正在使用spring集成jms通道来消费队列中的消息并对其进行处理。

Here is my inbound-channel-config.xml 这是我的inbound-channel-config.xml

<jms:message-driven-channel-adapter id="jmsIn"
            destination="requestQueue"
            channel="routingChannel" 
            connection-factory="cachingConnectionFactory" 
            error-channel="errorChannel"
            concurrent-consumers="${jms_adapter_concurrent_consumers}" />

Here when i set concurrent-consumers to a value greater than 1, the messages that i consume gets corrupted while processing. 在这里,当我将并发消费者的值设置为大于1时,我消耗的消息在处理时会损坏。 I'm consuming XML and Json messages from the queue and while parsing the data, i could see that some of its contents are changed and set to some random value. 我正在使用队列中的XML和Json消息,并且在解析数据时,我可以看到其某些内容已更改并设置为一些随机值。

The above config works fine only when concurrent-consumers value is set to 1. 仅当并发消费者值设置为1时,上述配置才能正常工作。

My question is, do i have to manually Synchronize (make thread safe) my code when i set concurrent-consumers to a value greater than 1? 我的问题是,当我将并发用户设置为大于1的值时,是否必须手动同步代码(使线程安全)?

Yes, your code must be thread safe. 是的,您的代码必须是线程安全的。 That's the case for any multi threaded code. 任何多线程代码就是这种情况。

However, synchronizing the whole thing will effectively defeat concurrency. 但是,同步整个过程将有效地消除并发性。 It's better to use stateless code (no fields), or use thread-safe variables ( AtomicInteger and friends), or limit synchronization to small blocks. 最好使用无状态代码(无字段),或者使用线程安全变量( AtomicIntegerAtomicInteger ),或者将同步限制在小块内。

If you synchronize the whole listener code, only one container thread can process at a time. 如果同步整个侦听器代码,则一次只能处理一个容器线程。

Vishal and I work together. 维沙尔和我一起工作。

I have to mention that the caching connection factory is being used, and I noticed that in this post you discouraged its use . 我不得不提到正在使用缓存连接工厂,并且我注意到在本文中您不鼓励使用它

    <bean id="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
    <property name="targetConnectionFactory" ref="MQConnectionFactory" />
    <property name="sessionCacheSize" value="10"/>
</bean>


@Bean(name="MQConnectionFactory")
public ConnectionFactory connectionFactory() {

    if (factory == null) {
        factory = new MQConnectionFactory();
        try {
            factory.setHostName(env.getRequiredProperty(HOST));
            factory.setPort(Integer.parseInt(env.getRequiredProperty(PORT)));            
            factory.setQueueManager(env.getRequiredProperty(QUEUE_MANAGER));
            factory.setChannel(env.getRequiredProperty(CHANNEL));
            factory.setTransportType(WMQConstants.WMQ_CM_CLIENT);  

        } catch (JMSException e) {
            throw new RuntimeException(e);
        }
    }
    return factory;
}

Could this be causing the issues? 这会导致问题吗? It seems so far that the issue happens in Spring Integration's default message convertors, as in some cases parts of the payload is "". 到目前为止,该问题似乎发生在Spring Integration的默认消息转换器中,因为在某些情况下,有效负载的一部分为“”。

Cheers Kris 干杯克里斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM