首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Spring Boot连接到远程jms队列

使用Spring Boot连接到远程jms队列
EN

Stack Overflow用户
提问于 2020-05-27 06:11:00
回答 1查看 1K关注 0票数 0

我正在尝试连接一个远程JBOSS,JBOSS EAP 7,JMS队列和spring boot。在我的代码下面,我只有一个配置类和消费者类。我通过日志看到我正在连接到本地主机,但是...为什么?

代码语言:javascript
运行
复制
@Configuration
@EnableJms
public class ActiveMqConnectionFactoryConfig {

    String queueName= "JMSTestQueueName";
    private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
    private static final String CONNECTION_FACTORY = "jms/RemoteConnectionFactory";

    public ConnectionFactory connectionFactory() {

        try {

            System.out.println("Retrieving JMS queue with JNDI name: " + CONNECTION_FACTORY);
            JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
            jndiObjectFactoryBean.setJndiName(CONNECTION_FACTORY);
            jndiObjectFactoryBean.setJndiEnvironment(getEnvProperties());       
            jndiObjectFactoryBean.afterPropertiesSet();

            return (QueueConnectionFactory) jndiObjectFactoryBean.getObject();

        } catch (NamingException e) {
            System.out.println("Error while retrieving JMS queue with JNDI name: [" + CONNECTION_FACTORY + "]");
        } catch (Exception ex) {
            System.out.println("Error error");
            ex.getStackTrace();
        }
        return null;
    }


    @Bean
    Properties getEnvProperties() throws NamingException {
        Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
        env.put(Context.PROVIDER_URL, "http-remoting://<REMOTE_ADDRESS>:8080?broker.persistent=false&broker.useJmx=false");
        env.put(Context.SECURITY_PRINCIPAL, <USER>);
        env.put(Context.SECURITY_CREDENTIALS, <PASSWORD>);
        namingContext = new InitialContext(env);
        return env;
    }

    @Bean
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory) throws NamingException {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setConcurrency("3-10");
        JndiDestinationResolver jndiDestinationResolver = new JndiDestinationResolver();
        jndiDestinationResolver.setJndiEnvironment(getEnvProperties());
        factory.setDestinationResolver(jndiDestinationResolver);
        return factory;

    }
}

监听程序类:

代码语言:javascript
运行
复制
@Configuration
public class jmsListener {

    @JmsListener(destination = "queue/JMSTestQueueName", containerFactory = "jmsListenerContainerFactory")
    public void receive(Message message) {
        System.out.println("Received Message: " + message);
    }   
}

日志:

代码语言:javascript
运行
复制
2020-05-27 00:21:58.571  INFO 10368 --- [           main] o.apache.activemq.broker.BrokerService   : Apache ActiveMQ 5.15.7 (localhost, ID:ITPC020248-60530-1590531718443-0:1) is starting
2020-05-27 00:21:58.576  INFO 10368 --- [           main] o.apache.activemq.broker.BrokerService   : Apache ActiveMQ 5.15.7 (localhost, ID:ITPC020248-60530-1590531718443-0:1) started 
2020-05-27 00:21:58.577  INFO 10368 --- [           main] o.apache.activemq.broker.BrokerService   : For help or more information please see: http://activemq.apache.org 
2020-05-27 00:21:58.617  INFO 10368 --- [           main] o.a.activemq.broker.TransportConnector   : Connector vm://localhost started 
2020-05-27 00:21:58.659  INFO 10368 --- [           main] jboss.ConsumerClass                      : Started ConsumerClass in 1.922 seconds (JVM running for 3.504)

如果JNDI URL上没有?broker.persistent=false&broker.useJmx=false,我会得到以下结果:

代码语言:javascript
运行
复制
2020-05-27 19:38:34.680  INFO 19752 --- [dpoint" task-13] org.jboss.ejb.client.remoting            : EJBCLIENT000016: Channel Channel ID 8f759d73 (outbound) of Remoting connection 336369ee to /172.16.68.80:8080 can no longer process messages
2020-05-27 19:38:37.479  INFO 19752 --- [           main] jboss.ConsumerClass                      : Started ConsumerClass in 7.194 seconds (JVM running for 9.26)
2020-05-27 19:38:42.772 ERROR 19752 --- [enerContainer-2] o.s.j.l.DefaultMessageListenerContainer  : Could not refresh JMS Connection for destination 'queue/JMSTestQueueName' - retrying using FixedBackOff{interval=5000, currentAttempts=0, maxAttempts=unlimited}. Cause: AMQ119031: Unable to validate user
2020-05-27 19:38:48.068 ERROR 19752 --- [enerContainer-2] o.s.j.l.DefaultMessageListenerContainer  : Could not refresh JMS Connection for destination 'queue/JMSTestQueueName' - retrying using FixedBackOff{interval=5000, currentAttempts=1, maxAttempts=unlimited}. Cause: AMQ119031: Unable to validate user
2020-05-27 19:38:53.401 ERROR 19752 --- [enerContainer-2] o.s.j.l.DefaultMessageListenerContainer  : Could not refresh JMS Connection for destination 'queue/JMSTestQueueName' - retrying using FixedBackOff{interval=5000, currentAttempts=2, maxAttempts=unlimited}. Cause: AMQ119031: Unable to validate user
2020-05-27 19:38:58.699 ERROR 19752 --- [enerContainer-2] o.s.j.l.DefaultMessageListenerContainer  : Could not refresh JMS Connection for destination 'queue/JMSTestQueueName' - retrying using FixedBackOff{interval=5000, currentAttempts=3, maxAttempts=unlimited}. Cause: AMQ119031: Unable to validate user
2020-05-27 19:39:04.006 ERROR 19752 --- [enerContainer-2] o.s.j.l.DefaultMessageListenerContainer  : Could not refresh JMS Connection for destination 'queue/JMSTestQueueName' - retrying using FixedBackOff{interval=5000, currentAttempts=4, maxAttempts=unlimited}. Cause: AMQ119031: Unable to validate user
2020-05-27 19:39:09.320 ERROR 19752 --- [enerContainer-2] o.s.j.l.DefaultMessageListenerContainer  : Could not refresh JMS Connection for destination 'queue/JMSTestQueueName' - retrying using FixedBackOff{interval=5000, currentAttempts=5, maxAttempts=unlimited}. Cause: AMQ119031: Unable to validate user
EN

回答 1

Stack Overflow用户

发布于 2020-05-27 07:39:32

您的connectionFactory()必须是一个@Bean,才能将其作为参数注入到容器工厂工厂方法中。

代码语言:javascript
运行
复制
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory) throws NamingException {

因为它不是一个@Bean,所以您将获得boot的默认连接工厂(假设您在类路径上有ActiveMQ )。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62032248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档