Description key Features Build & Configure Run Troubleshoot Related Topics
This example demonstrates how to establish a connection to a JMS queue. It also shows how to send, browse, and receive messages from that queue. The classes in this package operate on the same JMS queue. Run the classes together to observe messages being sent and received and to browse the queue for messages.
Directory Location:
D:\Oracle\Middleware\Oracle_Home\wlserver/samples/server/examples/src/examples/jms/simplifiedapi/queue/
File Click source files to view code. |
Description |
|---|---|
| build.xml | Ant build file that contains targets for building and running the example. |
| QueueBrowse.java | Used to establish a connection to a JMS queue and browse (but not dequeue) the queued messages. |
| QueueReceive.java | Used to establish a connection and receive messages from a JMS queue. |
| QueueReceiveInTx.java | Used to establish a connection and receive messages from a JMS queue in a client-demarcated transaction. |
| QueueSend.java | Used to establish a connection and send messages to the JMS queue. |
| deployqueue.py | Used to deploy the packaged files and configure the required resources - the JMS resources for this sample. |
| undeployqueue.py | Used to delete the JMS resources. |
Directory Location: D:\Oracle\Middleware\Oracle_Home\wlserver/samples/server/examples/src/examples/jms/simplifiedapi/queue/
QueueSend demonstrates how to send messages to the JMS queue.
public void readAndSend() throws IOException {
BufferedReader msgStream = new BufferedReader(new InputStreamReader(System.in));
boolean quitNow = false;
try (JMSContext jmsCtx = qconFactory.createContext()) {
JMSProducer qsender = jmsCtx.createProducer();
do {
...
} while (! quitNow);
} catch (JMSRuntimeException ex) {
...
}
}
QueueBrowse demonstrates how to browse (without removing) messages in a JMS queue.
public void displayQueue() throws JMSException {
try (JMSContext jmsCtx = qconFactory.createContext(); QueueBrowser qbrowser = jmsCtx.createBrowser(queue)){
Enumeration> e = qbrowser.getEnumeration();
...
} catch (JMSRuntimeException ex) {
...
}
}
QueueReceive demonstrates how to receive messages from a JMS queue.
public class QueueReceive implements MessageListener
public void onMessage(Message msg){
try {
String msgText;
if (msg instanceof TextMessage) {
msgText = msg.getBody(String.class);
} else {
msgText = msg.toString();
}
...
} catch (JMSException jmse) { ... }
}
}
QueueReceive demonstrates how to receive messages from a JMS queue in a client-demarcated transaction.
public void receiveMessages() throws Exception {
utx.setTransactionTimeout(1800);
utx.begin();
do {
msg = qreceiver.receive();
if (msg != null) {
if (msg instanceof TextMessage) {
msgText = msg.getBody(String.class);
} else {
msgText = msg.toString();
}
if (msgText.equalsIgnoreCase("quit")) {
utx.commit();
System.out.println("TRANSACTION COMMITTED");
}
}
} while(msg != null && ! msgText.equalsIgnoreCase("quit"));
...
}
Before working with this example:
No special configuration is required for this example.
The JMS resource for this example is the default one preconfigured for Examples Server. If you want to run this example on a different WebLogic Server domain, you need to configure the JMS resource using the following command.
D:\Oracle\Middleware\Oracle_Home\wlserver/samples/server/examples/src/examples/jms/simplifiedapi/queue/
directory.
ant deploy.jmsresourceYou can use the command ant
undeploy.jmsresource to undeploy resource.
D:\Oracle\Middleware\Oracle_Home\wlserver/samples/server/examples/src/examples/jms/simplifiedapi/queue
directory.
ant build
ant run.send
run.send:
[java] Enter message ("quit" to quit):
hello world
[java] JMS Message Sent: hello world
[java]
[java] Enter message ("quit" to quit):
quit
[java] JMS Message Sent: quit
[java]
ant run.browse run.browse: [java] Queued JMS Messages: [java] Message ID ID:<524204.1437029186890.0> delivered Thu Jul 16 14:46:26 CST 2015 to examples-jms!weblogic.examples.jms.exampleQueue [java] Expires never [java] Priority 4 [java] Mode PERSISTENT [java] Correlation ID null [java] Reply to null [java] Message type null [java] TextMessage "hello world" [java] Message ID ID:<524204.1437029198580.0> delivered Thu Jul 16 14:46:38 CST 2015 to examples-jms!weblogic.examples.jms.exampleQueue [java] Expires never [java] Priority 4 [java] Mode PERSISTENT [java] Correlation ID null [java] Reply to null [java] Message type null [java] TextMessage "quit"
ant run.receive run.receive: [java] JMS Ready To Receive Messages (To quit, send a "quit" message). [java] Message Received: hello world [java] Message Received: quit
ant run.send
run.send:
[java] Enter message ("quit" to quit):
hello world again
[java] JMS Message Sent: hello world again
[java]
[java] Enter message ("quit" to quit):
quit
[java] JMS Message Sent: quit
[java]
ant run.receivetx run.receivetx: [java] JMS Ready To Receive Messages (To quit, send a "quit" message). [java] TRANSACTION BEGUN [java] Message Received: hello world again [java] Message Received: quit [java] TRANSACTION COMMITTED
Copyright © 2013, 2020, Oracle and/or its affiliates. All rights reserved.