Description   key Features   Build & Configure   Run   Troubleshoot   Related Topics 

About the Example

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.


Files Used in the Example

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.


Description

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"));
    ...
}

Building and Configuring the Example

Prerequisites

Before working with this example:

  1. Install WebLogic Server, including the examples.
  2. Start the Examples server.
  3. Set up your environment.

Configure WebLogic Server

No special configuration is required for this example.

Configure Example Resources (Optional)

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.

  1. Change to the D:\Oracle\Middleware\Oracle_Home\wlserver/samples/server/examples/src/examples/jms/simplifiedapi/queue/ directory.
  2. Execute the following command:

    ant deploy.jmsresource

You can use the command ant undeploy.jmsresource to undeploy resource.

Build and Deploy the Example

  1. Change to the D:\Oracle\Middleware\Oracle_Home\wlserver/samples/server/examples/src/examples/jms/simplifiedapi/queue directory.
  2. Execute the following command:

    ant build

    to compile and stage the example.

Running the Examples

  1. Complete the steps in the "Building and Configuring the Example" section.
  2. Execute the following command:

    ant run.send

    which will send messages to the queue. Enter the message "hello world" and then enter the message "quit" to quit. The following result will be displayed on the console:
    		
    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]
    
  3. Execute the following command:

    ant run.browse

    which will browse messages from the queue, but this command will not remove messages. The following result will be displayed on the console:
    	
    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"
    
  4. Execute the following command:

    ant run.receive

    which will receive messages from the queue and remove messages from the queue. The following result will be displayed on the console:
    	
    run.receive:
    [java] JMS Ready To Receive Messages (To quit, send a "quit" message).
    [java] Message Received: hello world
    [java] Message Received: quit
    
  5. Execute the following command:

    ant run.send

    which will resend messages to the queue. Enter the message "hello world again" and then enter the message "quit" to quit. The following result will be displayed on the console:
    	
    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]
    
  6. Execute the following command:

    ant run.receivetx

    which will receive messages from the queue in a client-demarcated transaction, and remove messages from the queue. The following result will be displayed on the console:
    	
    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
    

Troubleshooting


Related Topics


Copyright © 2013, 2020, Oracle and/or its affiliates. All rights reserved.