java - How do you use cron jobs using Elastic Beanstalk and Java?
问题描述
I want to run cron jobs and use the same code base. I found a few solutions, but they don't appear ideal. For example, with Heroku, you can add a Scheduler element and fill in the commands to run in a web page.
- http://blog.rotaready.com/scheduled-tasks-elastic-beanstalk-cron/
- It seems overly complicated for load-balanced instances.
- It makes use of
require('async')
in Node, but what would be a Java Spring Boot equivalent?
- https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features-managing-env-tiers.html
- There doesn't appear to be any security. Any one the net could access the /path to POST and execute the job, causing a denial-of-service attack.
- it mentions
cron.yaml
which doesn't make sense as the app is deployed via a WAR/ZIP file to a Tomcat instance (Spring Boot). - It mentions Amazon DynamoDB, which we don't use. We use MySQL.
- It doesn't specify whether the load balancer connection draining timeout is in effect for these jobs (10s).
- It mentions "Worker Configuration card on the Configuration page in the environment management console" but there is no Worker Configuration card under Configuration page.
- Running a cron job in Elastic Beanstalk
- For Python/Django - uses
cron.yaml
.
- For Python/Django - uses
- I thought of just having a dedicated EC2 instance, but how can I deploy the latest code changes there?
This may also belong on SoftwareEngineering.StackExchange.
解决方案
使用其他 AWS 系统有一种简单的方法可以做到这一点。
您可以使用 CloudWatch 设置计划事件 ( https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/WhatIsCloudWatchEvents.html )。您可以设置规则以按既定时间表设置事件。
然后,您至少有两个选择:
将事件设置为发布 SNS 消息并使用该 SNS 在您的服务器上调用 Web 挂钩。关于如何执行此操作的许多示例,但您必须确保检查签名以确保从签名的 SNS 调用 Web API。但这将使用公共 API,可能不是您喜欢的。
设置事件以发布 SQS 消息。然后设置一个弹性 beanstalk worker 来处理 SQS 消息,或者只是在你的主服务器上运行一个后台脚本,这基本上是一个无限循环轮询 SQS 的工作。
不确定您对这些系统的熟悉程度,因此不确定是否清楚我在说什么,但没有办法给出详细的解决方案,所以希望这足以给您提供想法。
推荐阅读
- css - Bootstrap 4 Datepicker CSS 被 Angular 6 破坏
- c++ - 从函数c ++中的lambda函数返回一个值
- c# - s => s == 'w' - 帮助我理解这行代码
- javascript - OpenLayers5:hitTolerance 似乎不适用于 WMS 层
- c++ - 在 Raspberry Pi 上编译 JUMPCoin 钱包时出错(leveldb/libmemenv.a:添加符号时出错:)
- java - IntelliJ:查看代码中的属性值
- r - R散点图两组一条回归线
- javascript - 将猫头鹰轮播包含到 npm 项目中。一路的
- python - 如何修复'google.cloud.vision'在python3中没有属性'Client'?
- cucumber - 如何在每个场景之前运行后台