Sending Message to 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 put mesage to Queue using JMS Apis, Please follow below steps.
  • Create JMS Server --> Services -> Messaging -> JMS Server -> create new Ex. TestJMSServer
  • Create new JMS Module --> Services -> Messaging -> JMS Modules -> Create new JMS Module Ex. TestJMSModule 
  • Create Connection Factory ex. ConnectionFactory1 --> JNDI as jms/ConnectionFactory1
  • Create Queue ex. Queue1 --> JNDI as jms/Queue1 with target services as JMS Server ex. TestJMSServer
  • Write Java Program to send message to Queue
    •   Get QueueConnectionFactory
    •   Create QueueConnection 
    •   Create QueueSession
    •   Create QueueSender
    •   Send Message to Queue

JMS API Model - Refer to Oracle J2EE 6  Tutorial for more details

Sending Message to JMS Queue in Weblogic 12.1.1 using Java

package com.anuj.messages;

import java.util.Hashtable;

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

public class MessageSender {

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

 private static QueueSender queueSender;

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


 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 {
  queueSender = queueSession.createSender(queue1);

  for (int i = 0; i < 10; i++) {
   // Create TextMessage
   TextMessage textMessage = queueSession.createTextMessage("Message "+ i);

   // Send TextMessage



We can see that 10 messages submitted using code can be seen in WL as below.

Disclaimer - This tutorials are for learning purpose only.

No comments:

Post a Comment