Spring Singleton范围不适用于Spring集成应用程序

时间:2019-05-10 16:25:04

标签: spring spring-integration spring-jms

我有一个具有以下配置的弹簧集成应用程序:

<beans:bean id="customMessageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer" scope = "singleton">
    <beans:property name="errorHandler" ref="customErrorHandler"/>
    <beans:property name="connectionFactory" ref="connectionFactory" />
    <beans:property name="destination" ref="jmsInputQueueXssl" />
    <beans:property name="sessionTransacted" value="true" />
    <beans:property name="maxConcurrentConsumers" value="1" />
    <beans:property name="concurrentConsumers" value="1" />
    <beans:property name="receiveTimeout" value="5000" />
    <beans:property name="recoveryInterval" value="60000" />
    <beans:property name="autoStartup" value="true" />
    <beans:property name="exposeListenerSession" value="false" />
    <beans:property name="subscriptionDurable" value="true" />
    <beans:property name="durableSubscriptionName" value="${ibm.jms.subscription.id1}" />
    <beans:property name="backOff" ref="myBackoff" />
</beans:bean>

<beans:bean id="myBackoff" class="c.h.i.c.d.x.j.MyFixedBackOff"> 
    <beans:constructor-arg index="0" value="5000"/>
    <beans:constructor-arg index="1" value="5"/>
</beans:bean>

<!-- Custom error handler  -->
<beans:bean id="customErrorHandler" class="c.h.i.c.d.x.j.XsslCustomErrorHandler" />

<beans:beans profile="env">
    <jms:message-driven-channel-adapter
        id="jmsInboundAdapterXssl" channel="channel2"
        acknowledge="transacted" error-channel="errorChannel"
        container="customMessageListenerContainer"/>

</beans:beans>

但是,如果我阅读日志,似乎有该类customMessageListenerContainer的多个实例:

2019-05-10 17:54:39,783 INFO [main] o.s.i.e.SourcePollingChannelAdapter [AbstractEndpoint.java:97] started org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean#0
2019-05-10 17:54:39,783 INFO [main] o.s.c.s.DefaultLifecycleProcessor [DefaultLifecycleProcessor.java:341] Starting beans in phase 2147483647
2019-05-10 17:54:39,924 INFO [customMessageListenerContainer-1] c.h.i.c.d.x.j.MyFixedBackOff [MyFixedBackOff.java:115] MyFixedBackOff currentAttempts = 0
2019-05-10 17:54:44,930 WARN [customMessageListenerContainer-1] o.s.j.l.DefaultMessageListenerContainer [DefaultMessageListenerContainer.java:871] Setup of JMS message listener invoker failed for destination 'queue:///CT_XSSL.CT_CPMX.MRZC_DGRM.0013' - trying to recover. Cause: JMSWMQ2008: Failed to open MQ queue 'CT_XSSL.CT_CPMX.MRZC_DGRM.0013'.; nested exception is com.ibm.mq.MQException: JMSCMQ0001: WebSphere MQ call failed with compcode '2' ('MQCC_FAILED') reason '2085' ('MQRC_UNKNOWN_OBJECT_NAME').
2019-05-10 17:54:44,930 INFO [customMessageListenerContainer-1] o.s.j.l.DefaultMessageListenerContainer [DefaultMessageListenerContainer.java:921] Successfully refreshed JMS Connection
2019-05-10 17:54:44,930 INFO [customMessageListenerContainer-2] c.h.i.c.d.x.j.MyFixedBackOff [MyFixedBackOff.java:115] MyFixedBackOff currentAttempts = 0
2019-05-10 17:54:49,938 WARN [customMessageListenerContainer-2] o.s.j.l.DefaultMessageListenerContainer [DefaultMessageListenerContainer.java:871] Setup of JMS message listener invoker failed for destination 'queue:///CT_XSSL.CT_CPMX.MRZC_DGRM.0013' - trying to recover. Cause: JMSWMQ2008: Failed to open MQ queue 'CT_XSSL.CT_CPMX.MRZC_DGRM.0013'.; nested exception is com.ibm.mq.MQException: JMSCMQ0001: WebSphere MQ call failed with compcode '2' ('MQCC_FAILED') reason '2085' ('MQRC_UNKNOWN_OBJECT_NAME').
2019-05-10 17:54:49,938 INFO [customMessageListenerContainer-2] o.s.j.l.DefaultMessageListenerContainer [DefaultMessageListenerContainer.java:921] Successfully refreshed JMS Connection
2019-05-10 17:54:49,953 INFO [customMessageListenerContainer-3] c.h.i.c.d.x.j.MyFixedBackOff [MyFixedBackOff.java:115] MyFixedBackOff currentAttempts = 0
2019-05-10 17:54:54,962 WARN [customMessageListenerContainer-3] o.s.j.l.DefaultMessageListenerContainer [DefaultMessageListenerContainer.java:871] Setup of JMS message listener invoker failed for destination 'queue:///CT_XSSL.CT_CPMX.MRZC_DGRM.0013' - trying to recover. Cause: JMSWMQ2008: Failed to open MQ queue 'CT_XSSL.CT_CPMX.MRZC_DGRM.0013'.; nested exception is com.ibm.mq.MQException: JMSCMQ0001: WebSphere MQ call failed with compcode '2' ('MQCC_FAILED') reason '2085' ('MQRC_UNKNOWN_OBJECT_NAME').
2019-05-10 17:54:54,962 INFO [customMessageListenerContainer-3] o.s.j.l.DefaultMessageListenerContainer [DefaultMessageListenerContainer.java:921] Successfully refreshed JMS Connection

如何有效地使customMessageListenerContainer成为单身人士?

1 个答案:

答案 0 :(得分:2)

是什么让您认为这不是单身?

如果您的意思是线程名称在增加(customMessageListenerContainer-2等),那是因为默认情况下使用SimpleAsyncTaskExecutor并在每次失败后使用新线程(并且前一个线程终止)。 / p>

如果需要,可以将执行程序更改为其他实现(例如线程池)。

Singleton是默认范围。