有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用DropWizardian的java调度作业

我正在尝试使用dropwizard Sundian,但遇到资源问题。我不确定这是一个类路径问题还是我没有正确注册资源

这是我的应用程序类的运行方法:

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) 个答案

  1. # 1 楼答案

    @Jeyashree Narayanan,作业包不应如您所示在应用程序类中配置,它可以在yml文件中轻松完成。以下是简单步骤的说明:

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

    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 Sundail捆绑包

    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

  2. # 2 楼答案

    https://github.com/timmolter/XDropWizard查看一个工作示例。它使用带注释的作业。您需要在配置中添加包含注释作业的包名。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

  3. # 3 楼答案

    这似乎是一个类路径问题。 从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项目中使用日晷,我在jobs中定义了所有作业。xml,在中定义的日晷配置。yaml文件,并按如下方式启动它:

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