首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring @JmsListener注释不起作用

Spring @JmsListener注释不起作用
EN

Stack Overflow用户
提问于 2014-11-17 23:46:01
回答 1查看 7.1K关注 0票数 2

我有这样的方法

代码语言:javascript
运行
复制
    @JmsListener(containerFactory = "jmsListenerContainerFactory", destination = "myQName")
      public void rceive(MySerializableObject message) {
        log.info("received: {}", message);
      }

以及xml上的此类配置。

代码语言:javascript
运行
复制
<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" />

消费者似乎不是被创造出来的。我可以发送消息,但不能接收它们。知道这里出了什么问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-18 09:12:21

刚刚测试了你的配置,它运行得很好。唯一不同的是,我在这个上下文中使用@JmsListener作为<bean>创建了一个类:

代码语言:javascript
运行
复制
<bean class="org.springframework.integration.jms.JmsListenerAnnotationTests$TestService"/>

代码语言:javascript
运行
复制
@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>用于带有@Service类的package

干杯!

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26984101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档