简体   繁体   English

如何在java中的石英调度中将数组作为参数传递

[英]How to pass an array as a parameter in quartz scheduling in java

I want to pass a string array to the class which is being scheduled and called by the Scheduler object. 我想将一个字符串数组传递给正在调度和调用Scheduler对象的类。 The scheduler class is as follows : 调度程序类如下:

 String stuff[]={"",""}
 JobDetail job = JobBuilder.newJob(HelloJob.class)
                 .withIdentity("job", "group1").build();
 job.getJobDataMap().put(HelloJob.data, stuff);

Trigger trigger = TriggerBuilder
                   .newTrigger()
                   .withSchedule(
                    CronScheduleBuilder.cronSchedule(time))
                   .build();
    //schedule it
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
System.out.println("Before starting");
scheduler.start();
scheduler.scheduleJob(job, trigger);

passing the array to : 将数组传递给:

public class HelloJob implements Job {
    static String data[];
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("Job");  
    }
}

the job interface : 工作界面:

     public interface Job {
             void execute(JobExecutionContext context)
              throws JobExecutionException;
      }

Is there any solution to pass the array? 有没有传递数组的解决方案?

Yes, there is a solution. 是的,有一个解决方案。

  1. Delete static String data[] in HelloJob . 删除HelloJob static String data[] Avoid static (particularly arrays) as much as possible. 尽可能避免静电(特别是阵列)。

  2. When you call put on the JobDataMap you need to provide a key and a value. 当你在JobDataMap上调用put时,你需要提供一个键和一个值。 The value is your array, but you need to pick a key. 值是您的数组,但您需要选择一个键。 As it was, you were using the static String.data array reference which, in your code, was always null (not a good idea). 实际上,您使用的是静态String.data数组引用,在您的代码中,该引用始终为null (不是一个好主意)。 You need to define a suitable, well-known key. 您需要定义一个合适的,众所周知的密钥。 There are a number of options available to you, but a simple solution is... 您可以使用多种选项,但一个简单的解决方案是......

  3. Add static final String DATA_ARRAY_KEY = "data_array"; 添加static final String DATA_ARRAY_KEY = "data_array"; to HelloJob . HelloJob Despite what I said in #1 about avoiding statics, this one is safe because it's final and, unlike an array, a String cannot ever be modified. 尽管我在#1中说过避免使用静态,但这个是安全的,因为它是final并且与数组不同, String永远不会被修改。 We're using this as a constant , your well-known key. 我们将此作为常量 ,您众所周知的关键。 Now your put() call should look like: put(HelloJob.DATA_ARRAY_KEY, stuff) 现在你的put()调用应该是这样的: put(HelloJob.DATA_ARRAY_KEY, stuff)

  4. Now you need to get the array out when the Job runs its execute method. 现在,当Job运行其execute方法时,您需要将数组取出。 In that method, you can access your JobDataMap by calling context.getMergedJobDataMap() and from that you can get(DATA_ARRAY_KEY) and cast the result back to String[] . 在该方法中,您可以通过调用context.getMergedJobDataMap()来访问JobDataMap,然后从中get(DATA_ARRAY_KEY)并将结果转换回String[]

That should solve your original question. 这应该解决你原来的问题。 However, you didn't give an example of what was going to be in your array. 但是,您没有举例说明数组中的内容。 Depending on how you intended to use it, it might be a better solution to use the specialized methods on JobDataMap like putAsString(String key, String value) and getAsString(String key) . 根据您打算如何使用它,使用JobDataMap上的专用方法putAsString(String key, String value)putAsString(String key, String value)getAsString(String key)可能是更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM