Spring jms如何在持久主题监听器之间分发消息?

时间:2017-08-23 11:05:54

标签: java spring jms spring-jms jms-topic

原始jms代码:

    <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Add Bills </h4>
        </div>
        <div class="modal-body">
          <form method="post" action="#" enctype="multipart/form-data">
        <div class="form-group  col-md-6 col-sm-6">


         <?php 
    include('dbConfig.php'); 
    $query = mysql_query("SELECT * FROM `brands` ORDER BY brands_name  "); 
    $rowCount =  mysql_num_rows($query);
    ?>
    <center><select name="brand_name" id="course" class="input-xxlarge form-control">
        <option value="">Select Brands</option>
        <?php
        if($rowCount > 0){
            while($row =mysql_fetch_array($query)){ 
                echo '<option value="'.$row['brands_id'].'">'.$row['brands_name'].'</option>';
            }
        }else{
            echo '<option value="">Brands not available</option>';
        }
        ?>
          </select></center>
          </div>
        <center><div class ="form-group col-md-6 col-sm-6">

     <select name="product" id="subject" class="input-xxlarge form-control">
        <option value="">Select Product first</option>
       </select>
      </div></center>
        <center><div class ="form-group col-md-6 col-sm-6">

       <select name="model_no" id="testname" class="input-xxlarge form-control">
        <option value="">Select Model No first</option>
       </select>
      </div></center>

    <center><div class ="form-group col-md-6 col-sm-6">
    <input type="file" name="billscopy" class="form-control" accept="image/*" capture="camera" data-name="full-name" required></center>
   <br></center>
                <!---<center><input class="form-control" id="full-name-field" type="file" name="billscopy" accept="image/*" capture="camera" data-name="full-name" required>
                <div class="separator-fields"></div></center>
              </div>--->


        </div>
        <div class="modal-footer">
          <button type="submit" class="btn btn-success btn btn-lg" name="uploads">Upload</button>
        </div>
            </form>
        </div>

      </div>

当我使用以下代码时,每个侦听器都会获取所有消息。如果我使用相同的订阅名称 - 应用程序不会启动。

另一方面,我编写了使用spring jms的代码:

配置:

TopicSubscriber durSubscriber1 = receiverSession.createDurableSubscriber(topic,"subscription_1");
durSubscriber1.setMessageListener(new MessageListener() {
    @Override
    public void onMessage(Message message) {

        RMQTextMessage rmqTextMessage = ((RMQTextMessage) message);
        try {
            System.out.println("sub_1:" + rmqTextMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
});


TopicSubscriber durSubscriber2 = receiverSession.createDurableSubscriber(topic,"subscription_2");
    durSubscriber2.setMessageListener(new MessageListener() {
        @Override
        public void onMessage(Message message) {

            RMQTextMessage rmqTextMessage = ((RMQTextMessage) message);
            try {
                System.out.println("sub_2:" + rmqTextMessage.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }

        }
});

监听器:

@Bean
public JmsListenerContainerFactory<?> myFactory(DefaultJmsListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    // This provides all boot's default to this factory, including the message converter
    configurer.configure(factory, connectionFactory);
    // You could still override some of Boot's default if necessary.
    factory.setPubSubDomain(true);
    factory.setSubscriptionDurable(true);
    return factory;
}

在这种情况下,两个听众都会分割消息。我的意思是如果生产者发送10条消息,那么listener_1将获得N条消息,listener_2将获得M条消息 M + N = 10

请解释2个代码段的区别。 你能为spring-jms版本提供相应的jms代码吗?

1 个答案:

答案 0 :(得分:2)

这是由于Rabbit JMS客户端将JMS映射到本机AMQP的方式。您必须为每个侦听器提供一个订阅名称;否则他们将在同一队列中竞争消息。

@JmsListener(destination = "my_topic_new", containerFactory = "myFactory", subscription = "foo")
public void receiveTopic(String email) {
    System.out.println("list_1:" + email);
}

@JmsListener(destination = "my_topic_new", containerFactory = "myFactory", subscription = "bar")
public void receiveTopicDup(String email) {
    System.out.println("list_2:" + email);
}

这仅适用于持久订阅。