Here is my EJB2.1 conformal solution to the 'ejb fuction as daemon' problem.  Until availability of EJB2.1 timer services the basic functionality is emulated via JBoss scheduler service an some helper code. Later code migration to EJB2.1 based timer service should be quite simple:
 
Here is the concept:
 
Step 1:    Define EJB2.1 compatible interface TimedObject
 
Step 2:    Create a generic schedulable object that creates and calls a configurable session
               bean implementing TimedObject interface. The schedulable object is controlled by JBoss scheduler service
 
Step 3:    Implement your timed session bean that implements TimedObject interface.
               A Local/LocalHome EJB  interface is required for object creation.
               The schedulable object will invoke the ejbTimeout() method 
 
Step 4:    Configure a schedulable object MBean instance in your JBoss deployment
              directory that invokes your timed session bean. 
 
 
And here is the source code:
 
Step 1:
 
 
package de.mobilcom.eai.timerservice;
 
import java.util.Timer;
 

/**
 * This definition of'TimedObject' is a temporary workaround
 * until availability of EJB2.1 specification compatible
 * application server with standard javax.ejb.TimedObject
 * definition.
 *
 * @author      Ulf Schroeter
 * @version     1.0
 *
 */
public interface TimedObject
{
    public void ejbTimeout( Timer timer );
}
 
Step 2:    Create a schedulable object that is controlled by JBoss scheduler service
 
package de.mobilcom.eai.timerservice.jboss;
 
import java.util.Date;
import java.lang.reflect.Method;
 
import javax.naming.Context;
import javax.naming.InitialContext;
 
import org.jboss.varia.scheduler.Schedulable;
 
import de.mobilcom.eai.timerservice.TimedObject;
 
/**
 * The TimedObjectSchedulable class is a JBoss application server
 * specific implementation for time interval based invocation
 * of a controlled EJB.
 *
 * The controlled EJB class must implement a LocalHome interface
 * with create() method for EJB object instance creation.
 *
 * The controlled EJB class must implement the TimedObject
 * callback interface for time interval based EJB invocation.
 *
 * Object instances of TimedObjectSchedulabe will be created and
 * controlled by the JBoss scheduler service.
 *
 * @author      Ulf Schroeter
 * @version     1.0
 *
 */
 
public class TimedObjectSchedulable implements Schedulable
{
    /**
     * Attribute declaration
    */
    private String          m_TimedObjectJNDILocalHomeName  ;
    private TimedObject     m_TimedObject                   ;
   
   
    /**
     * constructor ( will be called by JBoss scheduler service )
     *
     * @param JNDILocalHomeName  JNDI lookup name of the local
     *                           home interface of the controlled
     *                           EJB object implementing TimedObject
     *                           interface
     */
    public TimedObjectSchedulable( String JNDILocalHomeName )
    {
        // store JNDI local home name
        m_TimedObjectJNDILocalHomeName = JNDILocalHomeName ;
    }
 
    /**
     * Schedulable callback method that will be called by
     * JBoss scheduler service when configured time interval
     * has elapsed.
     *
     * The method simply calls the ejbTimeout() methode of the
     * controlled EJB object implementing TimedObject interface.
     *
     * The lookup of the LocalHome interface of the controlled
     * EJB class within this function ensures that the timed
     * callback of the EJB class will continue even after EJB
     * redeployment ( e.g. due to maintenance reasons )
     * 
     * @see org.jboss.varia.scheduler.Schedulable#perform(Date, long)
     */
   
    public void perform( Date arg0, long arg1 )
    {
        // get TimedObject interface
        try
        {
            if( m_TimedObject == null )
            {
                /**
                 *  get initial context and lookup local home interface
                 *
                 *  remark:     will throw exception if EJB object hasn't
                 *              been deployed ( e.g. while app server boot
                 *              sequence or due to undeployment of EJB for
                 *              update ) and causes a new EJB object lookup
                 *              attemp on the next timed perform() call.
                 */
                Context ctx = new InitialContext();
               
                Object  obj = ctx.lookup( m_TimedObjectJNDILocalHomeName );
       
                // use java refection to call create() method for
                // obtaining EJB instance
                Method method = obj.getClass().getDeclaredMethod( "create",
                                                                   new Class[0] );
               
                // create EJB instance and cast to TimedObject reference
                m_TimedObject = (TimedObject) method.invoke( obj, new Object[0] );
            }
                           
            /**
             *  call timed object EJB callback method
             *
             *  remark:     will throw exception if EJB object has been
             *              undeployed or redeployed and causes a new
             *              EJB object lookup attemp on the next timed
             *              perform() call.
             */
 
            m_TimedObject.ejbTimeout( null );
        }
        catch( Exception e )
        {
            System.err.println("TimedObjectSchedulable(" +
                                m_TimedObjectJNDILocalHomeName +
                                ")::perform() failed with Exception '" +
                               e.getMessage() + "' !" );
                              
            // reset invalid timed object reference
            m_TimedObject = null;
        }
    }
}
 
 
Step 3:    Implement your timed session bean that implements TimedObject interface.
               A Local/LocalHome EJB  interface is required for object creation.
               The schedulable object will invoke the ejbTimeout() method 
 
 
package de.mobilcom.eai.adapter.mcbs.event;
 
import java.util.Timer;
 
.....
 
import de.mobilcom.eai.timerservice.TimedObject;
 

/**
 * .....
 
 * It implements a EJB2.1 specification compatible TimedObject
 * interface for time based invocation of ejbTimeout() method
 * by the application server timer service.
 *
 * Until availability of a EJB2.1 compatible application server
 * timer service a JBoss specific timer service emulation based
 * on the provided scheduler service is used.
 * 
 *
 * @author      Ulf Schroeter
 * @version  1.0
 *
 */
 
public class MCBSEventAdapterTDB implements SessionBean,
                                            TimedObject
{
    /**
     * Attribute declaration
    */
   
    private SessionContext  m_Ctx           ;
       
    public void setSessionContext(SessionContext ctx)  { }
 
    public void ejbCreate()     {}
    public void ejbActivate() {}
    ......
    ......
 
    /**
     * Called by the application server timer service when the
     * defined timer interval has elapsed. All required event
     * processing will be handled within this method.
     *
     * @param timer     triggering timer object ( will allways
     *                  be null for timer service emulation )
     */
   
    public void ejbTimeout( Timer timer )
    {
        System.out.println( "MCBSEventAdapterTDB::ejbTimeout(" +
                            Thread.currentThread().getName() + ")");
    }
 
}
 
 
Step 4:    Configure the schedulable object in your JBoss deployment directory
 
 
 * To set up timed EJB method invovation create an appropriate
 * scheduler service xml-file in the JBoss deploy directory, e.g
 *
 *  <?xml version="1.0" encoding="UTF-8"?>
 *  <!DOCTYPE server>
 *  <!-- $Id: scheduler-service.xml,v 1.2 2002/02/24 10:24:35 user57 Exp $ -->
 *
 *  <server>
 *
 *    <classpath codebase="lib" archives="scheduler-plugin.jar, JMSTimerService.jar"/>
 *
 *  <!-- ==================================================================== -->
 *  <!-- Scheduler Service                                                    -->
 *  <!-- ==================================================================== -->
 *
 *   <!-- TimedObjectSchedulable MCBSEventAdapterTDB -->
 *
 *   <mbean code="org.jboss.varia.scheduler.Scheduler"
 *          name=":service=Scheduler,schedule=MCBSEventAdapterTDB">
 *      <attribute name="StartAtStartup">true</attribute>
 *      <attribute name="SchedulableClass">de.mobilcom.eai.timerservice.jboss.TimedObjectSchedulable</attribute>
 *      <attribute name="SchedulableArguments">local/MCBSEventAdapterTDB</attribute>
 *      <attribute name="SchedulableArgumentTypes">java.lang.String</attribute>
 *      <attribute name="InitialStartDate">0</attribute>
 *      <attribute name="SchedulePeriod">10000</attribute>
 *      <attribute name="InitialRepetitions">-1</attribute>
 *   </mbean>
 *
 *  </server>
 *
 * for timed EJB callback invocation of MCBSEventAdapterTBD every 10 seconds
 *
 
Best Regards
 
Ulf Schroeter
MobilCom Communicationstechnik GmbH

IT Business Unit Consumer / Projekte und Prozesse
mailto:[EMAIL PROTECTED]

 
 
----- Original Message -----
From: "Pete Beck" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, September 20, 2002 10:41 AM
Subject: Re: [JBoss-user] Can an ejb function as a daemon?

> Short answer: Use the timer service in JBoss
>
> Long answer:
> Polling is not an ideal solution usually, but sometimes it is the only
> solution.
> A better solution is to use messaging if you can, as then your app will
> only run when it needs to.
>
> However in the draft EJB 2.1 spec, Sun have added a new timer feature
> which should do exactly what you need. 
> The advantage over the current JBoss method is that it will be part of
> the spec, so will port to other containers supporting 2.1.
>
> As JBoss already has a timer service, I expect it will be one of the
> first to implement this new feature
>
> On Thu, 2002-09-19 at 22:30, David McKnight wrote:
> > I have an application that polls a db table (legacy application) for data every few seconds. The data in this table will never get updated by beans, only by other tables, triggers, VB clients, etc. The app I have now runs as a daemon, when it starts up it initializes a few things then does it's first select from the table, waits a few seconds and repeats. My question is, can something like this be done in an ejb? Session or entity, preferably session since no data will be kept once it has been processed. It seems that no methods in a session bean get called when deployed by jboss - is there some parameter in jboss to set that would create at least one instance (and I only want one)? Thanks in advance,
> > David
>
> --
> Peter Beck BEng (hons)  - Managing Director, Electrostrata Ltd.
>
http://www.electrostrata.com  --+-+-+-+--  Experts in e-business and
> e-commerce
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
>
http://thinkgeek.com/sf
> _______________________________________________
> JBoss-user mailing list
>
[EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to