1 Reply Latest reply on Aug 10, 2015 5:51 AM by ckemper

    Problems with JNDI-lookup using SwiftMQ

    ckemper

      Hi all,

      I just migrated from JBoss Fuse 6.1 to 6.2 and now I have a problem with my bundle which uses a JNDI-looup via Swift-MQ. In 6.1 everything works fine, but now JNDI lookup fails. I don't know why. Maybe one of you can help me!?

      Logfile says:

       

      javax.naming.NoInitialContextException: Cannot instantiate class: com.swiftmq.jndi.InitialContextFactoryImpl [Root exception is java.lang.ClassNotFoundException: com.swiftmq.jndi.InitialContextFactoryImpl]

       

      My code looks like this:

       

          public void start(BundleContext bundleContext) {

               INITIAL_CONTEXT_FACTORY="com.swiftmq.jndi.InitialContextFactoryImpl";

               PROVIDER_URL="smqp://localhost:4001/timeout=10000";

               try {

                  InitialContextFactoryImpl icf = new InitialContextFactoryImpl();

               // Begin SwiftMQ

                  Hashtable env = new Hashtable();

                  env.put(Context.INITIAL_CONTEXT_FACTORY,INITIAL_CONTEXT_FACTORY);

                  env.put(Context.PROVIDER_URL,PROVIDER_URL);

                  InitialContext jndiContext;

                  jndiContext = new InitialContext(env);

                  System.out.println("Hey! It works! Returned:");

              } catch(Exception ex) {

                  System.err.println("oops!" + ex);

              }

          }

       

      My pom.xml looks like this:

       

      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>de.mycompany</groupId>

        <artifactId>jndi-example</artifactId>

        <version>0.0.1-SNAPSHOT</version>

        <packaging>bundle</packaging>

        <build>

        <plugins>

        <plugin>

        <groupId>org.apache.felix</groupId>

        <artifactId>maven-bundle-plugin</artifactId>

        <version>2.3.6</version>

        <extensions>true</extensions>

        <configuration>

        <archive>

        <addMavenDescriptor>false</addMavenDescriptor>

        </archive>

        <instructions>

        <Bundle-ManifestVersion>2</Bundle-ManifestVersion>

        <Bundle-Version>${project.version}</Bundle-Version>

        <Bundle-SymbolicName>${project.groupId}.jndi</Bundle-SymbolicName>

        <Bundle-Name>${project.artifactId}</Bundle-Name>

        <Bundle-Activator>de.mycompany.Activator</Bundle-Activator>

        <Bundle-Description>${project.description}</Bundle-Description>

        <Bundle-Category>JNDI</Bundle-Category>

        <Import-Package>!com.ibm.*,

        *;resolution:=optional

        </Import-Package>

      <!-- <Private-Package>*</Private-Package>

      --> <Include-Resource>src/main/resources</Include-Resource>

        <Embed-Dependency>*;scope=compile|runtime;type=!pom;inline=false</Embed-Dependency>

        </instructions>

        </configuration>

        </plugin>

        </plugins>

        </build>

        <dependencies>

        <!-- OSGi -->

        <dependency>

        <groupId>org.osgi</groupId>

        <artifactId>org.osgi.compendium</artifactId>

        <version>4.2.0</version>

        <type>jar</type>

        <scope>provided</scope>

        </dependency>

        <dependency>

        <groupId>org.osgi</groupId>

        <artifactId>org.osgi.core</artifactId>

        <version>4.2.0</version>

        <type>jar</type>

        <scope>provided</scope>

        </dependency>

        <dependency>

        <groupId>com.swiftmq</groupId>

        <artifactId>SwiftMQ</artifactId>

        <version>6.0</version>

        <scope>compile</scope>

        </dependency>

        <dependency>

        <groupId>javax.jms</groupId>

        <artifactId>jms</artifactId>

        <version>1.1</version>

        <scope>provided</scope>

        </dependency>

        </dependencies>

      </project>

       

      I wonder what has changed from 6.1 to 6.2?

      Can anybody help me?

       

      Thank you!

      Carsten

        • 1. Re: Problems with JNDI-lookup using SwiftMQ
          ckemper

          Finally I found the solution by myself.

          I just added

                  Thread.currentThread().setContextClassLoader(UsedClass.class.getClassLoader());

          before doing the JNDI-lookup. And it works.

           

          After JNDI-lookup is done, you can use the original class loader again (if you have stored it)

                    ClassLoader origCL = Thread.currentThread().getContextClassLoader();

          Set it back to original class loader:

                    Thread.currentThread().setContextClassLoader(origCL);

           

          Cheers

          Carsten