java - JMSListener 选择器不工作

标签 java jms spring-jms

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

要发送消息,我有这个:

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;
}

心跳消息创建者:

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 注释中删除选择器部分,它们就会被传递。我不确定我在这里做错了什么。有什么想法吗?

最佳答案

对我来说效果很好...

@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}

关于java - JMSListener 选择器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46453364/

相关文章:

java - 将图标设置为 JFrame

java - 如何将 Java JMS 与 MQSeries 一起使用

concurrency - 锁定 Camel 处理器

Spring JMS和Websphere MQ

java - 我如何测试阻塞队列实现的同步

java - 游戏中的注销流程!框架2.0,java

java - 如何跟踪 Android 中 RecyclerView 中的选定项目?

java - 使用大日志启动时 HornetQ OutOfMemory

java - Spring JMS 监听器容器并发属性不起作用

java - JmsTemplate 与 IBM MQ 队列的 CachingConnectionFactory 连接恢复