Page 1 of 1
Thread Pool Manager and Calendar
Posted: Tue Mar 01, 2011 6:54 pm
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:
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); }
Re: Thread Pool Manager and Calendar
Posted: Wed Mar 02, 2011 5:25 am
by tukune

long finalTime = currentTime.getTimeInMillis() - restartTime.getTimeInMillis();

long finalTime = restartTime.getTimeInMillis() - currentTime.getTimeInMillis();
Re: Thread Pool Manager and Calendar
Posted: Wed Mar 02, 2011 1:15 pm
by pinkcore
tukune wrote:
long finalTime = currentTime.getTimeInMillis() - restartTime.getTimeInMillis();

long finalTime = restartTime.getTimeInMillis() - currentTime.getTimeInMillis();

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); }
Re: Thread Pool Manager and Calendar
Posted: Fri Mar 04, 2011 11:04 pm
by Tryskell
global_tasks.sql
Re: Thread Pool Manager and Calendar
Posted: Sat Mar 05, 2011 10:05 am
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

Re: Thread Pool Manager and Calendar
Posted: Sat Mar 05, 2011 10:34 am
by JIV
u can use global task
Re: Thread Pool Manager and Calendar
Posted: Sat Mar 05, 2011 11:35 am
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.
Re: Thread Pool Manager and Calendar
Posted: Sat Mar 05, 2011 1:04 pm
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...
Re: Thread Pool Manager and Calendar
Posted: Sat Mar 05, 2011 1:14 pm
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.
Re: Thread Pool Manager and Calendar
Posted: Tue Mar 08, 2011 1:46 pm
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?
Re: Thread Pool Manager and Calendar
Posted: Wed Mar 09, 2011 6:26 am
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(); }}
Re: Thread Pool Manager and Calendar
Posted: Wed Mar 09, 2011 6:31 am
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.
