I have a few classes that I implement the functionality so here they are:
SEMFAWebJMSPubSub class implements all sub functionality on the client side.
public class SEMFAWebJMSPubSub implements MessageListener {
private String _sportBusTopic = "jms/TopicBus"; // Main topic to
listen for updates
private String _reqQueueStr = "jms/TestQueue"; // request queue
private ConnectionFactory _connectionFactory = null;
private Connection _connection = null;
private Session _session = null;
private MessageConsumer _msgBus = null;
private Queue _reqQueue = null;
private List<SEMFAWebUpdateListener> _listeners =
new CopyOnWriteArrayList<SEMFAWebUpdateListener>();
public void start() {
try {
//SEMFAJMSUtils.init(true);
_connectionFactory =
SEMFAJMSUtils.getConnectionFactory();
_connection = _connectionFactory.createConnection();
_session = _connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
_msgBus =
SEMFAJMSUtils.getMessageTopicConsumer(_sportBusTopic, _session);
_reqQueue = SEMFAJMSUtils.getQueue(_reqQueueStr);
_connection.start();
} catch(Exception e) {
e.printStackTrace();
}
}
public boolean subscribe() {
try {
_msgBus.setMessageListener(this);
return true;
} catch(Exception e) {
e.printStackTrace();
}
return false;
}
public void close() {
try {
if(_connectionFactory == null) return;
_msgBus.close();
_session.close();
_connection.close();
} catch(Exception e) {
SEMFALog.logError("org.semfa.web.SEMFAWebJMSPubSub.close()", e.getMessage());
}
public void addListener(SEMFAWebUpdateListener listener_) {
_listeners.add(listener_);
}
public void onMessage(Message message_) {
try {
SEMFAJMSMessageDispatcher.valueOf(
message_.getJMSType()).processMessage(message_, _listeners);
} catch(Exception e) {
e.printStackTrace();
}
}
}
SEMFAJMSUtils implement general JMS and JNDI methods:
package org.semfa.common;
import javax.naming.*;
import javax.jms.*;
public class SEMFAJMSUtils {
public static final String CONFAC = "jms/ConnectionFactory";
public static final String QUEUECONFAC = "jms/QueueConnectionFactory";
public static final String TOPICCONFAC = "jms/TopicConnectionFactory";
private static Context jndiContext = null;
public static void init(boolean web) throws NamingException {
if (jndiContext == null) {
try {
if(web == true) {
jndiContext = (Context) new
InitialContext().lookup("java:comp/env");
}
else {
jndiContext = (Context) new
InitialContext();
}
listContext(jndiContext, "");
} catch (NamingException e) {
SEMFALog.logError(SEMFAJMSUtils.class.getName(),
"Could not create JNDI API
context: " + e.toString());
throw e;
}
}
}
/**
* Returns a ConnectionFactory object.
*
* @return a ConnectionFactory object
* @throws javax.naming.NamingException (or other
* exception) if name cannot be found
*/
public static ConnectionFactory getConnectionFactory()
throws Exception {
return (ConnectionFactory) jndiLookup(CONFAC);
}
/**
* Returns the ConnectionFactory object with the specified
* name.
*
* @param name String specifying connection factory
* name
* @return a ConnectionFactory object
* @throws javax.naming.NamingException (or other
* exception) if name cannot be found
*/
public static ConnectionFactory getConnectionFactory(String name)
throws Exception {
return (ConnectionFactory) jndiLookup(name);
}
/**
* Returns a QueueConnectionFactory object.
*
* @return a QueueConnectionFactory object
* @throws javax.naming.NamingException (or other
* exception) if name cannot be found
*/
public static QueueConnectionFactory getQueueConnectionFactory()
throws Exception {
return (QueueConnectionFactory) jndiLookup(QUEUECONFAC);
}
/**
* Returns a TopicConnectionFactory object.
*
* @return a TopicConnectionFactory object
* @throws javax.naming.NamingException (or other
* exception) if name cannot be found
*/
public static TopicConnectionFactory getTopicConnectionFactory()
throws Exception {
return (TopicConnectionFactory) jndiLookup(TOPICCONFAC);
}
/**
* Returns a MessageConsumer object for queue connection.
*
* @param name String specifying queue name
* @param session a Session object
*
* @return a Queue object
* @throws javax.naming.NamingException (or other
* exception) if name cannot be
found
*/
public static MessageConsumer getMessageQueueConsumer(String name,
Session session)
throws Exception {
Queue queue = (Queue) jndiLookup(name);
return session.createConsumer(queue);
}
/**
* Returns a MessageProducer object for queue connection.
*
* @param name String specifying queue name
* @param session a Session object
*
* @return a Queue object
* @throws javax.naming.NamingException (or other
* exception) if name cannot be
found
*/
public static MessageProducer getMessageQueueProducer(String name,
Session session)
throws Exception {
Queue queue = (Queue) jndiLookup(name);
return session.createProducer(queue);
}
/**
* Returns a MessageProducer object for topic connection.
*
* @param name String specifying topic name
* @param session a Session object
*
* @return a Topic object
* @throws javax.naming.NamingException (or other
* exception) if name cannot be
found
*/
public static MessageProducer getMessageTopicProducer(String name,
Session session)
throws Exception {
Topic topic = (Topic) jndiLookup(name);
return session.createProducer(topic);
}
/**
* Returns a MessageConsumer object for topic connection.
*
* @param name String specifying topic name
* @param session a Session object
*
* @return a Topic object
* @throws javax.naming.NamingException (or other
* exception) if name cannot be
found
*/
public static MessageConsumer getMessageTopicConsumer(String name,
Session session)
throws Exception {
Topic topic = (Topic) jndiLookup(name);
return session.createConsumer(topic);
}
/**
* Returns a Queue object for topic connection.
*
* @param name String specifying queue name
* @param session a Session object
*
* @return a Queue object
* @throws javax.naming.NamingException (or other
* exception) if name cannot be
found
*/
public static Queue getQueue(String name)
throws Exception {
return (Queue) jndiLookup(name);
}
/**
* Returns a Topic object for topic connection.
*
* @param name String specifying queue name
* @param session a Session object
*
* @return a Topic object
* @throws javax.naming.NamingException (or other
* exception) if name cannot be
found
*/
public static Topic getTopic(String name)
throws Exception {
return (Topic) jndiLookup(name);
}
/**
* Creates a JNDI API InitialContext object if none exists
* yet. Then looks up the string argument and returns the
* associated object.
*
* @param name the name of the object to be looked up
*
* @return the object bound to name
* @throws javax.naming.NamingException (or other
* exception) if name cannot be found
*/
public static Object jndiLookup(String name) throws NamingException {
Object obj = null;
if (jndiContext == null) {
init(false);
}
try {
obj = jndiContext.lookup(name);
} catch (NamingException e) {
SEMFALog.logError(SEMFAJMSUtils.class.getName(),
"JNDI API lookup failed: " +
e.toString());
throw e;
}
return obj;
}
public static void printAllJndiNames() {
try {
Context ctx = (Context)new
InitialContext().lookup("java:comp/env");
listContext(ctx, "");
}
catch(Exception e) {
SEMFALog.logError(SEMFAJMSUtils.class.getName(),
"JNDI list failed: " +
e.toString());
}
}
/**
* Recursively exhaust the JNDI tree
*/
private static final void listContext(Context ctx, String indent) {
try {
NamingEnumeration list = ctx.listBindings("");
while (list.hasMore()) {
Binding item = (Binding) list.next();
String className = item.getClassName();
String name = item.getName();
System.out.println(indent + className + " " +
name);
Object o = item.getObject();
if (o instanceof javax.naming.Context) {
listContext((Context) o, indent + "
");
}
}
} catch (NamingException ex) {
System.out.println("JNDI failure: " + ex);
}
}
}
--
[Message sent by forum member 'nakoned']
View Post: http://forums.java.net/node/842104