【问题标题】:Connect to remote jms queue with Spring Boot使用 Spring Boot 连接到远程 jms 队列
【发布时间】:2020-09-13 20:27:53
【问题描述】:

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

@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&amp;broker.useJmx=false,我会得到这个:

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

【问题讨论】:

  • 为什么在 JNDI URL 上使用?broker.persistent=false&amp;broker.useJmx=false?我不认为 JBoss JNDI 实现支持这一点。
  • 测试完成,但我得到:MQ119031:无法验证用户
  • AMQ119031: Unable to validate user 表示您在尝试创建 JMS 连接时没有传递正确的凭据。需要明确的是,您在执行 JNDI 查找时正在设置凭据,但 JNDI 和 JMS 是 100% 相互独立的,因此您在创建 JMS 连接时也需要传递正确的凭据.
  • 谢谢!!!我添加了凭据属性,现在运行!谢谢
  • @Harry:您能否提供您的工作解决方案作为答案,以便其他人也可以从中受益?

标签: spring-boot jboss jms spring-jms


【解决方案1】:

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

public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory) throws NamingException {

由于它不是@Bean,因此您将获得引导的默认连接工厂(假设您在类路径上有 ActiveMQ)。

【讨论】:

  • &gt;ignored as the bean value is null - 这意味着 JNDI 查找失败。
猜你喜欢
  • 2019-05-19
  • 2022-01-07
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 2017-02-14
  • 1970-01-01
  • 1970-01-01
  • 2012-10-24
相关资源
最近更新 更多