如何从 Solace JMS 队列接收 JSON 消息,队列已经创建

时间:2020-12-19 11:18:19

标签: java spring-boot jms solace-mq

我正在尝试从 Solace JMS 队列接收 JSON 消息,但我没有收到任何消息。下面是我的代码

@Service
public class QueueConsumer {

    final String QUEUE_NAME = "test.Request.Q.V01";

    // Latch used for synchronizing between threads
    final CountDownLatch latch = new CountDownLatch(1);

    public void run(String... args) throws Exception {

        String host = "test.solace.com";
        String vpnName = "TEST_VPN";
        String username = "testVpn";
        String password = "test123";

        System.out.printf("QueueConsumer is connecting to Solace messaging at %s...%n", host);

        SolConnectionFactory connectionFactory = SolJmsUtility.createConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setVPN(vpnName);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);

        connectionFactory.setDynamicDurables(true);

        Connection connection = connectionFactory.createConnection();

        Session session = connection.createSession(false, SupportedProperty.SOL_CLIENT_ACKNOWLEDGE);

        System.out.printf("Connected to the Solace Message VPN '%s' with client username '%s'.%n", vpnName, username);

        Queue queue = session.createQueue(QUEUE_NAME);

        MessageConsumer messageConsumer = session.createConsumer(queue);

        messageConsumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                try {
                    if (message instanceof SolaceMsg) {
                        System.out.printf("TextMessage received: '%s'%n", ((SolaceMsg) message).getClass());
                    } else {
                        System.out.println("Message received.");
                    }
                    System.out.printf("Message Content:%n%s%n", SolJmsUtility.dumpMessage(message));

                    message.acknowledge();

                    latch.countDown(); // unblock the main thread
                } catch (JMSException ex) {
                    System.out.println("Error processing incoming message.");
                    ex.printStackTrace();
                }
            }
        });

        System.out.println("Start receiving messages....");
        connection.start();
        System.out.println("Awaiting message...");
        latch.await();

        connection.stop();
        messageConsumer.close();
        session.close();
        connection.close();
    }

    public static void main(String... args) throws Exception {
        new QueueConsumer().run(args);
    }
}

我的消息类型是下面的 JSON 广告,我为此创建了一个 POJO。

  {
    "customerDetails": {
        "customerID": "0001234",
        "customerName": "John"
    }
}

我收到一条警告说响应 - 400 队列已经存在,因为它是一个现有队列,我没有收到任何消息。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

您的代码片段看起来正确。您可以登录到事件代理的 PubSub+ 管理器,以验证客户端是否绑定到正确的队列,以及消息是否已成功发布到队列并等待使用。您还可以启用 Solace JMS API 日志记录以了解有关应用程序正在执行的操作的更多信息:https://docs.solace.com/Solace-JMS-API/Code-and-Compile-Guideli.htm