Search in sources :

Example 1 with TaskExecutor

use of cn.hutool.cron.TaskExecutor in project hutool by dromara.

the class CronTest method cronWithListenerTest.

@Test
@Ignore
public void cronWithListenerTest() {
    CronUtil.getScheduler().addListener(new TaskListener() {

        @Override
        public void onStart(TaskExecutor executor) {
            Console.log("Found task:[{}] start!", executor.getCronTask().getId());
        }

        @Override
        public void onSucceeded(TaskExecutor executor) {
            Console.log("Found task:[{}] success!", executor.getCronTask().getId());
        }

        @Override
        public void onFailed(TaskExecutor executor, Throwable exception) {
            Console.error("Found task:[{}] failed!", executor.getCronTask().getId());
        }
    });
    // 支持秒级别定时任务
    CronUtil.setMatchSecond(true);
    CronUtil.start();
    ThreadUtil.waitForDie();
    Console.log("Exit.");
}
Also used : TaskExecutor(cn.hutool.cron.TaskExecutor) TaskListener(cn.hutool.cron.listener.TaskListener) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with TaskExecutor

use of cn.hutool.cron.TaskExecutor in project Jpom by dromara.

the class CronUtils method start.

/**
 * 开始
 */
public static void start() {
    boolean matchSecond = ExtConfigBean.getInstance().getTimerMatchSecond();
    // 开启秒级
    CronUtil.setMatchSecond(matchSecond);
    // 
    Scheduler scheduler = CronUtil.getScheduler();
    // 
    boolean started = scheduler.isStarted();
    if (started) {
        return;
    }
    synchronized (CronUtils.class) {
        started = scheduler.isStarted();
        if (started) {
            return;
        }
        CronUtil.start();
        scheduler.addListener(new TaskListener() {

            @Override
            public void onStart(TaskExecutor executor) {
                TaskStat taskStat = TASK_STAT.computeIfAbsent(executor.getCronTask().getId(), s -> new TaskStat());
                taskStat.lastExecuteTime = SystemClock.now();
                taskStat.executeCount++;
            }

            @Override
            public void onSucceeded(TaskExecutor executor) {
                TaskStat taskStat = TASK_STAT.computeIfAbsent(executor.getCronTask().getId(), s -> new TaskStat());
                taskStat.succeedCount++;
            }

            @Override
            public void onFailed(TaskExecutor executor, Throwable exception) {
                TaskStat taskStat = TASK_STAT.computeIfAbsent(executor.getCronTask().getId(), s -> new TaskStat());
                taskStat.failedCount++;
                DefaultSystemLog.getLog().error("定时任务异常", exception);
            }
        });
    }
}
Also used : CronUtil(cn.hutool.cron.CronUtil) TaskExecutor(cn.hutool.cron.TaskExecutor) DefaultSystemLog(cn.jiangzeyin.common.DefaultSystemLog) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) TaskListener(cn.hutool.cron.listener.TaskListener) List(java.util.List) Task(cn.hutool.cron.task.Task) Map(java.util.Map) Scheduler(cn.hutool.cron.Scheduler) SystemClock(cn.hutool.core.date.SystemClock) JSONObject(com.alibaba.fastjson.JSONObject) ExtConfigBean(io.jpom.system.ExtConfigBean) TaskTable(cn.hutool.cron.TaskTable) TaskExecutor(cn.hutool.cron.TaskExecutor) Scheduler(cn.hutool.cron.Scheduler) TaskListener(cn.hutool.cron.listener.TaskListener)

Example 3 with TaskExecutor

use of cn.hutool.cron.TaskExecutor in project hutool by looly.

the class CronTest method cronWithListenerTest.

@Test
@Ignore
public void cronWithListenerTest() {
    CronUtil.getScheduler().addListener(new TaskListener() {

        @Override
        public void onStart(TaskExecutor executor) {
            Console.log("Found task:[{}] start!", executor.getCronTask().getId());
        }

        @Override
        public void onSucceeded(TaskExecutor executor) {
            Console.log("Found task:[{}] success!", executor.getCronTask().getId());
        }

        @Override
        public void onFailed(TaskExecutor executor, Throwable exception) {
            Console.error("Found task:[{}] failed!", executor.getCronTask().getId());
        }
    });
    // 支持秒级别定时任务
    CronUtil.setMatchSecond(true);
    CronUtil.start();
    ThreadUtil.waitForDie();
    Console.log("Exit.");
}
Also used : TaskExecutor(cn.hutool.cron.TaskExecutor) TaskListener(cn.hutool.cron.listener.TaskListener) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

TaskExecutor (cn.hutool.cron.TaskExecutor)3 TaskListener (cn.hutool.cron.listener.TaskListener)3 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 SystemClock (cn.hutool.core.date.SystemClock)1 CronUtil (cn.hutool.cron.CronUtil)1 Scheduler (cn.hutool.cron.Scheduler)1 TaskTable (cn.hutool.cron.TaskTable)1 Task (cn.hutool.cron.task.Task)1 DefaultSystemLog (cn.jiangzeyin.common.DefaultSystemLog)1 JSONObject (com.alibaba.fastjson.JSONObject)1 ExtConfigBean (io.jpom.system.ExtConfigBean)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1