如何使用JNDI和Spring连接到WildFly 10.1.0.Final ActiveMQ Artemis?

时间:2016-08-27 04:23:28

标签: spring jms jndi spring-jms wildfly-10

我有WildFly 10.1.0.Final,这有Artemis 1.1.0 什么是配置jndi.properties的正确方法?:

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

使用WildFly 9.0.0.Final的这个工作包有HornetQ:

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

我正在使用Spring 4.3.2,Spring-jms并在may Maven POM.xml中包含了wildfly-jms-client-bom。 接下来是我的连接工厂。 Whith Wildfly 9我尝试使用InitialContext.doLookup(..)和这项工作。但是使用WildFly 10时,这不起作用。

我的主要配置文件是:

@Configuration
@ComponentScan(basePackages = "org")
@Import({
    MessagingConfiguration.class,
    MessagingListenerConfiguration.class

    })
public class AppConfig {

    //Put Other Application configuration here.


}

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;
}
*/

}

我有下一个:

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;
    }
}

最后我有了这个听众。

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";

    /*
    *   <jms-queue name="shatTopic" entries="java:/jms/queue/shat java:/jboss/exported/jms/queue/shat"/>
    */
    private static final String ORDER_QUEUE = "shat";

    public MessageReceiver() {
        System.out.println(">>>>>>>>>>>>>>>>>>> OK");
    }

    @Autowired
    OrderService orderService;


    @JmsListener(destination = ORDER_QUEUE)
    public void receiveMessage(final Message<String> message) {

        MessageHeaders headers =  message.getHeaders();

        System.out.println("MessageReceiver::receiveMessage(product)   Application : headers received : "+headers);

        //orderService.processOrder(product);


    }

}

我收到此错误:

20:04:42,971 WARN [org.springframework.jms.listener.DefaultMessageListenerContainer](DefaultMessageListenerContainer-124)JMS消息监听器调用程序的设置失败,目的地'shat' - 尝试恢复。 原因:没有名称为shat的队列

20:04:42,971 INFO [org.springframework.jms.listener.DefaultMessageListenerContainer](DefaultMessageListenerContainer-124)成功刷新JMS连接

在MessageReceiver类中声明的 shat 队列。 我的standalone-full.xml是下一个:

<subsystem xmlns="urn:jboss:domain:messaging-activemq:1.0">
            <server name="default">
                <security-setting name="#">
                    <role name="guest" send="true" consume="true" create-non-durable-queue="true" delete-non-durable-queue="true"/>
                </security-setting>
                <security-setting name="jms.topic.chat">
                    <role name="guest2" send="true" consume="true" create-non-durable-queue="true"/>
                </security-setting>
                <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"/>
                <http-connector name="http-connector" socket-binding="http" endpoint="http-acceptor"/>
                <http-connector name="http-connector-throughput" socket-binding="http" endpoint="http-acceptor-throughput">
                    <param name="batch-delay" value="50"/>
                </http-connector>
                <in-vm-connector name="in-vm" server-id="0"/>
                <http-acceptor name="http-acceptor" http-listener="default"/>
                <http-acceptor name="http-acceptor-throughput" http-listener="default">
                    <param name="batch-delay" value="50"/>
                    <param name="direct-deliver" value="false"/>
                </http-acceptor>
                <remote-acceptor name="websocket-stomp" socket-binding="netty-ws"/>
                <in-vm-acceptor name="in-vm" server-id="0"/>
                <jms-queue name="ExpiryQueue" entries="java:/jms/queue/ExpiryQueue"/>
                <jms-queue name="DLQ" entries="java:/jms/queue/DLQ"/>
                <jms-queue name="shatTopic" entries="java:/jms/queue/shat java:/jboss/exported/jms/queue/shat"/>
                <connection-factory name="InVmConnectionFactory" entries="java:/ConnectionFactory" connectors="in-vm"/>
                <connection-factory name="RemoteConnectionFactory" entries="java:jboss/exported/jms/RemoteConnectionFactory" connectors="http-connector"/>
                <pooled-connection-factory name="activemq-ra" entries="java:/JmsXA java:jboss/DefaultJMSConnectionFactory" connectors="in-vm" transaction="xa"/>
            </server>
        </subsystem>

我已经定义了正确定义的队列:

有关如何声明队列的参考资料可在此处参考:http://www.mastertheboss.com/jboss-server/jboss-jms/jboss-jms-configuration

为什么我无法连接?我不明白。我实际上尝试了许多方法,不工作。

更新:2016年8月29日 - 10:00 A.M

我发现如果我用实际部署的应用程序重新启动所有wildfly,加载应用程序后,如果我连接客户端(我有一个Web客户端使用Websocket直接生成消息到“jms.queue.shat”队列通过STOMP与stomp.js)并发送消息,应用程序(java)正确消耗消息。但是如果我在应用程序正常工作后重新部署应用程序,这就会产生错误。 接下来是错误:

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)

.....

错误说: JMS消息侦听器调用者需要建立共享连接无法刷新目标“shat”的JMS连接...服务器定位器已关闭(可能是垃圾回收)

可能是ActiveMQ Artemis artemis 1.1.0.wildfly-017的错误?

有些已经解决了一些类似的问题吗?

更新时间:2016年8月29日 - 下午14:00

如果有人感兴趣...我发现如果你使用JndiObjectFactoryBean来查找JNDI对象,在这种情况下是JMS连接工厂,那么之前的问题就会消失。通常我发现了一些使用JndiObjectFactoryBean的示例,但是对于获取数据库数据源,几乎没有示例来获取JMS连接工厂。

更改MessagingConfiguration类:

@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");
}

使用:

 @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());
}

与Artemis的连接也正常工作,包括我是否重新部署了应用程序。但jndi.properties文件使用不多,“服务器定位器关闭”问题消失了。

我不知道JndiObjectFactoryBean是否正确,但我希望继续使用jndi.properties文件。怎么做?

1 个答案:

答案 0 :(得分:0)

尝试将ORDER_QUEUEshat更改为java:/jms/queue/shat