但行好事
莫论前程❤

创建一个新线程时提示无数据源

  • 原因不同域,数据源需要初始化。
if (SessionContext.getSessionContext().getDataSource() == null) {
    initSessionContext();
}
    /**
     * 初始化数据源
     */
protected void initSessionContext(){
  String dataSourceName = "design";
  SessionContext.getSessionContext().setServletContext(QuartzManager.servletContext);
  SessionContext.getSessionContext().initDataSourceByName(dataSourceName);
}
package nc.util.foundation;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.quartz.CronScheduleBuilder;
import org.quartz.DateBuilder;
import org.quartz.DateBuilder.IntervalUnit;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.WebApplicationContext;

import bsh.This;
import nc.bs.eprk.WorkdayBO;
import nc.bs.foundation.util.Payment_noticeJob;
import nc.bs.foundation.util.SessionContext;
import nc.itf.eprk.Workday;

public class QuartzManager implements ApplicationContextAware{
    public int hover = 0;
    public int min = 0;
    private static  SchedulerFactory sf = new StdSchedulerFactory();
    private static Scheduler scheduler =null;
    private static Scheduler scheduler2 = null;
    private static Scheduler scheduler3 = null;
    private static Scheduler scheduler4 = null;
    private static Scheduler scheduler5 = null;
    public static ServletContext servletContext = null;
    static Trigger trigger =null;
    static Trigger trigger2 = null;
    static Trigger trigger3 = null;
    static Trigger trigger4 = null;
    static Trigger trigger5 = null;
    public static boolean a = true;
    private ApplicationContext app ;
    public static final Logger log =  Logger.getLogger(This.class);
    public  void add_job() throws SchedulerException {
        if(SessionContext.getSessionContext().getDataSource() == null){
            initSessionContext();
        }
        scheduler = sf.getScheduler();
        JobDetail job = JobBuilder.newJob(ChangeWorkDay.class).
            withIdentity("workDay", "workDay").build();
        WorkdayBO workday = new WorkdayBO();
        workday.updateJDBC(
       "update eprk_work_day_time set status = 1 where pk_workdaytime = '0001'");
        List<Map<String, String>> list= 
            workday.queryForJDBC("select * from eprk_work_day_time");
        hover = Integer.parseInt( list.get(0).get("hover"));
        min = Integer.parseInt( list.get(0).get("min"));
        trigger = (Trigger) TriggerBuilder.newTrigger()
            .withIdentity("trgger1","trggerGroup")
            .withSchedule(CronScheduleBuilder.dailyAtHourAndMinute(hover,min))
            .build();
        scheduler.scheduleJob(job, trigger);
        scheduler.start();

        log.info("自动日终正在待机,将在每天"+hover+"点"+min+"分开始自动切换");

    }
    public static void shut_down() throws SchedulerException, InterruptedException{
        scheduler.shutdown();
        if(scheduler2!=null){
            scheduler2.shutdown();
        }
        scheduler = sf.getScheduler();
        JobDetail job = JobBuilder.newJob(ChangeWorkDay.class)
            .withIdentity("workDay", "workDay").build();
        Workday workday = new WorkdayBO();
        List<Map<String, String>> list= 
            workday.queryForJDBC("select * from eprk_work_day_time");
        trigger = TriggerBuilder.newTrigger()
            .withIdentity("trgger1", "trggerGroup")
            .startAt(DateBuilder.futureDate(24, IntervalUnit.HOUR))
        .withSchedule(CronScheduleBuilder.dailyAtHourAndMinute(Integer.parseInt( list.get(0).get("hover")),Integer.parseInt( list.get(0).get("min")))).build();
        scheduler.scheduleJob(job, trigger);
        scheduler.start();

        scheduler2 = sf.getScheduler();
        JobDetail job2 = JobBuilder.newJob(CanEdit.class)
            .withIdentity("canEdit", "canEdit").build();
        trigger2 = TriggerBuilder.newTrigger().withIdentity("canEdit","canEdit")
            .startAt(DateBuilder.futureDate(24, IntervalUnit.HOUR)).build();
        scheduler2.scheduleJob(job2,trigger2);
        scheduler2.start();

    }


    protected void initSessionContext(){
        String dataSourceName = "design";
        SessionContext.getSessionContext().setServletContext(servletContext);
        SessionContext.getSessionContext().initDataSourceByName(dataSourceName);
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        servletContext = 
            ((WebApplicationContext) applicationContext).getServletContext();
    servletContext.setAttribute(
        WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
    }
}

赞(1) 打赏
未经允许不得转载:刘鹏博客 » 创建一个新线程时提示无数据源
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏