当前位置: 首页 > 知识库问答 >
问题:

如何使用JNDI和Spring连接到WildFly10.1.0.final ActiveMQ Artemis?

汪茂
2023-03-14

我有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
@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);


    }

}

在MessageReceiver类中声明的shat队列。My 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.castertheboss.com/jboss-server/jboss-jms/jboss-jms-configuration

为什么我联系不上??我不懂.其实我试了很多很多方法,都不奏效。

更新时间:2016年8月29日上午10:00

我发现,如果我用实际部署的应用程序重新启动所有wildfly,在加载应用程序之后,如果我连接一个客户机(我有一个Web客户机,它使用STOMP.js在STOMP上使用Websocket直接将消息生成到“jms.queue.shat”队列)并发送消息,应用程序(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 message listener invoker需要建立共享连接,无法刷新目标“shat”得JMS连接...服务器定位器已关闭(可能已被垃圾收集)

可能是ActiveMQ Artemis Artemis 1.1.0.Wildfly-017的Bug?

正在更改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文件没有被更多地使用,“服务器定位器关闭”的问题也就消失了。

共有1个答案

邴宏大
2023-03-14

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

 类似资料:
  • 我很难弄清楚如何实现Spring Boot JMS Listener,在JBoss应用程序服务器中监听ActiveMQ队列。因此,我选择发布一个问题并用我的最终解决方案回答它,希望它可以为您节省几个小时。

  • 所以我在WASv8中设置了几个MQs。5.我已经测试了这些功能,它们运行良好,现在我需要测试一些功能。为了做到这一点,我下载了“新”MQJExplorer,并注意到了从上下文加载JNDI的能力。既然Websphere实例在本地运行,那么我可以直接挂接到Websphere上下文中吗?我试着选择WebSphereApplicationServer,它给了我一个以 iiop:// 我试着用。。。 iio

  • 使用JNDI,我可以连接到ActiveMQ。但是当切换提供程序类时,它给了我以下异常。 Spring JNDI配置: 例外情况: null

  • 我无法连接到两个数据源使用在JDNI与Spring Boot。 生成以下stacktrace: 我做错了什么?

  • 请以我的英语为基础,我正在使用spring Boot2和注册3 jndi连接的tomcat,如下所示:

  • 一般来说,我对Spring和AmazonAWS都很陌生,所以如果我做错了什么,我会提前道歉。 当部署到Elastic Beanstalk上时,我似乎无法让我的Spring Boot应用程序以任何方式与AWS服务通信。我一直在遵循这里的指南,试图正确设置我的项目。 我本质上是在获取他们提供的代码并尝试从我的项目中运行: 超级,看起来够简单。我还设置了我的application.properties文