Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/spring-boot-bridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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<TaskSubscriptionApi>,
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -17,7 +19,7 @@ import org.springframework.context.SmartLifecycle
class SpringProcessApplicationLifecycleDriver(
private val lifecycle: ProcessApplicationLifecycle,
private val taskSubscriptionApiProvider: ObjectProvider<TaskSubscriptionApi>
) : ApplicationListener<ApplicationStartedEvent>, SmartLifecycle {
) : ApplicationListener<ApplicationStartedEvent>, ApplicationContextAware, SmartLifecycle {

companion object {

Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProcessApplicationLifecycle>()
val driver = context.getBean<SpringProcessApplicationLifecycleDriver>()
val entries = context.getBean<PhaseRecorder>().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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProcessApplicationLifecycle>()
val subscriptions = parent.getBean<TaskSubscriptionApi>()
val driver = parent.getBean<SpringProcessApplicationLifecycleDriver>()

SpringApplicationBuilder(BridgeOnlyApplication::class.java)
.parent(parent)
.web(WebApplicationType.NONE)
.logStartupInfo(false)
.run().use { child ->
assertThat(child.getBean<ProcessApplicationLifecycle>()).isSameAs(lifecycle)
assertThat(child.getBean<TaskSubscriptionApi>()).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<SubscriptionRegistrationRecorder>()
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<ProcessApplicationLifecycle>()
val driver = parent.getBean<SpringProcessApplicationLifecycleDriver>()
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<ProcessApplicationLifecycle>().getPhase())
.isEqualTo(ProcessApplicationPhase.RUNNING)
assertThat(context.getBean(SubscriptionRegistrationRecorder::class.java).subscriptions)
.containsExactly(context.getBean(TaskSubscriptionApi::class.java))
assertThat(context.getBean<SubscriptionRegistrationRecorder>().subscriptions)
.containsExactly(context.getBean<TaskSubscriptionApi>())
}
}

Expand All @@ -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<SubscriptionRegistrationRecorder>()
assertThat(context.getBean<ProcessApplicationLifecycle>().getPhase()).isEqualTo(ProcessApplicationPhase.RUNNING)
val subscriptions = context.getBean<TaskSubscriptionApi>()
assertThat(recorder.subscriptions).containsExactly(subscriptions)
assertThat(recorder.subscriptionsAtEndOfRegistration).containsExactly(subscriptions)
assertThat(recorder.applicationStarted).isFalse()
Expand Down Expand Up @@ -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<ProcessApplicationLifecycle>().getPhase())
.isEqualTo(ProcessApplicationPhase.NOT_STARTED)
}
}
Expand Down