从spring官方网站下载Spring Integration的例子代码,里面包含了很多个分好类的例子
下载地址:https://github.com/SpringSource/spring-integration-samples
打开basic文件夹,里面有一个http的例子
我们可以在eclipse里进行调试,由于这些例子都是用maven构建的,最好先给eclipse安装maven插件,以及maven的wtp插件(用于WEB开发)
安装方式:eclipse-->help-->eclipemarketplace 搜索maven
安装前两个就行了
之后导入http这个例子的代码,eclipse-->import-->existing maven projects,
选择http文件夹(包含pom.xml的文件夹都可以导入)即可导入此项目,maven会自动去下载所需的依赖包,目录结构如下图:
首先,从目录可见,这是一个WEB项目,可以部署到WEB容器中,我们先看服务器端的代码
分析一下web.xml文件,
web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>multipart-http</display-name> <servlet> <servlet-name>Multipart</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/servlet-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Multipart</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>其中配置了一个servlet,名字叫Multipart,配置的类为DispatcherServlet,用过SPRING MVC的都比较熟悉这个配置,它就是SPRING表示层的控制器。
其contextConfigLocation配置的是/WEB-INF/servlet-config.xml
我们再看一下servlet-config.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" 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 http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd" xmlns:int="http://www.springframework.org/schema/integration" xmlns:int-file="http://www.springframework.org/schema/integration/file" xmlns:int-http="http://www.springframework.org/schema/integration/http" xmlns:int-mail="http://www.springframework.org/schema/integration/mail"> <int-http:inbound-gateway request-channel="receiveChannel" name="/receiveGateway" supported-methods="POST,GET"/> <int:channel id="receiveChannel"/> <int:service-activator input-channel="receiveChannel" expression="payload + ' from the other side'"/> </beans>
服务器端就是这么简单的内容,用spring的eclipse插件打开这个文件,可以看到图示:
我们可以将服务器端运行起来,选择pom右键-->run as-->maven build
在goal输入jetty:run,确定即可。
输出信息如下:
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< maven-jetty-plugin:6.1.10:run (default-cli) @ http <<<
[WARNING] The artifact org.apache.commons:commons-io:jar:1.3.2 has been relocated to commons-io:commons-io:jar:1.3.2
[INFO]
[INFO] --- maven-jetty-plugin:6.1.10:run (default-cli) @ http ---
[INFO] Configuring Jetty for project: Samples (Basic) - HTTP Demo
[INFO] Webapp source directory = /media/g_windows/ubuntudata/code/SpringSource-spring-integration-samples-84aefe6/basic/http/src/main/webapp
[INFO] web.xml file = /media/g_windows/ubuntudata/code/SpringSource-spring-integration-samples-84aefe6/basic/http/src/main/webapp/WEB-INF/web.xml
[INFO] Classes = /media/g_windows/ubuntudata/code/SpringSource-spring-integration-samples-84aefe6/basic/http/target/classes
2012-10-25 15:15:37.729::INFO: Logging to STDERR via org.mortbay.log.StdErrLog
[INFO] Context path = /http
[INFO] Tmp directory = determined at runtime
[INFO] Web defaults = org/mortbay/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
[INFO] Webapp directory = /media/g_windows/ubuntudata/code/SpringSource-spring-integration-samples-84aefe6/basic/http/src/main/webapp
[INFO] Starting jetty 6.1.10 ...
2012-10-25 15:15:37.823::INFO: jetty-6.1.10
2012-10-25 15:15:37.201::INFO: No Transaction manager found - if your webapp requires one, please configure one.
2012-10-25 15:15:38.179:/http:INFO: Initializing Spring FrameworkServlet 'Multipart'
15:15:38.803 INFO [main][org.springframework.web.servlet.DispatcherServlet] FrameworkServlet 'Multipart': initialization started
15:15:38.862 INFO [main][org.springframework.web.context.support.XmlWebApplicationContext] Refreshing WebApplicationContext for namespace 'Multipart-servlet': startup date [Thu Oct 25 15:15:38 CST 2012]; root of context hierarchy
15:15:38.927 INFO [main][org.springframework.beans.factory.xml.XmlBeanDefinitionReader] Loading XML bean definitions from ServletContext resource [/WEB-INF/servlet-config.xml]
15:15:39.712 INFO [main][org.springframework.integration.config.xml.DefaultConfiguringBeanFactoryPostProcessor] No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.
15:15:39.712 INFO [main][org.springframework.integration.config.xml.DefaultConfiguringBeanFactoryPostProcessor] No bean named 'taskScheduler' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created.
15:15:39.734 INFO [main][org.springframework.beans.factory.support.DefaultListableBeanFactory] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@66b4c2e4: defining beans [org.springframework.integration.internalDefaultConfiguringBeanFactoryPostProcessor,/receiveGateway,receiveChannel,org.springframework.integration.config.ServiceActivatorFactoryBean#0,org.springframework.integration.config.ConsumerEndpointFactoryBean#0,nullChannel,errorChannel,_org.springframework.integration.errorLogger,taskScheduler,org.springframework.integration.config.IdGeneratorConfigurer#0]; root of factory hierarchy
15:15:40.455 INFO [main][org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler] Initializing ExecutorService 'taskScheduler'
15:15:40.461 INFO [main][org.springframework.context.support.DefaultLifecycleProcessor] Starting beans in phase -2147483648
15:15:40.462 INFO [main][org.springframework.integration.endpoint.EventDrivenConsumer] Adding {service-activator} as a subscriber to the 'receiveChannel' channel
15:15:40.462 INFO [main][org.springframework.integration.channel.DirectChannel] Channel 'receiveChannel' has 1 subscriber(s).
15:15:40.462 INFO [main][org.springframework.integration.endpoint.EventDrivenConsumer] started org.springframework.integration.config.ConsumerEndpointFactoryBean#0
15:15:40.462 INFO [main][org.springframework.integration.endpoint.EventDrivenConsumer] Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
15:15:40.462 INFO [main][org.springframework.integration.channel.PublishSubscribeChannel] Channel 'errorChannel' has 1 subscriber(s).
15:15:40.462 INFO [main][org.springframework.integration.endpoint.EventDrivenConsumer] started _org.springframework.integration.errorLogger
15:15:40.462 INFO [main][org.springframework.context.support.DefaultLifecycleProcessor] Starting beans in phase 0
15:15:40.463 INFO [main][org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway] started /receiveGateway
15:15:40.500 INFO [main][org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping] Mapped URL path [/receiveGateway] onto handler '/receiveGateway'
15:15:40.766 INFO [main][org.springframework.web.servlet.DispatcherServlet] FrameworkServlet 'Multipart': initialization completed in 1962 ms
2012-10-25 15:15:40.169::INFO: Started [email protected]:8080[INFO] Started Jetty Server
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/integration/http-outbound-config.xml"); RequestGateway requestGateway = context.getBean("requestGateway", RequestGateway.class); String reply = requestGateway.echo("Hello"); logger.info("Replied with: " + reply); }其中一个main函数,里面创建一个spring 的context,使用配置文件为META-INF/spring/integration/http-outbound-config.xml
package org.springframework.integration.samples.http; /** * @author Oleg Zhurakousky * */ public interface RequestGateway { public String echo(String request); }
<int:gateway id="requestGateway" service-interface="org.springframework.integration.samples.http.RequestGateway" default-request-channel="requestChannel"/> <int:channel id="requestChannel"/> <int-http:outbound-gateway request-channel="requestChannel" url="http://localhost:8080/http/receiveGateway" http-method="POST" expected-response-type="java.lang.String"/>
可以从输出信息中查看到结果。
15:17:07.013 INFO [main][org.springframework.context.support.ClassPathXmlApplicationContext] Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@25a268f2: startup date [Thu Oct 25 15:17:06 CST 2012]; root of context hierarchy
15:17:07.097 INFO [main][org.springframework.beans.factory.xml.XmlBeanDefinitionReader] Loading XML bean definitions from class path resource [META-INF/spring/integration/http-outbound-config.xml]
15:17:07.700 INFO [main][org.springframework.integration.config.xml.DefaultConfiguringBeanFactoryPostProcessor] No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.
15:17:07.700 INFO [main][org.springframework.integration.config.xml.DefaultConfiguringBeanFactoryPostProcessor] No bean named 'taskScheduler' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created.
15:17:07.715 INFO [main][org.springframework.beans.factory.support.DefaultListableBeanFactory] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7fee6f88: defining beans [org.springframework.integration.internalDefaultConfiguringBeanFactoryPostProcessor,requestGateway,requestChannel,org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler#0,org.springframework.integration.config.ConsumerEndpointFactoryBean#0,nullChannel,errorChannel,_org.springframework.integration.errorLogger,taskScheduler,org.springframework.integration.config.IdGeneratorConfigurer#0]; root of factory hierarchy
15:17:07.934 INFO [main][org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler] Initializing ExecutorService 'taskScheduler'
15:17:07.988 INFO [main][org.springframework.integration.gateway.GatewayProxyFactoryBean$MethodInvocationGateway] started requestGateway
15:17:07.988 INFO [main][org.springframework.integration.gateway.GatewayProxyFactoryBean] started requestGateway
15:17:08.419 INFO [main][org.springframework.context.support.DefaultLifecycleProcessor] Starting beans in phase -2147483648
15:17:08.419 INFO [main][org.springframework.integration.endpoint.EventDrivenConsumer] Adding {message-handler} as a subscriber to the 'requestChannel' channel
15:17:08.419 INFO [main][org.springframework.integration.channel.DirectChannel] Channel 'requestChannel' has 1 subscriber(s).
15:17:08.420 INFO [main][org.springframework.integration.endpoint.EventDrivenConsumer] started org.springframework.integration.config.ConsumerEndpointFactoryBean#0
15:17:08.420 INFO [main][org.springframework.integration.endpoint.EventDrivenConsumer] Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
15:17:08.420 INFO [main][org.springframework.integration.channel.PublishSubscribeChannel] Channel 'errorChannel' has 1 subscriber(s).
15:17:08.420 INFO [main][org.springframework.integration.endpoint.EventDrivenConsumer] started _org.springframework.integration.errorLogger
15:17:08.420 INFO [main][org.springframework.context.support.DefaultLifecycleProcessor] Starting beans in phase 0
15:17:08.799 INFO [main][org.springframework.integration.samples.http.HttpClientDemo] Replied with: Hello from the other side