Configure JNDI for Tibco EMS in Tomcat

Steve Bosman picture Steve Bosman · Mar 20, 2012 · Viewed 7.9k times · Source

I have written some code for creating a topic connection from Tibco EMS

TopicConnectionFactory factory = new TibjmsTopicConnectionFactory(serverUrl);
TopicConnection connection = factory.createTopicConnection(username, password);
/* if clientID is specified we must set it right here */
if (clientID != null) {
  connection.setClientID(clientID);
}

where serverUrl, username, password and clientId are read from a config file. I've been told that I should be doing this using JNDI (something I'm woefully ignorant of), what do I need to do?

Update

from assorted searching I thought putting this in the context.xml might be appropriate

<Resource auth="Container" brokerName="not-tibco-952v" brokerURL="tcp://not-tibco-952v:10905"
          description="JMS Connection Factory" factory="com.tibco.tibjms.naming.TibjmsObjectFactory"
          name="jms/ProvisioningMessageConnectionFactory" type="com.tibco.tibjms.naming.TibjmsConnectionFactoryAttributes"
          factoryClass="com.tibco.tibjms.TibjmsConnectionFactory" jndiName="TopicConnectionFactory" serverUrl="not-tibco-952v:10905"
          username="tibco" password="tibco"/>

but

final Object obj = initialContext.lookup("java:comp/env/jms/ProvisioningMessageConnectionFactory");

brings back a null result

Answer

Steve Bosman picture Steve Bosman · Mar 28, 2012

In the absence of any better knowledge I created my own ObjectFactory

public class ProvisioningConnectionsFactory implements ObjectFactory {
  public Object getObjectInstance(Object obj,
                                  Name namex, Context nameCtx, Hashtable environment)
          throws NamingException {

    // Acquire an instance of our specified bean class
    ProvisioningConnection conns = new ProvisioningConnection();

    // Customize the bean properties from our attributes
    Reference ref = (Reference) obj;
    Enumeration addrs = ref.getAll();
    while (addrs.hasMoreElements()) {
      RefAddr addr = (RefAddr) addrs.nextElement();
      String name = addr.getType();
      String value = (String) addr.getContent();
      if (name.equals("serverUrl")) {
        conns.setServerUrl(value);
      } else if (name.equals("username")) {
        conns.setUsername(value);
      } else if (name.equals("password")) {
        conns.setPassword(value);
      } else if (name.equals("durableTopicSubscriberName")) {
        conns.setDurableTopicSubscriberName(value);
      } else if (name.equals("topicName")) {
        conns.setTopicName(value);
      }
    }

    // Return the customized instance
    return conns;

  }
}

which meant I could add the following to my context.xml:

<Resource name="jms/ProvisioningMessageConnection" auth="Container"
          type="com.foo.mytrialsprovisioning.ProvisioningConnection"
          factory="com.foo.mytrialsprovisioning.ProvisioningConnectionsFactory"
          serverUrl = "tcp://not-tibco-952v:10905"
          username = "tibco"
          password = "tibco"
          durableTopicSubscriberName = "PROVISIONING_SUBSCRIBER"
          topicName = "FOOBAR"
        />

and an instance of ProvisioningConnection:

public class ProvisioningConnection {
  private static final Log LOG = LogFactory.getLog(new CurrentClassGetter().getClassName());
  private static final String MESSAGE_SELECTOR = "client_ID='%s'";
  private String serverUrl;
  private String username;
  private String password;
  private String durableTopicSubscriberName;
  private String projectIdentifier;
  private String topicName;

  public void setServerUrl(String serverUrl) {
    this.serverUrl = serverUrl;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public void setDurableTopicSubscriberName(String durableTopicSubscriberName) {
    this.durableTopicSubscriberName = durableTopicSubscriberName;
  }

  public void setProjectIdentifier(String projectIdentifier) {
    this.projectIdentifier = projectIdentifier;
  }

  public void setTopicName(String topicName) {
    this.topicName = topicName;
  }

  public TopicConnection getTopicConnection()
          throws JMSException, NamingException {
    LOG.info("Provisioning against server: " + serverUrl);
    TopicConnectionFactory factory = new TibjmsTopicConnectionFactory(serverUrl);
    TopicConnection connection = factory.createTopicConnection(username, password);
    /* if clientID is specified we must set it right here */
    if (projectIdentifier != null) {
      connection.setClientID(projectIdentifier);
    }
    return connection;
  }

  public TopicSubscriber getTopicSubscriber(Session session)
          throws JMSException {
    LOG.info("Provisioning subscription on topic: " + topicName);
    // Use createTopic() to enable subscriptions to dynamic topics.
    Topic topic = session.createTopic(topicName);
    return session.createDurableSubscriber(topic, durableTopicSubscriberName, (
            Tools.isNullOrEmptyString(projectIdentifier) ?
            "" :
            String.format(MESSAGE_SELECTOR, projectIdentifier)
    ), true);
  }
}

could be created using:

Context envCtx = (Context) initCtx.lookup("java:comp/env");
return (ProvisioningConnection)envCtx.lookup("jms/ProvisioningMessageConnection");