u013752422
代码尘埃
采纳率0%
2017-08-18 07:37 阅读 2.2k

quartz jobstore存储方式,schedule启动成功,但是没有执行job

1

scheduleManger执行成功,job以及trigger的相关信息也都有写在数据库中,但是根本就没有执行job,希望大家可以帮我解惑,谢谢

这是quartz.properties

 org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false

org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority: 5

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.dataSource = myDS

org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL = jdbc:mysql://localhost:3306/quartz?characterEncoding=utf-8&useSSL=false
org.quartz.dataSource.myDS.user = root
org.quartz.dataSource.myDS.password = 1234
org.quartz.dataSource.myDS.maxConnections = 30

这是 QuartzManager.java 文件

public class QuartzManager {
private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();

/**
 * 增加任务
 * @param jobClass 任务实现类
 * @param jobName 任务名称
 * @param interval 间隔时间
 * @param data 数据
 */
public static void addJob(Class<? extends Job> jobClass, String jobName, int interval, Map<String, Object> data) {
    try {
        Scheduler sched = gSchedulerFactory.getScheduler();

        JobDetail jobDetail = JobBuilder.newJob(jobClass)
                .withIdentity(jobName, "test3")//任务名称和组构成任务key
                .build();
        jobDetail.getJobDataMap().putAll(data);
        // 触发器
        SimpleTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(jobName, "ttest3")//触发器key
                .startAt(DateBuilder.futureDate(1, IntervalUnit.SECOND))
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(interval)
                        .repeatForever())
                .build();
        sched.scheduleJob(jobDetail, trigger);
        // 启动
        if (!sched.isShutdown()) {
            sched.start();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

        public static void main(String[] args) throws InterruptedException {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", "1");
    String jobname = "aaa";
    addJob(TestJob.class, "aaa", 1, map);

    try {
        Thread.sleep(10000);
        System.out.println("main end----------");
        System.exit(1);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

    这是TestJob.Java文件
    import org.quartz.Job;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class TestJob implements Job {

public TestJob(){

}

public void execute(JobExecutionContext jobexecutioncontext) throws JobExecutionException {
    String id = jobexecutioncontext.getJobDetail().getJobDataMap().getString("id");
    System.out.println("threadId: " + Thread.currentThread().getId() + ", id: " + id);
    try {
        System.out.println("=====================>睡眠开始时间:"+System.currentTimeMillis());
        Thread.sleep(10000);
        System.out.println("=====================>睡眠结束时间:"+System.currentTimeMillis());
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
    }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

相关推荐