Skip to content

Commit b78caf7

Browse files
committed
Increased JMS temp destination sample timeout and small cleanups
1 parent e84e63e commit b78caf7

5 files changed

Lines changed: 79 additions & 71 deletions

File tree

jms/temp-destination/pom.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45

56
<parent>
67
<groupId>org.javaee7</groupId>
78
<artifactId>jms</artifactId>
89
<version>1.0-SNAPSHOT</version>
9-
</parent>
10+
</parent>
11+
1012
<artifactId>jms-temp-destination</artifactId>
1113
<packaging>war</packaging>
1214
<name>Java EE 7 Sample: jms - temp-destination</name>
15+
1316
<description>Request/Response over JMS</description>
1417
</project>
Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,53 @@
11
package org.javaee7.jms.temp.destination;
22

3+
import static javax.ejb.TransactionAttributeType.NOT_SUPPORTED;
4+
35
import javax.annotation.Resource;
46
import javax.ejb.Stateless;
57
import javax.ejb.TransactionAttribute;
6-
import javax.ejb.TransactionAttributeType;
78
import javax.inject.Inject;
8-
import javax.jms.*;
9-
import java.lang.IllegalStateException;
9+
import javax.jms.JMSConsumer;
10+
import javax.jms.JMSContext;
11+
import javax.jms.Queue;
12+
import javax.jms.TemporaryQueue;
13+
import javax.jms.TextMessage;
1014

1115
/**
12-
* Client receiving response to a message via temporary queue.
13-
* The client has to be non-trasactional, as we need to send message in the middle
14-
* of the method.
16+
* Client receiving response to a message via temporary queue. The client has to be non-trasactional, as we need to send
17+
* message in the middle of the method.
18+
*
1519
* @author Patrik Dudits
1620
*/
1721
@Stateless
1822
public class JmsClient {
1923

20-
@Resource(lookup = Resources.REQUEST_QUEUE)
21-
Queue requestQueue;
24+
@Resource(lookup = Resources.REQUEST_QUEUE)
25+
private Queue requestQueue;
2226

23-
@Inject
24-
JMSContext jms;
27+
@Inject
28+
private JMSContext jms;
2529

26-
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
27-
// <1> we need to send message in the middle of the method, therefore we cannot be transactional
28-
public
29-
String process(String request) {
30+
// <1> we need to send message in the middle of the method, therefore we cannot be transactional
31+
@TransactionAttribute(NOT_SUPPORTED)
32+
public String process(String request) {
3033

31-
TextMessage requestMessage = jms.createTextMessage(request);
32-
TemporaryQueue responseQueue = jms.createTemporaryQueue();
33-
jms.createProducer()
34-
.setJMSReplyTo(responseQueue) // <2> set the temporary queue as replyToDestination
35-
.send(requestQueue, requestMessage); // <3> immediately send the request message
34+
TextMessage requestMessage = jms.createTextMessage(request);
35+
TemporaryQueue responseQueue = jms.createTemporaryQueue();
36+
37+
jms.createProducer()
38+
.setJMSReplyTo(responseQueue) // <2> set the temporary queue as replyToDestination
39+
.send(requestQueue, requestMessage); // <3> immediately send the request message
3640

37-
try (JMSConsumer consumer = jms.createConsumer(responseQueue)) { // <4> listen on the temporary queue
41+
try (JMSConsumer consumer = jms.createConsumer(responseQueue)) { // <4> listen on the temporary queue
3842

39-
String response = consumer.receiveBody(String.class, 2000); // <5> wait for a +TextMessage+ to arrive
43+
String response = consumer.receiveBody(String.class, 20000); // <5> wait for a +TextMessage+ to arrive
4044

41-
if (response == null) { // <6> +receiveBody+ returns +null+ in case of timeout
42-
throw new IllegalStateException("Message processing timed out");
43-
} else {
44-
return response;
45-
}
46-
}
47-
}
45+
if (response == null) { // <6> +receiveBody+ returns +null+ in case of timeout
46+
throw new IllegalStateException("Message processing timed out");
47+
}
48+
49+
return response;
50+
51+
}
52+
}
4853
}

jms/temp-destination/src/main/java/org/javaee7/jms/temp/destination/RequestResponseOverJMS.java

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,41 @@
33
import javax.ejb.ActivationConfigProperty;
44
import javax.ejb.MessageDriven;
55
import javax.inject.Inject;
6-
import javax.jms.*;
6+
import javax.jms.Destination;
7+
import javax.jms.JMSContext;
8+
import javax.jms.JMSException;
9+
import javax.jms.Message;
10+
import javax.jms.MessageListener;
11+
import javax.jms.TextMessage;
712

813
/**
914
* @author Patrik Dudits
1015
*/
11-
@MessageDriven(activationConfig = {
12-
@ActivationConfigProperty(propertyName = "destinationLookup",
13-
propertyValue = Resources.REQUEST_QUEUE),
14-
@ActivationConfigProperty(propertyName = "destinationType",
15-
propertyValue = "javax.jms.Queue"),
16-
})
16+
@MessageDriven(activationConfig = {
17+
@ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = Resources.REQUEST_QUEUE),
18+
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), })
1719
public class RequestResponseOverJMS implements MessageListener {
1820

19-
@Inject
20-
JMSContext jms;
21+
@Inject
22+
private JMSContext jms;
2123

22-
@Override
23-
public void onMessage(Message message) {
24-
try {
25-
Destination replyTo = message.getJMSReplyTo(); // <1> get the destination for the response
26-
if (replyTo == null) {
27-
return;
28-
}
29-
TextMessage request = (TextMessage) message;
30-
String payload = request.getText(); // <2> read the payload
24+
@Override
25+
public void onMessage(Message message) {
26+
try {
27+
Destination replyTo = message.getJMSReplyTo(); // <1> get the destination for the response
28+
if (replyTo == null) {
29+
return;
30+
}
31+
32+
TextMessage request = (TextMessage) message;
33+
String payload = request.getText(); // <2> read the payload
3134

32-
System.out.println("Got request: " + payload);
35+
System.out.println("Got request: " + payload);
3336

34-
String response = "Processed: " + payload; // <3> process the request
35-
jms.createProducer().send(replyTo, response); // <4> send the response
36-
} catch (JMSException e) {
37-
e.printStackTrace();
38-
}
39-
}
37+
String response = "Processed: " + payload; // <3> process the request
38+
jms.createProducer().send(replyTo, response); // <4> send the response
39+
} catch (JMSException e) {
40+
e.printStackTrace();
41+
}
42+
}
4043
}
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
package org.javaee7.jms.temp.destination;
22

33
import javax.jms.JMSDestinationDefinition;
4-
import javax.jms.JMSDestinationDefinitions;
54

65
/**
76
* Application scoped JMS resources for the samples.
7+
*
88
* @author Patrik Dudits
99
*/
10-
@JMSDestinationDefinitions({
11-
@JMSDestinationDefinition(
12-
name = Resources.REQUEST_QUEUE,
13-
resourceAdapter = "jmsra",
14-
interfaceName = "javax.jms.Queue",
15-
destinationName = "requestQueue",
16-
description = "Queue for service requests"),
17-
})
10+
@JMSDestinationDefinition(
11+
name = Resources.REQUEST_QUEUE,
12+
interfaceName = "javax.jms.Queue",
13+
destinationName = "requestQueue",
14+
description = "Queue for service requests")
1815
public class Resources {
19-
public static final String REQUEST_QUEUE = "java:global/jms/requestQueue";
16+
public static final String REQUEST_QUEUE = "java:global/jms/requestQueue";
2017
}

jms/temp-destination/src/test/java/org/javaee7/jms/temp/destination/TempQueueTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package org.javaee7.jms.temp.destination;
22

3+
import static org.junit.Assert.assertEquals;
4+
5+
import javax.ejb.EJB;
6+
37
import org.jboss.arquillian.container.test.api.Deployment;
48
import org.jboss.arquillian.junit.Arquillian;
59
import org.jboss.shrinkwrap.api.ShrinkWrap;
610
import org.jboss.shrinkwrap.api.spec.WebArchive;
7-
import org.junit.Assert;
811
import org.junit.Test;
912
import org.junit.runner.RunWith;
1013

11-
import javax.ejb.EJB;
12-
1314
/**
1415
* Temporary queues are JMS queues that exist for the lifetime of single JMS connection.
1516
* Also the reception of the messages is exclusive to the connection, therefore no
@@ -44,14 +45,13 @@ public static WebArchive deployment() {
4445
}
4546

4647
@EJB
47-
JmsClient client;
48+
private JmsClient client;
4849

4950
/**
5051
* We invoke the client, and verify that the response is processed
5152
*/
5253
@Test
5354
public void testRequestResposne() {
54-
String response = client.process("Hello");
55-
Assert.assertEquals("Processed: Hello", response);
55+
assertEquals("Processed: Hello", client.process("Hello"));
5656
}
5757
}

0 commit comments

Comments
 (0)