使用Quartz安排工作

问题描述 投票:0回答:2

我使用Quartz运行这项工作,每1分钟使用Spring restful web服务自动运行,但我发现了问题。

日志:

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实现中打印,但从作业直到控制器。从控制器到服务时,我发现了一个错误。

我的代码:

hello job.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();
}

服务impl:

@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);
    }

}

DAO:

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";
    }
}

Quartz Listener:

@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);
        }
    }
}

为什么正在运行的工作不去服务?只是停在控制器然后错误?谢谢

java spring quartz-scheduler
2个回答
0
投票

您不应该使用new关键字来初始化新的控制器。

此外,使用Spring Dependency Injection。

public class HelloJob implements Job {

    @Autowired
    private TestingController testing;

    @Autowired
    private ModifyFileController modify;

    // ...
}

问题是ModifyFileController也依赖于FileService等。当您尝试以这种方式调用它时,此对象为null。

我必须在Job中添加它,我认为在Service层而不是在Controller层中调用方法是更好的做法。


0
投票

您应该在心理上将工作视为与控制器处于相同的应用程序级别。你永远不应该以编程方式从其他类中调用控制器方法。您应该直接与作业中的FileService进行交互。

© www.soinside.com 2019 - 2024. All rights reserved.