Spring 开机运行两次定时作业,但在本地运行正常
Spring Boot runs scheduled job twice, But it works fine in the local
我的问题是计划的方法 "subscription" 被调用了两次。
这是我使用的模板。
https://github.com/JetBrains/gcp-samples/tree/master/google-appengine-sample
当我使用 $mvn spring-boot:run
在本地工作正常
但是我用$mvn appengine:deploy
调度被调用了两次
我的代码:
@Component
class ScheduledTasks {
@Scheduled(cron = "0 0 * * * *")
fun subscription() {
var hasBeenInitialized = false
val firebaseApps = FirebaseApp.getApps()
for (app in firebaseApps) {
if (app.name == FirebaseApp.DEFAULT_APP_NAME) {
hasBeenInitialized = true
}
}
if(!hasBeenInitialized) {
InitializeGCP()
}
var db = FirestoreClient.getFirestore()
val query = db.collection("subscription").get()
val querySnapshot = query.get()
val documents = querySnapshot.documents
for (document in documents) {
if (localTime.equals(document.getString("time"))) {
...post to my ChatBot Api...
}
}
}
}
}
我的app.yaml:
runtime: java
env: flex
runtime_config:
jdk: openjdk8
我检查我的 GCP。
我有两个相同版本的实例。
这有关系吗?
Check is there any two instance of your server? this cron job will be unique for only one server.
我的问题是计划的方法 "subscription" 被调用了两次。
这是我使用的模板。
https://github.com/JetBrains/gcp-samples/tree/master/google-appengine-sample
当我使用 $mvn spring-boot:run
在本地工作正常
但是我用$mvn appengine:deploy
调度被调用了两次
我的代码:
@Component
class ScheduledTasks {
@Scheduled(cron = "0 0 * * * *")
fun subscription() {
var hasBeenInitialized = false
val firebaseApps = FirebaseApp.getApps()
for (app in firebaseApps) {
if (app.name == FirebaseApp.DEFAULT_APP_NAME) {
hasBeenInitialized = true
}
}
if(!hasBeenInitialized) {
InitializeGCP()
}
var db = FirestoreClient.getFirestore()
val query = db.collection("subscription").get()
val querySnapshot = query.get()
val documents = querySnapshot.documents
for (document in documents) {
if (localTime.equals(document.getString("time"))) {
...post to my ChatBot Api...
}
}
}
}
}
我的app.yaml:
runtime: java
env: flex
runtime_config:
jdk: openjdk8
我检查我的 GCP。
我有两个相同版本的实例。
这有关系吗?
Check is there any two instance of your server? this cron job will be unique for only one server.