【问题标题】:How to receive messages from IBM MQ JMS using spring boot continuously?如何使用 Spring Boot 连续接收来自 IBM MQ JMS 的消息?
【发布时间】:2020-05-06 02:01:51
【问题描述】:

我有 Spring Boot 应用程序,它接收 JSON 请求并将其推送到 IBM MQ JMS 队列中。可以有 n 个 JSON 请求将被推送到队列中。我的目标是处理队列中的每个请求。如何使用spring boot收听队列并一一获取消息以进行处理?

【问题讨论】:

  • 我在使用 IBM Mq 创建 spring-boot 应用程序时遇到问题。请您帮帮我,因为我无法连接到 Websphere MQ。

标签: java spring-boot ibm-mq


【解决方案1】:

在 pom.xml 中添加 'mq-jms-spring-boot-starter' 依赖如下:

在 application.yaml 中添加以下属性:

  mq:
    queue-manager: queueManager
    conn-name: connName(port)
    channel: channelName
    ssl-cipher-suite: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 or whatever ur app supports
    use-i-b-m-cipher-mappings: true (if using topic otherwise skip this property)

在你的监听方法中添加@JmsListener下面的注解

@EnableJms
Public class Consumer{
   @JmsListener(destination = "QueueNameToListenTo")
    public void listener(Object message) {
       logger.info("message received {}",message);
       //do something
    }
}

【讨论】:

    【解决方案2】:

    您需要将监听器实现为:

    1:创建配置如下:

    @Bean
    public MQConnectionFactory mqConnectionFactory(){
      MQConnectionFactory connectionFactory = new MQConnectionFactory();
      connectionFactory.setHostName(); //mq host name
      connectionFactory.setPort(); // mq port
      connectionFactory.setQueueManager(); //mq queue manager
      connectionFactory.setChannel(); //mq channel name
      connectionFactory.setTransportType(1);
      connectionFactory.setSSLCipherSuite(); //tls cipher suite name
      return connectionFactory;
    }
    
    
    @Bean()
    public DefaultMessageListenerContainer myMessageEventContainer() {
      DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
      container.setAutoStartup(true);
      container.setConnectionFactory(mqConnectionFactory);
      container.setDestinationName(//queue name//);
      container.setMessageListener(new MyEventListener());
      return container;
    }
    

    2:实现消息监听:

    public class MyEventListener implements MessageListener {
    
      @Override
      public void onMessage(Message message) {
        try {
          if (message instanceof TextMessage) {
            TextMessage textMessage = (TextMessage) message;
            String stringMessage = textMessage.getText();
            //do something with your message from queue
          }
        } catch (JMSException e) {
          //catch error
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 2017-01-17
      • 2023-03-05
      • 2021-08-04
      • 2021-09-25
      相关资源
      最近更新 更多