簡體   English   中英

用Quartz調度作業

[英]scheduling job with Quartz

我曾使用Quartz運行該作業,使用Spring Restful Web Service每1分鍾自動運行一次,但發現了問題。

日志:

17:24:00,016 INFO  [stdout] (DefaultQuartzScheduler_Worker-6) Hello Letter Printing Timer Quartz! Thu Dec 21 17:24:00 ICT 2017

17:24:00,016 INFO  [stdout] (DefaultQuartzScheduler_Worker-6) ini controller testing

17:24:00,016 INFO  [stdout] (DefaultQuartzScheduler_Worker-6) ----controller-----

17:24:00,016 ERROR [org.quartz.core.JobRunShell] (DefaultQuartzScheduler_Worker-6) Job DEFAULT.6da64b5bd2ee-929cc152-4d2a-4f06-8149-273dd89c6e36 threw an unhandled Exception: : java.lang.NullPointerException
    at com.prudential.letter.printing.controller.ModifyFileController.modifyFileForGenerateUniqueID(ModifyFileController.java:20) [classes:]
    at com.prudential.letter.printing.HelloJob.execute(HelloJob.java:20) [classes:]
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202) [quartz-2.2.1.jar:]
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.2.1.jar:]

17:24:00,016 ERROR [org.quartz.core.ErrorLogger] (DefaultQuartzScheduler_Worker-6) Job (DEFAULT.6da64b5bd2ee-929cc152-4d2a-4f06-8149-273dd89c6e36 threw an exception.: org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.NullPointerException]
    at org.quartz.core.JobRunShell.run(JobRunShell.java:213) [quartz-2.2.1.jar:]
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.2.1.jar:]
Caused by: java.lang.NullPointerException
    at com.prudential.letter.printing.controller.ModifyFileController.modifyFileForGenerateUniqueID(ModifyFileController.java:20) [classes:]
    at com.prudential.letter.printing.HelloJob.execute(HelloJob.java:20) [classes:]
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202) [quartz-2.2.1.jar:]
    ... 1 more

我在每個控制器,服務工具和dao工具中都使用println,但是從工作到控制器為止。 從控制器到服務時,我發現了一個錯誤。

我的代碼:

HelloJob.java

public class HelloJob implements Job {

    TestingController testing = new TestingController();
    ModifyFileController modify = new ModifyFileController();

    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("Hello Letter Printing Timer Quartz! " + new Date());
        testing.getData();
        modify.modifyFileForGenerateUniqueID(); // to controller
    }
}

我的控制器:

@RestController
@RequestMapping(value = "/")
public class ModifyFileController {

    @Autowired
    private FileService fileService;

    @PostMapping("modify")
    public String modifyFileForGenerateUniqueID() {
        System.out.println("----controller-----");
        return fileService.modify();
    }

}

接口:

public interface FileService {
    public String modify();
}

服務展示:

@Service
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class FileServiceImpl implements FileService {

    private static final Logger log = LoggerFactory
            .getLogger(FileServiceImpl.class);

    @Autowired
    private FileDao fileDao;

    @Autowired
    ConfigProperties configProperties;

    @Override
    @Transactional
    public String modify() {
        System.out.println("----service impl-----");
        String data = configProperties.getSeparator();
        String[] result = data.split(";");
        String timeout = configProperties.getTimeout();
        Integer i = Integer.parseInt(timeout);
        return fileDao.modify(result, i);
    }

}

道:

public interface FileDao {
    public String modify(String[] result, Integer timeout);
}

Dao Impl:

@Repository
public class FileDaoImpl implements FileDao {

    @Transactional
    @Override
    public String modify(String[] delimeter, Integer timeout) {
        // the content here
        return "Success";
    }
}

石英監聽器:

@WebListener
public class QuartzListener extends QuartzInitializerListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        super.contextInitialized(sce);
        ServletContext ctx = sce.getServletContext();
        StdSchedulerFactory factory = (StdSchedulerFactory) ctx.getAttribute(QUARTZ_FACTORY_KEY);
        try {
            Scheduler scheduler = factory.getScheduler();
            JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).build();
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("simple").withSchedule(
                    CronScheduleBuilder.cronSchedule("0 0/1 * 1/1 * ? *")).startNow().build();
            scheduler.scheduleJob(jobDetail, trigger);
            scheduler.start();
        } catch (Exception e) {
            ctx.log("There was an error scheduling the job.", e);
        }
    }
}

為什么正在運行的工作不提供服務暗示? 只是停在控制器,然后錯誤? 謝謝

您不應該使用new關鍵字來初始化新的Controller。

此外,使用Spring Dependency Injection。

public class HelloJob implements Job {

    @Autowired
    private TestingController testing;

    @Autowired
    private ModifyFileController modify;

    // ...
}

問題在於ModifyFileController也依賴於FileService等。 嘗試以這種方式調用該對象時,該對象為null。

我必須在Job中添加一點,我認為在Service層而不是Controller層中調用方法是一種更好的做法。

您應該從心理上將作業視為與控制器處於同一應用程序級別。 您永遠不要像這樣從其他類中以編程方式調用控制器方法。 您應該在工作中直接與FileService進行交互。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM