4 Replies Latest reply on Oct 7, 2008 7:12 PM by andeeh

    how to maintain a little daemon

    andeeh

      Hi 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";
           }
      }


        • 1. Re: how to maintain a little daemon
          kukeltje.ronald.jbpm.org

          Is it bad karma to spawn daemon-like processes in the Application scope?

          Yes




          I'm a J2EE noob, no experience with server side java

          We've all been that once



          but I've chosen Seam on JBoss AS as it seams to be a very logical framework.

          Good choice, but instead of building your own messaging framework, why not use JMS?

          • 2. Re: how to maintain a little daemon
            jguglielmin

            If you want to try ICEfaces, you get server-push along with it's ajax-enabled components.  Check out the sample  auctionMonitor or WebMC(has a server-push chat module) applications.  It's pretty simple to set up something like what you have described.

            • 3. Re: how to maintain a little daemon
              todd.nash

              The J2EE spec specifically addresses that user created daemon processes or threads should not be spawned in the container.


              I believe the latest J2EE spec has the notion of a Worker EJB that can be used for asynchronously processing and/or scheduled processing. Not sure whether or not JBoss 3.x implements this.


              I was involved in a project a while back where we created a JMS message bean that took messages sent via a web service call from a third party non-J2EE application. The JMS message bean processed the message asynchronously. JMS EJBs can call other EJBs.


              Most JMS servers allow you to specify parameters such as the number of JMS beans that can be pooled for a given queue or topic, etc. In a cluster, you can also specify a single server as the JMS server.


              For response activities you can have your JMS bean put a message on a different queue or topic.


              I would strongly suggest you look at using JMS for your asynchronously needs.

              • 4. Re: how to maintain a little daemon
                andeeh

                Thanks for the responses. The ICEFaces Auction Application looks interesting and I intend to have a look further.



                we created a JMS message bean that took messages sent via a web service call from a third party non-J2EE application

                I'm not sure whether JMS is appropriate if it is just beans reacting to external requests? Is it going possible for a JMS bean to actually initiate a connection and monitor ticks. i.e. a continuous process which monitors changes in the price of a currency pair and places trades. (this is tick by tick monitoring of price movements all via a third party proprietary API) The example you give still appears to be a server responding to externally generated requests, albeit with some co-ordination between different EJBs?


                (Sorry if my explanation is not clear but I'm not familiar with a lot of the terminology)


                I was looking around some more and found this on Resource Adapters. Could this be the way to go?


                Resource Adapters



                When the user deploys an RA, the application server instantiates a copy of the RA
                main class (the one implementing javax.resource.spi.ResourceAdapter) and
                then invokes its start method. The same process may also happen when an
                application server starts up with an already deployed RA. The start method is an
                opportunity for an RA to execute any initialization procedures and, in most cases, to
                establish communication with the remote EIS. Some RAs might even start the EIS
                service at this point. The RA can also get a BootstrapContext, which gives access
                to certain application server resources. For instance, the MailConnector-RA obtains
                the WorkManager object from this context and utilizes it to start a worker thread
                (called PollingThread) that will be used to monitor the email folders specified by
                the MDBs associated with the RA.

                The


                andy