From f92bee349503216d3e9531d182d75d479019950d Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Sun, 16 Dec 2007 18:34:33 +0000 Subject: [PATCH] Added odd-even routing demo --- .../integration/samples/oddeven/Counter.java | 44 +++++++++++++++++++ .../samples/oddeven/NumberLogger.java | 38 ++++++++++++++++ .../samples/oddeven/OddEvenDemo.java | 33 ++++++++++++++ .../samples/oddeven/oddEvenDemo.xml | 19 ++++++++ 4 files changed, 134 insertions(+) create mode 100644 spring-integration-core/src/main/java/org/springframework/integration/samples/oddeven/Counter.java create mode 100644 spring-integration-core/src/main/java/org/springframework/integration/samples/oddeven/NumberLogger.java create mode 100644 spring-integration-core/src/main/java/org/springframework/integration/samples/oddeven/OddEvenDemo.java create mode 100644 spring-integration-core/src/main/java/org/springframework/integration/samples/oddeven/oddEvenDemo.xml diff --git a/spring-integration-core/src/main/java/org/springframework/integration/samples/oddeven/Counter.java b/spring-integration-core/src/main/java/org/springframework/integration/samples/oddeven/Counter.java new file mode 100644 index 0000000000..c710a8b7ed --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/samples/oddeven/Counter.java @@ -0,0 +1,44 @@ +/* + * Copyright 2002-2007 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.samples.oddeven; + +import org.springframework.integration.endpoint.annotation.MessageEndpoint; +import org.springframework.integration.endpoint.annotation.Polled; +import org.springframework.integration.handler.annotation.Router; + +/** + * @author Mark Fisher + */ +@MessageEndpoint +public class Counter { + + private volatile int count = 1; + + @Polled(period=3000) + public int getNumber() { + return count++; + } + + @Router + public String resolveChannel(int i) { + if (i % 2 == 0) { + return "even"; + } + return "odd"; + } + +} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/samples/oddeven/NumberLogger.java b/spring-integration-core/src/main/java/org/springframework/integration/samples/oddeven/NumberLogger.java new file mode 100644 index 0000000000..4475fe22f9 --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/samples/oddeven/NumberLogger.java @@ -0,0 +1,38 @@ +/* + * Copyright 2002-2007 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.samples.oddeven; + +import org.springframework.integration.endpoint.annotation.Subscriber; +import org.springframework.stereotype.Component; + +/** + * @author Mark Fisher + */ +@Component +public class NumberLogger { + + @Subscriber(channel="even") + public void even(int i) { + System.out.println("even: " + i); + } + + @Subscriber(channel="odd") + public void odd(int i) { + System.out.println("odd: " + i); + } + +} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/samples/oddeven/OddEvenDemo.java b/spring-integration-core/src/main/java/org/springframework/integration/samples/oddeven/OddEvenDemo.java new file mode 100644 index 0000000000..a490f5f71c --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/samples/oddeven/OddEvenDemo.java @@ -0,0 +1,33 @@ +/* + * Copyright 2002-2007 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.samples.oddeven; + +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @author Mark Fisher + */ +public class OddEvenDemo { + + public static void main(String[] args) throws Exception { + AbstractApplicationContext context = new ClassPathXmlApplicationContext("oddEvenDemo.xml", OddEvenDemo.class); + context.start(); + System.in.read(); + } + +} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/samples/oddeven/oddEvenDemo.xml b/spring-integration-core/src/main/java/org/springframework/integration/samples/oddeven/oddEvenDemo.xml new file mode 100644 index 0000000000..bd899a335d --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/samples/oddeven/oddEvenDemo.xml @@ -0,0 +1,19 @@ + + + + + + + + + +