users@glassfish.java.net

Re: How ? Timer Service

From: <glassfish_at_javadesktop.org>
Date: Thu, 11 Sep 2008 23:11:39 PDT

Is it possible to save the information in form of private variables the way I am doing below?
(It works for me, as I have tested it, but I have doubts!! since its a Stateless bean...how will the private variables behave!!!)

startScheduler is the variable (boolean) where I store this information.

A. If in first @Timeout the scheduler was paused by ImportService
B. ImportService will start the scheduler again.
C. If it was not running, ImportService will not start that.

-----------------------------------------------------------------------------------------------------------------------------------
@Stateless
public class ImportServiceBean implements ImportService
{
    public String getName ()
    {
        return "ImportService";
    }
    
    public void start ()
    {
        try
        {
            for (Object o : timerService.getTimers())
            {
                ((Timer)o).cancel();
            }
            long initDuration = 0;
            long interval = 1000* 10;
            timerService.createTimer(initDuration,interval,null);
        }
        catch (Exception e)
        {
        }
    }
    
    public void startImport (boolean a, boolean b, boolean c,boolean d, Status status, int e)
    {
            this.a = a;
            this.b = b;
            this.c = c;
            this.d = d;
            this.status = status;
            this.e = e;
            this.startScheduler = false;
        start();
    }
    
    public void stop ()
    {
        try
        {
            for (Object o : timerService.getTimers())
            {
                ((Timer)o).cancel();
            }
        }
        catch (Exception e)
        {
        }
    }

    public boolean isRunning ()
    {
        return timerService.getTimers().size() > 0;
    }
    
    public Date getNextTimeout ()
    {
        for (Object o : timerService.getTimers())
        {
            return ((Timer)o).getNextTimeout();
        }
        return null;
    }
    
    public long getTimeRemaining ()
    {
        for (Object o : timerService.getTimers())
        {
            return ((Timer)o).getTimeRemaining();
        }
        return -1;
    }
    
    @Timeout
    public void ImportService (Timer timer)
    {
        if(scheduler.isRunning())
        {
                scheduler.stop();
                startScheduler =true;
        }
            else
            {
                    if(statusService.getStatus())
                    {
                            //keep waiting.
                    }
                    else
                    {
                            configurationService.readData(a, b, c,d, status, e);
                        stop();
                        if(startScheduler)
                        {
                                scheduler.start();
                        }
                    }
            }
        return;
    }

    private boolean a;
    private boolean b;
    private boolean c;
    private boolean d;
    private Status status = Status.DOWN;
    private int e = 1;
    private boolean startScheduler =false;

    @Resource
    private TimerService timerService;
    
    @EJB
    private ConfigurationService configurationService;
    
    @EJB
    private Scheduler scheduler;
    
    @EJB
    public StatusService statusService;
}
-----------------------------------------------------------------------------------------------------------------------------------
[Message sent by forum member 'suyogbarve' (suyogbarve)]

http://forums.java.net/jive/thread.jspa?messageID=298971