Tag: Queue

Steps to Configure Error Queue on Weblogic Server

Ravish Mody

All the people who have worked with JMS must have come across messages getting in pending state or message becoming a poison message due to which lot for issue are been faced like messages are not been picked up, outofmemory issue and lots more.

To reduce these kind of issues we can create an error queue on weblogic server. Now people who does not have any idea or much idea of what is an error queue this post would be a very useful you guys. So first thing first lets talk about

– What is a poison message?
– What is an Error queue?

What is a Poison Message?

Poison Message

Poison Message

Any message which is not able to get delivered to the consumer/listener due to which other messages in the queue gets stuck, then that message is called a poison message.

What is an Error Queue?

Error Queue

Error Queue

It is a queue which comes into the picture when any messages are not able get delivered to the consumer/listener are sent to the error queue and would again send it back to the normal queue so that it can try to resend the same message again.

Advantage of Error Queue

  • No poison messages are produced. So that other messages in the normal queue get delivered properly.
  • Make sure we can try to deliver the poison message again.
  • Can save us from outofmemory issue, pending messages etc.

Steps to Configure Error Queue:

1.  You have to follow steps from Step-1 to Step-5 from the post Basic JMS Demo using WebLogic Queue.

2.  Then you have to creating another queue called Error queue

ErrorQueue-1

Creating Error Queue

ErrorQueue-2

Giving Error Queue Name

ErrorQueue-3

Targeting Error Queue

3. Configuring the error queue in the normal queue.

ErrorQueue-4

Configuring Error Queue

ErrorQueue-5

Setting Parameters for the Error Queue

This way we can make sure that once the messages is not been able to be delivered then it would try to re-deliver the same message for 3 times as set in Redelivery Limit in an interval of 1000 milliseconds which was set in Redeliver Delay and if even then that message has not got delivered then based on the Expiration Policy as Redirect the message would be directed to the Error Destination which we have created as MyErrorQueue.

This way we take out the poison messages out of the normal queue and let the other messages get delivered properly without affecting the flow of the messages.

NOTE:
– Error destination must be configured on the same JMS server as the destination for which it is defined.
– If no error destination is configured/selected when we chose the Expiration Policy as Redirect , then undeliverable messages are simply deleted.

Hope this post would help you guys to understand the concept of error queue, so do let us know if it did by commenting below  Smile

Regards,

Ravish Mody


Basic JMS Demo using WebLogic Queue

Hi,

Jay SenSharma

Jay SenSharma

Java Messaging Service is an API that provides reliable, asynchronous communication between applications in a distributed computing environment. It enables a Java applications that share a messaging system to exchange messages and Simplifies application development by providing a standard interface for creating, sending, and receiving messages.

To Know about JMS Topic Please refer to: Basic JMS Demo using WebLogic Topic

What are major components of JMS system?

A). JMSServer: JMS server is an environment-related configuration entity that acts as management container for JMS queue and topic resources defined within JMS modules that are targeted to specific that JMS server.
B). JMS Modules: JMS modules contain configuration resources, such as standalone queue and topic destinations, distributed destinations, and connection factories. In WebLogic Server these Configurations can be seen inside “<DOMAIN_HOME>/config/jms” Directory.
C). JMS Client: A JMS is a JMS applications that either produce messages to destinations or consume messages from destinations.
D). JNDI : Java Naming And Directory interface,It is a Java API for a directory service that allows Java software clients to discover and look up data and objects via a name.
E). Persistent Store: It can be a UserDefined Persistent Store or a Default Persistent store. It is used for storing the Data (Messages).

Here we are going to see how we can configure WebLogic JMS Server, JMS Modules (Queue/ConnectionFactory) how to Target them and then How to test it using a Simpel Java Programs.

Step1). Start your WebLogic Server an Login to the AdminConsole.

Step2). Create a JMS Server…

Creating JMS Server

Creating JMS Server

Creating JMS Server-2

Creating JMS Server-2

Selecting JMS Server Store Type

Selecting JMS Server Store Type

Configuring JMS Store

Configuring JMS Store

Choosing JMS Store for JMSSErver

Choosing JMS Store for JMSSErver

Targeting to the JMSServer

Targeting to the JMSServer

Activating the JMS Server Configuration

Activating the JMS Server Configuration

Step3). Configuring JMS Module:

Creating JMS Module

Creating JMS Module

Entering JMS Module Name

Entering JMS Module Name

Targetting JMS Module

Targetting JMS Module

Finishing the Module Creation

Finishing the Module Creation

Step4). Creating Connection Factory:

Creating JMS Connection Factory

Creating JMS Connection Factory

Selecting JMS Module to be Created

Selecting JMS Module to be Created

Providing Name/JNDIName to Connection Factory

Providing Name/JNDIName to Connection Factory

Targeting the CnnectionFactory

Targeting the CnnectionFactory

Step5).Creating JMS Queue:

Selecting JMSQueue to be Created

Selecting JMSQueue to be Created

Queue Selection

Queue Selection

Assigning Name/JNDIName to Queue

Assigning Name/JNDIName to Queue

Creating SubDeployment

Creating SubDeployment

Assigning Name to the SubDeployment

Assigning Name to the SubDeployment

Targeting the JMS Queue

Targeting the JMS Queue

Activating the Final Changes

Activating the Final Changes

Step6). Now Write a Simple Java Program to send the Messages to this JMS Queue…like following:

“QueueSend.java”


import java.io.*;
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.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class QueueSend
{
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
public final static String JMS_FACTORY="QCF";
public final static String QUEUE="TestQ";

private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueSender qsender;
private Queue queue;
private TextMessage msg;

public void init(Context ctx, String queueName)
throws NamingException, JMSException
{
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();
}

public void send(String message) throws JMSException {
msg.setText(message);
qsender.send(msg);
}

public void close() throws JMSException {
qsender.close();
qsession.close();
qcon.close();
}

public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage: java QueueSend WebLogicURL");
return;
}
InitialContext ic = getInitialContext(args[0]);
QueueSend qs = new QueueSend();
qs.init(ic, QUEUE);
readAndSend(qs);
qs.close();
}

private static void readAndSend(QueueSend qs) throws IOException, JMSException
{
String line="Test Message Body with counter = ";
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
boolean readFlag=true;
System.out.println("ntStart Sending Messages (Enter QUIT to Stop):n");
while(readFlag)
{
System.out.print("&lt;Msg_Sender&gt; ");
String msg=br.readLine();
if(msg.equals("QUIT") || msg.equals("quit"))
{
qs.send(msg);
System.exit(0);
}
qs.send(msg);
System.out.println();
}
br.close();
}

private static InitialContext getInitialContext(String url) throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}
}

Step7). Now run the “setWLSEnv.cmd” and then compile and Run the QueueSend program.

java   QueueSend    t3://localhost:7001

Step8). Now Login to AdminConsole to check that the Message is arrived in the JMS Queue or Not?

home—> Services–> Messaging–> JMS Modules–> MySystemModule–>MyQueue–> Monitoring–> MySystemModule –> MyQueue (Check the CheckBox)–> Show Messages(Click This Button)

Monitoring JMS Queue Message In AdminConsole

Monitoring JMS Queue Message In AdminConsole

Step9). Now write the following “QueueReceive.java” program to read  JMS Messages from the JMS Queue.

“QueueReceive.java”

import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class QueueReceive implements MessageListener
{
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
public final static String JMS_FACTORY="QCF";
public final static String QUEUE="TestQ";
private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueReceiver qreceiver;
private Queue queue;
private boolean quit = false;

public void onMessage(Message msg)
{
try {
String msgText;
if (msg instanceof TextMessage)
{
msgText = ((TextMessage)msg).getText();
}
else
{
msgText = msg.toString();
}
System.out.println("nt&lt;Msg_Receiver&gt; "+ msgText );
if (msgText.equalsIgnoreCase("quit"))
{
synchronized(this)
{
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
}
catch (JMSException jmse)
{
jmse.printStackTrace();
}
}
public void init(Context ctx, String queueName) throws NamingException, JMSException
{
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qreceiver = qsession.createReceiver(queue);
qreceiver.setMessageListener(this);
qcon.start();
}

public void close()throws JMSException
{
qreceiver.close();
qsession.close();
qcon.close();
}

public static void main(String[] args) throws Exception
{
if (args.length != 1)
{
System.out.println("Usage: java QueueReceive WebLogicURL");
return;
}
InitialContext ic = getInitialContext(args[0]);
QueueReceive qr = new QueueReceive();
qr.init(ic, QUEUE);
System.out.println("JMS Ready To Receive Messages (To quit, send a "quit" message from QueueSender.class).");
// Wait until a "quit" message has been received.
synchronized(qr)
{
while (! qr.quit)
{
try
{
qr.wait();
}
catch (InterruptedException ie)
{}
}
}
qr.close();
}

private static InitialContext getInitialContext(String url) throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}
}

Step10). Now compile and run the QueueRecieve.java program to read the messages from WebLogic JMS  Queue.

java     QueueReceive    t3://localhost:7001

.

.

Thanks

Jay SenSharma


Copyright © 2010-2012 Middleware Magic. All rights reserved. |