Last active
August 29, 2015 14:03
-
-
Save lolson/8e3d6e2c16570989f790 to your computer and use it in GitHub Desktop.
Heartbeat Thread EJB
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
@Singleton | |
@Startup | |
@Remote(HeartbeatEvent.class) | |
public class HeartbeatEventEJB implements HeartbeatEvent { | |
private final static Logger logger = LoggerFactory.getLogger( HeartbeatEventEJB.class); | |
private Thread t; | |
private Worker w; | |
@Override | |
public void startHB() { | |
w = new Worker(); | |
t = new Thread(w); | |
w.setRunning(true); | |
t.start(); | |
logger.info("Starting heartbeat thread"); | |
} | |
@Override | |
public void stopHB() { | |
w.setRunning(false); | |
t.interrupt(); | |
logger.info("Stopping heartbeat thread"); | |
} | |
private class Worker implements Runnable { | |
private boolean running = false; | |
@Override | |
public void run() { | |
while (isRunning()) { | |
try { | |
logger.info("Sending heartbeat event ..."); | |
Thread.sleep(5000); | |
} catch (InterruptedException ex) { | |
logger.info("Heartbeat event interrupted ..."); | |
logger.error(ex.getMessage()); | |
} | |
} | |
} | |
public boolean isRunning() { | |
return running; | |
} | |
public void setRunning(boolean runState) { | |
this.running = runState; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment