JMS请求回复模式,无输出

时间:2015-08-05 11:52:40

标签: request jms listener message reply

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

}

嗯首先它在onMessage中创建TextMessage时显示NullPointerException,我改变了一些东西,并且没有更多异常,但也没有putput。救命! :)

1 个答案:

答案 0 :(得分:0)

Connection.Start()代码中创建后,您尚未调用TestAsyncReceiveMessage方法。应用程序必须调用Connection.Start()以通知消息传递提供程序开始传递消息。否则消息将不会传递给消费者。

Connection.Start通常在创建消费者并且任何消息监听器附加到消费者之后调用,以便消费者准备好接收消息。