|
| 1 | +/* |
| 2 | + * Copyright 2012-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.preinitialize; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.concurrent.CountDownLatch; |
| 21 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 22 | + |
| 23 | +import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; |
| 24 | +import org.springframework.boot.context.event.ApplicationFailedEvent; |
| 25 | +import org.springframework.boot.context.event.ApplicationReadyEvent; |
| 26 | +import org.springframework.boot.context.event.SpringApplicationEvent; |
| 27 | +import org.springframework.boot.context.logging.LoggingApplicationListener; |
| 28 | +import org.springframework.context.ApplicationListener; |
| 29 | +import org.springframework.core.NativeDetector; |
| 30 | +import org.springframework.core.Ordered; |
| 31 | +import org.springframework.core.io.support.SpringFactoriesLoader; |
| 32 | + |
| 33 | +/** |
| 34 | + * {@link ApplicationListener} to trigger early initialization in a background thread of |
| 35 | + * time-consuming tasks. |
| 36 | + * <p> |
| 37 | + * Set the {@link #IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME} system property to |
| 38 | + * {@code true} to disable this mechanism. |
| 39 | + * |
| 40 | + * @author Phillip Webb |
| 41 | + * @author Andy Wilkinson |
| 42 | + * @author Artsiom Yudovin |
| 43 | + * @author Sebastien Deleuze |
| 44 | + * @see BackgroundPreinitializer |
| 45 | + */ |
| 46 | +class BackgroundPreinitializingApplicationListener implements ApplicationListener<SpringApplicationEvent>, Ordered { |
| 47 | + |
| 48 | + /** |
| 49 | + * System property that instructs Spring Boot how to run pre initialization. When the |
| 50 | + * property is set to {@code true}, no pre-initialization happens and each item is |
| 51 | + * initialized in the foreground as it needs to. When the property is {@code false} |
| 52 | + * (default), pre initialization runs in a separate thread in the background. |
| 53 | + */ |
| 54 | + public static final String IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME = "spring.backgroundpreinitializer.ignore"; |
| 55 | + |
| 56 | + private static final AtomicBoolean started = new AtomicBoolean(); |
| 57 | + |
| 58 | + private static final CountDownLatch complete = new CountDownLatch(1); |
| 59 | + |
| 60 | + private final SpringFactoriesLoader factoriesLoader; |
| 61 | + |
| 62 | + private final boolean enabled; |
| 63 | + |
| 64 | + BackgroundPreinitializingApplicationListener() { |
| 65 | + this(SpringFactoriesLoader.forDefaultResourceLocation()); |
| 66 | + } |
| 67 | + |
| 68 | + BackgroundPreinitializingApplicationListener(SpringFactoriesLoader factoriesLoader) { |
| 69 | + this.factoriesLoader = factoriesLoader; |
| 70 | + this.enabled = !NativeDetector.inNativeImage() |
| 71 | + && !Boolean.getBoolean(IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME) |
| 72 | + && Runtime.getRuntime().availableProcessors() > 1; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public int getOrder() { |
| 77 | + return LoggingApplicationListener.DEFAULT_ORDER + 1; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void onApplicationEvent(SpringApplicationEvent event) { |
| 82 | + if (!this.enabled) { |
| 83 | + return; |
| 84 | + } |
| 85 | + if (event instanceof ApplicationEnvironmentPreparedEvent && started.compareAndSet(false, true)) { |
| 86 | + preinitialize(); |
| 87 | + } |
| 88 | + if ((event instanceof ApplicationReadyEvent || event instanceof ApplicationFailedEvent) && started.get()) { |
| 89 | + try { |
| 90 | + complete.await(); |
| 91 | + } |
| 92 | + catch (InterruptedException ex) { |
| 93 | + Thread.currentThread().interrupt(); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private void preinitialize() { |
| 99 | + Runner runner = new Runner(this.factoriesLoader.load(BackgroundPreinitializer.class)); |
| 100 | + try { |
| 101 | + Thread thread = new Thread(runner, "background-preinit"); |
| 102 | + thread.start(); |
| 103 | + } |
| 104 | + catch (Exception ex) { |
| 105 | + // This will fail on Google App Engine where creating threads is |
| 106 | + // prohibited. We can safely continue but startup will be slightly slower |
| 107 | + // as the initialization will now happen on the main thread. |
| 108 | + complete.countDown(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Runner thread to call the {@link BackgroundPreinitializer} instances. |
| 114 | + * |
| 115 | + * @param preinitializers the preinitializers |
| 116 | + */ |
| 117 | + record Runner(List<BackgroundPreinitializer> preinitializers) implements Runnable { |
| 118 | + |
| 119 | + @Override |
| 120 | + public void run() { |
| 121 | + for (BackgroundPreinitializer preinitializer : this.preinitializers) { |
| 122 | + try { |
| 123 | + preinitializer.preinitialize(); |
| 124 | + } |
| 125 | + catch (Throwable ex) { |
| 126 | + } |
| 127 | + } |
| 128 | + complete.countDown(); |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments