1 Reply Latest reply on Dec 27, 2015 2:04 PM by Guillermo González de Agüero

    Unable to access External JNDI Resources from my EJB in Wildfly

    Jose E. Chavez Newbie

      We are running on an external JMS broker (Fiorano MQ). We want to connect to this broker using JNDI. Our app is running on Wildfly 8.1.0.


      I have defined an external context in my standalone.xml as follows:

       

      <subsystem xmlns="urn:jboss:domain:naming:2.0">

          <bindings>

              <external-context name="java:global/federation/fiorano/mq" module="com.fiorano.mq.ra" class="fiorano.jms.runtime.naming.FioranoInitialContextFactory" cache="true">

                  <environment>

                      <property name="java.naming.factory.initial" value="fiorano.jms.runtime.naming.FioranoInitialContextFactory"/>

                      <property name="java.naming.provider.url" value="http://192.168.1.1:1900"/>

                  </environment>

              </external-context>

              <lookup name="java:jboss/exported/fiorano/mq" lookup="java:global/federation/fiorano/mq"/>

          </bindings>

          <remote-naming/>

      </subsystem>

       

      Now, I want to lookup the ConnectionFactory and Queue using @Resource in my EJB:


      package com.company.wildfly.ejb;

       

      import javax.annotation.PostConstruct;

      import javax.annotation.Resource;

      import javax.ejb.LocalBean;

      import javax.ejb.Stateless;

      import javax.jms.ConnectionFactory;

      import javax.jms.JMSConsumer;

      import javax.jms.JMSContext;

      import javax.jms.JMSException;

      import javax.jms.JMSProducer;

      import javax.jms.Message;

      import javax.jms.Queue;

      import javax.jms.TemporaryQueue;

      import javax.jms.TextMessage;

      import javax.naming.InitialContext;

      import javax.naming.NamingException;

       

      Stateless

      @LocalBean

      public class AuthenticatorBean {

       

        @Resource(lookup = "java:/global/federation/fiorano/mq/PRIMARYCF")

        private ConnectionFactory connectionFactory;

       

        @Resource(lookup = "java:/global/federation/fiorano/mq/TESTQUEUE")

        private Queue authenticationQueue;

       

        public AuthenticatorBean() {

        }

       

        public void authenticate(String password) {

          JMSContext context = connectionFactory.createContext();

          TemporaryQueue temporaryQueue = context.createTemporaryQueue();

          TextMessage textMessage = context.createTextMessage(password);

          JMSProducer producer = context.createProducer();

          JMSConsumer consumer = context.createConsumer(temporaryQueue);

          producer.send(authenticationQueue, textMessage);

          Message message = consumer.receive(5000);

          try {

            temporaryQueue.delete();

          } catch (JMSException e) {

            e.printStackTrace();

          }

        }

       

      }


      But when I do this, I get the exception:


      ...

      Caused by: javax.naming.NamingException: JBAS011878: Failed to lookup env/com.company.wildfly.ejb.AuthenticatorBean/connectionFactory [Root exception is java.lang.RuntimeException: JBAS011875: Resource lookup for injection failed: java:/global/federation/fiorano/mq/PRIMARYCF]

          at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:144)

          at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:81)

          at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:202)

          at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:188)

          at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:184)

          at org.jboss.as.naming.deployment.ContextNames$BindInfo$1$1.getReference(ContextNames.java:310)

          ... 157 more

      Caused by: java.lang.RuntimeException: JBAS011875: Resource lookup for injection failed: java:/global/federation/fiorano/mq/PRIMARYCF

          at org.jboss.as.naming.deployment.ContextNames$BindInfo$1$1.getReference(ContextNames.java:313)

          at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:131)

          ... 162 more

      Caused by: javax.naming.NameNotFoundException: global/federation/fiorano/mq/PRIMARYCF -- service jboss.naming.context.java.global.federation.fiorano.mq.PRIMARYCF

          at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:104)

          at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:202)

          at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:188)

          at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:184)

          at org.jboss.as.naming.deployment.ContextNames$BindInfo$1$1.getReference(ContextNames.java:310)

          ... 163 more

       

      Is there something wrong with my external JNDI configuration?

        • 1. Re: Unable to access External JNDI Resources from my EJB in Wildfly
          Guillermo González de Agüero Newbie

          Hi,

           

          I think you are confusing some concepts.

           

          java:jboss/exported should be used to expose a JNDI entry to an external client. In your case, you are connecting to the Fionaro JNDI, and I assume the entry you are searching is already exported.

           

          You have configured an external context and according to [WFLY-1727] Allow injection of resources from external JNDI contexts - JBoss Issue Tracker you should be able to navigate through it in the @Resource injectiom, but it didn't work for me.

           

          What you can actually do is to bind a lookup to the external context. and then inject based on that alias.

           

          Try with this configuration:

          <subsystem xmlns="urn:jboss:domain:naming:2.0">

              <bindings>

                  <external-context name="java:global/federation/fiorano/mq" module="com.fiorano.mq.ra" class="fiorano.jms.runtime.naming.FioranoInitialContextFactory" cache="true">

                      <environment>

                          <property name="java.naming.factory.initial" value="fiorano.jms.runtime.naming.FioranoInitialContextFactory"/>

                          <property name="java.naming.provider.url" value="http://192.168.1.1:1900"/>

                      </environment>

                  </external-context>

                  <lookup name="java:/global/mq/PRIMARYCF" lookup="java:global/federation/fiorano/mq/PRIMARYCF"/>

                  <lookup name="java:/global/mq/TESTQUEUE" lookup="java:global/federation/fiorano/mq/PRIMARYCF"/>

              </bindings>

              <remote-naming/>

          </subsystem>


          You should be able to inject now with

            @Resource(lookup = "java:/global/mq/PRIMARYCF")

            private ConnectionFactory connectionFactory;

           

            @Resource(lookup = "java:/global/mq/TESTQUEUE")

            private Queue authenticationQueue;


          Let me know if it helped!