aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes how to receive message from a queue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "how to receive message from a queue" Watch "how to receive message from a queue" New topic
Author

how to receive message from a queue

sadish kumar
Greenhorn

Joined: Jul 21, 2008
Posts: 7
I want to receive a particular message from a queue. Is it possible using jms?
Jetendra Ivaturi
Ranch Hand

Joined: Feb 08, 2007
Posts: 159
Yes very much possible.

You will have sample examples in bea folder, find them how to use topic or queue.

Before you need to cofigure the Queue to receive the messages.


SCJP 1.4 & 1.5, SCWCD 1.5. Learn and Let Learn.
sadish kumar
Greenhorn

Joined: Jul 21, 2008
Posts: 7
Thanks for your reply.

I am using stand alone java application which uses jms to send message to queue. Client installed the queue and they gave the queue name,queue manager name,etc to me... So if you have any sample programs, it will be good for me.
Jetendra Ivaturi
Ranch Hand

Joined: Feb 08, 2007
Posts: 159
The following is the Queue Receive class.

You have to change the ConnectionFactory and Queue Jndi.

Good Luck.

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

/**
* This example shows how to establish a connection to
* and receive messages from a JMS queue. The classes in this
* package operate on the same JMS queue. Run the classes together to
* witness messages being sent and received, and to browse the queue
* for messages. This class is used to receive and remove messages
* from the queue.
*
* @author Copyright (c) 1999-2006 by BEA Systems, Inc. All Rights Reserved.
*/
public class QueueReceiveS implements MessageListener
{
// Defines the JNDI context factory.
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";

// Defines the JMS connection factory for the queue.
public final static String JMS_FACTORY="btargetjndi";

// Defines the queue.
public final static String QUEUE="btargetqueuejndi";

private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueReceiver qreceiver;
private Queue queue;
private boolean quit = false;

/**
* Message listener interface.
* @param msg message
*/
public void onMessage(Message msg)
{
try {
String msgText;
if (msg instanceof TextMessage) {
msgText = ((TextMessage)msg).getText();
} else {
msgText = msg.toString();
}

System.out.println("Message Received: "+ msgText );

if (msgText.equalsIgnoreCase("quit")) {
synchronized(this) {
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}

/**
* Creates all the necessary objects for receiving
* messages from a JMS queue.
*
* @param ctxJNDI initial context
* @paramqueueNamename of queue
* @exception NamingException if operation cannot be performed
* @exception JMSException if JMS fails to initialize due to internal error
*/
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();
}

/**
* Closes JMS objects.
* @exception JMSException if JMS fails to close objects due to internal error
*/
public void close()throws JMSException
{
qreceiver.close();
qsession.close();
qcon.close();
}
/**
* main() method.
*
* @param args WebLogic Server URL
* @exception Exception if execution fails
*/

public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage: java examples.jms.queue.QueueReceive WebLogicURL");
return;
}
InitialContext ic = getInitialContext(args[0]);
QueueReceiveS qr = new QueueReceiveS();
qr.init(ic, QUEUE);

System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");

// 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);
}

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to receive message from a queue