Created
December 7, 2013 11:49
-
-
Save horiga/7840222 to your computer and use it in GitHub Desktop.
Google guava EventBus example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EventMessage { | |
final int msgcode; | |
final String msg; | |
public EventMessage(int msgcode, String msg) { | |
this.msgcode = msgcode; | |
this.msg = msg; | |
} | |
public int getMsgcode() { | |
return msgcode; | |
} | |
public String getMsg() { | |
return msg; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.TimeUnit; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import com.google.common.eventbus.DeadEvent; | |
import com.google.common.eventbus.EventBus; | |
import com.google.common.eventbus.Subscribe; | |
public class Main { | |
private static Logger log = LoggerFactory.getLogger(Main.class); | |
public static class DeadEventsSubscriver { | |
@Subscribe | |
public void handleDeadEvent(DeadEvent deadEvent) { | |
log.error("DEAD EVENT: {}", deadEvent.getEvent()); | |
} | |
} | |
/** | |
* example EventBus programming 'Google-Guava' | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
try { | |
final EventBus eventBus = new EventBus("example-events"); | |
final Subscriber subscriver = new Subscriber(); | |
final DeadEventsSubscriver des = new DeadEventsSubscriver(); | |
eventBus.register(subscriver); | |
eventBus.register(des); | |
// published | |
eventBus.post("This is dead event"); | |
eventBus.post(new EventMessage(5000, "This message from hoge")); | |
eventBus.post(new EventMessage(1000, "This message from fuga")); | |
log.debug("waiting..."); | |
TimeUnit.MILLISECONDS.sleep(10000); | |
log.info(">> end of main thread"); | |
eventBus.unregister(subscriver); | |
eventBus.unregister(des); | |
subscriver.shutdown(); | |
} catch (InterruptedException e) { | |
log.error("fail", e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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.horiga.study</groupId> | |
<artifactId>eventbus-example</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>com.google.guava</groupId> | |
<artifactId>guava</artifactId> | |
<version>15.0</version> | |
<!-- <type>bundle</type> --> | |
</dependency> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-api</artifactId> | |
<version>1.7.5</version> | |
</dependency> | |
<dependency> | |
<groupId>ch.qos.logback</groupId> | |
<artifactId>logback-classic</artifactId> | |
<version>1.0.13</version> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.TimeUnit; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import com.google.common.eventbus.Subscribe; | |
public class Subscriber { | |
private static Logger log = LoggerFactory.getLogger(Subscriber.class); | |
protected static ExecutorService worker = Executors.newCachedThreadPool(); | |
public static class EventHandler implements Runnable { | |
final EventMessage message; | |
public EventHandler( EventMessage message) { | |
this.message = message; | |
} | |
public void run() { | |
try { | |
log.debug("[start] Processing worker thread. / {} {}", message.msgcode, message.msg); | |
TimeUnit.MILLISECONDS.sleep(message.msgcode); // do something | |
log.debug("[end] worker thread"); | |
} catch (Exception e) { | |
log.error("Event failed", e); | |
} | |
} | |
} | |
@Subscribe | |
public void handleEvent(final EventMessage eventMessage) { | |
try { | |
log.debug("[start] dispatched event message to worker thread. {}/{}", eventMessage.msgcode, eventMessage.msg); | |
worker.execute(new EventHandler(eventMessage)); | |
log.debug("[end] dispatched events."); | |
} catch (Exception e) { | |
log.error("event failed.", e); | |
} | |
} | |
public void shutdown() { | |
worker.shutdown(); | |
try { | |
if (!worker.awaitTermination(1000, TimeUnit.MILLISECONDS)) { | |
worker.shutdownNow(); | |
} | |
log.info("shutdown success"); | |
} catch (InterruptedException e) { | |
log.error("shutdown failed",e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment