테스트

aion-server 4.8

Gitteol
최고관리자 · 1 · 💬 0 클론/새로받기
 4.8 61f661d · 1 commits 새로받기(Pull)
game-server/src/com/aionemu/gameserver/taskmanager/AbstractCronTask.java
package com.aionemu.gameserver.taskmanager;

import java.util.Date;
import java.util.concurrent.Semaphore;

import org.quartz.CronExpression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aionemu.commons.services.CronService;
import com.aionemu.gameserver.dao.ServerVariablesDAO;
import com.aionemu.gameserver.utils.ThreadPoolManager;

/**
 * @author Rolandas, Neon
 */
public abstract class AbstractCronTask implements Runnable {

	protected static final Long SERVER_STOP_MILLIS = ServerVariablesDAO.loadLong("serverLastRun");
	private static final Semaphore semaphore = new Semaphore(1);
	protected final Logger log = LoggerFactory.getLogger(getClass());
	private final CronExpression cronExpression;
	private Date lastPlannedRunBeforeServerStart;
	private Date lastRun;
	private Date nextRun;

	public AbstractCronTask(CronExpression cronExpression) {
		this.cronExpression = cronExpression;
		if (this.cronExpression == null) {
			log.info(getClass().getSimpleName() + " is deactivated");
			return;
		}
		this.nextRun = getNextRunAfter(new Date());
		this.lastPlannedRunBeforeServerStart = findLastPlannedRun();
		runAndScheduleAsyncWithLock();
	}

	/**
	 * Runs this task in another thread, so that constructor can finish and external references to this task don't throw null pointer exceptions.
	 * This additionally makes sure that other tasks don't run before a previous one is finished, so multiple tasks are initialized semi-synchronous.
	 */
	private void runAndScheduleAsyncWithLock() {
		semaphore.acquireUninterruptibly();
		ThreadPoolManager.getInstance().executeLongRunning(() -> {
			if (shouldRunOnStart())
				run();
			CronService.getInstance().schedule(this, cronExpression, true);
			log.info("Scheduled " + getClass().getSimpleName() + " with cron expression: " + cronExpression);
			semaphore.release();
		});
	}

	/**
	 * @return Default implementation returns true if the server was down when task should have run
	 */
	protected boolean shouldRunOnStart() {
		return SERVER_STOP_MILLIS != null && lastPlannedRunBeforeServerStart != null && SERVER_STOP_MILLIS < lastPlannedRunBeforeServerStart.getTime();
	}

	/**
	 * @return The last time this task started, null if it didn't during this uptime yet
	 */
	public final Date getLastRun() {
		return lastRun;
	}

	/**
	 * @return The last time this task started or should have started (in case task hasn't yet run since the server got restarted)
	 */
	public final Date getLastPlannedRun() {
		return lastRun == null ? lastPlannedRunBeforeServerStart : lastRun;
	}

	public final long getMillisSinceLastRun() {
		return lastRun == null ? -1 : System.currentTimeMillis() - lastRun.getTime();
	}

	/**
	 * @return Time of the next task start
	 */
	public final Date getNextRun() {
		return nextRun;
	}

	/**
	 * @return Time of the next task start after given date
	 */
	public final Date getNextRunAfter(Date date) {
		return cronExpression.getTimeAfter(date);
	}

	public final long getMillisUntilNextRun() {
		return nextRun.getTime() - System.currentTimeMillis();
	}

	protected abstract void executeTask();

	@Override
	public final void run() {
		lastRun = new Date();
		nextRun = getNextRunAfter(lastRun);
		executeTask();
	}

	/**
	 * @return Date when this task last should have run, whether the server was online or not. <b>NOTE</b>: The current implementation may not find the
	 *         correct date if the underlying cron expression is irregular
	 */
	private Date findLastPlannedRun() {
		long interval = getNextRunAfter(nextRun).getTime() - nextRun.getTime();
		long now = System.currentTimeMillis();
		long millis = now;
		Date lastRun;
		do {
			millis -= interval / 2;
			lastRun = cronExpression.getTimeAfter(new Date(millis));
		} while (lastRun.getTime() >= now);
		return lastRun;
	}
}

📎 첨부파일

댓글 작성 권한이 없습니다.
🏆 포인트 랭킹 TOP 10
순위 닉네임 포인트
1 no_profile 타키야겐지쪽지보내기 자기소개 아이디로 검색 전체게시물 102,949
2 no_profile 동가리쪽지보내기 자기소개 아이디로 검색 전체게시물 63,733
3 no_profile 라프텔쪽지보내기 자기소개 아이디로 검색 전체게시물 51,771
4 no_profile 불멸의행복쪽지보내기 자기소개 아이디로 검색 전체게시물 36,923
5 서번트쪽지보내기 자기소개 아이디로 검색 전체게시물 35,011
6 no_profile 닥터스쪽지보내기 자기소개 아이디로 검색 전체게시물 29,470
7 no_profile 검은고양이쪽지보내기 자기소개 아이디로 검색 전체게시물 29,077
8 no_profile Revolution쪽지보내기 자기소개 아이디로 검색 전체게시물 28,199
9 no_profile 보거스쪽지보내기 자기소개 아이디로 검색 전체게시물 26,731
10 no_profile 호롤롤로쪽지보내기 자기소개 아이디로 검색 전체게시물 17,020
알림 0