INTSAMPLES-64 Poller Sample using custom Trigger
For reference: https://jira.springsource.org/browse/INTSAMPLES-64 Shows how the polling period can be changed at runtime using a custom trigger.
This commit is contained in:
36
intermediate/dynamic-poller/README.md
Normal file
36
intermediate/dynamic-poller/README.md
Normal file
@@ -0,0 +1,36 @@
|
||||
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.
|
||||
|
||||
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/):
|
||||
- mvn package exec:java
|
||||
|
||||
You should see output like the following:
|
||||
|
||||
INFO : org.springframework.integration.samples.poller.Main -
|
||||
==========================================================
|
||||
|
||||
Welcome to the Spring Integration Dynamic Poller Sample!
|
||||
|
||||
For more information please visit:
|
||||
http://www.springsource.org/spring-integration
|
||||
|
||||
==========================================================
|
||||
INFO : org.springframework.integration.samples.poller.Main -
|
||||
=========================================================
|
||||
|
||||
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
|
||||
|
||||
70
intermediate/dynamic-poller/pom.xml
Normal file
70
intermediate/dynamic-poller/pom.xml
Normal file
@@ -0,0 +1,70 @@
|
||||
<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/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.springframework.integration.samples</groupId>
|
||||
<artifactId>dynamic-poller</artifactId>
|
||||
<version>2.1.0.BUILD-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>dynamic-poller</name>
|
||||
<url>http://www.springsource.org/spring-integration</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<spring.integration.version>2.1.0.RELEASE</spring.integration.version>
|
||||
<log4j.version>1.2.16</log4j.version>
|
||||
<junit.version>4.10</junit.version>
|
||||
<java.main.class>org.springframework.integration.samples.poller.Main</java.main.class>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-core</artifactId>
|
||||
<version>${spring.integration.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<!-- test-scoped dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.3.2</version>
|
||||
<configuration>
|
||||
<source>1.5</source>
|
||||
<target>1.5</target>
|
||||
<compilerArgument>-Xlint:all</compilerArgument>
|
||||
<showWarnings>true</showWarnings>
|
||||
<showDeprecation>true</showDeprecation>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>1.2</version>
|
||||
<configuration>
|
||||
<mainClass>${java.main.class}</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>repo.springsource.org.milestone</id>
|
||||
<name>Spring Framework Maven Milestone Repository</name>
|
||||
<url>https://repo.springsource.org/milestone</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
</project>
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.poller;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.scheduling.Trigger;
|
||||
import org.springframework.scheduling.TriggerContext;
|
||||
import org.springframework.scheduling.support.PeriodicTrigger;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
|
||||
/**
|
||||
* This is a dynamically changeable {@link Trigger}. It is based on the
|
||||
* {@link PeriodicTrigger} implementations. However, the fields of this dynamic
|
||||
* trigger are not final and the properties can be inspected and set via
|
||||
* explicit getters and setters.
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
*
|
||||
*/
|
||||
public class DynamicPeriodicTrigger implements Trigger {
|
||||
|
||||
private volatile long period;
|
||||
|
||||
private volatile TimeUnit timeUnit;
|
||||
|
||||
private volatile long initialDelay = 0;
|
||||
|
||||
private volatile boolean fixedRate = false;
|
||||
|
||||
/**
|
||||
* Create a trigger with the given period in milliseconds.
|
||||
*/
|
||||
public DynamicPeriodicTrigger(long period) {
|
||||
this(period, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a trigger with the given period and time unit. The time unit will
|
||||
* apply not only to the period but also to any 'initialDelay' value, if
|
||||
* configured on this Trigger later via {@link #setInitialDelay(long)}.
|
||||
*/
|
||||
public DynamicPeriodicTrigger(long period, TimeUnit timeUnit) {
|
||||
Assert.isTrue(period >= 0, "period must not be negative");
|
||||
this.timeUnit = (timeUnit != null) ? timeUnit : TimeUnit.MILLISECONDS;
|
||||
this.period = this.timeUnit.toMillis(period);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the delay for the initial execution. It will be evaluated in
|
||||
* terms of this trigger's {@link TimeUnit}. If no time unit was explicitly
|
||||
* provided upon instantiation, the default is milliseconds.
|
||||
*/
|
||||
public void setInitialDelay(long initialDelay) {
|
||||
this.initialDelay = this.timeUnit.toMillis(initialDelay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify whether the periodic interval should be measured between the
|
||||
* scheduled start times rather than between actual completion times.
|
||||
* The latter, "fixed delay" behavior, is the default.
|
||||
*/
|
||||
public void setFixedRate(boolean fixedRate) {
|
||||
this.fixedRate = fixedRate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the time after which a task should run again.
|
||||
*/
|
||||
public Date nextExecutionTime(TriggerContext triggerContext) {
|
||||
if (triggerContext.lastScheduledExecutionTime() == null) {
|
||||
return new Date(System.currentTimeMillis() + this.initialDelay);
|
||||
}
|
||||
else if (this.fixedRate) {
|
||||
return new Date(triggerContext.lastScheduledExecutionTime().getTime() + this.period);
|
||||
}
|
||||
return new Date(triggerContext.lastCompletionTime().getTime() + this.period);
|
||||
}
|
||||
|
||||
public long getPeriod() {
|
||||
return period;
|
||||
}
|
||||
|
||||
public void setPeriod(long period) {
|
||||
this.period = period;
|
||||
}
|
||||
|
||||
public TimeUnit getTimeUnit() {
|
||||
return timeUnit;
|
||||
}
|
||||
|
||||
public void setTimeUnit(TimeUnit timeUnit) {
|
||||
this.timeUnit = timeUnit;
|
||||
}
|
||||
|
||||
public long getInitialDelay() {
|
||||
return initialDelay;
|
||||
}
|
||||
|
||||
public boolean isFixedRate() {
|
||||
return fixedRate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof DynamicPeriodicTrigger)) {
|
||||
return false;
|
||||
}
|
||||
DynamicPeriodicTrigger other = (DynamicPeriodicTrigger) obj;
|
||||
return this.fixedRate == other.fixedRate
|
||||
&& this.initialDelay == other.initialDelay
|
||||
&& this.period == other.period;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return (this.fixedRate ? 14 : 41) +
|
||||
(int) (38 * this.period) +
|
||||
(int) (43 * this.initialDelay);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.poller;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* Starts the Spring Context and will initialize the Spring Integration routes.
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @version 1.0
|
||||
*
|
||||
*/
|
||||
public final class Main {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(Main.class);
|
||||
|
||||
private Main() { }
|
||||
|
||||
/**
|
||||
* Load the Spring Integration Application Context
|
||||
*
|
||||
* @param args - command line arguments
|
||||
*/
|
||||
public static void main(final String... args) {
|
||||
|
||||
LOGGER.info("\n=========================================================="
|
||||
+ "\n "
|
||||
+ "\n Welcome to the Spring Integration Dynamic Poller Sample! "
|
||||
+ "\n "
|
||||
+ "\n For more information please visit: "
|
||||
+ "\n http://www.springsource.org/spring-integration "
|
||||
+ "\n "
|
||||
+ "\n==========================================================" );
|
||||
|
||||
final AbstractApplicationContext context =
|
||||
new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/*-context.xml");
|
||||
|
||||
context.registerShutdownHook();
|
||||
|
||||
final Scanner scanner = new Scanner(System.in);
|
||||
|
||||
final DynamicPeriodicTrigger trigger = context.getBean(DynamicPeriodicTrigger.class);
|
||||
|
||||
LOGGER.info("\n========================================================="
|
||||
+ "\n "
|
||||
+ "\n Please press 'q + Enter' to quit the application. "
|
||||
+ "\n "
|
||||
+ "\n=========================================================" );
|
||||
|
||||
System.out.print("Please enter a non-negative numeric value and press <enter>: ");
|
||||
|
||||
while (!scanner.hasNext("q")) {
|
||||
|
||||
int triggerPeriod = scanner.nextInt();
|
||||
|
||||
System.out.println(String.format("Setting trigger period to '%s' ms", triggerPeriod));
|
||||
|
||||
trigger.setPeriod(triggerPeriod);
|
||||
|
||||
System.out.print("Please enter a non-negative numeric value and press <enter>: ");
|
||||
}
|
||||
|
||||
LOGGER.info("Exiting application...bye.");
|
||||
|
||||
System.exit(0);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?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.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.1.xsd
|
||||
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"
|
||||
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:poller max-messages-per-poll="1" trigger="dynamicTrigger" />
|
||||
</int:inbound-channel-adapter>
|
||||
|
||||
<bean id="dynamicTrigger" class="org.springframework.integration.samples.poller.DynamicPeriodicTrigger">
|
||||
<constructor-arg name="period" value="5000"/>
|
||||
</bean>
|
||||
|
||||
<int:logging-channel-adapter id="logger" logger-name="org.springframework.integration.samples.poller"/>
|
||||
|
||||
<task:executor id="executor" queue-capacity="20" pool-size="5-20"/>
|
||||
</beans>
|
||||
28
intermediate/dynamic-poller/src/main/resources/log4j.xml
Normal file
28
intermediate/dynamic-poller/src/main/resources/log4j.xml
Normal file
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
|
||||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
|
||||
|
||||
<!-- Appenders -->
|
||||
<appender name="console" class="org.apache.log4j.ConsoleAppender">
|
||||
<param name="Target" value="System.out" />
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<!-- Loggers -->
|
||||
<logger name="org.springframework">
|
||||
<level value="warn" />
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.integration.samples">
|
||||
<level value="debug" />
|
||||
</logger>
|
||||
|
||||
<!-- Root Logger -->
|
||||
<root>
|
||||
<priority value="warn" />
|
||||
<appender-ref ref="console" />
|
||||
</root>
|
||||
|
||||
</log4j:configuration>
|
||||
Reference in New Issue
Block a user