From 45d8f6d0aabcf623f5638bb228fabc4555948a77 Mon Sep 17 00:00:00 2001 From: Marcel Verpaalen Date: Wed, 17 Feb 2021 20:43:36 +0100 Subject: [PATCH] [miio] fix bundle restart error Signed-off-by: Marcel Verpaalen --- .../miio/internal/MiIoHandlerFactory.java | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/MiIoHandlerFactory.java b/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/MiIoHandlerFactory.java index 129ce0a6b8277..08fa2c001702f 100644 --- a/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/MiIoHandlerFactory.java +++ b/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/MiIoHandlerFactory.java @@ -15,6 +15,8 @@ import static org.openhab.binding.miio.internal.MiIoBindingConstants.*; import java.util.Map; +import java.util.concurrent.Future; +import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.ScheduledExecutorService; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -35,7 +37,10 @@ import org.openhab.core.thing.type.ChannelTypeRegistry; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; import org.osgi.service.component.annotations.Reference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * The {@link MiIoHandlerFactory} is responsible for creating things and thing @@ -49,17 +54,20 @@ public class MiIoHandlerFactory extends BaseThingHandlerFactory { private static final String THING_HANDLER_THREADPOOL_NAME = "thingHandler"; protected final ScheduledExecutorService scheduler = ThreadPoolManager .getScheduledPool(THING_HANDLER_THREADPOOL_NAME); - private MiIoDatabaseWatchService miIoDatabaseWatchService; private CloudConnector cloudConnector; private ChannelTypeRegistry channelTypeRegistry; private BasicChannelTypeProvider basicChannelTypeProvider; + private @Nullable Future scheduledTask; + private final Logger logger = LoggerFactory.getLogger(MiIoHandlerFactory.class); @Activate public MiIoHandlerFactory(@Reference ChannelTypeRegistry channelTypeRegistry, @Reference MiIoDatabaseWatchService miIoDatabaseWatchService, @Reference CloudConnector cloudConnector, @Reference BasicChannelTypeProvider basicChannelTypeProvider, Map properties) { this.miIoDatabaseWatchService = miIoDatabaseWatchService; + this.channelTypeRegistry = channelTypeRegistry; + this.basicChannelTypeProvider = basicChannelTypeProvider; this.cloudConnector = cloudConnector; @Nullable String username = (String) properties.get("username"); @@ -68,9 +76,23 @@ public MiIoHandlerFactory(@Reference ChannelTypeRegistry channelTypeRegistry, @Nullable String country = (String) properties.get("country"); cloudConnector.setCredentials(username, password, country); - scheduler.submit(() -> cloudConnector.isConnected()); - this.channelTypeRegistry = channelTypeRegistry; - this.basicChannelTypeProvider = basicChannelTypeProvider; + try { + if (!scheduler.isShutdown()) { + scheduledTask = scheduler.submit(() -> cloudConnector.isConnected()); + } else { + logger.debug("Unexpected: ScheduledExecutorService is shutdown."); + } + } catch (RejectedExecutionException e) { + logger.debug("Unexpected: ScheduledExecutorService task rejected.", e); + } + } + + @Deactivate + private void dispose() { + final Future scheduledTask = this.scheduledTask; + if (scheduledTask != null && !scheduledTask.isDone()) { + scheduledTask.cancel(true); + } } @Override