Page 1 of 1

Flexible schedule system

Posted: Sat Sep 03, 2011 8:56 am
by VlLight
It seems to be handy for me, so maybe it would useful for someone.
I'm often facing with task of scheduling some events. It isn't big problem to create (and make it configurable) shedule for one launche or several launches at predefined time per day, like TVT Event (howewer, there is still no universal method for handling schedule string like "3:00,9:00,15:00,21:00" in server).
But it becomes much harder to make universal configurable definitions for "3:00,9:00 in every n days", "n times in month" and so on.
I think, many people know about cron, and it looks like suitable solution for this case. After some time, spendig in search of java implementation of cron , i've found quartz library (actual version is quartz-2.0.2), that contains one class, that is able to calculate nearest launch time from cron-like string.
So, in general it looks like this:

Code: Select all

import com.l2jserver.gameserver.model.Quest; import java.util.Calendar;import org.quartz.impl.triggers.CronTriggerImpl; public class SampleEvent extends Quest{    private static final String cronDef = "0 0 3,9,15,21 ? * *";        public SampleEvent(int questId, String name, String descr)    {        super(questId, name, descr);                long timeToStart = getNextRunInterval("cronDef");                if (timeToStart >= 0)            startQuestTimer("start_event", timeToStart, null, null);            }     private void endEvent()    {        long timeToStart = getNextRunInterval("cronDef");        if (timeToStart >= 0)            startQuestTimer("start_event", timeToStart, null, null);            }     /**     * @param cronString - cron-like string     * @return interval to next run for timer     */    private long getNextRunInterval(String cronString)    {        long ret;        Date currTime = Calendar.getInstance().getTime();        CronTriggerImpl tr = new CronTriggerImpl();        try        {            tr.setCronExpression(cronString);            Date nextFireAt = tr.getFireTimeAfter(currTime);            ret = nextFireAt.getTime() - System.currentTimeMillis();         }        catch(Exception e)        {            ret = -1;        }                return ret;    }     public static void main(String[] args)    {        new SampleEvent(-1, "SampleEvent", "events");    }} 

Re: Flexible schedule system

Posted: Sat Sep 03, 2011 9:27 am
by jurchiks
Nice find! Unless some devs are against flexibility, this might even get used in l2j (but ofc, testing first).
Good thing it is simple to use and open-source.
SPOILER:
official site: here

Re: Flexible schedule system

Posted: Sat Sep 03, 2011 4:25 pm
by UnAfraid
hmm.. sounds interesting

Re: Flexible schedule system

Posted: Wed Sep 07, 2011 3:00 pm
by Nik
seems pretty interesting :)

Re: Flexible schedule system

Posted: Wed Sep 07, 2011 10:51 pm
by nonom
jurchiks wrote:Nice find! Unless some devs are against flexibility, this might even get used in l2j (but ofc, testing first).
Good thing it is simple to use and open-source.
SPOILER:
official site: here
@jurchiks, but cron is not multi-platform :) bye flexibility and simplicity

Anyways is an useful tool, thanks for share

Re: Flexible schedule system

Posted: Thu Sep 08, 2011 6:11 pm
by jurchiks
where did you read this tool uses linux cron?

Re: Flexible schedule system

Posted: Fri Sep 09, 2011 11:14 pm
by nonom
jurchiks wrote:where did you read this tool uses linux cron?
Sorry jurchiks I was wrong