使用来自其他应用程序的网关

时间:2015-05-05 00:40:48

标签: spring spring-boot spring-integration

我正在使用spring-boot,spring-integration和hornetq。

我有一个中央消息传递桥项目" app-bridge"以及从桥上请求信息的许多其他项目。所有项目都部署为" war"文件到tomcat服务器。

我需要从" app-1"创建同步请求到了#app; app-bridge"应用程序(" app-bridge"向远程应用程序发出MQ请求以获取响应,我不想公开它将数据提供给每个应用程序的方式。即仅#34; app-bridge"应该知道如何获得响应)。

在" app-bridge"我定义了以下网关。

<int:gateway service-interface="org.company.SendAndReceive"  
    default-request-channel="synchronousOutChannel" 
    default-reply-channel="synchronousInChannel"
    default-reply-timeout="30000"
    default-request-timeout="30000">
</int:gateway>

从&#34; app-bridge&#34;项目

@Autowired
private final SendAndReceive sendAndReceive;

...

@Scheduled(fixedDelay = 30000L)
public void testing() {
    sendAndReceive.send("HELLO");
    String resposne = sendAndReceive.receive();
    System.out.println(resposne); //prints the response or null if a timeout occurred
}

问题是我需要从&#34; app-1&#34;项目

我怎样才能做到这一点?

@Gary

的更新

在app-bridge项目中集成xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:int-file="http://www.springframework.org/schema/integration/file"
    xmlns:int-jms="http://www.springframework.org/schema/integration/jms"
    xmlns:int-http="http://www.springframework.org/schema/integration/http"
    xsi:schemaLocation="
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.1.xsd
        http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd
        http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">

    <!--************************* SENDING ********************************-->

    <!-- handle errors -->
    <int:channel id="as400SynchronousOutFailedChannel" />
    <int-jms:outbound-channel-adapter 
        id="as400SynchronousOutFailed" 
        destination-name="as400.synchronous.out.failed" 
        channel="as400SynchronousOutFailedChannel"
        connection-factory="jmsConnectionFactory"/>


    <!-- Read local messages from hornet -->
    <int:channel id="as400SynchronousOutChannel" />

    <int-jms:message-driven-channel-adapter
        id="jmsSynchronousAS400Out"
        acknowledge="transacted"
        destination-name="as400.synchronous.out"
        channel="as400SynchronousOutChannel"
        connection-factory="jmsConnectionFactory"
        error-channel="as400SynchronousOutFailedChannel"
        concurrent-consumers="1"
        pub-sub-domain="false" /> 

    <!-- Send messages to AS/400 -->
    <int-jms:outbound-channel-adapter
        id="jmsSynchronousOut"
        destination="as400SynchronousOutQueue"
        channel="as400SynchronousOutChannel"
        jms-template="as400JmsTemplate">
        <int-jms:request-handler-advice-chain>
            <int:retry-advice max-attempts="3">
                <int:exponential-back-off initial="2000" multiplier="2" />
            </int:retry-advice>
        </int-jms:request-handler-advice-chain>
    </int-jms:outbound-channel-adapter>

    <!-- Connection to remote AS/400 Queue -->
    <bean id="as400SynchronousOutQueue" class="com.ibm.mq.jms.MQQueue">
        <constructor-arg value="AS400.SYNCHRONOUS.IN" />
         <property name="targetClient">
            <bean id="com.ibm.mq.jms.JMSC.MQJMS_CLIENT_NONJMS_MQ" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>
        </property>
    </bean>

    <!-- Place to put messages that have failed -->
    <int-jms:outbound-channel-adapter 
        id="jmsAS400SynchronousOutFailed" 
        destination-name="as400.synchronous.out.failed" 
        channel="as400SynchronousOutFailedChannel"
        connection-factory="jmsConnectionFactory"/>


<!--************************* RECEIVING ********************************-->

    <!-- handle errors -->
    <int:channel id="as400SynchronousInFailedChannel" />
    <int-jms:outbound-channel-adapter 
        id="as400SynchronousInFailed" 
        destination-name="as400.synchronous.in.failed" 
        channel="as400SynchronousInFailedChannel"
        connection-factory="jmsConnectionFactory"/>

    <!-- Receive messages from AS/400 -->
    <int:channel id="as400SynchronousInChannel">
        <int:rendezvous-queue/>
    </int:channel>
    <int-jms:message-driven-channel-adapter
        id="jmsAS400SynchronousIn"
        acknowledge="transacted"
        destination="as400SynchronousInQueue"
        channel="as400SynchronousInChannel"
        connection-factory="as400ConnectionFactory"
        error-channel="as400SynchronousInFailedChannel"/> 

    <!-- Connection to remote AS/400 Queue -->
    <bean id="as400SynchronousInQueue" class="com.ibm.mq.jms.MQQueue">
        <constructor-arg value="AS400.SYNCHRONOUS.OUT" />
    </bean>

    <int:gateway service-interface="com.example.bridge.as400.As400SendAndReceive"  
        default-request-channel="as400SynchronousOutChannel" 
        default-reply-channel="as400SynchronousInChannel"
        default-reply-timeout="30000"
        default-request-timeout="30000">
    </int:gateway>

</beans>

com.example.bridge.as400.As400SendAndReceive java class。

public interface As400SendAndReceive {

    public void send(final String message);

    public String receive();

}

所以我希望我所有的其他战争应用程序(app-1,app-2,app-3)能够调用&#34; com.example.bridge.as400.As400SendAndReceive&#34;以某种方式在&#34; app-bridge&#34;中定义的网关战争。同样重要的是,如果同时说出&#34; app-1&#34;和&#34; app-2&#34;请求消息,它被发送回正确的请求者。 As400消息不支持HEADERS,因此它将作为普通MQSTR发送。

1 个答案:

答案 0 :(得分:1)

<int:gateway/>生成本地java API;你无法单独使用它来处理对远程系统的请求。

您的app-bridge应该有一个<int-jms:inbound-gateway/>来通过JMS处理请求。

其他应用会使用有线<int:gateway/><int-jms:outbound-gateway/>发送请求,以便将消息发送到app-1入站网关正在侦听的同一目的地。

修改

远程应用无法在app-bridge中“呼叫”网关;它是一个简单的java对象,只能在app-bridge中看到。

您需要在app-n和app-bridge之间进行某种外部通信。您可以选择您选择的技术,JMS,RMI,RabbitMQ,HTTP等等。

app-n中需要<int-x:outbound-gateway/>,app桥中需要<int-x:inbound-gateway/>

x是您选择用于通信的任何内容。探索the documentation以做出选择。鉴于您已经在使用JMS与AS / 400通信,可能JMS将是最佳选择(但您需要不同的队列)。