From 0e5ddbdbf88ff30cea72fb412f3e4033ea1d7fed Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Fri, 3 Sep 2010 14:18:07 +0000 Subject: [PATCH] INT-1419 - adding sample of Gateway with Futures --- .../gateway-with-futures/.classpath | 10 +++ .../gateway-with-futures/.project | 29 +++++++ .../.settings/org.maven.ide.eclipse.prefs | 9 ++ .../gateway-with-futures/pom.xml | 19 +++++ .../gateway/futures/DemoClient.java | 85 +++++++++++++++++++ .../gateway/futures/ExceptionMapper.java | 34 ++++++++ .../gateway/futures/MathService.java | 33 +++++++ .../gateway/futures/MathServiceGateway.java | 27 ++++++ .../integration/math-service-config.xml | 35 ++++++++ 9 files changed, 281 insertions(+) create mode 100644 spring-integration-samples/gateway-with-futures/.classpath create mode 100644 spring-integration-samples/gateway-with-futures/.project create mode 100644 spring-integration-samples/gateway-with-futures/.settings/org.maven.ide.eclipse.prefs create mode 100644 spring-integration-samples/gateway-with-futures/pom.xml create mode 100644 spring-integration-samples/gateway-with-futures/src/main/java/org/springframework/integration/gateway/futures/DemoClient.java create mode 100644 spring-integration-samples/gateway-with-futures/src/main/java/org/springframework/integration/gateway/futures/ExceptionMapper.java create mode 100644 spring-integration-samples/gateway-with-futures/src/main/java/org/springframework/integration/gateway/futures/MathService.java create mode 100644 spring-integration-samples/gateway-with-futures/src/main/java/org/springframework/integration/gateway/futures/MathServiceGateway.java create mode 100644 spring-integration-samples/gateway-with-futures/src/main/resources/META-INF/spring/integration/math-service-config.xml diff --git a/spring-integration-samples/gateway-with-futures/.classpath b/spring-integration-samples/gateway-with-futures/.classpath new file mode 100644 index 0000000000..96489ff1c9 --- /dev/null +++ b/spring-integration-samples/gateway-with-futures/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/spring-integration-samples/gateway-with-futures/.project b/spring-integration-samples/gateway-with-futures/.project new file mode 100644 index 0000000000..2e76779d46 --- /dev/null +++ b/spring-integration-samples/gateway-with-futures/.project @@ -0,0 +1,29 @@ + + + gateway-with-futures + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.maven.ide.eclipse.maven2Builder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + + org.springframework.ide.eclipse.core.springnature + org.eclipse.jdt.core.javanature + org.maven.ide.eclipse.maven2Nature + + diff --git a/spring-integration-samples/gateway-with-futures/.settings/org.maven.ide.eclipse.prefs b/spring-integration-samples/gateway-with-futures/.settings/org.maven.ide.eclipse.prefs new file mode 100644 index 0000000000..8bbec67ffa --- /dev/null +++ b/spring-integration-samples/gateway-with-futures/.settings/org.maven.ide.eclipse.prefs @@ -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 diff --git a/spring-integration-samples/gateway-with-futures/pom.xml b/spring-integration-samples/gateway-with-futures/pom.xml new file mode 100644 index 0000000000..41a41c5a43 --- /dev/null +++ b/spring-integration-samples/gateway-with-futures/pom.xml @@ -0,0 +1,19 @@ + + 4.0.0 + + org.springframework.integration.samples + spring-integration-samples + 2.0.0.BUILD-SNAPSHOT + + gateway-with-futures + Gateway with Futures Demo + jar + + + org.springframework.integration + spring-integration-core + 2.0.0.BUILD-SNAPSHOT + + + \ No newline at end of file diff --git a/spring-integration-samples/gateway-with-futures/src/main/java/org/springframework/integration/gateway/futures/DemoClient.java b/spring-integration-samples/gateway-with-futures/src/main/java/org/springframework/integration/gateway/futures/DemoClient.java new file mode 100644 index 0000000000..343f8bf513 --- /dev/null +++ b/spring-integration-samples/gateway-with-futures/src/main/java/org/springframework/integration/gateway/futures/DemoClient.java @@ -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> results = new HashMap>(); + Random random = new Random(); + for (int i = 0; i < 100; i++) { + int number = random.nextInt(200); + Future result = mathService.multiplyByTwo(number); + results.put(number, result); + } + for (final Map.Entry> 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> resultEntry){ + int originalNumber = resultEntry.getKey(); + Future 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; + } +} diff --git a/spring-integration-samples/gateway-with-futures/src/main/java/org/springframework/integration/gateway/futures/ExceptionMapper.java b/spring-integration-samples/gateway-with-futures/src/main/java/org/springframework/integration/gateway/futures/ExceptionMapper.java new file mode 100644 index 0000000000..8a8ad03685 --- /dev/null +++ b/spring-integration-samples/gateway-with-futures/src/main/java/org/springframework/integration/gateway/futures/ExceptionMapper.java @@ -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{ + + @Override + public Message toMessage(MessageHandlingException messageHandlingException) + throws Exception { + return messageHandlingException.getFailedMessage(); + } + +} diff --git a/spring-integration-samples/gateway-with-futures/src/main/java/org/springframework/integration/gateway/futures/MathService.java b/spring-integration-samples/gateway-with-futures/src/main/java/org/springframework/integration/gateway/futures/MathService.java new file mode 100644 index 0000000000..639e5c77e2 --- /dev/null +++ b/spring-integration-samples/gateway-with-futures/src/main/java/org/springframework/integration/gateway/futures/MathService.java @@ -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; + } +} diff --git a/spring-integration-samples/gateway-with-futures/src/main/java/org/springframework/integration/gateway/futures/MathServiceGateway.java b/spring-integration-samples/gateway-with-futures/src/main/java/org/springframework/integration/gateway/futures/MathServiceGateway.java new file mode 100644 index 0000000000..12c1d8918e --- /dev/null +++ b/spring-integration-samples/gateway-with-futures/src/main/java/org/springframework/integration/gateway/futures/MathServiceGateway.java @@ -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 multiplyByTwo(int i); +} diff --git a/spring-integration-samples/gateway-with-futures/src/main/resources/META-INF/spring/integration/math-service-config.xml b/spring-integration-samples/gateway-with-futures/src/main/resources/META-INF/spring/integration/math-service-config.xml new file mode 100644 index 0000000000..9fb416fc80 --- /dev/null +++ b/spring-integration-samples/gateway-with-futures/src/main/resources/META-INF/spring/integration/math-service-config.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + +