Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad269e0

Browse files
authored
Merge pull request javaee-samples#420 from MattGill98/jms-send-receive-time-dependency-fix
Jms send receive time dependency fix
2 parents 2400065 + 865c9d4 commit ad269e0

File tree

13 files changed

+140
-87
lines changed

13 files changed

+140
-87
lines changed

jms/send-receive/src/main/java/org/javaee7/jms/send/receive/Resources.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
description = "My Sync Queue for Container-managed JMSContext")
3232
})
3333
public class Resources {
34-
public static final String SYNC_APP_MANAGED_QUEUE = "java:global/jms/mySyncAppQueue";
35-
public static final String SYNC_CONTAINER_MANAGED_QUEUE = "java:global/jms/mySyncContainerQueue";
36-
public static final String ASYNC_QUEUE = "java:global/jms/myAsyncQueue";
37-
public static final String CLASSIC_QUEUE = "java:global/jms/classicQueue";
34+
public static final String SYNC_APP_MANAGED_QUEUE = "java:app/jms/mySyncAppQueue";
35+
public static final String SYNC_CONTAINER_MANAGED_QUEUE = "java:app/jms/mySyncContainerQueue";
36+
public static final String ASYNC_QUEUE = "java:app/jms/myAsyncQueue";
37+
public static final String CLASSIC_QUEUE = "java:app/jms/classicQueue";
3838
}

jms/send-receive/src/main/java/org/javaee7/jms/send/receive/classic/ClassicMessageReceiver.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
*/
4040
package org.javaee7.jms.send.receive.classic;
4141

42+
import java.util.concurrent.TimeoutException;
4243
import javax.annotation.Resource;
4344
import javax.ejb.Stateless;
4445
import javax.jms.Connection;
@@ -53,6 +54,7 @@
5354

5455
/**
5556
* Synchronized message receiver using classic API.
57+
*
5658
* @author Arun Gupta
5759
*/
5860
@Stateless
@@ -64,26 +66,27 @@ public class ClassicMessageReceiver {
6466
@Resource(mappedName = Resources.CLASSIC_QUEUE)
6567
Queue demoQueue;
6668

67-
public String receiveMessage() {
69+
/**
70+
* Waits to receive a message from the JMS queue. Times out after a given
71+
* number of milliseconds.
72+
*
73+
* @param timeoutInMillis The number of milliseconds this method will wait
74+
* before throwing an exception.
75+
* @return The contents of the message.
76+
* @throws JMSException if an error occurs in accessing the queue.
77+
* @throws TimeoutException if the timeout is reached.
78+
*/
79+
public String receiveMessage(int timeoutInMillis) throws JMSException, TimeoutException {
6880
String response = null;
69-
Connection connection = null;
70-
try {
71-
connection = connectionFactory.createConnection();
81+
try (Connection connection = connectionFactory.createConnection()) {
7282
connection.start();
7383
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
7484
MessageConsumer messageConsumer = session.createConsumer(demoQueue);
75-
Message message = messageConsumer.receive(5000);
76-
response = message.getBody(String.class);
77-
} catch (JMSException ex) {
78-
ex.printStackTrace();
79-
} finally {
80-
if (connection != null) {
81-
try {
82-
connection.close();
83-
} catch (JMSException ex) {
84-
ex.printStackTrace();
85-
}
85+
Message message = messageConsumer.receive(timeoutInMillis);
86+
if (message == null) {
87+
throw new TimeoutException("No message received after " + timeoutInMillis + "ms");
8688
}
89+
response = message.getBody(String.class);
8790
}
8891
return response;
8992
}

jms/send-receive/src/main/java/org/javaee7/jms/send/receive/classic/ClassicMessageSender.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353

5454
/**
5555
* Sending a message using classic JMS API.
56+
*
5657
* @author Arun Gupta
5758
*/
5859
@Stateless
@@ -64,25 +65,19 @@ public class ClassicMessageSender {
6465
@Resource(mappedName = Resources.CLASSIC_QUEUE)
6566
Queue demoQueue;
6667

67-
public void sendMessage(String payload) {
68-
Connection connection = null;
69-
try {
70-
connection = connectionFactory.createConnection();
68+
/**
69+
* Send a message to the JMS queue.
70+
*
71+
* @param payload the contents of the message.
72+
* @throws JMSException if an error occurs in accessing the queue.
73+
*/
74+
public void sendMessage(String payload) throws JMSException {
75+
try (Connection connection = connectionFactory.createConnection()) {
7176
connection.start();
72-
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
77+
Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE);
7378
MessageProducer messageProducer = session.createProducer(demoQueue);
7479
TextMessage textMessage = session.createTextMessage(payload);
7580
messageProducer.send(textMessage);
76-
} catch (JMSException ex) {
77-
ex.printStackTrace();
78-
} finally {
79-
if (connection != null) {
80-
try {
81-
connection.close();
82-
} catch (JMSException ex) {
83-
ex.printStackTrace();
84-
}
85-
}
8681
}
8782
}
8883
}

jms/send-receive/src/main/java/org/javaee7/jms/send/receive/mdb/MessageReceiverAsync.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@
5252

5353
/**
5454
* A message driven bean with newly standardized activation config properties.
55+
*
5556
* @author Arun Gupta
5657
*/
5758
@MessageDriven(activationConfig = {
5859
@ActivationConfigProperty(propertyName = "destinationLookup",
59-
propertyValue = Resources.ASYNC_QUEUE),
60+
propertyValue = Resources.ASYNC_QUEUE),
6061
@ActivationConfigProperty(propertyName = "destinationType",
61-
propertyValue = "javax.jms.Queue"),
62-
})
62+
propertyValue = "javax.jms.Queue"),})
6363
public class MessageReceiverAsync implements MessageListener {
6464

6565
@Override

jms/send-receive/src/main/java/org/javaee7/jms/send/receive/simple/MessageReceiverSync.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,21 @@
3939
*/
4040
package org.javaee7.jms.send.receive.simple;
4141

42-
import java.util.logging.Level;
43-
import java.util.logging.Logger;
42+
import java.util.concurrent.TimeoutException;
4443
import javax.annotation.Resource;
4544
import javax.ejb.Stateless;
4645
import javax.inject.Inject;
4746
import javax.jms.JMSContext;
4847
import javax.jms.JMSException;
48+
import javax.jms.JMSRuntimeException;
4949
import javax.jms.Queue;
5050
import javax.jms.QueueBrowser;
5151

5252
import org.javaee7.jms.send.receive.Resources;
5353

5454
/**
5555
* Synchronous message reception with container-managed JMSContext.
56+
*
5657
* @author Arun Gupta
5758
*/
5859
@Stateless
@@ -64,20 +65,30 @@ public class MessageReceiverSync {
6465
@Resource(mappedName = Resources.SYNC_CONTAINER_MANAGED_QUEUE)
6566
Queue myQueue;
6667

67-
public String receiveMessage() {
68-
return context.createConsumer(myQueue).receiveBody(String.class, 1000);
68+
/**
69+
* Waits to receive a message from the JMS queue. Times out after a given
70+
* number of milliseconds.
71+
*
72+
* @param timeoutInMillis The number of milliseconds this method will wait
73+
* before throwing an exception.
74+
* @return The contents of the message.
75+
* @throws JMSRuntimeException if an error occurs in accessing the queue.
76+
* @throws TimeoutException if the timeout is reached.
77+
*/
78+
public String receiveMessage(int timeoutInMillis) throws JMSRuntimeException, TimeoutException {
79+
String message = context.createConsumer(myQueue).receiveBody(String.class, timeoutInMillis);
80+
if (message == null) {
81+
throw new TimeoutException("No message received after " + timeoutInMillis + "ms");
82+
}
83+
return message;
6984
}
7085

71-
public void receiveAll() {
86+
public void receiveAll(int timeoutInMillis) throws JMSException {
7287
System.out.println("--> Receiving redundant messages ...");
73-
try {
74-
QueueBrowser browser = context.createBrowser(myQueue);
75-
while (browser.getEnumeration().hasMoreElements()) {
76-
System.out.println("--> here is one");
77-
context.createConsumer(myQueue).receiveBody(String.class, 1000);
78-
}
79-
} catch (JMSException ex) {
80-
Logger.getLogger(MessageReceiverSync.class.getName()).log(Level.SEVERE, null, ex);
88+
QueueBrowser browser = context.createBrowser(myQueue);
89+
while (browser.getEnumeration().hasMoreElements()) {
90+
System.out.println("--> here is one");
91+
context.createConsumer(myQueue).receiveBody(String.class, timeoutInMillis);
8192
}
8293
}
8394
}

jms/send-receive/src/main/java/org/javaee7/jms/send/receive/simple/MessageSenderAsync.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@
4747
import javax.jms.CompletionListener;
4848
import javax.jms.JMSContext;
4949
import javax.jms.JMSException;
50+
import javax.jms.JMSProducer;
51+
import javax.jms.JMSRuntimeException;
5052
import javax.jms.Message;
5153
import javax.jms.Queue;
5254

5355
import org.javaee7.jms.send.receive.Resources;
5456

5557
/**
5658
* Asynchronous message sending is not supported in Java EE 7.
59+
*
5760
* @author Arun Gupta
5861
*/
5962
@Stateless
@@ -66,9 +69,17 @@ public class MessageSenderAsync {
6669
@Resource(lookup = Resources.ASYNC_QUEUE)
6770
Queue asyncQueue;
6871

69-
public void sendMessage(String message) {
72+
/**
73+
* Send a message to the JMS queue. Prin
74+
*
75+
* @param message the contents of the message.
76+
* @throws JMSRuntimeException if an error occurs in accessing the queue.
77+
*/
78+
public void sendMessage(String message) throws JMSRuntimeException {
79+
JMSProducer producer = context.createProducer();
80+
7081
try {
71-
context.createProducer().setAsync(new CompletionListener() {
82+
producer.setAsync(new CompletionListener() {
7283
@Override
7384
public void onCompletion(Message msg) {
7485
try {
@@ -87,10 +98,10 @@ public void onException(Message msg, Exception e) {
8798
}
8899
}
89100
});
90-
} catch (RuntimeException e) {
91-
System.out.println("Caught RuntimeException trying to invoke setAsync - not permitted in Java EE");
101+
} catch (JMSRuntimeException ex) {
102+
System.out.println("Caught RuntimeException trying to invoke setAsync - not permitted in Java EE. Resorting to synchronous sending...");
92103
}
93104

94-
context.createProducer().send(asyncQueue, message);
105+
producer.send(asyncQueue, message);
95106
}
96107
}

jms/send-receive/src/main/java/org/javaee7/jms/send/receive/simple/MessageSenderSync.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@
4343
import javax.ejb.Stateless;
4444
import javax.inject.Inject;
4545
import javax.jms.JMSContext;
46+
import javax.jms.JMSRuntimeException;
4647
import javax.jms.Queue;
4748

4849
import org.javaee7.jms.send.receive.Resources;
4950

5051
/**
5152
* Synchronous message sending with container-managed JMSContext.
53+
*
5254
* @author Arun Gupta
5355
*/
5456
@Stateless
@@ -61,7 +63,13 @@ public class MessageSenderSync {
6163
@Resource(mappedName = Resources.SYNC_CONTAINER_MANAGED_QUEUE)
6264
Queue syncQueue;
6365

64-
public void sendMessage(String message) {
66+
/**
67+
* Send a message to the JMS queue.
68+
*
69+
* @param message the contents of the message.
70+
* @throws JMSRuntimeException if an error occurs in accessing the queue.
71+
*/
72+
public void sendMessage(String message) throws JMSRuntimeException {
6573
context.createProducer().send(syncQueue, message);
6674
}
6775
}

jms/send-receive/src/main/java/org/javaee7/jms/send/receive/simple/appmanaged/MessageReceiverAppManaged.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.javaee7.jms.send.receive.simple.appmanaged;
22

3+
import java.util.concurrent.TimeoutException;
34
import javax.annotation.Resource;
45
import javax.ejb.Stateless;
56
import javax.jms.ConnectionFactory;
67
import javax.jms.JMSContext;
8+
import javax.jms.JMSRuntimeException;
79
import javax.jms.Queue;
810
import org.javaee7.jms.send.receive.Resources;
911

@@ -19,9 +21,23 @@ public class MessageReceiverAppManaged {
1921
@Resource(mappedName = Resources.SYNC_APP_MANAGED_QUEUE)
2022
Queue myQueue;
2123

22-
public String receiveMessage() {
24+
/**
25+
* Waits to receive a message from the JMS queue. Times out after a given
26+
* number of milliseconds.
27+
*
28+
* @param timeoutInMillis The number of milliseconds this method will wait
29+
* before throwing an exception.
30+
* @return The contents of the message.
31+
* @throws JMSRuntimeException if an error occurs in accessing the queue.
32+
* @throws TimeoutException if the timeout is reached.
33+
*/
34+
public String receiveMessage(int timeoutInMillis) throws JMSRuntimeException, TimeoutException {
2335
try (JMSContext context = factory.createContext()) {
24-
return context.createConsumer(myQueue).receiveBody(String.class, 1000);
36+
String message = context.createConsumer(myQueue).receiveBody(String.class, timeoutInMillis);
37+
if (message == null) {
38+
throw new TimeoutException("No message received after " + timeoutInMillis + "ms");
39+
}
40+
return message;
2541
}
2642
}
2743
}

jms/send-receive/src/main/java/org/javaee7/jms/send/receive/simple/appmanaged/MessageSenderAppManaged.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import javax.ejb.Stateless;
55
import javax.jms.ConnectionFactory;
66
import javax.jms.JMSContext;
7+
import javax.jms.JMSRuntimeException;
78
import javax.jms.Queue;
89

910
import org.javaee7.jms.send.receive.Resources;
1011

1112
/**
12-
* Synchronous message sending with app-managed JMSContext.
13-
* JMSContext can be used with try-with-resources construct.
13+
* Synchronous message sending with app-managed JMSContext. JMSContext can be
14+
* used with try-with-resources construct.
15+
*
1416
* @author Arun Gupta
1517
*/
1618
@Stateless
@@ -22,7 +24,13 @@ public class MessageSenderAppManaged {
2224
@Resource(mappedName = Resources.SYNC_APP_MANAGED_QUEUE)
2325
Queue myQueue;
2426

25-
public void sendMessage(String message) {
27+
/**
28+
* Send a message to the JMS queue.
29+
*
30+
* @param message the contents of the message.
31+
* @throws JMSRuntimeException if an error occurs in accessing the queue.
32+
*/
33+
public void sendMessage(String message) throws JMSRuntimeException {
2634
try (JMSContext context = factory.createContext()) {
2735
context.createProducer().send(myQueue, message);
2836
}

0 commit comments

Comments
 (0)