diff --git a/docs/spring-boot-bridge.md b/docs/spring-boot-bridge.md index b053cbc..d118624 100644 --- a/docs/spring-boot-bridge.md +++ b/docs/spring-boot-bridge.md @@ -13,6 +13,8 @@ The bridge also configures: - `SpringProcessApplicationLifecycleEventPublisher`, which projects named lifecycle phases as synchronous Spring application events. - `SpringProcessApplicationLifecycleDriver`, which drives startup from `ApplicationStartedEvent` and shutdown through Spring's `SmartLifecycle` callback. +In a context hierarchy, the context that defines the `ProcessApplicationLifecycle` owns its driver, event publisher, and startup validation. A child that inherits that lifecycle shares the parent's coordinator. The driver handles `ApplicationStartedEvent` from its own context only. + The driver uses `Int.MAX_VALUE - 1000` as its Spring `SmartLifecycle` phase. This leaves room for application components that must stop before or after the bridge. ## Startup and shutdown @@ -29,6 +31,8 @@ lifecycle.transitionTo( The lifecycle expands that request into all missing named startup transitions in order. During context shutdown, the driver requests `STOPPED`, which transitions through `STOPPING` first. This lets the lifecycle collect shutdown failures while still invoking every shutdown participant. If a startup participant throws an exception, the driver requests `STOPPED` immediately so participants can release partially started resources. Spring Boot reports the original startup exception, with any rollback failure attached as a suppressed exception. +Calling `startLifecycle()` again while the lifecycle is running has no effect. Once shutdown has begun, the driver rejects further calls to `startLifecycle()`. Calling Spring's `context.start()` after `context.stop()` leaves the process lifecycle at `STOPPED`. Start a fresh Spring application context to get a new lifecycle. + ### Startup validation After `ApplicationStartedEvent` has been dispatched and before `ApplicationReadyEvent`, the bridge checks the lifecycle in an early `ApplicationRunner`. diff --git a/src/main/kotlin/dev/bpmcrafters/processengineapi/springboot/lifecycle/ProcessApplicationLifecycleAutoConfiguration.kt b/src/main/kotlin/dev/bpmcrafters/processengineapi/springboot/lifecycle/ProcessApplicationLifecycleAutoConfiguration.kt index ebb80ce..b145c54 100644 --- a/src/main/kotlin/dev/bpmcrafters/processengineapi/springboot/lifecycle/ProcessApplicationLifecycleAutoConfiguration.kt +++ b/src/main/kotlin/dev/bpmcrafters/processengineapi/springboot/lifecycle/ProcessApplicationLifecycleAutoConfiguration.kt @@ -9,7 +9,9 @@ import org.springframework.beans.factory.ObjectProvider import org.springframework.boot.ApplicationArguments import org.springframework.boot.ApplicationRunner import org.springframework.boot.autoconfigure.AutoConfiguration +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean +import org.springframework.boot.autoconfigure.condition.SearchStrategy import org.springframework.context.ApplicationContext import org.springframework.context.ApplicationEventPublisher import org.springframework.context.annotation.Bean @@ -42,6 +44,7 @@ class ProcessApplicationLifecycleAutoConfiguration { * Projects named lifecycle phases as Spring application events. */ @Bean + @ConditionalOnBean(value = [ProcessApplicationLifecycle::class], search = SearchStrategy.CURRENT) fun springProcessApplicationLifecycleEventPublisher( lifecycle: ProcessApplicationLifecycle, applicationEventPublisher: ApplicationEventPublisher @@ -52,6 +55,7 @@ class ProcessApplicationLifecycleAutoConfiguration { * Drives lifecycle startup and shutdown when task subscriptions are supported. */ @Bean + @ConditionalOnBean(value = [ProcessApplicationLifecycle::class], search = SearchStrategy.CURRENT) fun springProcessApplicationLifecycleDriver( lifecycle: ProcessApplicationLifecycle, taskSubscriptionApiProvider: ObjectProvider, @@ -63,6 +67,7 @@ class ProcessApplicationLifecycleAutoConfiguration { * Verifies that the lifecycle is active before readiness when a subscription API is available. */ @Bean + @ConditionalOnBean(value = [ProcessApplicationLifecycle::class], search = SearchStrategy.CURRENT) fun processApplicationLifecycleStartupCheck( lifecycle: ProcessApplicationLifecycle, applicationContext: ApplicationContext diff --git a/src/main/kotlin/dev/bpmcrafters/processengineapi/springboot/lifecycle/SpringProcessApplicationLifecycleDriver.kt b/src/main/kotlin/dev/bpmcrafters/processengineapi/springboot/lifecycle/SpringProcessApplicationLifecycleDriver.kt index e36a3f7..a96ecce 100644 --- a/src/main/kotlin/dev/bpmcrafters/processengineapi/springboot/lifecycle/SpringProcessApplicationLifecycleDriver.kt +++ b/src/main/kotlin/dev/bpmcrafters/processengineapi/springboot/lifecycle/SpringProcessApplicationLifecycleDriver.kt @@ -6,6 +6,8 @@ import dev.bpmcrafters.processengineapi.lifecycle.TaskSubscriptionRegistrationCo import dev.bpmcrafters.processengineapi.task.TaskSubscriptionApi import org.springframework.beans.factory.ObjectProvider import org.springframework.boot.context.event.ApplicationStartedEvent +import org.springframework.context.ApplicationContext +import org.springframework.context.ApplicationContextAware import org.springframework.context.ApplicationListener import org.springframework.context.SmartLifecycle @@ -17,7 +19,7 @@ import org.springframework.context.SmartLifecycle class SpringProcessApplicationLifecycleDriver( private val lifecycle: ProcessApplicationLifecycle, private val taskSubscriptionApiProvider: ObjectProvider -) : ApplicationListener, SmartLifecycle { +) : ApplicationListener, ApplicationContextAware, SmartLifecycle { companion object { @@ -27,17 +29,28 @@ class SpringProcessApplicationLifecycleDriver( @Volatile private var started = false + private var owningContext: ApplicationContext? = null + + override fun setApplicationContext(applicationContext: ApplicationContext) { + owningContext = applicationContext + } + override fun start() { } override fun onApplicationEvent(event: ApplicationStartedEvent) { - startLifecycle() + if (event.applicationContext === owningContext) { + startLifecycle() + } } override fun supportsAsyncExecution(): Boolean = false @Synchronized fun startLifecycle() { + check(lifecycle.getPhase() < ProcessApplicationPhase.STOPPING) { + "The process application lifecycle has already entered shutdown. Start a fresh lifecycle or application context." + } if (started) { return } diff --git a/src/test/kotlin/dev/bpmcrafters/processengineapi/springboot/lifecycle/SpringProcessApplicationLifecycleIntegrationTest.kt b/src/test/kotlin/dev/bpmcrafters/processengineapi/springboot/lifecycle/SpringProcessApplicationLifecycleIntegrationTest.kt index 85a09f9..ef91f80 100644 --- a/src/test/kotlin/dev/bpmcrafters/processengineapi/springboot/lifecycle/SpringProcessApplicationLifecycleIntegrationTest.kt +++ b/src/test/kotlin/dev/bpmcrafters/processengineapi/springboot/lifecycle/SpringProcessApplicationLifecycleIntegrationTest.kt @@ -30,6 +30,34 @@ import java.util.concurrent.CompletableFuture internal class SpringProcessApplicationLifecycleIntegrationTest { + @Test + fun `repeated startup is idempotent while running and rejected after shutdown`() { + SpringApplicationBuilder(TestApplication::class.java) + .web(WebApplicationType.NONE) + .logStartupInfo(false) + .run().use { context -> + val lifecycle = context.getBean() + val driver = context.getBean() + val entries = context.getBean().entries + val startupEntries = entries.toList() + + driver.startLifecycle() + + assertThat(entries).containsExactlyElementsOf(startupEntries) + assertThat(driver.isRunning).isTrue() + + context.stop() + val shutdownEntries = entries.toList() + context.start() + assertThatIllegalStateException().isThrownBy { driver.startLifecycle() } + .withMessageContaining("fresh lifecycle or application context") + + assertThat(entries).containsExactlyElementsOf(shutdownEntries) + assertThat(lifecycle.getPhase()).isEqualTo(ProcessApplicationPhase.STOPPED) + assertThat(driver.isRunning).isFalse() + } + } + @Test fun `reaches stopped when a stopping participant fails`() { val lifecycle = DefaultProcessApplicationLifecycle() diff --git a/src/test/kotlin/dev/bpmcrafters/processengineapi/springboot/startup/SpringProcessApplicationLifecycleStartupTest.kt b/src/test/kotlin/dev/bpmcrafters/processengineapi/springboot/startup/SpringProcessApplicationLifecycleStartupTest.kt index 9f8a2c8..3160b36 100644 --- a/src/test/kotlin/dev/bpmcrafters/processengineapi/springboot/startup/SpringProcessApplicationLifecycleStartupTest.kt +++ b/src/test/kotlin/dev/bpmcrafters/processengineapi/springboot/startup/SpringProcessApplicationLifecycleStartupTest.kt @@ -2,42 +2,85 @@ package dev.bpmcrafters.processengineapi.springboot.startup import dev.bpmcrafters.processengineapi.lifecycle.ProcessApplicationLifecycle import dev.bpmcrafters.processengineapi.lifecycle.ProcessApplicationPhase -import dev.bpmcrafters.processengineapi.springboot.lifecycle.ProcessApplicationLifecycleAutoConfiguration -import dev.bpmcrafters.processengineapi.springboot.lifecycle.TaskSubscriptionRegistrationEvent -import dev.bpmcrafters.processengineapi.springboot.lifecycle.TaskSubscriptionsRegisteredEvent +import dev.bpmcrafters.processengineapi.springboot.lifecycle.* import dev.bpmcrafters.processengineapi.task.TaskSubscriptionApi import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatExceptionOfType import org.junit.jupiter.api.Test -import org.mockito.kotlin.any -import org.mockito.kotlin.eq -import org.mockito.kotlin.mock -import org.mockito.kotlin.verify -import org.mockito.kotlin.whenever +import org.mockito.kotlin.* +import org.springframework.beans.factory.getBean import org.springframework.boot.WebApplicationType import org.springframework.boot.autoconfigure.AutoConfiguration import org.springframework.boot.autoconfigure.ImportAutoConfiguration import org.springframework.boot.builder.SpringApplicationBuilder import org.springframework.boot.context.event.ApplicationStartedEvent +import org.springframework.context.annotation.AnnotationConfigApplicationContext import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.context.event.EventListener import org.springframework.context.event.SimpleApplicationEventMulticaster import org.springframework.context.support.AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME -import java.util.ArrayDeque +import java.util.* internal class SpringProcessApplicationLifecycleStartupTest { + @Test + fun `child startup and shutdown leave the inherited lifecycle running in the parent`() { + SpringApplicationBuilder(LateAdapterApplication::class.java) + .web(WebApplicationType.NONE) + .logStartupInfo(false) + .run().use { parent -> + val lifecycle = parent.getBean() + val subscriptions = parent.getBean() + val driver = parent.getBean() + + SpringApplicationBuilder(BridgeOnlyApplication::class.java) + .parent(parent) + .web(WebApplicationType.NONE) + .logStartupInfo(false) + .run().use { child -> + assertThat(child.getBean()).isSameAs(lifecycle) + assertThat(child.getBean()).isSameAs(subscriptions) + assertThat(child.getBeansOfType(SpringProcessApplicationLifecycleDriver::class.java)).isEmpty() + assertThat(child.getBeansOfType(SpringProcessApplicationLifecycleEventPublisher::class.java)).isEmpty() + assertThat(lifecycle.getPhase()).isEqualTo(ProcessApplicationPhase.RUNNING) + } + + val recorder = parent.getBean() + assertThat(recorder.subscriptions).containsExactly(subscriptions) + assertThat(lifecycle.getPhase()).isEqualTo(ProcessApplicationPhase.RUNNING) + assertThat(driver.isRunning).isTrue() + } + } + + @Test + fun `child startup event does not start an idle parent lifecycle`() { + AnnotationConfigApplicationContext(LateAdapterApplication::class.java).use { parent -> + val lifecycle = parent.getBean() + val driver = parent.getBean() + assertThat(lifecycle.getPhase()).isEqualTo(ProcessApplicationPhase.NOT_STARTED) + + SpringApplicationBuilder(BridgeOnlyApplication::class.java) + .parent(parent) + .web(WebApplicationType.NONE) + .logStartupInfo(false) + .run().use { + assertThat(lifecycle.getPhase()).isEqualTo(ProcessApplicationPhase.NOT_STARTED) + assertThat(driver.isRunning).isFalse() + } + } + } + @Test fun `starts the lifecycle with an adapter configured after the bridge`() { SpringApplicationBuilder(LateAdapterApplication::class.java) .web(WebApplicationType.NONE) .logStartupInfo(false) .run().use { context -> - assertThat(context.getBean(ProcessApplicationLifecycle::class.java).getPhase()) + assertThat(context.getBean().getPhase()) .isEqualTo(ProcessApplicationPhase.RUNNING) - assertThat(context.getBean(SubscriptionRegistrationRecorder::class.java).subscriptions) - .containsExactly(context.getBean(TaskSubscriptionApi::class.java)) + assertThat(context.getBean().subscriptions) + .containsExactly(context.getBean()) } } @@ -54,10 +97,9 @@ internal class SpringProcessApplicationLifecycleStartupTest { context.beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, multicaster) }) .run().use { context -> - val recorder = context.getBean(SubscriptionRegistrationRecorder::class.java) - assertThat(context.getBean(ProcessApplicationLifecycle::class.java).getPhase()) - .isEqualTo(ProcessApplicationPhase.RUNNING) - val subscriptions = context.getBean(TaskSubscriptionApi::class.java) + val recorder = context.getBean() + assertThat(context.getBean().getPhase()).isEqualTo(ProcessApplicationPhase.RUNNING) + val subscriptions = context.getBean() assertThat(recorder.subscriptions).containsExactly(subscriptions) assertThat(recorder.subscriptionsAtEndOfRegistration).containsExactly(subscriptions) assertThat(recorder.applicationStarted).isFalse() @@ -92,7 +134,7 @@ internal class SpringProcessApplicationLifecycleStartupTest { .web(WebApplicationType.NONE) .logStartupInfo(false) .run().use { context -> - assertThat(context.getBean(ProcessApplicationLifecycle::class.java).getPhase()) + assertThat(context.getBean().getPhase()) .isEqualTo(ProcessApplicationPhase.NOT_STARTED) } }