-1

TestRequestresponse:

public static void main(String args[]) throws JMSException {
    TibjmsConnectionFactory connectionFactory = new TibjmsConnectionFactory(
            "tcp://localhost:7222");
    Connection con = connectionFactory.createConnection("admin", "");
    con.start();
    Session s = con.createSession();
    System.out.println("Successfully created JMS Connection and Session!");
    Queue q1 = s.createQueue("train.ems.queue.test");
    System.out.println(q1);
    System.out.println("Queue created!");
    TemporaryQueue tq = s.createTemporaryQueue();
    MessageProducer mp = s.createProducer(q1);
    MessageConsumer mc = s.createConsumer(tq);
    TextMessage tm = s.createTextMessage("Hi this is ABHISHEK!");
    tm.setStringProperty("Country", "IN");
    tm.setJMSCorrelationID("SENDER");
    tm.setJMSReplyTo(tq);
    mp.setTimeToLive(30000);
    mp.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    mp.setPriority(Message.DEFAULT_PRIORITY);
    mp.send(tm);
    Message recv = mc.receive(60000);
    if (recv != null) {
        System.out.println(recv.getBody(String.class));
    }
    mp.close();
    s.close();
    con.close();

}

TestAsyncReceiveMessage:

public class TestAsyncReceiveMessage implements MessageListener {
Session s;
Queue q1;

MessageProducer mp;

public static void main(String ars[]) throws JMSException {
    TestAsyncReceiveMessage obj = new TestAsyncReceiveMessage();
    obj.createSession();
    obj.createQueue();
    obj.msgConsumer();

}

private void msgConsumer() throws JMSException {
    // TODO Auto-generated method stub
    MessageConsumer mc = s.createConsumer(q1, "Country='IN'");
    mc.setMessageListener(new TestAsyncReceiveMessage());
}

private void createQueue() throws JMSException {
    // TODO Auto-generated method stub
    q1 = s.createQueue("train.ems.queue.test");
    // t1=s.createTopic("train.ems.topic.test");
}

private void createSession() throws JMSException {
    // TODO Auto-generated method stub
    TibjmsConnectionFactory connectionFactory = new TibjmsConnectionFactory(
            "tcp://localhost:7222");
    Connection con = connectionFactory.createConnection("admin", "");
    s = con.createSession();
    System.out.println("Successfully created JMS Connection and Session!");

}

public void onMessage(Message arg0) {

    try {
        System.out.println(arg0.getBody(String.class));
        TextMessage tm = s.createTextMessage("ACk");
        Queue t = (Queue) arg0.getJMSReplyTo();
        mp = s.createProducer(t);
        mp.send(tm);
    } catch (JMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

Well First it showed me NullPointerException when creating TextMessage in onMessage, i changed something and theres no more exception, but theres no putput either. Help! :)

adhameja
  • 1
  • 1

1 Answers1

0

You have not called Connection.Start() method after creating it in TestAsyncReceiveMessage code. Application has to call Connection.Start() to inform the messaging provider to start delivering messages. Otherwise messages will not be delivered to consumer.

Connection.Start is typically called after consumer is created and any message listeners are attached to consumer so that the consumer is ready to receive messages.

Shashi
  • 14,980
  • 2
  • 33
  • 52
  • I added a con.start() statement after creating the session in the create session method and got I received the original message but got a `NullPointerException` on the line where i have created the `TextMessage`to send as reply in the `onMessage` method. Ty @Shashi – adhameja Aug 06 '15 at 04:52
  • I addition to the lack of a start your on message is not thread safe. You shouldn't store the message Producer in a field, you don't close it either. The session field isn't marked transient so there is no guarantee the session is visible to another thread, which would cause a null pointer exception. – Alasdair Aug 07 '15 at 01:59