Michael R. Head (suppressingfire) wrote,
Michael R. Head

Running code that loads a managed JMS connection factory outside of Websphere

Previously, I had some code that used a container managed JDBC data source that ran in Tomcat. Today, I've got some code that uses a managed JMS connection factory to access an ActiveMQ server from within WAS 7.

This code lives in a particular Eclipse project (ZZZ_Test_JAR), and I want to set up my testing classes from a new project (ZZZ_Test_JUNIT) that just refers to the ZZZ_Test_JAR project and doesn't change any of the code or the project metadata.

I was able to eventually achieve this by setting up a Tomcat 6.0 server runtime and loading a couple jars into the ZZZ_Test_JUNIT project.

First trick: load activemq-all-5.2.0.jar (from the ActiveMQ binary package) and tomcat-juli.jar (from the Tomcat 6.0 binary package) into ZZZ_Test_JUNIT's build path.

Second trick: install Apache Tomcat 6.0 as a "Server" in Eclipse. (Servers view, New/Server/Apache/Tomcat v6.0 Server). Add "Apache Tomcat v6.0" as a "Library" to ZZZ_Test_JUNIT's build path.

Third trick: set up the naming context when starting the test program using Tomcat's classes (import org.apache.naming.java.javaURLContextFactory and classes under org.apache.naming). Setting the Context.URL_PKG_PREFIXES property is important because a WAS runtime is likely going to be in the classpath at runtime and it's really important to use the Tomcat JNDI naming classes.
  private static void setupNamingContext() throws NamingException {
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, javaURLContextFactory.class.getName());
    System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
    Context ctx = new InitialContext();
    // Next two lines are needed if code looks up the connection factory like: ctx.lookup("java:comp/env/jms/CF")
    Context jmsCtx = ctx.createSubcontext("java:comp").createSubcontext("env").createSubcontext("jms");
    jmsCtx.bind("CF", new ActiveMQConnectionFactory());
    // Next two lines are needed if code looks up the connection factory like: ctx.lookup("jms/CF")
    Context jmsCtx2 = ctx.createSubcontext("jms");
    jmsCtx2.bind("CF", new ActiveMQConnectionFactory());

Fourth trick: be sure the code that looks up the connection factory in one of the two ways described in the comments above. If the code uses JNDI to look up its queues, too, they can be bound into the environment like this (where the queue's physical name is "queue-name":
  jmsCtx2.bind("Q", new ActiveMQQueue("queue-name"));
Tags: eclipse, java

  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.