繁体   English   中英

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

[英]AWS lambda run Spring batch Job from from request handler with out using scheduler

我的应用程序使用带有批处理的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";
    }

}

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

暂无
暂无

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

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