You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package xyz.wbsite.dbtool.web.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import xyz.wbsite.dbtool.web.action.GlobalController;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.Executors;
/**
* Task配置默认只在开发环境生效,根据实际情况是否需要Task选择开启
* 开启方法:将@Profile("dev") 删除(所有环境生效) 或改为 @Profile("prod")(只对生产环境生效)
*/
@Configuration
@EnableScheduling
@Profile("prod")
public class TaskConfig implements SchedulingConfigurer {
@Scheduled(cron="0/30 * * * * ? ")
public void task(){
// todo 自定实现的定时任务
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
HashMap<String, String> data = new HashMap<>();
data.put("time", format.format(new Date()));
GlobalController.pushAll(data);
}
/**
* Spring的 Schedule 默认是单线程执行
* 若要多线程并行运行任务 需实现SchedulingConfigurer并重写configureTasks方法
*
* @param scheduledTaskRegistrar
*/
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(3));
}
}

Powered by TurnKey Linux.