ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » IBM MQ Java / JMS » Auotmatic message retrieval

Post new topic  Reply to topic
 Auotmatic message retrieval « View previous topic :: View next topic » 
Author Message
spasco
PostPosted: Wed Jul 23, 2003 1:17 pm    Post subject: Auotmatic message retrieval Reply with quote

Newbie

Joined: 23 Jul 2003
Posts: 4
Location: North Hollywood

I've setup message retrieval using JMS and JNDI and it works. My test process is to put an XML message on the queue. I then manually kick off my JMS app to retrieve the message. This works. How do I set up my JMS app to always "listen" to the queue. Currently it just stops after it's intial fetch.

Our system will intermittently send messages to the queue, the JMS app needs to constantly listen without manual intervention. Is this possible without an application server or setting up a Thread?


Thank you,
Stephen


Here's the JMS code I'm using to retrieve messages:


package disney.wdpro.ps.test.processing;

import com.ibm.jms.JMSBytesMessage;

import javax.jms.*;

// Imports for JNDI
import javax.naming.*;
import javax.naming.directory.*;

// Used for JNDI initialisation
import java.util.Hashtable;

public class TEST_Container_JMS {

// The following can be modified if you want to experiment with
// using other administered objects with different content.
public static final String qcfLookup = "ivtQCF";
public static final String qLookup = "ivtQ";

// edit these to match your environment
public static final String icf = "com.sun.jndi.fscontext.RefFSContextFactory";
public static final String url = "file:/C://JNDI";

/** entry point when called from command line. */
public static void main(String[] args) {
QueueSession session = null;
QueueConnection connection = null;
QueueConnectionFactory factory = null;

System.out.println("PTPSample02 - simple example of using asynchronous message delivery.");

try {

System.out.println("Getting administered objects from JNDI");

Context ctx = initJNDI(icf, url);
factory = getConnectionFactoryFromJNDI(ctx, qcfLookup);
Queue Q1 = getQueueFromJNDI(ctx, qLookup);

// Create a connection
System.out.println("creating connection");
connection = factory.createQueueConnection();

// Create a session
System.out.println("creating session");
session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

// Obtain queue
System.out.println("creating JMS Queue");
Queue q1 = session.createQueue("PS2WDPRO.Q");

// Create QueueReceiver with a message selector for 'red' messages
System.out.println("creating queue receiver");
QueueReceiver receiver1 = session.createReceiver(q1);

// Register a listener
System.out.println("registering message listener");
PTPSample02MListener listener1 = new PTPSample02MListener(1);
receiver1.setMessageListener(listener1);

// Now that we have stopped sending messages, we can start the
// connection. This also starts asynchronous receive on
// the session - it would be illegal to send a message while
// asynchronous receive was active on the same session.
System.out.println("starting connection");
connection.start();

Thread.sleep(5000); // give the receivers time to print messages

}
catch (JMSException je) {
System.err.println("caught " + je);
Exception e = je.getLinkedException();
if (e != null) {
System.err.println("subexception: " + e);
}
}
catch (Exception e) {
System.err.println("caught " + e);
}
finally {
if (session != null) {
try {
System.out.println("closing session");
session.close();
}
catch (JMSException e) {
System.err.println("caught " + e);
}
}
if (connection != null) {
try {
System.out.println("closing connection");
connection.close();
}
catch (JMSException e) {
System.err.println("connection.close() threw " + e);
}
}

}

System.out.println("Finished ===============================\n");

}

//=========================================================================
/** create an InitialDirContext for retrieving JNDI objects. */
static InitialDirContext initJNDI(String icf, String url)
throws JMSException, Exception {
// the context to be returned from this routine
InitialDirContext ctx = null;

// hashtable for the JNDI initialisation properties
Hashtable environment = new Hashtable();

if (url == null) {
// A URL must be supplied, so throw an exception if none is available
throw new Exception("You must specify the -url providerURL parameter");
}

// Firstly, store the name of the 'initial context factory'
environment.put(Context.INITIAL_CONTEXT_FACTORY, icf);

// Secondly, specify the location of the JNDI service provider
environment.put(Context.PROVIDER_URL, url);

// Finally, ensure that referrals are thrown. This is required
// as a workaround to a problem encountered with certain comb-
// inations of JNDI service provider and JNDI classes
environment.put(Context.REFERRAL, "throw");

try {
// Attempt to connect to the JNDI service provider, supply-
// ing the properties previously supplied
ctx = new InitialDirContext(environment);

}
catch (Exception e) {
// return an exception to the caller
JMSException je = new JMSException("Cannot create JNDI InitialContext!");
je.setLinkedException(e);
throw je;
}

return ctx;

}

//=========================================================================
/** Get the named QueueConnectionFactory object from the JNDI namespace.
*/
static QueueConnectionFactory getConnectionFactoryFromJNDI(Context ctx,
String name)
throws JMSException {
// the factory to be returned
QueueConnectionFactory factory = null;

System.out.println("Retrieving a QueueConnectionFactory from JNDI");

try {
// Attempt to retrieve a QueueConnectionFactory from the
// JNDI namespace
factory = (QueueConnectionFactory) ctx.lookup("cn=" + name);

}
catch (Exception e) {
// If this has failed, try the operation again without the cn prefix
try {
factory = (QueueConnectionFactory) ctx.lookup(name);
}
catch (Exception e2) {
// After a second failure pass an exception to the caller
JMSException je =
new JMSException("Unable to retrieve the QCF object from JNDI");
je.setLinkedException(e2);
throw je;
}
}

return factory;

}

//=========================================================================
/** Get the named Queue object from the JNDI namespace. */
static Queue getQueueFromJNDI(Context ctx, String name)
throws JMSException {
// The Queue to be returned
Queue ioQueue = null;

System.out.println("Retrieving a Queue from JNDI");
try {
// Attempt to retrieve a Queue from the JNDI namespace
ioQueue = (Queue) ctx.lookup("cn=" + name);
}
catch (Exception e) {
try {
// If this fails, attempt again without the "cn=" prefix
ioQueue = (Queue) ctx.lookup(name);

}
catch (Exception e2) {
// After a second failure pass an exception back to the caller
JMSException je = new JMSException("Unable to retrieve the Queue '" +
name + "' from JNDI");
je.setLinkedException(e2);
throw je;
}
}

return ioQueue;
}
}

/*
* This class provides the message listener.
*/

class PTPSample02MListener implements MessageListener {
// an id to allow us to distinguish between different instances
int id;

PTPSample02MListener(int id) {
this.id = id;
}


/** This method provides the implementation of the MessageListener
interface. */
public void onMessage(Message message) {
String body;

try {
System.out.println("in onMessage of listener " + id);

if (message instanceof JMSBytesMessage) {
System.out.println("BytesMessage");
System.out.println((BytesMessage)message);
// display the message contents

}
else {
System.out.println("error: message was not a TextMessage as expected");
System.out.println(message);
}
}
catch (Exception e) {
System.out.println("onMessage caught " + e);
}
}
}
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » IBM MQ Java / JMS » Auotmatic message retrieval
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.