From 3e7d121dd4bcf65736bd7bbe0e593cc2e0eccdd8 Mon Sep 17 00:00:00 2001 From: Gunnar Hillert Date: Wed, 15 Feb 2012 12:17:52 -0500 Subject: [PATCH] INTSAMPLES-64 - Code Review Changes * Better documentation in README.md * Better error handling * Using a better data-format --- intermediate/dynamic-poller/README.md | 18 +++++++------ .../poller/DynamicPeriodicTrigger.java | 3 +++ .../integration/samples/poller/Main.java | 25 ++++++++++++++----- .../spring-integration-context.xml | 2 +- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/intermediate/dynamic-poller/README.md b/intermediate/dynamic-poller/README.md index e5dfe951..99921203 100644 --- a/intermediate/dynamic-poller/README.md +++ b/intermediate/dynamic-poller/README.md @@ -3,9 +3,13 @@ Dynamic Poller Sample By default this application will (poll) print out the current system time every 5 seconds. From the command line you can enter a non-negative numeric value to change the polling period (in milliseconds) at runtime. -Under the cover an **Inbound Channel Adapter** polls for the current system time. The **Poller** which is used by the **Inbound Channel Adapter** is configured with a custom trigger. The resulting message contains as payload the time in milliseconds and the message is sent to a **Logging Channel Adapter**, which will print the time to the command prompt. +Under the cover an **Inbound Channel Adapter** polls for the current system time. The **Poller**, which is used by the **Inbound Channel Adapter**, is configured with a custom Trigger (*org.springframework.integration.samples.poller.DynamicPeriodicTrigger*). The resulting message contains as payload the time in milliseconds and the message is sent to a **Logging Channel Adapter**, which will print the time to the command prompt. -You can run the application by either +When changing the polling period, the change to the trigger will occur after the NEXT poll at the current rate. Therefore, if the current polling period is 60 seconds and you change it to 1 second, it can take up to 60 seconds to take effect, depending on when in the polling cycle you make the change. + +### Running the Application + +You can run the application by either: * running the "Main" class from within STS (Right-click on Main class --> Run As --> Java Application) * or from the command line using the [Exec Maven Plugin](http://mojo.codehaus.org/exec-maven-plugin/): @@ -28,9 +32,9 @@ You should see output like the following: Please press 'q + Enter' to quit the application. ========================================================= - Please enter a non-negative numeric value and press : INFO : org.springframework.integration.samples.poller - 1329257519165 - INFO : org.springframework.integration.samples.poller - 1329257524207 - INFO : org.springframework.integration.samples.poller - 1329257529208 - INFO : org.springframework.integration.samples.poller - 1329257534209 - INFO : org.springframework.integration.samples.poller - 1329257539210 + Please enter a non-negative numeric value and press : + INFO : org.springframework.integration.samples.poller - 2012-02-15 12:14:37.503 + INFO : org.springframework.integration.samples.poller - 2012-02-15 12:14:42.504 + INFO : org.springframework.integration.samples.poller - 2012-02-15 12:14:47.505 + diff --git a/intermediate/dynamic-poller/src/main/java/org/springframework/integration/samples/poller/DynamicPeriodicTrigger.java b/intermediate/dynamic-poller/src/main/java/org/springframework/integration/samples/poller/DynamicPeriodicTrigger.java index b0ad47d1..c3954779 100644 --- a/intermediate/dynamic-poller/src/main/java/org/springframework/integration/samples/poller/DynamicPeriodicTrigger.java +++ b/intermediate/dynamic-poller/src/main/java/org/springframework/integration/samples/poller/DynamicPeriodicTrigger.java @@ -67,6 +67,7 @@ public class DynamicPeriodicTrigger implements Trigger { * provided upon instantiation, the default is milliseconds. */ public void setInitialDelay(long initialDelay) { + Assert.isTrue(initialDelay >= 0, "initialDelay must not be negative"); this.initialDelay = this.timeUnit.toMillis(initialDelay); } @@ -97,6 +98,7 @@ public class DynamicPeriodicTrigger implements Trigger { } public void setPeriod(long period) { + Assert.isTrue(period >= 0, "period must not be negative"); this.period = period; } @@ -105,6 +107,7 @@ public class DynamicPeriodicTrigger implements Trigger { } public void setTimeUnit(TimeUnit timeUnit) { + Assert.notNull(timeUnit, "timeUnit must not be null"); this.timeUnit = timeUnit; } diff --git a/intermediate/dynamic-poller/src/main/java/org/springframework/integration/samples/poller/Main.java b/intermediate/dynamic-poller/src/main/java/org/springframework/integration/samples/poller/Main.java index d4f5dd90..187980b7 100644 --- a/intermediate/dynamic-poller/src/main/java/org/springframework/integration/samples/poller/Main.java +++ b/intermediate/dynamic-poller/src/main/java/org/springframework/integration/samples/poller/Main.java @@ -66,16 +66,29 @@ public final class Main { + "\n=========================================================" ); System.out.print("Please enter a non-negative numeric value and press : "); - - while (!scanner.hasNext("q")) { + + while (true) { - int triggerPeriod = scanner.nextInt(); + final String input = scanner.nextLine(); - System.out.println(String.format("Setting trigger period to '%s' ms", triggerPeriod)); + if("q".equals(input.trim())) { + break; + } - trigger.setPeriod(triggerPeriod); - + try { + + int triggerPeriod = Integer.valueOf(input); + + System.out.println(String.format("Setting trigger period to '%s' ms", triggerPeriod)); + + trigger.setPeriod(triggerPeriod); + + } catch (Exception e) { + LOGGER.error("An exception was caught: " + e); + } + System.out.print("Please enter a non-negative numeric value and press : "); + } LOGGER.info("Exiting application...bye."); diff --git a/intermediate/dynamic-poller/src/main/resources/META-INF/spring/integration/spring-integration-context.xml b/intermediate/dynamic-poller/src/main/resources/META-INF/spring/integration/spring-integration-context.xml index 15431c3b..9fd09af6 100644 --- a/intermediate/dynamic-poller/src/main/resources/META-INF/spring/integration/spring-integration-context.xml +++ b/intermediate/dynamic-poller/src/main/resources/META-INF/spring/integration/spring-integration-context.xml @@ -7,7 +7,7 @@ xmlns:int="http://www.springframework.org/schema/integration" xmlns:task="http://www.springframework.org/schema/task"> - +