无法使activemq消费者出队列

时间:2018-10-16 13:28:57

标签: spring-boot activemq jms-topic

我正在使用activemq和spring boot创建一个JMS聊天应用程序。我正在尝试将消息从生产者发送到多个订户。我能够发送消息,即消息已入队。但是在我的接收方中,消息无法出队。我正在使用以下代码将消息从生产者传递到多个订户。

public class WelcomeController implements MessageListener {

    public static Boolean TRANSACTIONAL = false;
    public static String TOPIC_NAME = "firstTopic";

    public static String BROKER_URL = "tcp://localhost:61616";
    public static String BROKER_USERNAME = "admin";
    public static String BROKER_PASSWORD = "admin";


    public void createProducer() throws JMSException {
       Connection connection = null;
        Session session = null;
        try {
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
            connectionFactory.setBrokerURL(BROKER_URL);
            connectionFactory.setPassword(BROKER_USERNAME);
            connectionFactory.setUserName(BROKER_PASSWORD);

            connection = connectionFactory.createConnection();
            connection.setClientID("CircliTopic");
            connection.start();
             session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);


            for (int i = 1; i <= 3; i++) {
               session = connection.createSession(TRANSACTIONAL,
                  Session.AUTO_ACKNOWLEDGE);
              Topic destination = session.createTopic(TOPIC_NAME);
              MessageProducer producer = session.createProducer(destination);
              TextMessage message = session.createTextMessage();
              message.setText( "My text message was send and received");//
                System.out.println("Sending text '" + message + "'");
                producer.send(message);
              MessageConsumer consumer = session
                  .createDurableSubscriber(destination, "Listener" + i);

              consumer.setMessageListener(new WelcomeController());

            }



        } finally {
            connection.close();
        }`
}

@Override
public void onMessage(Message message) {

     try {

          if (message instanceof TextMessage) {
            TextMessage text = (TextMessage) message;
            System.out.println(" - Consuming text msg: " + text.getText());
          } else if (message instanceof ObjectMessage) {
            ObjectMessage objmsg = (ObjectMessage) message;
            Object obj = objmsg.getObject();
            System.out.println(" - Consuming object msg: " + obj);
          } else {
            System.out.println(
                " - Unrecognized Message type " + message.getClass());
          }
        } catch (JMSException e) {
          e.printStackTrace();
        }

}

我能够在控制台中使用文本消息,但是我的消息不会出队给订阅者,而且我的activemq服务器消息也不会出队。

1 个答案:

答案 0 :(得分:0)

仅在发送邮件后创建主题订阅,该订阅将不起作用,因为这些是主题,而没有订阅的主题只是丢弃所有发送给它的消息。您需要在发送任何消息之前建立持久的主题订阅,或者如果您的设计允许,则切换到队列,因为队列将存储发送给它的消息,直到被消耗为止。

在不知道您的要求的情况下很难说更多,但是似乎您需要花更多的时间来了解主题的工作原理。