#
SEND a message to a QUEUE
#
JMS Queue
This tutorial explains to you how we can write (send) a simple message to a queue with Java EE. For testing the code I used a WebLogic Application Server.
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.
In my case I used a WebLogic Queue. Here is a picture showing the Queue without any message received:
After the message is send, you can see a message on the queue:
#
How to write (send) a message to a Queue with Java
For testing, I used the following: Eclipse, JDK 8, a WebLogic Server, and I created a Web application using JSF (PrimeFaces). Take a look at Create PrimeFaces Applications if needed.
On a JSF page (.xhtml) I put a button to send the message:
<html>
<body>
<h:form>
<p:commandbutton value="Send Message" id="ajax" actionlistener="#{sendMessageQueueClass.sendMessage}">
</p:commandbutton></h:form>
</body>
</html>
Here is the SendMessageQueueClass.java
file :
/*
* This example shows you how you can send a simple message to a WebLogic Queue
*/
package com;
import java.util.Hashtable;
import javax.faces.bean.ManagedBean;
import javax.naming.*;
import javax.jms.*;
@ManagedBean
public class SendMessageQueueClass {
// 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 sendMessage(String textMessage) throws Exception {
System.out.println(textMessage);
//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 QueueSender object
QueueSender sender=ses.createSender(t);
//6) Create TextMessage object
TextMessage msg=ses.createTextMessage();
msg.setText(textMessage);
//7) Send message
sender.send(msg);
System.out.println("Message successfully sent.");
//8) Close the connection
con.close();
}
}
Here are the steps for sending a message to a JMS Queue:
- 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();
- Start the connection
con.start();
- Create queue session
QueueSession ses=con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
- Get the Queue object
Queue t = (Queue)ic.lookup(JMS_QUEUE);
- Create QueueSender object
QueueSender sender=ses.createSender(t);
- Create TextMessage object for the queue
TextMessage msg=ses.createTextMessage();
msg.setText(textMessage);
- Send message to queue
sender.send(msg);
System.out.println("Message successfully sent.");
- Close the connection
con.close();
Messages can be consumed in either of 2 ways:
Synchronously: A subscriber or a receiver explicitly fetches the message from the destination by calling the "receive" method. The "receive" method can block until a message arrives or can time out if a message does not arrive within a specified time limit. That means the messages are read in the order they arrive in the queue (there is only one thread).
Asynchronously: A client can register a message listener with a consumer. A message listener is similar to an event listener. Whenever a message arrives at the destination, the JMS provider delivers the message by calling the listener’s onMessage method, which acts on the contents of the message. That means the messages are read in the order they arrive in the queue, but there is one thread for each read (2 messages can be read in the same time).