Skip to content

Commit

Permalink
[miio] Avoid shutting down shared executor service (openhab#11166)
Browse files Browse the repository at this point in the history
* [miio] Avoid shutting down shared executor service

* Closing openhab#11152
* Better identify communication threads

Signed-off-by: Marcel Verpaalen <[email protected]>
  • Loading branch information
marcelrv authored and frederictobiasc committed Oct 26, 2021
1 parent 90289b6 commit a1a968e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public abstract class MiIoAbstractHandler extends BaseThingHandler implements Mi
protected static final Gson GSON = new GsonBuilder().create();
protected static final String TIMESTAMP = "timestamp";

protected ScheduledExecutorService miIoScheduler = scheduler;
protected ScheduledExecutorService miIoScheduler = new ScheduledThreadPoolExecutor(3,
new NamedThreadFactory("binding-" + getThing().getUID().getAsString(), true));

protected @Nullable ScheduledFuture<?> pollingJob;
protected MiIoDevices miDevice = MiIoDevices.UNKNOWN;
protected boolean isIdentified;
Expand Down Expand Up @@ -134,7 +136,7 @@ public void initialize() {
getThing().getThingTypeUID());

ScheduledThreadPoolExecutor miIoScheduler = new ScheduledThreadPoolExecutor(3,
new NamedThreadFactory(getThing().getUID().getAsString(), true));
new NamedThreadFactory("binding-" + getThing().getUID().getAsString(), true));
miIoScheduler.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
miIoScheduler.setRemoveOnCancelPolicy(true);
this.miIoScheduler = miIoScheduler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ MiIoSendCommand sendMiIoSendCommand(MiIoSendCommand miIoSendCommand) {
public synchronized void startReceiver() {
MessageSenderThread senderThread = this.senderThread;
if (senderThread == null || !senderThread.isAlive()) {
senderThread = new MessageSenderThread();
senderThread = new MessageSenderThread(deviceId);
senderThread.start();
this.senderThread = senderThread;
}
Expand All @@ -261,14 +261,17 @@ public synchronized void startReceiver() {
*
*/
private class MessageSenderThread extends Thread {
public MessageSenderThread() {
super("Mi IO MessageSenderThread");
private final String deviceId;

public MessageSenderThread(String deviceId) {
super("OH-binding-miio-MessageSenderThread-" + deviceId);
setDaemon(true);
this.deviceId = deviceId;
}

@Override
public void run() {
logger.debug("Starting Mi IO MessageSenderThread");
logger.debug("Starting Mi IO MessageSenderThread {}", deviceId);
while (!interrupted()) {
try {
if (concurrentLinkedQueue.isEmpty()) {
Expand All @@ -291,11 +294,11 @@ public void run() {
// That's our signal to stop
break;
} catch (Exception e) {
logger.warn("Error while polling/sending message", e);
logger.warn("Error while polling/sending message for {}", deviceId, e);
}
}
closeSocket();
logger.debug("Finished Mi IO MessageSenderThread");
logger.debug("Finished Mi IO MessageSenderThread {}", deviceId);
}
}

Expand Down

0 comments on commit a1a968e

Please sign in to comment.