Spring integration Spring集成-请求-应答实现

Spring integration Spring集成-请求-应答实现,spring-integration,ibm-mq,Spring Integration,Ibm Mq,我不熟悉Spring集成,也不熟悉堆栈溢出。我正在寻找一些帮助来理解Spring集成,因为它与请求-应答模式有关。通过在web上阅读,我认为我应该使用服务激活器来启用这种类型的用例 我使用JMS来促进基于XML的消息的发送和接收。我们的底层实现是IBM Websphere MQ 我还使用SpringBoot(版本1.3.6.RELEASE),并尝试使用纯基于注释的配置方法(如果可能的话)。我在网上搜索了一些例子,但到目前为止,我所能看到的任何东西都不能帮助我理解这一切是如何结合在一起的。Spri

我不熟悉Spring集成,也不熟悉堆栈溢出。我正在寻找一些帮助来理解Spring集成,因为它与请求-应答模式有关。通过在web上阅读,我认为我应该使用服务激活器来启用这种类型的用例

我使用JMS来促进基于XML的消息的发送和接收。我们的底层实现是IBM Websphere MQ

我还使用SpringBoot(版本1.3.6.RELEASE),并尝试使用纯基于注释的配置方法(如果可能的话)。我在网上搜索了一些例子,但到目前为止,我所能看到的任何东西都不能帮助我理解这一切是如何结合在一起的。Spring集成文档非常优秀,但我仍在努力解决如何将所有部分组合在一起的问题。如果我错过了什么,我会提前道歉。我把在这里发帖当作最后的选择

以下是我的配置:

package com.daluga.spring.integration.configuration

import com.ibm.mq.jms.MQConnectionFactory;
import com.ibm.mq.jms.MQQueue;
import com.ibm.msg.client.wmq.WMQConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;

//import com.ibm.msg.client.services.Trace;

@Configuration
public class MQConfiguration {

    private static final Logger LOGGER = LoggerFactory.getLogger(MQConfiguration.class);

    @Value("${host-name}")
    private String hostName;

    @Value("${port}")
    private int port;

    @Value("${channel}")
    private String channel;

    @Value("${time-to-live}")
    private int timeToLive;

    @Autowired
    @Qualifier("MQConnectionFactory")
    ConnectionFactory connectionFactory;

    @Bean(name = "jmsTemplate")
    public JmsTemplate provideJmsTemplate() {
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        jmsTemplate.setExplicitQosEnabled(true); 
        jmsTemplate.setTimeToLive(timeToLive);
        jmsTemplate.setDeliveryMode(DeliveryMode.NON_PERSISTENT);     
        return jmsTemplate;
    }

    @Bean(name = "MQConnectionFactory")
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory ccf  = new CachingConnectionFactory();

        //Trace.setOn();

        try {
            MQConnectionFactory mqcf = new MQConnectionFactory();
            mqcf.setHostName(hostName);
            mqcf.setPort(port);
            mqcf.setChannel(channel);
            mqcf.setTransportType(WMQConstants.WMQ_CM_CLIENT);
            ccf.setTargetConnectionFactory(mqcf);
            ccf.setSessionCacheSize(2);   
        } catch (JMSException e) {
            throw new RuntimeException(e);
        }

        return ccf;
    }

    @Bean(name = "requestQueue")
    public Destination createRequestQueue() {

        Destination queue = null;

        try {
            queue = new MQQueue("REQUEST.QUEUE");
        } catch (JMSException e) {
            throw new RuntimeException(e);
        }

        return queue;
    }

    @Bean(name = "replyQueue")
    public Destination createReplyQueue() {

        Destination queue = null;

        try {
            queue = new MQQueue("REPLY.QUEUE");
        } catch (JMSException e) {
            throw new RuntimeException(e);
        }

        return queue;
    }

    @Bean(name = "requestChannel")
    public QueueChannel createRequestChannel() {

        QueueChannel channel = new QueueChannel();

        return channel;
    }

    @Bean(name = "replyChannel")
    public QueueChannel createReplyChannel() {

        QueueChannel channel = new QueueChannel();

        return channel;
    }

}
这是我的服务课:

package com.daluga.spring.integration.service

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.stereotype.Service;


@Service
public class MyRequestReplyService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyRequestReplyService.class);

    @ServiceActivator(inputChannel = "replyChannel")
    public void sendAndReceive(String requestPayload) {
        // How to get replyPayload
    }

}
因此,在这一点上,我不太确定如何将所有这些粘合在一起,使其工作。我不知道如何将我的请求和回复队列粘合到ServiceActivator,以使这一切正常工作

我正在调用的服务(基于JMS/webheremq)使用典型的消息和相关id,这样我就可以将请求正确地绑定到相应的响应

有谁能给我提供一些指导,告诉我如何让它发挥作用?请让我知道我可以提供哪些其他信息来明确这一点

提前感谢您的帮助


Dan网关提供请求/应答语义

与其直接使用JmsTemplate,不如使用

如果您想自己启动,请更改service activator方法,返回回复类型,并使用模板
sendAndReceive()
convertSendAndReceive()
方法之一


使用XML配置,但应该提供一些额外的指导。

谢谢,Gary!这有所帮助。我退了一步,决定使用基于xml的配置。我配置了入站和出站网关,可以看到在我的回复队列上创建了消息使用者。启动过程中没有错误。我能够将消息放入回复队列(使用JMS实用程序),并看到它被拾取。但我还不太明白的是如何通过服务激活器启动对请求队列的调用。我创建了一个类,并使用MessageEndpoint对其进行注释,使用ServiceActivator对该类进行方法注释。然后我通过main方法调用它。将
requestChannel()
更改为
DirectChannel
,并直接或通过。我们通常建议使用后者,而不是直接与消息传递基础结构交互的用户代码。看看示例应用程序。Gary,谢谢你的指导!。我能把它全部连接起来并工作。一旦你了解了发生了什么,就很容易把其中一个连接起来。保重,丹。
@Bean
@ServiceActivator(inputChannel="requestChannel")
public MessageHandler jmsOutGateway() {
    JmsOutboundGateway outGateway = new JmsOutboundGateway();
    // set properties
    outGateway.setOutputChannel(replyChannel());
    return outGateway;
}