INTSAMPLES-64 - Code Review Changes

* Better documentation in README.md
* Better error handling
* Using a better data-format
This commit is contained in:
Gunnar Hillert
2012-02-15 12:17:52 -05:00
parent edd3f90299
commit 3e7d121dd4
4 changed files with 34 additions and 14 deletions

View File

@@ -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 <enter>: 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 <enter>:
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

View File

@@ -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;
}

View File

@@ -66,16 +66,29 @@ public final class Main {
+ "\n=========================================================" );
System.out.print("Please enter a non-negative numeric value and press <enter>: ");
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 <enter>: ");
}
LOGGER.info("Exiting application...bye.");

View File

@@ -7,7 +7,7 @@
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:task="http://www.springframework.org/schema/task">
<int:inbound-channel-adapter expression="T(java.lang.System).currentTimeMillis()" channel="logger">
<int:inbound-channel-adapter expression="new java.text.SimpleDateFormat('yyyy-MM-dd HH:mm:ss.SSS').format(new java.util.Date())" channel="logger">
<int:poller max-messages-per-poll="1" trigger="dynamicTrigger" />
</int:inbound-channel-adapter>