|
47 | 47 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
48 | 48 | import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
49 | 49 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
| 50 | +import org.springframework.boot.context.event.ApplicationReadyEvent; |
50 | 51 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
51 | 52 | import org.springframework.context.ApplicationListener;
|
52 | 53 | import org.springframework.context.annotation.Bean;
|
53 | 54 | import org.springframework.context.annotation.Conditional;
|
54 | 55 | import org.springframework.context.annotation.Configuration;
|
55 | 56 | import org.springframework.context.annotation.DependsOn;
|
56 |
| -import org.springframework.context.event.ContextStartedEvent; |
57 | 57 |
|
58 | 58 | @Configuration
|
59 | 59 | @EnableConfigurationProperties(TemporalProperties.class)
|
@@ -159,23 +159,15 @@ public WorkerFactoryStarter workerFactoryStarter(WorkerFactory workerFactory) {
|
159 | 159 | return new WorkerFactoryStarter(workerFactory);
|
160 | 160 | }
|
161 | 161 |
|
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> { |
171 | 163 | private final WorkerFactory workerFactory;
|
172 | 164 |
|
173 | 165 | public WorkerFactoryStarter(WorkerFactory workerFactory) {
|
174 | 166 | this.workerFactory = workerFactory;
|
175 | 167 | }
|
176 | 168 |
|
177 | 169 | @Override
|
178 |
| - public void onApplicationEvent(@Nonnull ContextStartedEvent event) { |
| 170 | + public void onApplicationEvent(@Nonnull ApplicationReadyEvent event) { |
179 | 171 | workerFactory.start();
|
180 | 172 | }
|
181 | 173 | }
|
|
0 commit comments