Saturday, April 02, 2016

Reading Messages from JMS Queue in Weblogic 12.1.1 using Java

Suppose I have Queue configured in Weblogic and I want to write application in which Program A will put messages to this queue and Program B will read data from this Queue.

 There are multiple Persistence Store options availble in WL like The file or database in which this JMS server stores persistent messages. If unspecified, the JMS server uses the default persistent store that is configured on each targeted WebLogic Server instance As part of below example We will be using default one.

In order to retrieve messages from queue, you have should queue containing messages.
Please refer to previous article to send messages to JMS Queue.

Write Java Program to send message to Queue

  •   Get QueueConnectionFactory
  •   Create QueueConnection 
  •   Create QueueSession
  •   Create QueueReceiver
  •   Retrieve Message to Queue
Note : please use same QueueConnectionFactory,Queue to retrieve messages.

Reading Messages from JMS Queue in Weblogic 12.1.1 using Java
package com.anuj.messages;

import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

 * @author Anuj Patel
 * @source
public class MessageReceiver {

 private QueueConnection queueConnection;
 private QueueSession queueSession;
 private static Queue queue1;
 private static QueueReceiver queueReceiver;

 public static void main(String[] args) throws NamingException, JMSException {
  MessageReceiver messageReceiver = new MessageReceiver();

 public void setup() throws NamingException, JMSException {
  Hashtable<String, String> ht = new Hashtable<>();
  ht.put(Context.PROVIDER_URL, "t3://localhost:7001");

  // Prepare Initial Context
  Context ctx = new InitialContext(ht);

  // Get Queue using Lookup
  queue1 = (Queue) ctx.lookup("jms/Queue1");

  QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ctx.lookup("jms/ConnectionFactory1");
  queueConnection = queueConnectionFactory.createQueueConnection();
  queueSession = queueConnection.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE);


 public void sendAndReceive() throws JMSException {
  queueReceiver = queueSession.createReceiver(queue1);

  for (int i = 0; i < 10; i++) {
   Message message = queueReceiver.receive();
   TextMessage textMessage = (TextMessage) message;



TextMessage[ID:<293625.1459631893023.0>, Message 0]
TextMessage[ID:<293625.1459631893023.1>, Message 1]
TextMessage[ID:<293625.1459631893039.0>, Message 2]
TextMessage[ID:<293625.1459631893039.1>, Message 3]
TextMessage[ID:<293625.1459631893039.2>, Message 4]
TextMessage[ID:<293625.1459631893039.3>, Message 5]
TextMessage[ID:<293625.1459631893039.4>, Message 6]
TextMessage[ID:<293625.1459631893039.5>, Message 7]
TextMessage[ID:<293625.1459631893039.6>, Message 8]
TextMessage[ID:<293625.1459631893039.7>, Message 9]

We can see that Queue cleared :)

If You liked my tutorials, You can subscribe my blog or share with your friends.

 Disclaimer - This tutorials are for learning purpose only.