Scheduler

jinglan123abc 2010-12-08 01:56:39
public class TaskManager implements ServletContextListener {
private SiteConfigService siteConfigService;
public void contextDestroyed(ServletContextEvent arg0) {
}

public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext servletContext=servletContextEvent.getServletContext();

String contextRealPath=servletContext.getRealPath(".");
String daoContextPath=contextRealPath+"/WEB-INF/daoContext.xml";
String applictionContextPath=contextRealPath+"/WEB-INF/applicationContext.xml";
String proxool=contextRealPath+"/WEB-INF/proxool.xml";
String[] contextFiles=new String[]{"file:"+applictionContextPath,"file:"+daoContextPath};

try{
JAXPConfigurator.configure(proxool, false);
} catch (ProxoolException e) {
System.out.println("Problem configuring " + proxool);
e.printStackTrace();
}


try {
ApplicationContext ctx = new FileSystemXmlApplicationContext(contextFiles);

//通过SchedulerFactory获取一个调度器实例
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();

//创建一个JobDetail实现,并指定此JobDetail的任务类
JobDetail jobDetail = new JobDetail("CashRemindJob", "remindGroup",
CashReminder.class);

Map fieldsMap=new HashMap();
String REMIND_NAME1 = Constants.KEY_FIRST_REMIND_DAYS;
fieldsMap.put("REMIND_NAME", REMIND_NAME1);
fieldsMap.put("ctx", ctx);

JobDataMap jobDataMap=new JobDataMap(fieldsMap);
jobDetail.setJobDataMap(jobDataMap);
//获取REMINDER_WAKEUP_TIME,
String REMINDER_WAKEUP_TIME=Constants.REMINDER_WAKEUP_TIME;
siteConfigService=(SiteConfigService)ctx.getBean("siteConfigService");
SiteConfigForm siteConfigForm=siteConfigService.queryConfigName(REMINDER_WAKEUP_TIME);
//reminder催款提醒时间点
String defaultWakeTime=siteConfigForm.getConfigValue();
System.out.println("defaultWakeTime:"+defaultWakeTime);
//使用Cron表达式定义调度规则
CronTrigger cronTrigger = new CronTrigger("trigger", "remindGroup");
//lucy add 12/03 如果提醒时间取出为空
if(defaultWakeTime!=null)
{
CronExpression cexp = new CronExpression(defaultWakeTime);
cronTrigger.setCronExpression(cexp);

//在Scheduler容器中注册JobDetail与CronTrigger的对应关系。
scheduler.scheduleJob(jobDetail, cronTrigger);



//Payment2
HashMap fieldsMap2=new HashMap();
String REMIND_NAME2 = Constants.KEY_SECOND_REMIND_DAYS;
fieldsMap2.put("REMIND_NAME", REMIND_NAME2);
fieldsMap2.put("ctx", ctx);
JobDataMap jobDataMap2=new JobDataMap(fieldsMap2);
JobDetail jobDetail2 = new JobDetail("CashRemindJob2", "remindGroup2",
CashReminder.class);

jobDetail2.setJobDataMap(jobDataMap2);
CronTrigger cronTrigger2 = new CronTrigger("trigger2", "remindGroup2");
cronTrigger2.setCronExpression(cexp);
scheduler.scheduleJob(jobDetail2, cronTrigger2);



//Guarantee
HashMap gufieldsMap=new HashMap();
gufieldsMap.put("ctx", ctx);
JobDataMap gujobDataMap=new JobDataMap(gufieldsMap);
JobDetail gujobDetail = new JobDetail("GuReminderJob", "guremindGroup",
GuaranteeReminder.class);
gujobDetail.setJobDataMap(gujobDataMap);
CronTrigger gucronTrigger = new CronTrigger("trigger", "guremindGroup");
gucronTrigger.setCronExpression(cexp);
scheduler.scheduleJob(gujobDetail, gucronTrigger);
//启动调度
scheduler.start();
}
else
{
System.out.println("时间为空,可能网络配置中配置名称被修改");
}
} catch (Exception e) {
e.printStackTrace();
}

}
protected ContextLoader createContextLoader() {
return new ContextLoader();
}
}
上面是我写的代码,我设置的时间是0 10 15 * * ? 到了时间点程序会自己执行一次,自动发送邮件,而发现邮件有发重复的,我的CashReminder.class代码是没有问题的,因为有通过手动触发事件来进行测试,(手动触发CashReminder,一条邮件是不会重复发的)而有时自动发送邮件,是没有重复的,望高手赐教,很急,这问题都折磨我了两天了,实在不知道哪里有问题
...全文
216 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

67,516

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧