简体   繁体   English

使用JNDI和Spring Boot连接JMS队列

[英]Connect JMS queue using JNDI with Spring Boot

I had a hard time figuring out how to implement a Spring Boot JMS Listener, listening to an ActiveMQ queue within a JBoss application server. 我很难弄清楚如何实现Spring Boot JMS侦听器,侦听JBoss应用程序服务器中的ActiveMQ队列。 Therefore I choose to post a question and answer it with my final solution, hoping it could save some of you a few hours. 因此,我选择发布一个问题并用我的最终解决方案回答该问题,希望它可以为您节省一些时间。

ActiveMQ is supported by Spring Boot autoconfiguration, but since it was inside the JBoss server Spring Boot was failing to connect ActiveMQ. Spring Boot自动配置支持ActiveMQ,但由于它位于JBoss服务器内部,因此Spring Boot无法连接ActiveMQ。 In fact you need to define connectionFactory and jmsListenerContainerFactory beans yourself by doing a lookup on the JNDI provider. 实际上,您需要通过在JNDI提供程序上进行查找来自己定义connectionFactoryjmsListenerContainerFactory bean。

@Configuration
@EnableJms
public class ActiveMqConnectionFactoryConfig {

  @Value("${broker.url}")
  String brokerUrl;

  @Value("${borker.username}")
  String userName;

  @Value("${borker.password}")
  String password;

  @Value("${queue}")
  String queueName;

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


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

  Properties getEnvProperties() {
    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
    env.put(Context.PROVIDER_URL, brokerUrl);
    env.put(Context.SECURITY_PRINCIPAL, userName);
    env.put(Context.SECURITY_CREDENTIALS, password);
    return env;
  }

  @Bean
  public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory) {

    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    JndiDestinationResolver jndiDestinationResolver = new JndiDestinationResolver();

    jndiDestinationResolver.setJndiEnvironment(getEnvProperties());
    factory.setDestinationResolver(jndiDestinationResolver);
    return factory;
  }

Then if you want to consume the queue you just define your JMS consumer class with a method annotated with @JmsListener(destination = "${queue}") 然后,如果要使用队列,只需使用带有@JmsListener(destination = "${queue}")注释的方法来定义JMS消费者类。

 @JmsListener(destination = "${queue}")
  public void receive(Message message) {
    System.out.println("Received Message: " + message);
  }

Hope that helps save a few hours of research ;) 希望可以帮助节省一些研究时间;)

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 spring 引导重新连接安慰 jms 队列 - spring boot reconnect solace jms queue Spring Boot,多个数据源和JNDI连接 - Spring Boot, Multiple DataSources and JNDI connect Spring Boot:使用 Java Bean 类从 application.properties 文件中读取 spring.jms.jndi-name 属性 - Spring Boot: Reading spring.jms.jndi-name property from application.properties file using a Java Bean class 使用Spring Jms问题异步发送到队列 - Asynchronous send to a queue using Spring Jms issue 更新:IBM MQ 系列上的 Spring 引导 JMS static 回复队列 - Update: Spring Boot JMS static reply queue on IBM MQ Series Spring Boot JMS应用程序的单元测试将消息留在队列中 - Unit Test of Spring Boot JMS Application Leaves Messages on Queue 使用JmsListenerConfigurer的Spring Boot持久JMS使用者 - Spring Boot Durable JMS Consumer using JmsListenerConfigurer 使用Spring Boot JMS发送对象 - Sending object using spring boot JMS Apache Camel JMS:如何使用 JNDI 连接发布或订阅使用 java DSL 的队列? - Apache Camel JMS : How to use JNDI connection to publish or Subscribe to a Queue using java DSL? 如何从WAS上的Java应用程序中将WAS JMS资源用于IIB MQ队列(使用JNDI) - How to use WAS JMS resourses for IIB MQ Queue from a Java Application on WAS (using JNDI)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM