Skip to content

Commit d5d96f8

Browse files
Use ApplicationReadyEvent to start spring workers (#1882)
1 parent 1606de5 commit d5d96f8

File tree

2 files changed

+3
-75
lines changed

2 files changed

+3
-75
lines changed

temporal-spring-boot-autoconfigure-alpha/src/main/java/io/temporal/spring/boot/autoconfigure/RootNamespaceAutoConfiguration.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@
4747
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
4848
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
4949
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
50+
import org.springframework.boot.context.event.ApplicationReadyEvent;
5051
import org.springframework.boot.context.properties.EnableConfigurationProperties;
5152
import org.springframework.context.ApplicationListener;
5253
import org.springframework.context.annotation.Bean;
5354
import org.springframework.context.annotation.Conditional;
5455
import org.springframework.context.annotation.Configuration;
5556
import org.springframework.context.annotation.DependsOn;
56-
import org.springframework.context.event.ContextStartedEvent;
5757

5858
@Configuration
5959
@EnableConfigurationProperties(TemporalProperties.class)
@@ -159,23 +159,15 @@ public WorkerFactoryStarter workerFactoryStarter(WorkerFactory workerFactory) {
159159
return new WorkerFactoryStarter(workerFactory);
160160
}
161161

162-
// It needs to listed on ContextStartedEvent, not ContextRefreshedEvent.
163-
// Using ContextRefreshedEvent will cause start of workers early, during the context
164-
// initialization
165-
// and potentially incorrect order of bean initialization.
166-
// Refresh event can also be fired multiple times during the context initialization.
167-
// For this listener to ever work, Spring context needs to be actually started.
168-
// Note that a lot of online samples for Spring don't start the context at all:
169-
// https://stackoverflow.com/questions/48099355/contextstartedevent-not-firing-in-custom-listener
170-
public static class WorkerFactoryStarter implements ApplicationListener<ContextStartedEvent> {
162+
public static class WorkerFactoryStarter implements ApplicationListener<ApplicationReadyEvent> {
171163
private final WorkerFactory workerFactory;
172164

173165
public WorkerFactoryStarter(WorkerFactory workerFactory) {
174166
this.workerFactory = workerFactory;
175167
}
176168

177169
@Override
178-
public void onApplicationEvent(@Nonnull ContextStartedEvent event) {
170+
public void onApplicationEvent(@Nonnull ApplicationReadyEvent event) {
179171
workerFactory.start();
180172
}
181173
}

temporal-spring-boot-autoconfigure-alpha/src/test/java/io/temporal/spring/boot/autoconfigure/WorkersAreNotStartingBeforeContextTest.java

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)