什么是Gateway



Gateway的作用就像是一个门面(门面模式),使得用户不需要直接跟Spring Integration的api交互,只需要跟自己定义的接口做交互就行了。这一点很重要,例如用户的业务代码中不需要耦合进spring integration框架的代码。


我们在上一篇Spring Integration --- Message Channel我们在main方法里面,通过手动获取Channel,然后新建Message来往channel发送消息,这样的话我们的业务代码就耦合上了Spring Integraion框架了

public class HelloWorldExample {

  public static void main(String args[]) {
	String cfg = "siia/helloworld/channel/context.xml";
    ApplicationContext context = new ClassPathXmlApplicationContext(cfg);
    MessageChannel channel = context.getBean("names", MessageChannel.class);
    Message<String> message = MessageBuilder.withPayload("World").build();
    channel.send(message);
  }
}


下面我们利用Gateway来隐藏Spring Integration Api的直接调用,使得业务代码感知不到Spring Integration框架的存在

我们在Spring Integration --- Message Channel配置文件里面添加一个gateway元素,如下

<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
-->
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://www.springframework.org/schema/integration"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
			http://www.springframework.org/schema/beans/spring-beans.xsd
			http://www.springframework.org/schema/integration
			http://www.springframework.org/schema/integration/spring-integration.xsd">

  <gateway id="helloGateway"
           service-interface="siia.helloworld.gateway.HelloService"
           default-request-channel="names"/>

  <channel id="names"/>

  <service-activator input-channel="names" ref="helloService"
                     method="sayHello"/>

  <beans:bean id="helloService"
              class="siia.helloworld.gateway.MyHelloService"/>

</beans:beans>

gateway有一个service-interface的属性,这个属性指向一个interface,这样,当用户调用该接口的方法的时候,spring integration就会自动地帮我们新建一个message,并提交到default-request -channel指定地channel上。于是我们可以把main方法改为

public class HelloWorldExample {

  public static void main(String args[]) {
	String cfg = "siia/helloworld/gateway/context.xml";
    ApplicationContext context = new ClassPathXmlApplicationContext(cfg);
    HelloService helloService =
      context.getBean("helloGateway", HelloService.class);
    System.out.println(helloService.sayHello("World"));
  }

}

从代码里面我们已经看不到对spring integration api地依赖了,gateway很好地把细节给隐藏起来。这里有一点要说的是,当我们用一个接口声明一个gateway时,spring integration会自动帮我们生成该接口的代理类,这样当我们往gateway发送消息时,spring integration会通过代理类把消息转发到default-request-channel中去

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐