简体   繁体   English

无法使activemq消费者出队

[英]unable to make activemq consumer to deque

I am creating a JMS chat application using activemq and spring boot. 我正在使用activemq和spring boot创建一个JMS聊天应用程序。 I am trying to send message from producer to multiple subscribers. 我正在尝试将消息从生产者发送到多个订户。 I am able to send message ie message is en-queued. 我能够发送消息,即消息已排队。 but in my receiver part message is unable to de-queue.` I am using the below code for communicating message from producer to multiple subscribers. 但是在我的接收方中,消息无法出队。我正在使用以下代码将消息从生产者传递到多个订户。

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

}

I am able to get consuming text message in my console but my message is not de-queued to the subscribers and also in my activemq server message is not dequeued. 我可以在控制台中使用文本消息,但是我的消息不会出队给订阅者,并且我的activemq服务器消息也不会出队。

You are creating a Topic subscription only after the message has been sent and that won't work because these are Topics and a Topic with no subscriptions simply discards all messages sent to it. 您仅在发送邮件后创建主题订阅,该订阅将不起作用,因为它们是主题,而没有订阅的主题只是丢弃所有发送给它的消息。 You need to establish a durable Topic subscription prior to any messages being sent, or switch to Queues if your design allows as a Queue will store a message sent to it until consumed. 您需要在发送任何消息之前建立持久的主题订阅,或者如果您的设计允许,则切换到队列,因为队列将存储发送给它的消息,直到被消耗为止。

It is hard to say more without knowing your requirements but it seems you need to spend a little bit more time understanding how Topics work. 在不知道您的要求的情况下很难说更多,但是似乎您需要花更多的时间来了解主题的工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM