title: How connect to WildFly 10.1.0.Final ActiveMQ Artemis using JNDI and Spring?
body: <p>I have WildFly 10.1.0.Final, this have Artemis 1.1.0
  Whats the correct way of configure jndi.properties?:</p>
  
  <pre><code>java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory
  java.naming.provider.url=http-remoting://127.0.0.1:8080
  jboss.naming.client.ejb.context=false
  </code></pre>
  
  <p>This worket with WildFly 9.0.0.Final that have HornetQ:</p>
  
  <pre><code>java.naming.factory.initial=org.jboss.naming.remote.client.InitialContextFactory
  java.naming.provider.url=http-remoting://127.0.0.1:8080
  jboss.naming.client.ejb.context=false
  </code></pre>
  
  <p>I'm using Spring 4.3.2, Spring-jms and included the wildfly-jms-client-bom in may Maven POM.xml.
  The Next is my connection Factory.  Whith Wildfly 9 I try InitialContext.doLookup(..) and this work. But with WildFly 10 this don't work in same way.</p>
  
  <p>My principal configuration files is:</p>
  
  <pre><code>@Configuration
  @ComponentScan(basePackages = "org")
  @Import({
      MessagingConfiguration.class,
      MessagingListenerConfiguration.class
  
      })
  public class AppConfig {
  
      //Put Other Application configuration here.
  
  
  }
  </code></pre>
  
  <p>.</p>
  
  <pre><code>package org.jms.configuration;
  @Configuration
  public class MessagingConfiguration {
  
  @Bean
  public ConnectionFactory connectionFactory() throws NamingException{
  
      ConnectionFactory connectionFactory = InitialContext.doLookup("java:/ConnectionFactory");
      return connectionFactory;
  }
  
  /*
  @Bean
  public JmsTemplate jmsTemplate() throws NamingException {
      JmsTemplate template = new JmsTemplate();
      template.setConnectionFactory(connectionFactory());
      //template.setDefaultDestinationName(ORDER_RESPONSE_QUEUE);
      return template;
  }
  */
  
  }
  </code></pre>
  
  <p>And I have the next:</p>
  
  <pre><code>package org.jms.configuration;
  @Configuration
  @EnableJms
  public class MessagingListenerConfiguration {
  
      @Autowired
      ConnectionFactory connectionFactory;
  
  
      public MessagingListenerConfiguration() {
          System.out.println("MessagingListenerConfiguration::Constructor!!");
      }
  
      @Bean
      public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
  
          DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
  
          factory.setConnectionFactory(connectionFactory);
  
          factory.setConcurrency("1-1");
  
          return factory;
      }
  }
  </code></pre>
  
  <p>Finally I have this listener. </p>
  
  <pre><code>package org.jms.messaging;
  
  import javax.jms.JMSException;
  
  import org.gasmart.jms.model.Product;
  import org.gasmart.jms.service.OrderService;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.jms.annotation.JmsListener;
  import org.springframework.messaging.Message;
  import org.springframework.messaging.MessageHeaders;
  import org.springframework.stereotype.Component;
  
  @Component
  public class MessageReceiver {
  
      //private static final String ORDER_QUEUE = "order-queue";
  
      /*
      *   &lt;jms-queue name="shatTopic" entries="java:/jms/queue/shat java:/jboss/exported/jms/queue/shat"/&gt;
      */
      private static final String ORDER_QUEUE = "shat";
  
      public MessageReceiver() {
          System.out.println("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; OK");
      }
  
      @Autowired
      OrderService orderService;
  
  
      @JmsListener(destination = ORDER_QUEUE)
      public void receiveMessage(final Message&lt;String&gt; message) {
  
          MessageHeaders headers =  message.getHeaders();
  
          System.out.println("MessageReceiver::receiveMessage(product)   Application : headers received : "+headers);
  
          //orderService.processOrder(product);
  
  
      }
  
  }
  </code></pre>
  
  <p>I get this error:</p>
  
  <p>20:04:42,971 WARN  [org.springframework.jms.listener.DefaultMessageListenerContainer] (DefaultMessageListenerContainer-124) Setup of JMS message listener invoker failed for destination 'shat' - trying to recover. <strong>Cause: There is no queue with name shat</strong></p>
  
  <p>20:04:42,971 INFO  [org.springframework.jms.listener.DefaultMessageListenerContainer] (DefaultMessageListenerContainer-124) Successfully refreshed JMS Connection</p>
  
  <p>The <strong>shat</strong> queue es declared in MessageReceiver class.
  My standalone-full.xml is the next:</p>
  
  <pre><code>&lt;subsystem xmlns="urn:jboss:domain:messaging-activemq:1.0"&gt;
              &lt;server name="default"&gt;
                  &lt;security-setting name="#"&gt;
                      &lt;role name="guest" send="true" consume="true" create-non-durable-queue="true" delete-non-durable-queue="true"/&gt;
                  &lt;/security-setting&gt;
                  &lt;security-setting name="jms.topic.chat"&gt;
                      &lt;role name="guest2" send="true" consume="true" create-non-durable-queue="true"/&gt;
                  &lt;/security-setting&gt;
                  &lt;address-setting name="#" dead-letter-address="jms.queue.DLQ" expiry-address="jms.queue.ExpiryQueue" max-size-bytes="10485760" page-size-bytes="2097152" message-counter-history-day-limit="10"/&gt;
                  &lt;http-connector name="http-connector" socket-binding="http" endpoint="http-acceptor"/&gt;
                  &lt;http-connector name="http-connector-throughput" socket-binding="http" endpoint="http-acceptor-throughput"&gt;
                      &lt;param name="batch-delay" value="50"/&gt;
                  &lt;/http-connector&gt;
                  &lt;in-vm-connector name="in-vm" server-id="0"/&gt;
                  &lt;http-acceptor name="http-acceptor" http-listener="default"/&gt;
                  &lt;http-acceptor name="http-acceptor-throughput" http-listener="default"&gt;
                      &lt;param name="batch-delay" value="50"/&gt;
                      &lt;param name="direct-deliver" value="false"/&gt;
                  &lt;/http-acceptor&gt;
                  &lt;remote-acceptor name="websocket-stomp" socket-binding="netty-ws"/&gt;
                  &lt;in-vm-acceptor name="in-vm" server-id="0"/&gt;
                  &lt;jms-queue name="ExpiryQueue" entries="java:/jms/queue/ExpiryQueue"/&gt;
                  &lt;jms-queue name="DLQ" entries="java:/jms/queue/DLQ"/&gt;
                  &lt;jms-queue name="shatTopic" entries="java:/jms/queue/shat java:/jboss/exported/jms/queue/shat"/&gt;
                  &lt;connection-factory name="InVmConnectionFactory" entries="java:/ConnectionFactory" connectors="in-vm"/&gt;
                  &lt;connection-factory name="RemoteConnectionFactory" entries="java:jboss/exported/jms/RemoteConnectionFactory" connectors="http-connector"/&gt;
                  &lt;pooled-connection-factory name="activemq-ra" entries="java:/JmsXA java:jboss/DefaultJMSConnectionFactory" connectors="in-vm" transaction="xa"/&gt;
              &lt;/server&gt;
          &lt;/subsystem&gt;
  </code></pre>
  
  <p>I have defined the queue, that is correctly defined:</p>
  
  <p></p>
  
  <p>References about how declare a queue can consult here:  <a href="http://www.mastertheboss.com/jboss-server/jboss-jms/jboss-jms-configuration" rel="nofollow">http://www.mastertheboss.com/jboss-server/jboss-jms/jboss-jms-configuration</a></p>
  
  <p>Why I cant connect?? I don't understand. I actually tryied many many ways, don't work.</p>
  
  <p><strong>UPDATE:  2016-August-29 - 10:00 A.M</strong></p>
  
  <p>I found that if I restart all the wildfly with the application actually deployed, after load the application, If I connect a client (I have a Web client that produce messages directly to the "jms.queue.shat" queue using Websocket over STOMP with stomp.js) and send messages, the application (java) consume correctly the messages.  But if I redeploy the application after it was correctly working, this beging to produce ERROR.
  The errors is next:</p>
  
  <pre><code>10:43:51,607 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool - 113) WFLYUT0021: Registered web context: /ServerApp
  10:43:51,632 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) WFLYSRV0010: Deployed "ServApp.war" (runtime-name : "ServApp.war")
  10:43:56,588 INFO  [org.springframework.jms.listener.DefaultMessageListenerContainer] (DefaultMessageListenerContainer-2) JMS message listener invoker needs to establish shared Connection
  10:43:56,592 ERROR [org.springframework.jms.listener.DefaultMessageListenerContainer] (DefaultMessageListenerContainer-2) Could not refresh JMS Connection for destination 'shat' - retrying using FixedBackOff{interval=5000, currentAttempts=0, maxAttempts=unlimited}. Cause: Failed to create session factory; nested exception is java.lang.IllegalStateException: Server locator is closed (maybe it was garbage collected)
  10:44:01,592 ERROR [org.springframework.jms.listener.DefaultMessageListenerContainer] (DefaultMessageListenerContainer-2) Could not refresh JMS Connection for destination 'shat' - retrying using FixedBackOff{interval=5000, currentAttempts=1, maxAttempts=unlimited}. Cause: Failed to create session factory; nested exception is java.lang.IllegalStateException: Server locator is closed (maybe it was garbage collected)
  10:44:06,593 ERROR [org.springframework.jms.listener.DefaultMessageListenerContainer] (DefaultMessageListenerContainer-2) Could not refresh JMS Connection for destination 'shat' - retrying using FixedBackOff{interval=5000, currentAttempts=2, maxAttempts=unlimited}. Cause: Failed to create session factory; nested exception is java.lang.IllegalStateException: Server locator is closed (maybe it was garbage collected)
  </code></pre>
  
  <p>.....</p>
  
  <p>The error say: <strong>JMS message listener invoker needs to establish shared Connection</strong> Could not refresh JMS Connection for destination 'shat'... Server locator is closed (maybe it was garbage collected)</p>
  
  <p>Could be a Bug of ActiveMQ Artemis artemis 1.1.0.wildfly-017 ?</p>
  
  <p>Some has resolved some similar?</p>
  
  <p><strong>UPDATE:  2016-August-29 - 14:00 P.M.</strong></p>
  
  <p>If someone is interested... I found that if you use JndiObjectFactoryBean to  looks up a JNDI object, in this case the JMS connection factory, the before problem goes away. Normally I found some examples using JndiObjectFactoryBean but for get Database data sources, almost no example to obtain a JMS connection factory.</p>
  
  <p>Changing MessagingConfiguration class:</p>
  
  <pre><code>@Bean
  public ConnectionFactory connectionFactory() throws NamingException{
  
      ConnectionFactory connectionFactory = InitialContext.doLookup("java:/ConnectionFactory");
      return connectionFactory;
  }
  
  @Bean
  public ConnectionFactory connectionFactory() throws NamingException{
      return ConnectionFactory connectionFactory = InitialContext.doLookup("java:/ConnectionFactory");
  }
  </code></pre>
  
  <p>with:</p>
  
  <pre><code> @Bean
  public JndiObjectFactoryBean solicitudesConnectionFactory() {
      JndiObjectFactoryBean jndi = new JndiObjectFactoryBean();
      jndi.setJndiName("java:/ConnectionFactory");
      jndi.setLookupOnStartup(true);
      jndi.setProxyInterface(ConnectionFactory.class);
      return jndi;
  }
  
  public ConnectionFactory notificacionesConnectionFactory() {
      return  new  SingleConnectionFactory((ConnectionFactory)solicitudesConnectionFactory().getObject());
  }
  </code></pre>
  
  <p>the connection to Artemis work correctly too, incluse if I do redeploy of the aplication.  But the jndi.properties file is not used more, and the "Server locator is closed" problem disappears.</p>
  
  <p>I don't know if the JndiObjectFactoryBean if correct, but I will want continue using the jndi.properties file. How do it??</p>