使用Spring Boot连接到远程JMS队列 [英] Connect to remote jms queue with Spring Boot

查看:71
本文介绍了使用Spring Boot连接到远程JMS队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to connect a remote JBOSS, JBOSS EAP 7, JMS queue with spring boot. Below my code, I have only a configuration class and consumer class. I see through logs that I'm connecting to localhost, but... WHY???

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

    }
}

监听器类:

@Configuration
public class jmsListener {

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

日志:

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 时,我得到以下信息:

Without ?broker.persistent=false&broker.useJmx=false on the JNDI URL I obtain this:

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

推荐答案

您的 connectionFactory()必须是 @Bean ,以便将其作为参数注入您的容器工厂工厂方法.

Your connectionFactory() needs to be a @Bean in order to inject it as an argument into your container factory factory method.

public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory) throws NamingException {

由于它不是 @Bean ,因此您将获得启动的默认连接工厂(大概在类路径上具有ActiveMQ).

Since it's not a @Bean you are getting boot's default connection factory (presumably you have ActiveMQ on the class path).

这篇关于使用Spring Boot连接到远程JMS队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆