Temporary destinations

Run
How to run the sample
The source code for this sample can be found in the javaee7-samples GitHub repository. The first thing we need to do is to get the source by downloading the repository and then go into the samples folder:
git clone git://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples/jms/temp-destination/
Now we are ready to start testing. You can run all the tests in this sample by executing:
mvn test
Or you can run individual tests by executing one of the following:
mvn test -Dtest=TempQueueTest

Request/Response over JMS

TempQueueTest

Temporary queues are JMS queues that exist for the lifetime of single JMS connection. Also the reception of the messages is exclusive to the connection, therefore no reasonable use case exist for temporary topic within Java EE container, as connection is exclusive to single component.

Temporary queues are usually used as reply channels for request / response communication over JMS.

In this test we created a server component RequestResponseOverJMS, that listens on a Queue and passes the response to the destination specified in JMSReplyTo header of the message.

@Override
public void onMessage(Message message) {
    try {
        Destination replyTo = message.getJMSReplyTo(); (1)
        if (replyTo == null) {
            return;
        }
        TextMessage request = (TextMessage) message;
        String payload = request.getText();            (2)

        System.out.println("Got request: "+payload);

        String response = "Processed: "+payload;       (3)
        jms.createProducer().send(replyTo, response);  (4)
    } catch (JMSException e) {
        e.printStackTrace();
    }
}
  1. get the destination for the response

  2. read the payload

  3. process the request

  4. send the response

JmsClient is a client to this server, and has to be non transactional, otherwise the request would be first sent upon commit, i. e. after the business method finishes. That would be too late. We need to send the message immediately, and wait for the response to arrive.

@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)        (1)
public String process(String request) {

    TextMessage requestMessage = jms.createTextMessage(request);
    TemporaryQueue responseQueue = jms.createTemporaryQueue();
    jms.createProducer()
            .setJMSReplyTo(responseQueue)                            (2)
            .send(requestQueue, requestMessage);                     (3)

    try (JMSConsumer consumer = jms.createConsumer(responseQueue)) { (4)

        String response = consumer.receiveBody(String.class, 2000);  (5)

        if (response == null) {                                      (6)
            throw new IllegalStateException("Message processing timed out");
        } else {
            return response;
        }
    }
}
  1. we need to send message in the middle of the method, therefore we cannot be transactional

  2. set the temporary queue as replyToDestination

  3. immediately send the request message

  4. listen on the temporary queue

  5. wait for a TextMessage to arrive

  6. receiveBody returns null in case of timeout

@Deployment
public static WebArchive deployment() {
    return ShrinkWrap.create(WebArchive.class)
            .addClasses(RequestResponseOverJMS.class, JmsClient.class, Resources.class);
}

We invoke the client, and verify that the response is processed

@Test
public void testRequestResposne() {
    String response = client.process("Hello");
    Assert.assertEquals("Processed: Hello", response);
}

Share the Knowledge

Find this sample useful? Share on

There's a lot more about JavaEE to cover. If you're ready to learn more, check out the other available samples.

Help Improve

Find a bug in the sample? Something missing? You can fix it by editing the source, making the correction and sending a pull request. Or report the problem to the issue tracker

Recent Changelog

  • Oct 05, 2014: #252 fixed arquillian configuration for the jobs by John D. Ament
  • Mar 20, 2014: Update arquillian.xml cr1 references to final by Aslak Knutsen
  • Jan 12, 2014: Fix tempqueuetest for glassfish by pdudits
  • Jan 12, 2014: Improving documentation for tempqueuetest by pdudits
  • Jan 11, 2014: Issue #80: test for temporary queue by pdudits
  • Sep 12, 2013: Using cdi 1.1 "all" style beans.xml by Arun Gupta
  • Aug 29, 2013: Adding a message that this sample is not working right now by Arun Gupta
  • Aug 27, 2013: Moving the source code from http://svn.java.net/svn/glassfish~svn/branches/arun/javaee7-samples/samples/ by Arun Gupta
How to help improve this sample
The source code for this sample can be found in the javaee7-samples GitHub repository. The first thing you need to do is to get the source by downloading the repository and then go into the samples folder:
git clone git://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples/jms/temp-destination/

Do the changes as you see fit and send a pull request!

Good Luck!