骆驼。两个线程同时处理同一条消息

时间:2018-10-09 07:15:48

标签: java apache-camel activemq

您能帮我解决我遇到的问题吗?

我有一个应用程序,可以从ActiveMq接收和处理消息,然后在Web浏览器中显示弹出窗口。 工作一段时间后,通知将停止工作。 我阅读了日志文件,发现有时两个骆驼线程开始处理同一条消息。 为什么会发生?

我应该同步process(final Exchange exchange)方法吗?

日志文件片段:

05.10.2018 21:15:52.859 [Camel (app) thread #8 - JmsConsumer[core.consume]] DEBUG InboundMessageProcessor [3.7.13 (default) /]- Process inbound message...

05.10.2018 21:15:52.859 [Camel (app) thread #11 - JmsConsumer[core.consume]] DEBUG InboundMessageProcessor [3.7.13 (default) /]- Process inbound message...

我的处理器如下:

public abstract class BaseProcessor implements Processor {

    private static final String LOG4J_VERSION = "version";

    @Override
    public void process(final Exchange exchange) throws MyException {
        try {
            final Message msg = exchange.getIn();

            final String id = msg.getHeader(CustomJMSFields.UNIQUE_ID, String.class);
            if (eventId != null) {
                MDC.put(CustomJMSFields.UNIQUE_ID, id);
            }

            final String name = msg.getHeader(CustomJMSFields.TEL_NAME, String.class);
            if (name != null) {
                MDC.put("name", name );
            }

            final String stationId = msg.getHeader(CustomJMSFields.TEL_STATION_ID, String.class);
            if (stationId != null) {
                MDC.put("stationId", stationId);
            }

            processMessage(msg);
        } finally {
            MDC.remove(CustomJMSFields.UNIQUE_ID);
            MDC.remove("name");
            MDC.remove("stationId");
            MDC.remove(LOG4J_VERSION);
        }
    }

    public abstract void processMessage(Message message) throws MyException;
}

任何帮助将不胜感激。

这是我的JMS设置:

    <bean id="jmsConnectionFactoryParent"
      class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="${amq.broker.url}"/>
</bean>

<bean id="jmsConnectionFactory" parent="jmsConnectionFactoryParent"/>

<bean id="simpleMessageConverter"
      class="org.springframework.jms.support.converter.SimpleMessageConverter">
</bean>

<bean id="pooledConnectionFactory"
      class="org.apache.activemq.pool.PooledConnectionFactory" init-method="start" destroy-method="stop">
    <property name="maxConnections" value="20"/>
    <property name="maximumActiveSessionPerConnection" value="25"/>
    <property name="idleTimeout" value="0"/>
    <property name="connectionFactory" ref="jmsConnectionFactory"/>
</bean>

<bean id="jmsConfig"
      class="org.apache.camel.component.jms.JmsConfiguration">
    <property name="connectionFactory" ref="pooledConnectionFactory"/>
    <property name="concurrentConsumers" value="5"/>
    <property name="preserveMessageQos" value="true"/>
    <property name="messageConverter" ref="simpleMessageConverter"/>
</bean>

<bean id="activemq"
      class="org.apache.activemq.camel.component.ActiveMQComponent">
    <property name="configuration" ref="jmsConfig"/>
</bean>

0 个答案:

没有答案