Skip to content

Commit aa9b25f

Browse files
committed
Issue #80: Test for temporary queue
1 parent 84cc953 commit aa9b25f

File tree

9 files changed

+189
-168
lines changed

9 files changed

+189
-168
lines changed

jms/temp-destination/pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
<version>1.0-SNAPSHOT</version>
88
</parent>
99

10-
<groupId>org.javaee7.jms</groupId>
1110
<artifactId>temp-destination</artifactId>
12-
<version>1.0-SNAPSHOT</version>
1311
<packaging>war</packaging>
12+
13+
<name>Temporary destinations</name>
14+
<description>Request/Response over JMS</description>
1415
</project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.javaee7.jms.temp.destination;
2+
3+
import javax.annotation.Resource;
4+
import javax.ejb.Stateless;
5+
import javax.ejb.TransactionAttribute;
6+
import javax.ejb.TransactionAttributeType;
7+
import javax.inject.Inject;
8+
import javax.jms.*;
9+
import java.lang.IllegalStateException;
10+
11+
/**
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.
15+
* @author Patrik Dudits
16+
*/
17+
@Stateless
18+
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
19+
public class JmsClient {
20+
21+
@Resource(name = Resources.REQUEST_QUEUE)
22+
Queue requestQueue;
23+
24+
@Inject
25+
JMSContext jms;
26+
27+
public String process(String request) {
28+
29+
// prepare the request message
30+
TextMessage requestMessage = jms.createTextMessage(request);
31+
TemporaryQueue responseQueue = jms.createTemporaryQueue();
32+
33+
// send the request
34+
jms.createProducer()
35+
.setJMSReplyTo(responseQueue)
36+
.send(requestQueue, requestMessage);
37+
38+
// start listening on the temp queue for response
39+
try (JMSConsumer consumer = jms.createConsumer(responseQueue)) {
40+
41+
// wait for the response
42+
String response = consumer.receiveBody(String.class, 2000);
43+
44+
if (response == null) {
45+
throw new IllegalStateException("Message processing timed out");
46+
} else {
47+
return response;
48+
}
49+
}
50+
}
51+
}

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

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.javaee7.jms.temp.destination;
2+
3+
import javax.ejb.ActivationConfigProperty;
4+
import javax.ejb.MessageDriven;
5+
import javax.inject.Inject;
6+
import javax.jms.*;
7+
8+
/**
9+
* @author Patrik Dudits
10+
*/
11+
@MessageDriven(activationConfig = {
12+
@ActivationConfigProperty(propertyName = "destinationLookup",
13+
propertyValue = Resources.REQUEST_QUEUE),
14+
@ActivationConfigProperty(propertyName = "destinationType",
15+
propertyValue = "javax.jms.Queue"),
16+
})
17+
public class RequestResponseOverJMS implements MessageListener {
18+
19+
@Inject
20+
JMSContext jms;
21+
22+
@Override
23+
public void onMessage(Message message) {
24+
try {
25+
Destination replyTo = message.getJMSReplyTo();
26+
if (replyTo == null) {
27+
// no response required, finish now.
28+
return;
29+
}
30+
TextMessage request = (TextMessage) message;
31+
String payload = request.getText();
32+
33+
System.out.println("Got request: "+payload);
34+
35+
String response = "Processed: "+payload;
36+
jms.createProducer().send(replyTo, response);
37+
} catch (JMSException e) {
38+
e.printStackTrace();
39+
}
40+
}
41+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.javaee7.jms.temp.destination;
2+
3+
import javax.jms.JMSDestinationDefinition;
4+
import javax.jms.JMSDestinationDefinitions;
5+
6+
/**
7+
* Application scoped JMS resources for the samples.
8+
* @author Patrik Dudits
9+
*/
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+
})
18+
public class Resources {
19+
public static final String REQUEST_QUEUE = "java:global/jms/requestQueue";
20+
}

jms/temp-destination/src/main/webapp/WEB-INF/beans.xml

Lines changed: 0 additions & 49 deletions
This file was deleted.

jms/temp-destination/src/main/webapp/index.jsp

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.javaee7.jms.temp.destination;
2+
3+
import org.jboss.arquillian.container.test.api.Deployment;
4+
import org.jboss.arquillian.junit.Arquillian;
5+
import org.jboss.shrinkwrap.api.ShrinkWrap;
6+
import org.jboss.shrinkwrap.api.spec.WebArchive;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import javax.ejb.EJB;
12+
13+
/**
14+
* Temporary queues are JMS queues that exist for the lifetime of single JMS connection.
15+
* Also the reception of the messages is exclusive to the connection, therefore no
16+
* reasonable use case exist for temporary topic within Java EE container, as connection
17+
* is usually exclusive to single component.
18+
*
19+
* Temporary queues are usually used as reply channels for request / response communication
20+
* over JMS.
21+
*/
22+
@RunWith(Arquillian.class)
23+
public class TempQueueTest {
24+
25+
/**
26+
* In this test we created a server component +RequestResponseOverJMS+, that
27+
* listens on a Queue and passes the response to the destination specified in
28+
* +JMSReplyTo+ header of the message.
29+
*
30+
* +JmsClient+ is a client to this server, and has to be non transactional,
31+
* otherwise the request would be first sent upon commit, i. e. after the
32+
* business method finishes. That would be too late. We need to send the message
33+
* immediately, and wait for the response to arrive.
34+
*
35+
*/
36+
@Deployment
37+
public static WebArchive deployment() {
38+
return ShrinkWrap.create(WebArchive.class)
39+
.addClasses(RequestResponseOverJMS.class, JmsClient.class, Resources.class);
40+
}
41+
42+
@EJB
43+
JmsClient client;
44+
45+
/**
46+
* We invoke the client, and verify that the response is processed
47+
*/
48+
@Test
49+
public void testRequestResposne() {
50+
String response = client.process("Hello");
51+
Assert.assertEquals("Processed: Hello", response);
52+
}
53+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://jboss.org/schema/arquillian" xsi:schemaLocation="http://jboss.org/schema/arquillian
3+
http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
4+
5+
<defaultProtocol type="Servlet 3.0"/>
6+
7+
<container qualifier="test" default="true">
8+
<configuration>
9+
<property name="jbossHome">${serverRoot:target/wildfly-8.0.0.CR1}</property>
10+
<property name="serverConfig">${serverProfile:standalone-full.xml}</property>
11+
</configuration>
12+
</container>
13+
<container qualifier="debug">
14+
<configuration>
15+
<property name="jbossHome">${serverRoot:target/wildfly-8.0.0.CR1}</property>
16+
<property name="serverConfig">${serverProfile:standalone-full.xml}</property>
17+
<property name="javaVmArguments">-Xrunjdwp:transport=dt_socket,address=55407,server=y,suspend=y -Xmx512m -XX:MaxPermSize=128m</property>
18+
</configuration>
19+
</container>
20+
21+
</arquillian>

0 commit comments

Comments
 (0)