how to maintain a little daemon
andeeh Oct 2, 2008 3:06 PMHi I'm looking for a way to create a daemon
so to speak that is able to access the perstistence architecture, processing some events synchronously within request and conversation scopes, and other events asynchronously as data arrives from an other connection.
erm... basically I am creating a trading platform for forex trading and there is a process that connects to and then monitors the connection to the Oanda Forex servers and processes and acts on ticks
ie changes in price of a given financial instrument.
Simultaneously we have users logging into the system via the web and changing various parameters and those are what you might call trading strategies
that will be persisted and then implemented by our daemon process.
I'm jumping in at the deep end here, I have spent plenty of time with desktop java apps using SWT/JFace and I have even used Hibernate on the desktop, but here I am choosing an application server and framework and I would appreciate any general pointers.
Here is a trivial something I made earlier to test a daemon type process. It seams (ho ho) to to work fine. Just displays the time in seconds (#{daemon.getMessage()}
) to my front end. I just created a class in the application scope and spawned a thread that counts the number of seconds. How would I annotate this so that the thread is run at server startup and stopped gracefully at shutdown? Maybe I would like to send an email at startup and shutdown too, stuff like that. Just looking for general pointers here. Is it bad karma to spawn daemon-like processes in the Application scope? I'm a J2EE noob, no experience with server side java, but I've chosen Seam on JBoss AS as it seams to be a very logical framework.
many thanks,
andy
import org.jboss.seam.ScopeType; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Scope; @Name("daemon") @Scope(ScopeType.APPLICATION) public class Daemon { private static boolean running; private static int secondsRunning; public Daemon() { if(!running) { running = true; Thread t = new Thread(){ @Override public void run() { while (running) { try { Thread.sleep(1000); } catch (InterruptedException e){} secondsRunning ++; } } }; t.start(); } } public void stopme() { running = false; } public String getMessage() { if(running) return "Been running for " + secondsRunning + " seconds"; else return "Stopped @ " + secondsRunning + " seconds"; } }