scheduledthreadpoolexecutor定时任务
package util; import java.text.ParseException; import java.util.ArrayList; import java.util.Date; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.SimpleTrigger; import org.quartz.impl.StdSchedulerFactory; public class QuartzManager { private static SchedulerFactory sf = new StdSchedulerFactory(); private static String DB_JOB_GROUP_NAME = "groupDB"; private static String DB_TRIGGER_GROUP_NAME = "triggerDB"; private static String JPUSH_JOB_GROUP_NAME = "groupPush"; private static String JPUSH_TRIGGER_GROUP_NAME = "triggerPush"; public static Scheduler sched ; /** * 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名 * * @param jobName 任务名 * @param job 任务 * @param time 时间设置(cron表达式),参考quartz说明文档 * @throws SchedulerException * @throws ParseException */ public static void addDBJob(String jobName, Class jobClass, String time) throws SchedulerException, ParseException { if(sched == null){ sched = sf.getScheduler(); } JobDetail jobDetail = new JobDetail(jobName, DB_JOB_GROUP_NAME, jobClass);// 任务名,任务组,任务执行类 // 触发器 CronTrigger trigger = new CronTrigger(jobName, DB_TRIGGER_GROUP_NAME);//触发器名,触发器组 trigger.setCronExpression(time);//触发器时间设定 sched.scheduleJob(jobDetail,trigger); System.out.println("addDBJob执行了:::time:"+time); // 启动 if (!sched.isShutdown()) sched.start(); } /** * 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名 * * @param jobName 任务名 * @param job 任务 * @param time时间设置(date规定时间触发) * @throws SchedulerException * @throws ParseException */ public static void addJpushJob(String jobName, Class jobClass, Date date,ArrayList<ArrayList<String>> user_ids_begintimes) throws SchedulerException, ParseException { if(sched == null){ sched = sf.getScheduler(); } JobDetail jobDetail = new JobDetail(jobName, JPUSH_JOB_GROUP_NAME, jobClass);// 任务名,任务组,任务执行类 jobDetail.getJobDataMap().put("user_ids", user_ids_begintimes); jobDetail.getJobDataMap().put("date", date); // 触发器 SimpleTrigger simpleTrigger = new SimpleTrigger(jobName, JPUSH_TRIGGER_GROUP_NAME); simpleTrigger.setStartTime(date); sched.scheduleJob(jobDetail, simpleTrigger); System.out.println("addJpushJob执行了:::user_ids:"+user_ids_begintimes+":::date:"+date); // 启动 if (!sched.isShutdown()) sched.start(); } /** * 移除一个任务(使用默认的任务组名,触发器名,触发器组名) * * @param jobName * @throws SchedulerException */ public static void removeJob(String jobName) throws SchedulerException { sched.pauseTrigger(jobName, DB_TRIGGER_GROUP_NAME);// 停止触发器 sched.unscheduleJob(jobName, DB_TRIGGER_GROUP_NAME);// 移除触发器 sched.deleteJob(jobName, DB_JOB_GROUP_NAME);// 删除任务 } /** * 移除一个任务 * * @param jobName * @param jobGroupName * @param triggerName * @param triggerGroupName * @throws SchedulerException */ public static void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) throws SchedulerException { // Scheduler sched = sf.getScheduler(); sched.pauseTrigger(triggerName, triggerGroupName);// 停止触发器 sched.unscheduleJob(triggerName, triggerGroupName);// 移除触发器 sched.deleteJob(jobName, jobGroupName);// 删除任务 } }
package util; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import server.NoteServer; import server.impl.NoteServerImpl; public class DBJob implements Job { NoteServer noteServer = new NoteServerImpl(); private static SimpleDateFormat sformat = new SimpleDateFormat("yyyy/MM/dd HH:mm"); public void execute(JobExecutionContext context) throws JobExecutionException { try { HashMap<String, String> paramMap = new HashMap<String, String>(); Date nowDate = new Date(); Long end_time = nowDate.getTime() + 10 * 60 * 1000;// 开始时间添加10分钟 paramMap.put("start_date", sformat.format(nowDate) + ":00");// 开始时间 paramMap.put("end_date", sformat.format(new Date(end_time)) + ":00");// 结束时间 HashMap<String, ArrayList<ArrayList<String>>> pushMemoMap = noteServer.getPushMemo(paramMap); System.out.println("开始dbjob>>>>>>pushMemoMap:::"+pushMemoMap); for (String key : pushMemoMap.keySet()) { String jobName = key.replace("/", "").replace(" ", "").replace(":", ""); QuartzManager.addJpushJob(jobName, PushJob.class, sformat.parse(key), pushMemoMap.get(key)); } } catch (Exception e) { e.printStackTrace(); } } }
package util; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class PushJob implements Job { SimpleDateFormat sformat = new SimpleDateFormat("yyyy/MM/dd HH:mm"); public static HashMap<String,ArrayList<String>> blockMap = new HashMap<String,ArrayList<String>>();//屏蔽发送的列表 @SuppressWarnings("unchecked") public void execute(JobExecutionContext context) throws JobExecutionException { ArrayList<ArrayList<String>> user_id_begintime = (ArrayList<ArrayList<String>>)context.getJobDetail().getJobDataMap().get("user_ids"); ArrayList<String> user_id_list = user_id_begintime.get(0); ArrayList<String> user_begintime_list = user_id_begintime.get(1); Date date = (Date)context.getJobDetail().getJobDataMap().get("date"); String[] user_id_arr =new String[user_id_list.size()]; ArrayList<String> blockList = blockMap.get(sformat.format(date)); if(blockList != null && blockList.size() > 0){//判断如果blockMap中存在此时的数据则在发送列表中删除此数据 for(String block_user_id : blockList){ if(user_id_list.contains(block_user_id)) user_id_list.remove(user_id_list.indexOf(block_user_id)); user_begintime_list.remove(user_id_list.indexOf(block_user_id)); } } for(int index=0; index<user_id_list.size(); index++){ user_id_arr[index] = user_id_list.get(index);//将list转成array } for(int i=0 ;i<user_id_arr.length; i++){//一次发送多个,就不用循环了 Map<String,String> map = new HashMap<String,String>(); map.put("type", "998"+"#sb#"+sformat.format(date)+"#sb#"+user_begintime_list.get(i)); JPushUtil.sendUserMessage(user_id_arr[i], "您有一个待办事项!", map); System.out.println("事件推送执行:::"+user_id_list.toString()+">>>"+map); } Iterator<Entry<String, ArrayList<String>>> blockIterator = blockMap.entrySet().iterator(); while(blockIterator.hasNext()){ Entry<String, ArrayList<String>> entry= blockIterator.next(); String key_date = entry.getKey(); if(key_date.compareTo(sformat.format(date))<=0){ blockIterator.remove(); } } } }