Thread Pool Manager and Calendar

This is not a Support area! Discuss about the Server here. Non-Server related discussion goes in Off-Topic Discussion.
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply
User avatar
pinkcore
Posts: 247
Joined: Fri Jul 24, 2009 3:04 am
Location: Czech Republic

Thread Pool Manager and Calendar

Post by pinkcore »

Hello there. I'v tried a lot of experiments about Thread Pool Manager. I searched on this forum and I looked into core too for any example. But I can't solve it.

How can I make Thread Pool Manager, which will be runes for example at 16:45? (Likes TvT Event)

Here is my experiment: :D

Code: Select all

    public static void main(String[] args)    {        Calendar restartTime = null;        Calendar currentTime = null;                restartTime = Calendar.getInstance();        restartTime.set(Calendar.HOUR_OF_DAY, 16);        restartTime.set(Calendar.MINUTE, 45);        currentTime = Calendar.getInstance();                long finalTime = currentTime.getTimeInMillis() - restartTime.getTimeInMillis();                ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()        {            public void run()            {                // DO SOMETHING            }        }, finalTime);    }
I'm not here only for food!
User avatar
tukune
Posts: 533
Joined: Sun Mar 29, 2009 2:35 pm
Location: Japan

Re: Thread Pool Manager and Calendar

Post by tukune »

:evil: long finalTime = currentTime.getTimeInMillis() - restartTime.getTimeInMillis();
:D long finalTime = restartTime.getTimeInMillis() - currentTime.getTimeInMillis();
User avatar
pinkcore
Posts: 247
Joined: Fri Jul 24, 2009 3:04 am
Location: Czech Republic

Re: Thread Pool Manager and Calendar

Post by pinkcore »

tukune wrote::evil: long finalTime = currentTime.getTimeInMillis() - restartTime.getTimeInMillis();
:D long finalTime = restartTime.getTimeInMillis() - currentTime.getTimeInMillis();
:P I'm very sorry, I'll test it soon. :) Thanks you for report. ;)

EDIT:

Okey, thank you very much. I did it by this way (likes TvT Manager). Is there better solution?

Code: Select all

    public static void main(String[] args)    {        Calendar restartTime = null;        Calendar currentTime = null;                restartTime = Calendar.getInstance();        restartTime.set(Calendar.HOUR_OF_DAY, 16);        restartTime.set(Calendar.MINUTE, 45);        currentTime = Calendar.getInstance();                if (restartTime.getTimeInMillis() < currentTime.getTimeInMillis())        {              restartTime.add(Calendar.DAY_OF_WEEK, 1);        }                long finalTime = restartTime.getTimeInMillis() - currentTime.getTimeInMillis();         ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()        {            public void run()            {                // DO SOMETHING            }        }, finalTime);    }
I'm not here only for food!
User avatar
Tryskell
Posts: 256
Joined: Wed Nov 25, 2009 5:57 pm
Location: France :)

Re: Thread Pool Manager and Calendar

Post by Tryskell »

global_tasks.sql
User avatar
pinkcore
Posts: 247
Joined: Fri Jul 24, 2009 3:04 am
Location: Czech Republic

Re: Thread Pool Manager and Calendar

Post by pinkcore »

Tryskell wrote:global_tasks.sql
Not really, I can't do restarts for example at sunday 12:00 and wednesday at 23:30 ...

I improved this script a little bit so I can configure restarts I want :)
I'm not here only for food!
User avatar
JIV
L2j Veteran
L2j Veteran
Posts: 1882
Joined: Sun Jan 06, 2008 8:17 pm
Location: Slovakia
Contact:

Re: Thread Pool Manager and Calendar

Post by JIV »

u can use global task
User avatar
Tryskell
Posts: 256
Joined: Wed Nov 25, 2009 5:57 pm
Location: France :)

Re: Thread Pool Manager and Calendar

Post by Tryskell »

pinkcore wrote:
Tryskell wrote:global_tasks.sql
Not really, I can't do restarts for example at sunday 12:00 and wednesday at 23:30 ...

I improved this script a little bit so I can configure restarts I want :)
Ofc you can, basically I setup recommendations restart at 6:30 of morning.

It exists since Interlude, so I doubt they deleted it.

If system doesn't allow one thing (like a particular day) just improve the system, but I guess it works like you want from first try.
User avatar
pinkcore
Posts: 247
Joined: Fri Jul 24, 2009 3:04 am
Location: Czech Republic

Re: Thread Pool Manager and Calendar

Post by pinkcore »

I searched alot about global tasks but doesn't exist proper documentation how to set restarts for example on sunday and wednesday. Every time it was only from day to day...
I'm not here only for food!
User avatar
JIV
L2j Veteran
L2j Veteran
Posts: 1882
Joined: Sun Jan 06, 2008 8:17 pm
Location: Slovakia
Contact:

Re: Thread Pool Manager and Calendar

Post by JIV »

l2j was never well documented or documentation is obsolete,better check code. Its TYPE_GLOBAL_TASK just put 7 days interval, in your case u will need 2 independent tasks.
User avatar
pinkcore
Posts: 247
Joined: Fri Jul 24, 2009 3:04 am
Location: Czech Republic

Re: Thread Pool Manager and Calendar

Post by pinkcore »

JIV wrote:l2j was never well documented or documentation is obsolete,better check code. Its TYPE_GLOBAL_TASK just put 7 days interval, in your case u will need 2 independent tasks.
If I understand, I can use TYPE_TIME and input everything in Date format into 0th parameter.
I'm right?
I'm not here only for food!
User avatar
tukune
Posts: 533
Joined: Sun Mar 29, 2009 2:35 pm
Location: Japan

Re: Thread Pool Manager and Calendar

Post by tukune »

JIV have a big mouth. dont trust him.
I made this code last tuesday. Do something useful?

Code: Select all

package cron; import java.io.File;import java.io.IOException;import java.util.Arrays;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar; import com.l2jserver.gameserver.ThreadPoolManager; /** * @author JOJO *  * How to use: *   Add "cron/ServerRestartTask.java" into scripts.cfg *  * Option: *  *   Write BACKUP.BAT yourself. using mysqldump etc... *   And patch startGameServer.bat-------------------------------------     if ERRORLEVEL 2 goto restart  if ERRORLEVEL 1 goto error  goto end  :restart  echo.  echo Admin Restart ...  echo.+ IF EXIST __backup__ CALL BACKUP.BAT  goto start-------------------------------------    *  */public class ServerRestartTask{    private static final String TASK_NAME = ServerRestartTask.class.getSimpleName();    private static final int SECONDS_SHUT = 180;    private static final long MINIMUM_DELAY = 300000;   // safety guard time.        private static final String BACKUP_FLAG = "__backup__";        public ServerRestartTask()    {        long[] timeTable = new long[]{                calcDelay(MINIMUM_DELAY, Calendar.SUNDAY, 12, 0),                calcDelay(MINIMUM_DELAY, Calendar.WEDNESDAY, 23, 30),                calcDelay(MINIMUM_DELAY, Calendar.FRIDAY, 9, 0),        };        Arrays.sort(timeTable);        final long delay = timeTable[0] - System.currentTimeMillis();                new File(BACKUP_FLAG).delete(); // remove flag file.                ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()        {            public void run()            {            //  com.l2jserver.gameserver.Announcements.getInstance().announceToAll("*****");                com.l2jserver.gameserver.Shutdown.getInstance().startTelnetShutdown(TASK_NAME, SECONDS_SHUT, true);                try                {                    new File(BACKUP_FLAG).createNewFile();  // create flag file.                }                catch (IOException e)                {                    e.printStackTrace();                }            }        }, delay);                System.out.println(TASK_NAME + ": Scheduled " + new Date(timeTable[0]).toString());    }        private long calcDelay(long minimumDelay, int dayOfWeek, int hourOfDay, int miniute)    {        long now = System.currentTimeMillis();        GregorianCalendar reset = new GregorianCalendar();        reset.setTimeInMillis(now);        reset.set(Calendar.MILLISECOND, 0);        reset.set(Calendar.SECOND, 0);        reset.set(Calendar.MINUTE, miniute);        reset.set(Calendar.HOUR_OF_DAY, hourOfDay);        while (reset.getTimeInMillis() - now < minimumDelay)            reset.add(Calendar.DATE, 1);        while (reset.get(Calendar.DAY_OF_WEEK) != dayOfWeek)            reset.add(Calendar.DATE, 1);        return reset.getTimeInMillis();    }        public static void main(String args[])    {        new ServerRestartTask();    }}
User avatar
pinkcore
Posts: 247
Joined: Fri Jul 24, 2009 3:04 am
Location: Czech Republic

Re: Thread Pool Manager and Calendar

Post by pinkcore »

tukune wrote:JIV have a big mouth. dont trust him.
I made this code last tuesday. Do something useful?

Code: Select all

package cron; import java.io.File;import java.io.IOException;import java.util.Arrays;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar; import com.l2jserver.gameserver.ThreadPoolManager; /** * @author JOJO *  * How to use: *   Add "cron/ServerRestartTask.java" into scripts.cfg *  * Option: *  *   Write BACKUP.BAT yourself. using mysqldump etc... *   And patch startGameServer.bat-------------------------------------     if ERRORLEVEL 2 goto restart  if ERRORLEVEL 1 goto error  goto end  :restart  echo.  echo Admin Restart ...  echo.+ IF EXIST __backup__ CALL BACKUP.BAT  goto start-------------------------------------    *  */public class ServerRestartTask{    private static final String TASK_NAME = ServerRestartTask.class.getSimpleName();    private static final int SECONDS_SHUT = 180;    private static final long MINIMUM_DELAY = 300000;   // safety guard time.        private static final String BACKUP_FLAG = "__backup__";        public ServerRestartTask()    {        long[] timeTable = new long[]{                calcDelay(MINIMUM_DELAY, Calendar.SUNDAY, 12, 0),                calcDelay(MINIMUM_DELAY, Calendar.WEDNESDAY, 23, 30),                calcDelay(MINIMUM_DELAY, Calendar.FRIDAY, 9, 0),        };        Arrays.sort(timeTable);        final long delay = timeTable[0] - System.currentTimeMillis();                new File(BACKUP_FLAG).delete(); // remove flag file.                ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()        {            public void run()            {            //  com.l2jserver.gameserver.Announcements.getInstance().announceToAll("*****");                com.l2jserver.gameserver.Shutdown.getInstance().startTelnetShutdown(TASK_NAME, SECONDS_SHUT, true);                try                {                    new File(BACKUP_FLAG).createNewFile();  // create flag file.                }                catch (IOException e)                {                    e.printStackTrace();                }            }        }, delay);                System.out.println(TASK_NAME + ": Scheduled " + new Date(timeTable[0]).toString());    }        private long calcDelay(long minimumDelay, int dayOfWeek, int hourOfDay, int miniute)    {        long now = System.currentTimeMillis();        GregorianCalendar reset = new GregorianCalendar();        reset.setTimeInMillis(now);        reset.set(Calendar.MILLISECOND, 0);        reset.set(Calendar.SECOND, 0);        reset.set(Calendar.MINUTE, miniute);        reset.set(Calendar.HOUR_OF_DAY, hourOfDay);        while (reset.getTimeInMillis() - now < minimumDelay)            reset.add(Calendar.DATE, 1);        while (reset.get(Calendar.DAY_OF_WEEK) != dayOfWeek)            reset.add(Calendar.DATE, 1);        return reset.getTimeInMillis();    }        public static void main(String args[])    {        new ServerRestartTask();    }}
For this script you don't need constructor, but it neverminds. :)
I'm not here only for food!
Post Reply