Spring Jms @JmsListener注释不起作用

时间:2014-11-17 23:46:02

标签: spring annotations jms

我有这样的方法

    @JmsListener(containerFactory = "jmsListenerContainerFactory", destination = "myQName")
      public void rceive(MySerializableObject message) {
        log.info("received: {}", message);
      }

和xml上的配置

<jms:annotation-driven />
    <bean id="jmsListenerContainerFactory" class="org.springframework.jms.config.DefaultJmsListenerContainerFactory">
        <property name="connectionFactory" ref="pooledConnectionFactory" />
        <property name="concurrency" value="3-10" />
    </bean>
<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="${brokerURL}" />
    </bean>

    <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
        <property name="maxConnections" value="5" />
        <property name="maximumActiveSessionPerConnection" value="500" />
        <property name="connectionFactory" ref="jmsConnectionFactory" />
    </bean>


    <bean id="jmsContainerFactory" class="org.springframework.jms.config.DefaultJmsListenerContainerFactory">
        <property name="connectionFactory" ref="pooledConnectionFactory" />
        <property name="concurrency" value="3-10" />
    </bean>
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate" p:connectionFactory-ref="pooledConnectionFactory" />

似乎没有创建消费者。我可以发送消息但无法接收消息。 这里有什么想法错了吗?

1 个答案:

答案 0 :(得分:4)

刚测试过您的配置,效果很好。唯一不同的是,我在该上下文中将@JmsListener作为<bean>创建了一个类:

<bean class="org.springframework.integration.jms.JmsListenerAnnotationTests$TestService"/>

@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
public class JmsListenerAnnotationTests {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private TestService testService;

    @Test
    public void test() throws InterruptedException {
        this.jmsTemplate.convertAndSend("myQName", "foo");
        assertTrue(this.testService.receiveLatch.await(10, TimeUnit.SECONDS));
        assertNotNull(this.testService.received);
        assertEquals("foo", this.testService.received);
    }

    public static class TestService {

        private CountDownLatch receiveLatch = new CountDownLatch(1);

        private Object received;

        @JmsListener(containerFactory = "jmsListenerContainerFactory", destination = "myQName")
        public void receive(String message) {
            this.received = message;
            this.receiveLatch.countDown();
        }

    }

}

<jms:annotation-driven />使@JmsListener基础结构可用,但是要强制Spring查看这些方法,无论如何你的类应该是bean。

例如<component-scan>的{​​{1}}类package

干杯!