2017-09-27 80 views
0

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

要发送信息,我有这样的:

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:

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) { 

    ... 

    } 
} 

在此配置中,消费者从未看到消息进入,但如果我删除了sele ctor部分来自@JmsListener注释,它们被交付。我不确定我在这里做错了什么。任何想法 ?

回答

0

它工作正常,我...

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

你是绝对正确的。我在我的案例中发现了这个问题:在生产者和消费者之间有一个组件,它应该简单地转发消息。但是它的完成方式意味着消息属性没有达到最终目的地。 Thx为你的时间。 –