File tree Expand file tree Collapse file tree 3 files changed +16
-18
lines changed
main/java/org/javaee7/jms/temp/destination
test/java/org/javaee7/jms/temp/destination Expand file tree Collapse file tree 3 files changed +16
-18
lines changed Original file line number Diff line number Diff line change 1515 * @author Patrik Dudits
1616 */
1717@ Stateless
18- @ TransactionAttribute (TransactionAttributeType .NOT_SUPPORTED )
1918public class JmsClient {
2019
21- @ Resource (name = Resources .REQUEST_QUEUE )
20+ @ Resource (lookup = Resources .REQUEST_QUEUE )
2221 Queue requestQueue ;
2322
2423 @ Inject
2524 JMSContext jms ;
2625
26+ @ TransactionAttribute (TransactionAttributeType .NOT_SUPPORTED ) // <1> we need to send message in the middle of the method, therefore we cannot be transactional
2727 public String process (String request ) {
2828
29- // prepare the request message
3029 TextMessage requestMessage = jms .createTextMessage (request );
3130 TemporaryQueue responseQueue = jms .createTemporaryQueue ();
32-
33- // send the request
3431 jms .createProducer ()
35- .setJMSReplyTo (responseQueue )
36- .send (requestQueue , requestMessage );
32+ .setJMSReplyTo (responseQueue ) // <2> set the temporary queue as replyToDestination
33+ .send (requestQueue , requestMessage ); // <3> immediately send the request message
3734
38- // start listening on the temp queue for response
39- try (JMSConsumer consumer = jms .createConsumer (responseQueue )) {
35+ try (JMSConsumer consumer = jms .createConsumer (responseQueue )) { // <4> listen on the temporary queue
4036
41- // wait for the response
42- String response = consumer .receiveBody (String .class , 2000 );
37+ String response = consumer .receiveBody (String .class , 2000 ); // <5> wait for a +TextMessage+ to arrive
4338
44- if (response == null ) {
39+ if (response == null ) { // <6> +receiveBody+ returns +null+ in case of timeout
4540 throw new IllegalStateException ("Message processing timed out" );
4641 } else {
4742 return response ;
Original file line number Diff line number Diff line change @@ -22,18 +22,17 @@ public class RequestResponseOverJMS implements MessageListener {
2222 @ Override
2323 public void onMessage (Message message ) {
2424 try {
25- Destination replyTo = message .getJMSReplyTo ();
25+ Destination replyTo = message .getJMSReplyTo (); // <1> get the destination for the response
2626 if (replyTo == null ) {
27- // no response required, finish now.
2827 return ;
2928 }
3029 TextMessage request = (TextMessage ) message ;
31- String payload = request .getText ();
30+ String payload = request .getText (); // <2> read the payload
3231
3332 System .out .println ("Got request: " +payload );
3433
35- String response = "Processed: " +payload ;
36- jms .createProducer ().send (replyTo , response );
34+ String response = "Processed: " +payload ; // <3> process the request
35+ jms .createProducer ().send (replyTo , response ); // <4> send the response
3736 } catch (JMSException e ) {
3837 e .printStackTrace ();
3938 }
Original file line number Diff line number Diff line change 1414 * Temporary queues are JMS queues that exist for the lifetime of single JMS connection.
1515 * Also the reception of the messages is exclusive to the connection, therefore no
1616 * reasonable use case exist for temporary topic within Java EE container, as connection
17- * is usually exclusive to single component.
17+ * is exclusive to single component.
1818 *
1919 * Temporary queues are usually used as reply channels for request / response communication
2020 * over JMS.
@@ -27,11 +27,15 @@ public class TempQueueTest {
2727 * listens on a Queue and passes the response to the destination specified in
2828 * +JMSReplyTo+ header of the message.
2929 *
30+ * include::RequestResponseOverJMS#onMessage[]
31+ *
3032 * +JmsClient+ is a client to this server, and has to be non transactional,
3133 * otherwise the request would be first sent upon commit, i. e. after the
3234 * business method finishes. That would be too late. We need to send the message
3335 * immediately, and wait for the response to arrive.
3436 *
37+ * include::JmsClient#process[]
38+ *
3539 */
3640 @ Deployment
3741 public static WebArchive deployment () {
You can’t perform that action at this time.
0 commit comments