From 3dd4eb8b480ebf733ff6fcbd07c767db7f77d08e Mon Sep 17 00:00:00 2001 From: Hendy Irawan Date: Tue, 5 Jan 2010 03:48:58 +0700 Subject: [PATCH] initial commit --- .gitignore | 5 +++ pom.xml | 42 +++++++++++++++++ .../com/soluvas/samples/eventfx/si/App.java | 9 ++++ .../samples/eventfx/si/AsyncListener.java | 8 ++++ .../soluvas/samples/eventfx/si/Display.java | 38 ++++++++++++++++ .../soluvas/samples/eventfx/si/Sensor.java | 45 +++++++++++++++++++ .../samples/eventfx/si/SensorEvent.java | 20 +++++++++ .../samples/eventfx/si/SensorSimulator.java | 43 ++++++++++++++++++ .../samples/eventfx/si/SyncListener.java | 8 ++++ .../resources/META-INF/spring/eventfx-si.xml | 38 ++++++++++++++++ .../soluvas/samples/eventfx/si/AppTest.java | 38 ++++++++++++++++ 11 files changed, 294 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/soluvas/samples/eventfx/si/App.java create mode 100644 src/main/java/com/soluvas/samples/eventfx/si/AsyncListener.java create mode 100644 src/main/java/com/soluvas/samples/eventfx/si/Display.java create mode 100644 src/main/java/com/soluvas/samples/eventfx/si/Sensor.java create mode 100644 src/main/java/com/soluvas/samples/eventfx/si/SensorEvent.java create mode 100644 src/main/java/com/soluvas/samples/eventfx/si/SensorSimulator.java create mode 100644 src/main/java/com/soluvas/samples/eventfx/si/SyncListener.java create mode 100644 src/main/resources/META-INF/spring/eventfx-si.xml create mode 100644 src/test/java/com/soluvas/samples/eventfx/si/AppTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..48ba3ed --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +target +.classpath +.project +.settings +.springBeans diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..d80eeb9 --- /dev/null +++ b/pom.xml @@ -0,0 +1,42 @@ + + 4.0.0 + com.soluvas.samples + eventfx.si + jar + 0.0.1-SNAPSHOT + eventfx.si + http://maven.apache.org + + + junit + junit + 4.7 + test + + + org.springframework.integration + spring-integration-core + 2.0.0.M2 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.6 + 1.6 + + + + + + + spring-milestone + Spring Portfolio Milestone Repository + http://s3.amazonaws.com/maven.springframework.org/milestone + + + diff --git a/src/main/java/com/soluvas/samples/eventfx/si/App.java b/src/main/java/com/soluvas/samples/eventfx/si/App.java new file mode 100644 index 0000000..d0e087f --- /dev/null +++ b/src/main/java/com/soluvas/samples/eventfx/si/App.java @@ -0,0 +1,9 @@ +package com.soluvas.samples.eventfx.si; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class App { + public static void main(String[] args) { + new ClassPathXmlApplicationContext("META-INF/spring/*.xml"); + } +} diff --git a/src/main/java/com/soluvas/samples/eventfx/si/AsyncListener.java b/src/main/java/com/soluvas/samples/eventfx/si/AsyncListener.java new file mode 100644 index 0000000..761af5b --- /dev/null +++ b/src/main/java/com/soluvas/samples/eventfx/si/AsyncListener.java @@ -0,0 +1,8 @@ +package com.soluvas.samples.eventfx.si; + +import java.util.EventListener; + +public interface AsyncListener extends EventListener { + + void notify(E event); +} diff --git a/src/main/java/com/soluvas/samples/eventfx/si/Display.java b/src/main/java/com/soluvas/samples/eventfx/si/Display.java new file mode 100644 index 0000000..e239c31 --- /dev/null +++ b/src/main/java/com/soluvas/samples/eventfx/si/Display.java @@ -0,0 +1,38 @@ +package com.soluvas.samples.eventfx.si; + +import javax.annotation.PostConstruct; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +public class Display implements SyncListener, AsyncListener { + + private static Log log = LogFactory.getLog(Display.class); + private String name; + + @PostConstruct + public void initialize() { + log.info("Display " + getName() + " created."); + } + + @Override + public String update(SensorEvent event) { + log.info("[" + name + "] is updated: '" + event.getText() + "'"); + return getName() + " received " + event.getText(); + } + + @Override + public void notify(SensorEvent event) { + log.info("[" + name + "] is notified: '" + event.getText() + "'"); + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/src/main/java/com/soluvas/samples/eventfx/si/Sensor.java b/src/main/java/com/soluvas/samples/eventfx/si/Sensor.java new file mode 100644 index 0000000..5bef8f1 --- /dev/null +++ b/src/main/java/com/soluvas/samples/eventfx/si/Sensor.java @@ -0,0 +1,45 @@ +package com.soluvas.samples.eventfx.si; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +public class Sensor { + + private static Log log = LogFactory.getLog(Sensor.class); + + private SyncListener sensorSyncListener; + private AsyncListener sensorAsyncListener; + + private void fireSensor(SensorEvent event) { + if (sensorSyncListener != null) { + String result = sensorSyncListener.update(event); + log.info("Response: " + result); + } + if (sensorAsyncListener != null) { + sensorAsyncListener.notify(event); + } + } + + public void updateText(String text) { + log.info("updateText: " + text); + fireSensor(new SensorEvent(text)); + } + + public SyncListener getSensorSyncListener() { + return sensorSyncListener; + } + + public void setSensorSyncListener(SyncListener sensorSyncListener) { + this.sensorSyncListener = sensorSyncListener; + } + + public AsyncListener getSensorAsyncListener() { + return sensorAsyncListener; + } + + public void setSensorAsyncListener( + AsyncListener sensorAsyncListener) { + this.sensorAsyncListener = sensorAsyncListener; + } + +} diff --git a/src/main/java/com/soluvas/samples/eventfx/si/SensorEvent.java b/src/main/java/com/soluvas/samples/eventfx/si/SensorEvent.java new file mode 100644 index 0000000..2a01e50 --- /dev/null +++ b/src/main/java/com/soluvas/samples/eventfx/si/SensorEvent.java @@ -0,0 +1,20 @@ +package com.soluvas.samples.eventfx.si; + +public class SensorEvent { + + private String text; + + public SensorEvent(String text) { + super(); + this.text = text; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + +} diff --git a/src/main/java/com/soluvas/samples/eventfx/si/SensorSimulator.java b/src/main/java/com/soluvas/samples/eventfx/si/SensorSimulator.java new file mode 100644 index 0000000..2ec8323 --- /dev/null +++ b/src/main/java/com/soluvas/samples/eventfx/si/SensorSimulator.java @@ -0,0 +1,43 @@ +package com.soluvas.samples.eventfx.si; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class SensorSimulator { + + private static Log log = LogFactory.getLog(SensorSimulator.class); + + @Autowired + private Sensor sensor; + private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(); + + @PostConstruct + public void initialize() { + scheduledExecutor.scheduleAtFixedRate(new Runnable() { + + public void run() { + sensor.updateText("Something happens at " + SimpleDateFormat.getTimeInstance().format(new Date())); + } + }, 2000, 2000, TimeUnit.MILLISECONDS); + log.info("Sensor simulator initialized."); + } + + @PreDestroy + public void destroy() throws InterruptedException { + log.info("Destroying Sensor simulator..."); + scheduledExecutor.shutdown(); + scheduledExecutor.awaitTermination(2000, TimeUnit.MILLISECONDS); + } +} diff --git a/src/main/java/com/soluvas/samples/eventfx/si/SyncListener.java b/src/main/java/com/soluvas/samples/eventfx/si/SyncListener.java new file mode 100644 index 0000000..b46aa10 --- /dev/null +++ b/src/main/java/com/soluvas/samples/eventfx/si/SyncListener.java @@ -0,0 +1,8 @@ +package com.soluvas.samples.eventfx.si; + +import java.util.EventListener; + +public interface SyncListener extends EventListener { + + R update(E event); +} diff --git a/src/main/resources/META-INF/spring/eventfx-si.xml b/src/main/resources/META-INF/spring/eventfx-si.xml new file mode 100644 index 0000000..d0cb44c --- /dev/null +++ b/src/main/resources/META-INF/spring/eventfx-si.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/java/com/soluvas/samples/eventfx/si/AppTest.java b/src/test/java/com/soluvas/samples/eventfx/si/AppTest.java new file mode 100644 index 0000000..b4a345a --- /dev/null +++ b/src/test/java/com/soluvas/samples/eventfx/si/AppTest.java @@ -0,0 +1,38 @@ +package com.soluvas.samples.eventfx.si; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +}