# READ message from a QUEUE

In 
JMS
Published 2022-12-03

This tutorial explains to you how we can read (receive) a simple message from a queue with Java. For testing the code I used a WebLogic Application Server.

# How to receive (read) a message from a WebLogic Queue with Java

In my case I used a WebLogic Queue. Here is a picture showing the Queue with 8 message received and 2 messages that are no read:

After the message is read, you can see that a message is removed from the queue:

For testing, I used the following: Eclipse, JDK 8, a WebLogic Server, and I created a Web application using JSF (PrimeFaces).

On a JSF page (.xhtml) I put a button to receive (read) the message from the WebLogic Queue:

<html>
 <body>
   <h:form>
     <p:commandbutton value="Receive Message" id="id1" actionlistener="#{receiveMessage.recMessage}">
     </p:commandbutton></h:form>
 </body>
</html>

Here is the ReceiveMessage.java file :

/*
 *  This example shows you how you can receive a simple message
 *  from a WebLogic Queue
 */

package com;

import java.util.Hashtable;
import javax.faces.bean.ManagedBean;
import javax.naming.*;
import javax.jms.*;

@ManagedBean
public class ReceiveMessage {

  // Specify the JNDI context factory name.
  public final static String JNDI_CFACTORY="weblogic.jndi.WLInitialContextFactory";

  // Specify the JMS context factory name.
  public final static String JMS_CFACTORY="MyConnectionFactory";

  // Specify the Queue name.
  public final static String JMS_QUEUE="MyQueue1";

  // Specify application server URL. The port is for the Managed Server 
  // or Admin Server where the JMS Server is running.
  public final static String WebLogicURL="t3://pc:7004";
  
  public void recMessage(String message) throws Exception {
    System.out.println(message);

    //1) Create the connection 
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_CFACTORY);
    env.put(Context.PROVIDER_URL, WebLogicURL);

    InitialContext ic=new InitialContext(env);
    QueueConnectionFactory f=(QueueConnectionFactory)ic.lookup(JMS_CFACTORY) ;
    QueueConnection con=f.createQueueConnection();

    //2) Start the connection      
    con.start();

    //3) Create queue session  
    QueueSession ses=con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

    //4) Get the Queue object  
    Queue t = (Queue)ic.lookup(JMS_QUEUE);

    //5) Create QueueReceiver object         
    QueueReceiver receiver=ses.createReceiver(t);

    //6) Read the TextMessage object from the WebLogic Queue
    TextMessage msg = (TextMessage) receiver.receive();

    System.err.println("Received: "+msg.getText());

    //7) Close the connection 
    con.close();

  }
}

# JMS queue

In point-to-point messaging system, messages are routed to an individual consumer which maintains a queue of "incoming" messages. This messaging type is built on the concept of message queues, senders, and receivers. Each message is addressed to a specific queue, and the receiving clients extract messages from the queues established to hold their messages. While any number of producers can send messages to the queue, each message is guaranteed to be delivered, and consumed by one consumer. Queues retain all messages sent to them until the messages are consumed or until the messages expire. If no consumers are registered to consume the messages, the queue holds them until a consumer registers to consume them.