Spring Boot + IBM MQ - Handle Correlation id

时间:2018-04-12 20:25:56

标签: java spring spring-boot ibm-mq spring-jms

我在java spring启动应用程序中从远程IBM MQ队列获取消息。我需要发回已处理的消息。如何设置发送回队列的响应的相关ID。

Mq Config

@Bean
public MQQueueConnectionFactory mqQueueConnectionFactory()  {

    MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
    mqQueueConnectionFactory.setHostName(host);
    try {
        mqQueueConnectionFactory.setTargetClientMatching(true);
        mqQueueConnectionFactory.setCCSID(1208);
        mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
        mqQueueConnectionFactory.setChannel(channel);
        mqQueueConnectionFactory.setPort(port);
        mqQueueConnectionFactory.setQueueManager(queueManager);


    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(e.getStackTrace().toString());
        logger.error("MqQueueConnectionFactory ", e);
    }
    return mqQueueConnectionFactory;
}

@Bean
UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter(
        MQQueueConnectionFactory mqQueueConnectionFactory) {
    UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
    userCredentialsConnectionFactoryAdapter.setUsername(username);
    userCredentialsConnectionFactoryAdapter.setPassword(password);
    userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(mqQueueConnectionFactory);
    return userCredentialsConnectionFactoryAdapter;
}

@Bean
@Primary
public CachingConnectionFactory cachingConnectionFactory(
        UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter) {
    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
    cachingConnectionFactory.setTargetConnectionFactory(userCredentialsConnectionFactoryAdapter);
    cachingConnectionFactory.setSessionCacheSize(500);
    cachingConnectionFactory.setReconnectOnException(true);
    return cachingConnectionFactory;
}

@Bean
public PlatformTransactionManager jmsTransactionManager(CachingConnectionFactory cachingConnectionFactory) {
    JmsTransactionManager jmsTransactionManager = new JmsTransactionManager();
    jmsTransactionManager.setConnectionFactory(cachingConnectionFactory);
    return jmsTransactionManager;
}

@Bean
public JmsOperations jmsOperations(CachingConnectionFactory cachingConnectionFactory) {
    JmsTemplate jmsTemplate = new JmsTemplate(cachingConnectionFactory);
    jmsTemplate.setReceiveTimeout(receiveTimeout);
    return jmsTemplate;
}

@Bean
public JmsListenerContainerFactory<?> myFactory(CachingConnectionFactory cachingConnectionFactory,
        DefaultJmsListenerContainerFactoryConfigurer configurer) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    configurer.configure(factory, cachingConnectionFactory);
    return factory;
}

@Bean
public MessageConverter jacksonJmsMessageConverter() {
    MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
    converter.setTargetType(MessageType.TEXT);
    converter.setTypeIdPropertyName("_type");
    converter.setObjectMapper(getObjectMapper());
    return converter;
}

我正在使用注释从队列中接收消息并发回。但我需要设置响应的相关ID =请求的消息ID。

@JmsListener(destination = "destination", containerFactory = "myFactory")
@SendTo("desination")
public void onMessage(Message message)

有任何建议吗?

0 个答案:

没有答案