简体   繁体   English

如何使JMS生产者使用TemporaryQueue侦听来自Consumer的响应?

[英]How to make a JMS Producer to listen to a Response back from Consumer using TemporaryQueue?

I made a Java component in a Mule Flow that sends a message to a Queue, i want to do it programatically instead of using the Mule JMS component. 我在Mule Flow中制作了一个Java组件,该组件将消息发送到队列,我想以编程方式完成此工作,而不是使用Mule JMS组件。

From the Producer i got this code: 从生产者我得到以下代码:

@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
    String payload = eventContext.getMessage().getPayloadAsString();
    JmsConnector amqConnector = (JmsConnector) eventContext.getMuleContext().getRegistry().lookupConnector("Active_MQ");
    ConnectionFactory factory = amqConnector.getConnectionFactory();
    Connection connection; 

    connection = factory.createConnection();

    try {
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue("ExampleQueue");

        MessageProducer producer = session.createProducer(queue);
        TemporaryQueue replyQueue = session.createTemporaryQueue();

        TextMessage message = session.createTextMessage(payload);
        message.setJMSReplyTo(replyQueue);
        message.setJMSDestination(queue);
        message.setJMSCorrelationID("TestID");
        producer.send(message, DeliveryMode.NON_PERSISTENT, 0, 5000);

        MessageConsumer consumer = session.createConsumer(replyQueue);
        MessageListener listener = consumer.getMessageListener();
        listener.onMessage(message);



    }finally {
        connection.close();
    }

    return eventContext;
}

Now, from the Consumer i got a normal Mule JMS Component listening to that very same Queue (ExampleQueue). 现在,从Consumer那里,我得到了一个普通的Mule JMS组件,该组件正在监听相同的Queue(ExampleQueue)。

Now, i want this to be request-response. 现在,我希望这是请求-响应。 But when i run it now, i'm getting a NullPointerException at line MessageListener listener = consumer.getMessageListener(); 但是,当我现在运行它时,我在MessageListener listener = consumer.getMessageListener();行上得到了NullPointerException MessageListener listener = consumer.getMessageListener();

How can i make the Client Producer to be listening to the TemporaryQueue until it gets the response message? 我如何使客户端生产者在收到响应消息之前一直在监听TemporaryQueue?

Thanks 谢谢

Made it :D 做到了:D

Had to add this: 必须添加以下内容:

MessageConsumer consumer = session.createConsumer(replyQueue);      
            TextMessage receivedMessage = (TextMessage) consumer.receive(); 
            text = receivedMessage.getText();

In order to get the Text from the consumer! 为了从消费者那里获得文字!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JMS使用者使用Glassfish和OpenMQ同步接收来自远程生产者的消息 - JMS Consumer to Synchronously receive Message from Remote Producer using Glassfish and OpenMQ JMS:在同一个应用程序中有一个生产者和相关的消费者有意义吗? - JMS: does it make sense to have a Producer and the related Consumer in the same application? 发生异常后,我们如何从系统中正确关闭和清理 JMS 连接、Session、消费者和生产者? - How can we close and clean properly JMS Connection, Session, Consumer and Producer from system after Exception? Spring JMS生产者和消费者交互 - Spring JMS Producer and Consumer interaction Pentaho JMS使用者-单个使用者有多个生产者 - Pentaho JMS consumer - multiple producer to a single consumer 如何使用消费者从wildfly jms队列接收消息 - How to receive message from wildfly jms queue using consumer JMS Queue,作为生产者和使用者的一个应用程序 - JMS Queue, One application as producer and consumer 如何使用信号量解决消费者/生产者的任务 - How solve consumer/producer task using semaphores 如何使用信号量解决生产者-消费者? - How to solve the producer-consumer using semaphores? Spring JMS 生产者和消费者中的相同 ThreadLocal - Same ThreadLocal in Spring JMS producer and consumer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM