spring-jms 选择器不适用于服务总线

spring-jms selector doesn't work with servicebus

我将 spring-jms 与 azure 的服务总线一起使用。我正在尝试使用选择器来区分消息类型,但无法正常工作。这是我的代码。

@Component
@Slf4j
public class MessageTestRunner implements CommandLineRunner {

    private static final String QUEUE_NAME = "lva-test-queue";
    private static final String PING_SELECTOR = "selector = 'PING'";
    private static final String PONG_SELECTOR = "selector = 'PONG'";

    private static final String SB_SCHEDULED_ENQUEUE_HEADER = "x-opt-scheduled-enqueue-time";


    private final JmsTemplate jmsTemplate;

    public MessageTestRunner(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    @Override
    public void run(String... args) throws Exception {
        jmsTemplate.convertAndSend(QUEUE_NAME, ping(), m -> {
            m.setStringProperty("selector", "PING");
            return m;
        });
        jmsTemplate.convertAndSend(QUEUE_NAME, pong(), m -> {
            m.setStringProperty("selector", "PONG");
            return m;
        });
    }

    private PingMessage ping() {
        final PingMessage msg = new PingMessage();
        msg.setAt(ZonedDateTime.now());
        return msg;
    }

    private PongMessage pong() {
        final PongMessage msg = new PongMessage();
        msg.setAt(ZonedDateTime.now());
        return msg;
    }


    @JmsListener(destination = QUEUE_NAME, selector = PING_SELECTOR, containerFactory = JMS_FACTORY_NAME)
    public void handle(PingMessage message) {
        log.debug("Handling ping message [{}]", message);
    }

    @JmsListener(destination = QUEUE_NAME, selector = PONG_SELECTOR, containerFactory = JMS_FACTORY_NAME)
    public void handle(PongMessage message) {
        log.debug("Handling pong message [{}]", message);
    }

}

这是我得到的异常

org.springframework.jms.listener.adapter.ListenerExecutionFailedException: Listener method could not be invoked with incoming message
Endpoint handler details:
Method [public void com.example.MessageTestRunner.handle(com.example.data.model.PingMessage)]
Bean [com.example.MessageTestRunner@7aae1170]
; nested exception is org.springframework.messaging.converter.MessageConversionException: Cannot convert from [com.example.data.model.PongMessage] to [com.example.data.model.PingMessage] for org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener$MessagingMessageConverterAdapter$LazyResolutionMessage@72eb85b7, failedMessage=org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener$MessagingMessageConverterAdapter$LazyResolutionMessage@72eb85b7
    at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:118) ~[spring-jms-5.3.10.jar:5.3.10]
    at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:77) ~[spring-jms-5.3.10.jar:5.3.10]
    at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:736) ~[spring-jms-5.3.10.jar:5.3.10]
    at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:696) ~[spring-jms-5.3.10.jar:5.3.10]
    at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:674) ~[spring-jms-5.3.10.jar:5.3.10]
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:318) ~[spring-jms-5.3.10.jar:5.3.10]
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:257) ~[spring-jms-5.3.10.jar:5.3.10]

是否有一些配置/...我应该更改哪个以启用选择器?

编辑: 问题似乎是当我禁用发送 PONG 消息时,PONG 处理程序也接收到 PING 消息,我可以在日志中看到错误。但是我希望 PING 消息永远不会发送到 PONG 处理程序,因为存在不允许这样做的选择器。

编辑2: 似乎选择器仅在支持 JMS 2.0 (link)

service bus premium tier 中受支持

所以问题出在服务总线本身。我们在不支持选择器的 标准 层中使用服务总线。更多详情 here and here