Testing JMS Topic/Queue

Hi Everyone,

I’m am very new to JMS and I was wondering if there is a way we can do an automated testing for Topic/Queue. Just a test where a message is published successfully to the Topic/Queue.

Thanks in advance
Jay

Below is some sample of test code where we can send some messages to topic and check the count of them on listener:


    Connection con = null;
    Session session = null;
    ConnectionFactory connectionFactory = new ConnectionFactoryImpl("nsp://localhost:9000");
    con = connectionFactory.createConnection();
    con.start();
    session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTopic(topicName);

    new TestMessageListener(rname, topic.getTopicName(), true)

    MessageProducer producer = session.createProducer(topic);
    for (int j = 0; j < MESSAGE_COUNT; j++) {
        TextMessage message = session.createTextMessage();
        message.setText("publishToTopic:" + j);
        producer.send(topic, message);
    }

    assertEquals("listener should receive all messages", MESSAGE_COUNT, listener.getCount());

HTH,
Chandra

1 Like