我正在构建一个概念证明,展示了使用Spring Integration构建的基于XML的RESTful应用程序的端到端流程,总体思路如下:
1)HTTP入站网关,接受来自主叫客户端的传入请求
2)一组@ Router,@ ServiceActivator,@ Splitter& @Aggregator工件处理传入的请求并发送一组可能的响应(响应XSD在父元素内部有可选元素,根据用例填充)回到调用客户端
我使用Chrome的高级REST客户端扩展测试了本地Tomcat 7.0.68实例上的流程,并且它第一次运行正常,这就是它的结束。
此后的每个测试都以完全相同的方式失败 - 在聚合器中最终调用@CorrelationStrategy方法之后,流程仅以此消息结束,“在超时内没有收到回复”。
文档(http://docs.spring.io/spring-integration/docs/4.3.8.RELEASE/reference/htmlsingle/#http-timeout)表示对于HTTP入站网关,超时属性都默认为1000毫秒。我实际上尝试手动将reply-timeout设置为50000,但它不起作用。
另外,http://docs.spring.io/spring-integration/docs/4.3.8.RELEASE/reference/htmlsingle/#aggregator-config说, “将回复消息发送到输出通道或丢弃通道时等待的超时间隔。默认为-1 - 无限期阻塞”
由于Spring Integration依赖于某些默认超时似乎失败了,有人可以告诉我是否还有其他事情可以尝试?
以下是我项目中最相关的代码:
上下文文件: product-integration.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:int="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:int-stream="http://www.springframework.org/schema/integration/stream"
xmlns:int-http="http://www.springframework.org/schema/integration/http"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/stream
http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd
http://www.springframework.org/schema/integration/http
http://www.springframework.org/schema/integration/http/spring-integration-http.xsd ">
<int:annotation-config/>
<context:component-scan base-package="com.amb.splitteraggregator"/>
<!-- POST -->
<int-http:inbound-gateway request-channel="gatewayProductRequests"
reply-channel="gatewayProductResponses"
supported-methods="POST"
path="/product"
request-payload-type="java.lang.Stringe>
<int-http:request-mapping consumes="application/xml" produces="application/xml"/>
</int-http:inbound-gateway>
<int:channel id="gatewayProductResponses" />
<int:channel id="validatedProductRequests" />
<int:channel id="upstreamProductResponses" />
<int:channel id="downStreamProductRequests" />
<int:channel id="downStreamRawProductsAndRequest" />
<int:channel id="downStreamProductRequestBatches" />
<int:channel id="compositeMessagesWithResponse" />
<int:channel id="compositeMessagesForAggregation" />
<int:channel id="compositeMessagesWithRawProductsAndRequestOrResponse" />
聚合器: ProductAggregator.java
@MessageEndpoint
public class ProductAggregator {
final static Logger logger = Logger.getLogger(ProductAggregator.class);
@Autowired
ProductServiceHelper productServiceHelper;
@Aggregator(inputChannel="compositeMessagesForAggregation", outputChannel="upstreamProductResponses", sendTimeout="10000")
public Message<Composite> generateAggregatedResponse(List<Message<Composite>> listOfCompositeMessagesForAggregation) {
logger.info("generateAggregatedResponse :: START");
PRODUCTRESPONSE productResponse = new PRODUCTRESPONSE();
PRODUCTRESPONSEDATA productResponseData = new PRODUCTRESPONSEDATA();
ERROR error = new ERROR();
PRODUCTRESPONSE productResponseInComposite = null;
List<PRODUCT> listOfProductsInComposite = null;
List<PRODUCT> listOfProductsToReturn = new ArrayList<PRODUCT>();
StringBuilder errorsToReturn = new StringBuilder();
String uuid = null;
for(Message<Composite> compositeMessage : listOfCompositeMessagesForAggregation) {
productResponseInComposite = compositeMessage.getPayload().getProductResponse();
if (null != productResponseInComposite.getPRODUCTRESPONSEDATA()) {
uuid = productResponseInComposite.getPRODUCTRESPONSEDATA().getUuid();
listOfProductsInComposite = productResponseInComposite.getPRODUCTRESPONSEDATA().getPRODUCT();
listOfProductsToReturn.addAll(listOfProductsInComposite);
} else if (null != productResponseInComposite.getERROR()) {
errorsToReturn.append(productResponseInComposite.getERROR().getErrorMsgCode());
errorsToReturn.append(",");
}
}
if (null != listOfProductsToReturn && !listOfProductsToReturn.isEmpty()) {
productResponseData.getPRODUCT().addAll(listOfProductsToReturn);
productResponseData.setUuid(uuid);
productResponse.setPRODUCTRESPONSEDATA(productResponseData);
}
if (errorsToReturn.length() != 0) {
error.setErrorMsgCode(errorsToReturn.toString());
error.setUuid(uuid);
productResponse.setERROR(error);
}
Composite compositeWithAggregatedResponse = productServiceHelper.buildComposite(false, productResponse);
Message<Composite> compositeMessageWithAggregatedResponse = MessageBuilder.withPayload(compositeWithAggregatedResponse).build();
logger.info("generateAggregatedResponse :: END");
return compositeMessageWithAggregatedResponse;
}
@CorrelationStrategy
public UUID correlateByUUID(Message<Composite> compositeMessageForAggregation) {
logger.info("correlateByUUID :: START");
logger.info("Correlation by UUID done");
logger.info("correlateByUUID :: END");
UUID correlationUUID = null;
if (null != compositeMessageForAggregation.getPayload().getProductResponse().getPRODUCTRESPONSEDATA()) {
correlationUUID = UUID.fromString(compositeMessageForAggregation.getPayload().getProductResponse().getPRODUCTRESPONSEDATA().getUuid());
} else if (null != compositeMessageForAggregation.getPayload().getProductResponse().getERROR()) {
correlationUUID = UUID.fromString(compositeMessageForAggregation.getPayload().getProductResponse().getERROR().getUuid());
}
return correlationUUID;
}
}
自定义复合对象在通道中移动: Composite.java
public class Composite {
private PRODUCTREQUEST productRequest;
private PRODUCTRESPONSE productResponse;
private List<RawProduct> listOfRawProducts;
private boolean isError;
public Composite(boolean isError, PRODUCTREQUEST productRequest) {
this.isError = isError;
this.productRequest = productRequest;
}
public Composite(boolean isError, PRODUCTRESPONSE productResponse) {
this.isError = isError;
this.productResponse = productResponse;
}
public Composite(boolean isError, List<RawProduct> listOfRawProducts) {
this.isError = isError;
this.listOfRawProducts = new ArrayList<RawProduct>(listOfRawProducts);
}
public Composite(boolean isError, PRODUCTREQUEST productRequest, List<RawProduct> listOfRawProducts) {
this.isError = isError;
this.productRequest = productRequest;
this.listOfRawProducts = new ArrayList<RawProduct>(listOfRawProducts);
}
public PRODUCTREQUEST getProductRequest() {
return productRequest;
}
public PRODUCTRESPONSE getProductResponse() {
return productResponse;
}
public boolean isError() {
return isError;
}
public List<RawProduct> getListOfRawProducts() {
return this.listOfRawProducts;
}
}
的pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>com.amb</groupId>
<artifactId>splitteraggregator</artifactId>
<name>SpringWebSplitterAggregator</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.7</java-version>
<org.springframework-version>4.3.7.RELEASE</org.springframework-version>
<spring.integration.version>4.3.8.RELEASE</spring.integration.version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
<log4j.version>1.2.17</log4j.version>
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- Spring Integration -->
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<!-- Spring Integration Stream -->
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-stream</artifactId>
<version>${spring.integration.version}</version>
<!-- <scope>compile</scope> -->
</dependency>
<!-- Spring Integration HTTP -->
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-http</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>compile</scope>
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
如果我的项目需要更多的代码片段,请告诉我 - 这是我在Stack Overflow上的第一个问题,所以请原谅我,如果有任何我错过的礼仪。
- 巴拉斯