INT-1419 - adding sample of Gateway with Futures
This commit is contained in:
10
spring-integration-samples/gateway-with-futures/.classpath
Normal file
10
spring-integration-samples/gateway-with-futures/.classpath
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
|
||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
||||
29
spring-integration-samples/gateway-with-futures/.project
Normal file
29
spring-integration-samples/gateway-with-futures/.project
Normal file
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>gateway-with-futures</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.maven.ide.eclipse.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.springframework.ide.eclipse.core.springbuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.springframework.ide.eclipse.core.springnature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.maven.ide.eclipse.maven2Nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,9 @@
|
||||
#Fri Sep 03 00:46:56 EDT 2010
|
||||
activeProfiles=
|
||||
eclipse.preferences.version=1
|
||||
fullBuildGoals=process-test-resources
|
||||
includeModules=false
|
||||
resolveWorkspaceProjects=false
|
||||
resourceFilterGoals=process-resources resources\:testResources
|
||||
skipCompilerPlugin=true
|
||||
version=1
|
||||
19
spring-integration-samples/gateway-with-futures/pom.xml
Normal file
19
spring-integration-samples/gateway-with-futures/pom.xml
Normal file
@@ -0,0 +1,19 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.integration.samples</groupId>
|
||||
<artifactId>spring-integration-samples</artifactId>
|
||||
<version>2.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>gateway-with-futures</artifactId>
|
||||
<name>Gateway with Futures Demo</name>
|
||||
<packaging>jar</packaging>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-core</artifactId>
|
||||
<version>2.0.0.BUILD-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
*/
|
||||
package org.springframework.integration.gateway.futures;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.FileSystemXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class DemoClient {
|
||||
|
||||
private static ExecutorService executor = Executors.newFixedThreadPool(100);
|
||||
private static int timeout = 20;
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext ac = new FileSystemXmlApplicationContext("src/main/resources/META-INF/spring/integration/*.xml");
|
||||
MathServiceGateway mathService = ac.getBean("mathService", MathServiceGateway.class);
|
||||
Map<Integer, Future<Integer>> results = new HashMap<Integer, Future<Integer>>();
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
int number = random.nextInt(200);
|
||||
Future<Integer> result = mathService.multiplyByTwo(number);
|
||||
results.put(number, result);
|
||||
}
|
||||
for (final Map.Entry<Integer, Future<Integer>> resultEntry : results.entrySet()) {
|
||||
executor.execute(new Runnable() {
|
||||
public void run() {
|
||||
int[] result = processFuture(resultEntry);
|
||||
|
||||
if (result[1] == -1){
|
||||
System.out.println("Multiplying " + result[0] + " should be easy. You should be able to multiply any number < 100 by 2 in your head");
|
||||
} else if (result[1] == -2){
|
||||
System.out.println("Multiplication of " + result[0] + " by 2 is can not be accomplished in " + timeout + " seconds");
|
||||
} else {
|
||||
System.out.println("Result of multiplication of " + result[0] + " by 2 is " + result[1]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static int[] processFuture(Map.Entry<Integer, Future<Integer>> resultEntry){
|
||||
int originalNumber = resultEntry.getKey();
|
||||
Future<Integer> result = resultEntry.getValue();
|
||||
try {
|
||||
int finalResult = result.get(timeout, TimeUnit.SECONDS);
|
||||
return new int[]{originalNumber, finalResult};
|
||||
} catch (ExecutionException e) {
|
||||
return new int[]{originalNumber, -1};
|
||||
} catch (TimeoutException tex){
|
||||
return new int[]{originalNumber, -2};
|
||||
} catch (Exception ex){
|
||||
System.out.println();
|
||||
// ignore
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
*/
|
||||
package org.springframework.integration.gateway.futures;
|
||||
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageHandlingException;
|
||||
import org.springframework.integration.mapping.InboundMessageMapper;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class ExceptionMapper implements InboundMessageMapper<MessageHandlingException>{
|
||||
|
||||
@Override
|
||||
public Message<?> toMessage(MessageHandlingException messageHandlingException)
|
||||
throws Exception {
|
||||
return messageHandlingException.getFailedMessage();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
*/
|
||||
package org.springframework.integration.gateway.futures;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class MathService {
|
||||
private final Random random = new Random();
|
||||
|
||||
public int multiplyByTwo(int i) throws Exception{
|
||||
long sleep = random.nextInt(10) * 1000;
|
||||
Thread.sleep(sleep);
|
||||
//System.out.println("Multiplied " + i);
|
||||
return i*2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
*/
|
||||
package org.springframework.integration.gateway.futures;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public interface MathServiceGateway {
|
||||
|
||||
Future<Integer> multiplyByTwo(int i);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?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-3.0.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
|
||||
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:task="http://www.springframework.org/schema/task">
|
||||
|
||||
<int:gateway id="mathService"
|
||||
service-interface="org.springframework.integration.gateway.futures.MathServiceGateway"
|
||||
default-request-channel="requestChannel"/>
|
||||
|
||||
<int:channel id="requestChannel">
|
||||
<int:queue/>
|
||||
</int:channel>
|
||||
|
||||
<int:filter input-channel="requestChannel" output-channel="calculatingChannel" expression="payload > 100" throw-exception-on-rejection="true">
|
||||
<int:poller fixed-rate="100" max-messages-per-poll="10" task-executor="executor"/>
|
||||
</int:filter>
|
||||
|
||||
<int:channel id="calculatingChannel" >
|
||||
<int:dispatcher task-executor="executor"/>
|
||||
</int:channel>
|
||||
|
||||
<int:service-activator input-channel="calculatingChannel">
|
||||
<bean class="org.springframework.integration.gateway.futures.MathService"/>
|
||||
</int:service-activator>
|
||||
|
||||
<task:executor id="executor" pool-size="10"/>
|
||||
|
||||
<context:mbean-export/>
|
||||
</beans>
|
||||
Reference in New Issue
Block a user