使用dropwizard-sundial调度作业

时间:2015-04-07 15:13:09

标签: java job-scheduling

我正在尝试使用dropwizard-sundial并且遇到资源问题。我不确定它是否是类路径问题,或者我是否未能正确注册资源。

这是我的申请类'运行方法:

public void run(DataLoaderApplicationConfiguration configuration, Environment environment) throws Exception {
    logger.info("Started DataLoader Application");
    final String template = configuration.getTemplate();
    environment.healthChecks().register("TemplateHealth", new TemplateHealthCheck(template));
    // JOBS
    environment.jersey().packages("com.tradier.dataloader.jobs");
}

我在运行时遇到以下错误:

INFO  [2015-04-07 15:00:19,737] com.xeiam.sundial.plugins.AnnotationJobTriggerPlugin: Loading annotated jobs from com.tradier.dataloader.jobs.

[WARNING] 
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.RuntimeException: Unexpected problem: No resource for com/tradier/dataloader/jobs
at org.quartz.classloading.CascadingClassLoadHelper.getJobClasses(CascadingClassLoadHelper.java:217)
at com.xeiam.sundial.plugins.AnnotationJobTriggerPlugin.start(AnnotationJobTriggerPlugin.java:72)
at org.quartz.QuartzScheduler.startPlugins(QuartzScheduler.java:1102)
at org.quartz.QuartzScheduler.start(QuartzScheduler.java:211)
at com.xeiam.sundial.SundialJobScheduler.startScheduler(SundialJobScheduler.java:102)

3 个答案:

答案 0 :(得分:0)

(我宁愿将此作为评论发布,但似乎我需要50个声誉才能做到这一点)

你能解决这个问题吗?它似乎是一个类路径问题。 来自https://github.com/timmolter/Sundial/blob/develop/src/main/java/com/xeiam/sundial/SundialJobScheduler.java#L102

public static void startScheduler(int threadPoolSize, String annotatedJobsPackageName) {

try {
  createScheduler(threadPoolSize, annotatedJobsPackageName);
  getScheduler().start(); // ---> Line 102
} catch (SchedulerException e) {
  logger.error("COULD NOT START SUNDIAL SCHEDULER!!!", e);
  throw new SchedulerStartupException(e);
}

我也在我的dropwizard项目中使用Sundial,我在job.xml中定义了所有作业,在.yaml文件中定义了日期配置,并按如下方式启动:

     SundialJobScheduler.startScheduler();
     SundialManager sm = new SundialManager(config.getSundialConfiguration(),environment); 
     environment.lifecycle().manage(sm);

答案 1 :(得分:0)

查看https://github.com/timmolter/XDropWizard处的工作示例。它使用带注释的作业。您需要在config.yaml文件中添加包含带注释的作业的包名称,如下所示:

sundial:
  thread-pool-size: 5
  shutdown-on-unload: true
  wait-on-shutdown: false
  start-delay-seconds: 0
  start-scheduler-on-load: true
  global-lock-on-load: false
  annotated-jobs-package-name: org.knowm.xdropwizard.jobs

如果您仍然收到例外情况,请在以下网址发送报告:https://github.com/timmolter/dropwizard-sundial/issues

答案 2 :(得分:0)

@Jeyashree Narayanan,不应如您所显示的那样在应用程序类中配置Jobs包,可以在yml文件中轻松完成。这是简单步骤的解释:

第1步:在yml文件和Configuration类中进行配置

sundial:
  thread-pool-size: 10
  shutdown-on-unload: true
  start-delay-seconds: 0
  start-scheduler-on-load: true
  global-lock-on-load: false
  annotated-jobs-package-name: com.tradier.dataloader.jobs
  tasks: [startjob, stopjob]

配置类:

@JsonIgnoreProperties(ignoreUnknown = true)
public class DropwizardSundialConfiguration extends Configuration {

    @Valid
    @NotNull
    public SundialConfiguration sundialConfiguration = new SundialConfiguration();

    @JsonProperty("sundial")
    public SundialConfiguration getSundialConfiguration() {
        return sundialConfiguration;
    }
}

第2步:在应用程序类中添加并配置dropwizard-sundial捆绑包。

public class DropwizardSundialApplication extends Application<DropwizardSundialConfiguration> {

    private static final Logger logger = LoggerFactory.getLogger(DropwizardSundialApplication.class);

 public static void main(String[] args) throws Exception {
  new DropwizardSundialApplication().run("server", args[0]);
 }

    @Override
    public void initialize(Bootstrap<DropwizardSundialConfiguration> b) {
  b.addBundle(new SundialBundle<DropwizardSundialConfiguration>() {

   @Override
   public SundialConfiguration getSundialConfiguration(DropwizardSundialConfiguration configuration) {
    return configuration.getSundialConfiguration();
   }
  });
 }
}

第3步:添加所需的作业类别。 这是一个示例Cron作业类:

@CronTrigger(cron = "0 19 13 * * ?")
public class CronJob extends Job {

    private static final Logger logger = LoggerFactory.getLogger(CronJob.class);
    @Override
    public void doRun() throws JobInterruptException {
        logger.info("Hello from Cron Job");
    }
}

我还写了一篇博客文章和一个有效的应用程序,可以通过以下步骤在GitHub上找到它们。请检查:http://softwaredevelopercentral.blogspot.com/2019/05/dropwizard-sundial-scheduler-tutorial.html