|
| 1 | +/* |
| 2 | + * Copyright 2024 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.integration.endpoint.management; |
| 18 | + |
| 19 | +import java.time.Instant; |
| 20 | +import java.util.concurrent.CountDownLatch; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 23 | + |
| 24 | +import org.apache.commons.logging.Log; |
| 25 | +import org.apache.commons.logging.LogFactory; |
| 26 | + |
| 27 | +import org.springframework.beans.BeansException; |
| 28 | +import org.springframework.beans.factory.BeanFactory; |
| 29 | +import org.springframework.beans.factory.BeanFactoryAware; |
| 30 | +import org.springframework.beans.factory.SmartInitializingSingleton; |
| 31 | +import org.springframework.context.SmartLifecycle; |
| 32 | +import org.springframework.integration.context.IntegrationContextUtils; |
| 33 | +import org.springframework.integration.context.IntegrationProperties; |
| 34 | +import org.springframework.integration.endpoint.AbstractPollingEndpoint; |
| 35 | +import org.springframework.scheduling.TaskScheduler; |
| 36 | + |
| 37 | +/** |
| 38 | + * The component to keep an application alive when there are no non-daemon threads. |
| 39 | + * Some application might just not rely on the loops in specific threads for their logic. |
| 40 | + * Or target protocol to integrate with communicates via daemon threads. |
| 41 | + * <p> |
| 42 | + * A bean for this class is registered automatically by Spring Integration infrastructure. |
| 43 | + * It is started by application context for a blocked keep-alive dedicated thread |
| 44 | + * only if there is no {@link AbstractPollingEndpoint} beans in the application context |
| 45 | + * or {@link TaskScheduler} is configured for daemon (or virtual) threads. |
| 46 | + * <p> |
| 47 | + * Can be stopped (or started respectively) manually after injection into some target service if found redundant. |
| 48 | + * <p> |
| 49 | + * The {@link IntegrationProperties#KEEP_ALIVE} integration global |
| 50 | + * property can be set to {@code false} to disable this component regardless of the application logic. |
| 51 | + * |
| 52 | + * @author Artem Bilan |
| 53 | + * |
| 54 | + * @since 6.4 |
| 55 | + */ |
| 56 | +public class IntegrationKeepAlive implements SmartLifecycle, SmartInitializingSingleton, BeanFactoryAware { |
| 57 | + |
| 58 | + private static final Log LOG = LogFactory.getLog(IntegrationKeepAlive.class); |
| 59 | + |
| 60 | + private final AtomicBoolean running = new AtomicBoolean(); |
| 61 | + |
| 62 | + private BeanFactory beanFactory; |
| 63 | + |
| 64 | + private boolean autoStartup; |
| 65 | + |
| 66 | + private volatile Thread keepAliveThread; |
| 67 | + |
| 68 | + @Override |
| 69 | + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
| 70 | + this.beanFactory = beanFactory; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void afterSingletonsInstantiated() { |
| 75 | + IntegrationProperties integrationProperties = IntegrationContextUtils.getIntegrationProperties(this.beanFactory); |
| 76 | + this.autoStartup = |
| 77 | + integrationProperties.isKeepAlive() |
| 78 | + && (isTaskSchedulerDaemon() || !isAbstractPollingEndpointPresent()); |
| 79 | + } |
| 80 | + |
| 81 | + private boolean isTaskSchedulerDaemon() { |
| 82 | + TaskScheduler taskScheduler = IntegrationContextUtils.getTaskScheduler(this.beanFactory); |
| 83 | + AtomicBoolean isDaemon = new AtomicBoolean(); |
| 84 | + CountDownLatch checkDaemonThreadLatch = new CountDownLatch(1); |
| 85 | + taskScheduler.schedule(() -> { |
| 86 | + isDaemon.set(Thread.currentThread().isDaemon()); |
| 87 | + checkDaemonThreadLatch.countDown(); |
| 88 | + }, Instant.now()); |
| 89 | + |
| 90 | + boolean logWarning = false; |
| 91 | + try { |
| 92 | + if (!checkDaemonThreadLatch.await(10, TimeUnit.SECONDS)) { |
| 93 | + logWarning = true; |
| 94 | + } |
| 95 | + } |
| 96 | + catch (InterruptedException ex) { |
| 97 | + logWarning = true; |
| 98 | + } |
| 99 | + if (logWarning) { |
| 100 | + LOG.warn("The 'IntegrationKeepAlive' cannot check a 'TaskScheduler' daemon threads status. " + |
| 101 | + "Falling back to 'keep-alive'"); |
| 102 | + } |
| 103 | + return isDaemon.get(); |
| 104 | + } |
| 105 | + |
| 106 | + private boolean isAbstractPollingEndpointPresent() { |
| 107 | + return this.beanFactory.getBeanProvider(AbstractPollingEndpoint.class) |
| 108 | + .stream() |
| 109 | + .findAny() |
| 110 | + .isPresent(); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public boolean isAutoStartup() { |
| 115 | + return this.autoStartup; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void start() { |
| 120 | + if (this.running.compareAndSet(false, true)) { |
| 121 | + this.keepAliveThread = |
| 122 | + new Thread(() -> { |
| 123 | + while (true) { |
| 124 | + try { |
| 125 | + Thread.sleep(Long.MAX_VALUE); |
| 126 | + } |
| 127 | + catch (InterruptedException ex) { |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
| 131 | + }); |
| 132 | + this.keepAliveThread.setDaemon(false); |
| 133 | + this.keepAliveThread.setName("spring-integration-keep-alive"); |
| 134 | + this.keepAliveThread.start(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void stop() { |
| 140 | + if (this.running.compareAndSet(true, false)) { |
| 141 | + this.keepAliveThread.interrupt(); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public boolean isRunning() { |
| 147 | + return this.running.get(); |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments