How to receive JSON message from a Solace JMS queue, the queue is already created

1k views Asked by At

I am trying to receive JSON messages from a Solace JMS queue but I am not receiving any message. Below is my code

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

My message type is JSON ad below, and I have created a POJO for this.

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

I am getting one warning saying Response - 400 Queue already exists as it is an existing queue, and I am not receiving any messages. What am I doing wrong here?

1

There are 1 answers

0
Alexandra Masse On

Your code snippet looks correct. You can log on to the PubSub+ Manager of your event broker to verify that the client is binding to the correct queue and that the messages were successfully published to the queue and are waiting to be consumed. You can also enable Solace JMS API logging to understand more about what the application is doing: https://docs.solace.com/Solace-JMS-API/Code-and-Compile-Guideli.htm