AWS Lambda从请求处理程序运行Spring批处理作业,而无需使用调度程序

时间:2018-11-15 20:29:37

标签: java spring-boot aws-lambda spring-batch

我的应用程序使用批处理的spring boot并在aws lambda中对其进行测试,我想以main方法而不是通过Scheduler来运行作业。有可能这样做吗?

    @SpringBootApplication
@EnableAutoConfiguration
@EnableJpaRepositories("com.myrepo.repository")
@ComponentScan("com.myrepo")
@EnableScheduling
public class Main {

    private static final Logger LOG = LoggerFactory.getLogger(hMain.class);
    @Autowired
    JobLauncher launcher;

    @Autowired
    Job job;

    public static void main(String[] args) {

        try {
            LOG.info("Start of application - debt card notofication JOB");
            SpringApplication.run(BatchMain.class, args);

        } catch (Exception e) {
            LOG.error("Exception caught bathch Main, );
        }
    }
}

编辑-我写了下面的代码,但是在aws lambda函数内部不起作用

    @Scheduled(cron = "0/1 * * * * *")
public void performBatchOpertaion() {
    try {
        LOG.info("Scheduling Job and Launcher {}, {}", job, launcher);
        JobParameters params = new JobParametersBuilder()
                .addString(Constants.MYBATCH, String.valueOf(System.currentTimeMillis()))
                .toJobParameters();
        launcher.run(job, params);
    } catch (Exception e) {
        LOG.error("Unable to schedules ", e.getCause());

    }
}

 public static void startApp() {
        LOG.info("start batch job ");
        SpringApplication.run(Main.class);
        LOG.info("end batch job ");
    }

这是我的请求处理程序类,它调用Main类的statApp()

    --------------------------------------------------------
public class MyHandler implements RequestHandler<Map<String, Object>, String> {

    private static final Logger LOG = LoggerFactory.getLogger(MyHandler.class);

    @Autowired
    BatchMain main;

    @Override
    public String handleRequest(Map<String, Object> input, Context context) {
        LOG.info("Inside the handler request");
        BatchMain.startApp();
        LOG.info("End of handler request");
        return "End Of Application";
    }

}

1 个答案:

答案 0 :(得分:0)

handleRequest方法等待BatchMain完成。您可以使用Thread join。一旦句柄请求返回结果,Lambda执行引擎就会暂停主线程。在下一个事件中,它可能会完成之前暂停的任务