From 6762e882fac513e6c8c927982fb3e3e7eb8985ac Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Wed, 30 Jun 2021 14:20:21 -0400 Subject: [PATCH] Cherry pick 610b88479f6caf2ced0fe5814bdd1132646dd8d4 --- .../commons/util/TaskSchedulerWrapper.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/TaskSchedulerWrapper.java diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/TaskSchedulerWrapper.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/TaskSchedulerWrapper.java new file mode 100644 index 00000000..34e64900 --- /dev/null +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/TaskSchedulerWrapper.java @@ -0,0 +1,59 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.commons.util; + +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.scheduling.TaskScheduler; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; +import org.springframework.util.Assert; + +/** + * Wrapper that downstream projects can use to keep {@link ThreadPoolTaskScheduler} local. + * + * Implementation adapted from Spring Vault. + * + */ +public class TaskSchedulerWrapper + implements InitializingBean, DisposableBean { + + private final T taskScheduler; + + public TaskSchedulerWrapper(T taskScheduler) { + Assert.notNull(taskScheduler, "ThreadPoolTaskScheduler must not be null"); + this.taskScheduler = taskScheduler; + } + + public T getTaskScheduler() { + return this.taskScheduler; + } + + @Override + public void destroy() throws Exception { + if (DisposableBean.class.isInstance(taskScheduler)) { + ((DisposableBean) this.taskScheduler).destroy(); + } + } + + @Override + public void afterPropertiesSet() throws Exception { + if (InitializingBean.class.isInstance(taskScheduler)) { + ((InitializingBean) this.taskScheduler).afterPropertiesSet(); + } + } + +}