All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pro.jk.ejoker.common.service.impl.ScheduleServiceImpl Maven / Gradle / Ivy

There is a newer version: 3.0.7.1
Show newest version
package pro.jk.ejoker.common.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import pro.jk.ejoker.common.context.annotation.context.Dependence;
import pro.jk.ejoker.common.context.annotation.context.EInitialize;
import pro.jk.ejoker.common.context.annotation.context.EService;
import pro.jk.ejoker.common.service.IScheduleService;
import pro.jk.ejoker.common.service.Scavenger;

@EService
public class ScheduleServiceImpl implements IScheduleService {

	private final static Logger logger = LoggerFactory.getLogger(ScheduleServiceImpl.class);

	private Map timerTaskList = new HashMap<>();

	@Dependence
	private Scavenger scavenger;

	@EInitialize
	private void init() {
		scavenger.addFianllyJob(this::cleanAll);
	}

	@Override
	public void startTask(String name, Runnable action, long dueTime, long period) {
		Timer timer;
		if (null != timerTaskList.putIfAbsent(name, (timer = new Timer())))
			throw new RuntimeException(String.format("Task name of [%s] is exists before!!!", name));
		timer.schedule(new RemindTask(name, action), dueTime, period);
	}

	@Override
	public void stopTask(String name) {
		Timer timer;
		if (null != (timer = timerTaskList.getOrDefault(name, null))) {
			timer.cancel();
		}
	}

	private void cleanAll() {
		Set> entrySet = timerTaskList.entrySet();
		for (Entry entry : entrySet) {
			Timer timer = entry.getValue();
			timer.cancel();
		}
		timerTaskList.clear();
	}

	public static class RemindTask extends TimerTask {

		private final String taskName;
		
		private final Runnable action;

		public RemindTask(String taskName, Runnable action) {
			this.taskName = taskName;
			this.action = action;
		}

		public void run() {
			try {
				action.run();
			} catch (Exception re) {
				logger.warn("Caught an exception on task!!! You should handle exception on your finally block or ignore this message if you ensure what happens. [taskName={}]", taskName, re);
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy