如果未注入其组件类,则不会执行Spring调度程序
突然,调度程序停止工作,它只在调度程序被注入另一个组件时工作 如果它没有被注入,它甚至不会出现在IntelliJ Endpoints->Beans->Application上,因此触发器不会被执行,因为我猜bean没有被实例化。在这种情况下,不会实例化ProjectFormTriggerExecutor、ProjectTriggerExecutor、DistrictTriggerExecutor和Area Monitoring FragmentTriggerExecutor bean的目的是异步触发PostgreSQL触发器,还有一个触发器在满足条件时只设置markedForUpdate=true 当我将它虚拟注入另一个组件时,一切都正常 为什么会这样?是否有一些spring配置被我的团队中的某个人更改为不实例化未注入的bean或其他东西如果未注入其组件类,则不会执行Spring调度程序,spring,kotlin,Spring,Kotlin,突然,调度程序停止工作,它只在调度程序被注入另一个组件时工作 如果它没有被注入,它甚至不会出现在IntelliJ Endpoints->Beans->Application上,因此触发器不会被执行,因为我猜bean没有被实例化。在这种情况下,不会实例化ProjectFormTriggerExecutor、ProjectTriggerExecutor、DistrictTriggerExecutor和Area Monitoring FragmentTriggerExecutor bean的目的是异步
@file:Suppress("JpaQlInspection")
package com.gilbertjolly.ulsi.mgmt.application
import com.gilbertjolly.lib.crud.CrudEntity
import com.gilbertjolly.lib.crud.RecalculationPeriod
import com.gilbertjolly.lib.crud.request_util.newTransaction
import com.gilbertjolly.ulsi.mgmt.feature.model.country.District
import com.gilbertjolly.ulsi.mgmt.feature.model.metrics.entities.AreaMonitoringFragment
import com.gilbertjolly.ulsi.mgmt.feature.model.monitoring.forms.ProjectForm
import com.gilbertjolly.ulsi.mgmt.feature.model.org.Project
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.core.env.Environment
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
import org.springframework.transaction.PlatformTransactionManager
import java.time.Instant
import java.util.logging.Logger
import javax.persistence.EntityManager
import kotlin.reflect.KClass
@Component
abstract class ScheduledTriggerExecutor {
@Autowired
lateinit var txManager: PlatformTransactionManager
@Autowired
lateinit var entityManager: EntityManager
val recalculationPeriod: RecalculationPeriod get() = RecalculationPeriod.EVERY_MINUTE
val logger = Logger.getLogger("TriggerRecalculationLog")!!
abstract val entityClass: KClass<out CrudEntity>
fun executeTrigger() {
val startMilliseconds = Instant.now().toEpochMilli()
txManager.newTransaction {
entityManager.createQuery("UPDATE ${entityClass.simpleName} SET markedForUpdate = FALSE WHERE markedForUpdate = TRUE")
.executeUpdate()
}
val triggerExecutionMilliseconds = Instant.now().minusMillis(startMilliseconds).toEpochMilli()
if (triggerExecutionMilliseconds > 5000) logger.warning("Trigger execution of ${entityClass.simpleName} took ${triggerExecutionMilliseconds / 1000.0f}")
}
}
@Component
class TriggerScheduler(val env: Environment,
val txManager: PlatformTransactionManager,
val scheduledTriggerExecutors: List<ScheduledTriggerExecutor>) {
@Scheduled(cron = "0 1 1 * * ?")
fun everyNightAt101() {
performRecalculation(RecalculationPeriod.AT_NIGHT)
}
@Scheduled(cron = "0 1 * * * ?")
fun everyHour() {
performRecalculation(RecalculationPeriod.HOURLY)
}
@Scheduled(cron = "0 0/30 * * * ?")
fun every30Minutes() {
performRecalculation(RecalculationPeriod.EVERY_HALF_AN_HOUR)
}
@Scheduled(cron = "0 0/2 * * * ?")
fun every2Minutes() {
performRecalculation(RecalculationPeriod.EVERY_TWO_MINUTES)
}
@Scheduled(cron = "0 0/1 * * * ?")
fun everyMinute() {
performRecalculation(RecalculationPeriod.EVERY_MINUTE)
}
fun performRecalculation(recalculationPeriod: RecalculationPeriod) {
scheduledTriggerExecutors
.filter { it.recalculationPeriod == recalculationPeriod }
.forEach { it.executeTrigger() }
}
}
@Component
class ProjectFormTriggerExecutor : ScheduledTriggerExecutor() {
override val entityClass = ProjectForm::class
}
@Component
class ProjectTriggerExecutor : ScheduledTriggerExecutor() {
override val entityClass = Project::class
}
@Component
class DistrictTriggerExecutor : ScheduledTriggerExecutor() {
override val entityClass = District::class
}
@Component
class AreaMonitoringFragmentTriggerExecutor : ScheduledTriggerExecutor() {
override val entityClass = AreaMonitoringFragment::class
}
@文件:抑制(“JpaQlInspection”)
包com.gilbertjolly.ulsi.mgmt.application
导入com.gilbertjolly.lib.crud.CrudEntity
导入com.gilbertjolly.lib.crud.RecomationPeriod
导入com.gilbertjolly.lib.crud.request_util.newTransaction
导入com.gilbertjolly.ulsi.mgmt.feature.model.country.District
导入com.gilbertjolly.ulsi.mgmt.feature.model.metrics.entities.AreaMonitoringFragment
导入com.gilbertjolly.ulsi.mgmt.feature.model.monitoring.forms.ProjectForm
导入com.gilbertjolly.ulsi.mgmt.feature.model.org.Project
导入org.springframework.beans.factory.annotation.Autowired
导入org.springframework.core.env.env.Environment
导入org.springframework.scheduling.annotation.Scheduled
导入org.springframework.stereotype.Component
导入org.springframework.transaction.PlatformTransactionManager
导入java.time.Instant
导入java.util.logging.Logger
导入javax.persistence.EntityManager
导入kotlin.reflect.KClass
@组成部分
抽象类ScheduledTriggereExecutor{
@自动连线
lateinit var txManager:PlatformTransactionManager
@自动连线
lateinit var entityManager:entityManager
val RecreactionPeriod:RecreactionPeriod get()=RecreactionPeriod.EVERY_分钟
val logger=logger.getLogger(“TriggerRecomationLog”)!!
抽象val实体类:KClass
fun executeTrigger(){
val startMilliseconds=Instant.now().toEpochMilli()
txManager.newTransaction{
entityManager.createQuery(“更新${entityClass.simpleName}设置markedForUpdate=FALSE,其中markedForUpdate=TRUE”)
.executeUpdate()
}
val triggerexecutionmillseconds=Instant.now()
if(TriggerExecutionMillionSeconds>5000)logger.warning(“触发执行${entityClass.simpleName}花费了${TriggerExecutionMillionSeconds/1000.0f}”)
}
}
@组成部分
类TriggerScheduler(val env:Environment,
val txManager:PlatformTransactionManager,
val scheduledTriggerExecutors:列表){
@已计划(cron=“0 1**?”)
快乐每夜101(){
执行计算(夜间重新计算时段)
}
@已计划(cron=“0 1***?”)
快乐每小时{
性能计算(重新计算周期。每小时)
}
@计划(cron=“0/30***?”)
每30分钟玩一次{
执行计算(每半小时重新计算一次)
}
@已计划(cron=“0/2***?”)
每2分钟一次{
执行计算(每两分钟重新计算一次)
}
@已计划(cron=“0/1***?”)
快乐每分钟(){
执行计算(每分钟重新计算一次)
}
乐趣性能计算(重新计算期间:重新计算期间){
调度触发器执行器
.filter{it.RecreactionPeriod==RecreactionPeriod}
.forEach{it.executeTrigger()}
}
}
@组成部分
类ProjectFormTriggereExecutor:ScheduledTriggereExecutor(){
重写val entityClass=ProjectForm::class
}
@组成部分
类ProjectTriggereExecutor:ScheduledTriggereExecutor(){
覆盖val entityClass=Project::class
}
@组成部分
类DistrictTriggereExecutor:ScheduledTriggereExecutor(){
覆盖val entityClass=District::class
}
@组成部分
类区域监视FragmentTriggereExecutor:ScheduledTriggereExecutor(){
重写val entityClass=AreaMonitoringFragment::class
}
可能在某个地方打开了全局惰性bean初始化。要快速测试是否正确,请尝试使用@Lazy(false)
注释TriggerScheduler
。您还可以在spring环境中的某个地方查找spring.main.lazy initialization=true
属性。是spring启动应用程序吗?你忘了添加@EnableScheduling
?我打开了它,正如沙皇评论的那样,这是延迟初始化Spring DI容器将如何创建TriggerScheduler?您有一个参数val scheduledTriggerExecutors:List
,我不知道DI将如何解决这个问题?您需要从TriggerScheduler中删除@Component,并在@Configuration类中使用@Bean方法来创建它。然后它应该按照预期的方式运行。非常感谢,这就是问题所在,一位队友设置了spring.main.lazy initialization=true。很高兴我能提供帮助:)