JMSListener选择器不起作用 [英] JMSListener selector not working

查看:788
本文介绍了JMSListener选择器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JMS生产者发送两种消息:业务逻辑消息和心跳消息.当前,两者都由同一个接收器处理,但是我现在正尝试通过使用选择器为每个接收器提供专用的类.我遇到的问题是,每当我将选择器添加到接收器时,它就会停止接收消息.到目前为止,这就是我所拥有的.为简单起见,我只添加了心跳代码:

I have a JMS producer sending 2 kinds of messages: business logic and heart beat messages. Currently, both are handled by the same receiver, but I am now trying to have dedicated classes for each by using selectors. The problem I have is whenever I add the selector to the receiver, it stops receiving messages. Here is what I have so far. For simplicity, I have only added the code for the heart beat:

要发送消息,我有这个:

To send the message, I have this:

private void sendHeartBeat() {
    this.buildTemplate().send(new HeartbeatMessageCreator(this.someId));
}

private JmsTemplate buildTemplate() {
    if (this.cachedJmsTemplate == null) {
        final ActiveMQTopic activeMQTopic = new ActiveMQTopic(this.topic);
        this.cachedJmsTemplate = new JmsTemplate(this.config.getCachedConnectionFactory());
        this.cachedJmsTemplate.setDefaultDestination(activeMQTopic);
        this.cachedJmsTemplate.setPubSubDomain(true);
    }
    return this.cachedJmsTemplate;
}

HeartbeatMessageCreator:

HeartbeatMessageCreator:

class HeartbeatMessageCreator implements MessageCreator {
private final String someID;

HeartbeatMessageCreator(final String someID) {
    this.someID = someID;
}

@Override
public Message createMessage(final Session session) throws JMSException {
    final Serializable message = new ZHeartBeat(this.someID);
    final Message jmsMessage = session.createObjectMessage(message);
    jmsMessage.setJMSType(message.getClass().getName());
    jmsMessage.setStringProperty("InternalMessageType", "HeartBeat"); // <-- Setting my separator here

    return jmsMessage;
}

消费者如下:

@Component
public class MyListener {

    @JmsListener(destination = "${myTopic}", containerFactory = "myJmsContainer", selector = "InternalMessageType = 'HeartBeat'")
    public final void onMessage(final Message message) {

    ...

    }
}

在这种配置下,使用者永远看不到传入的消息,但是如果我从@JmsListener批注中删除选择器部分,它们将被传递.我不确定在这里我做错了什么.有什么主意吗?

In this configuration, the consumer never sees the messages coming in, but if I remove the selector part from the @JmsListener annotation, they are delivered. I am not sure what I am doing wrong here. Any idea ?

推荐答案

对我来说很好...

@SpringBootApplication
public class So46453364Application implements CommandLineRunner {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext ctx = SpringApplication.run(So46453364Application.class, args);
        Thread.sleep(10_000);
        ctx.close();
    }

    @Autowired
    private JmsTemplate template;

    @Override
    public void run(String... arg0) throws Exception {
        this.template.convertAndSend("foo", "foo", m -> {
            m.setStringProperty("foo", "bar");
            return m;
        });
        this.template.convertAndSend("foo", "foo", m -> {
            m.setStringProperty("foo", "baz");
            return m;
        });
    }

    @JmsListener(destination = "foo", selector = "foo = 'bar'")
    public void bar(Message in) {
        System.out.println("bar: " + in);
    }

    @JmsListener(destination = "foo", selector = "foo = 'baz'")
    public void baz(Message in) {
        System.out.println("baz: " + in);
    }

}

结果

bar: ActiveMQTextMessage {commandId = 5, responseRequired = true, messageId = ID:gollum.local-53472-1506533911909-4:3:1:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:gollum.local-53472-1506533911909-4:3:1:1, destination = queue://foo, transactionId = null, expiration = 0, timestamp = 1506533912140, arrival = 0, brokerInTime = 1506533912141, brokerOutTime = 1506533912144, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 1030, properties = {foo=bar}, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false, text = foo}
baz: ActiveMQTextMessage {commandId = 5, responseRequired = true, messageId = ID:gollum.local-53472-1506533911909-4:4:1:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:gollum.local-53472-1506533911909-4:4:1:1, destination = queue://foo, transactionId = null, expiration = 0, timestamp = 1506533912150, arrival = 0, brokerInTime = 1506533912150, brokerOutTime = 1506533912150, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 1030, properties = {foo=baz}, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false, text = foo}

这篇关于JMSListener选择器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆