From e0a3ca2f9b2ec1a5c3bb542ff7543a65b775a92a Mon Sep 17 00:00:00 2001 From: lolodomo Date: Sun, 19 Feb 2023 20:55:16 +0100 Subject: [PATCH] [tesla] Normalize thread names and avoid a possible IAE (#14351) Signed-off-by: Laurent Garnier --- .../tesla/internal/handler/TeslaEventEndpoint.java | 11 ++++++----- .../tesla/internal/handler/TeslaVehicleHandler.java | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaEventEndpoint.java b/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaEventEndpoint.java index 826f7100810..beec69d7f9f 100644 --- a/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaEventEndpoint.java +++ b/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaEventEndpoint.java @@ -22,7 +22,6 @@ import java.nio.charset.CodingErrorAction; import java.nio.charset.StandardCharsets; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jetty.websocket.api.Session; @@ -32,6 +31,8 @@ import org.eclipse.jetty.websocket.api.WebSocketPingPongListener; import org.eclipse.jetty.websocket.client.WebSocketClient; import org.openhab.binding.tesla.internal.protocol.Event; import org.openhab.core.io.net.http.WebSocketFactory; +import org.openhab.core.thing.ThingUID; +import org.openhab.core.thing.util.ThingWebClientUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,7 +55,6 @@ public class TeslaEventEndpoint implements WebSocketListener, WebSocketPingPongL private String endpointId; protected WebSocketFactory webSocketFactory; - private static final AtomicInteger INSTANCE_COUNTER = new AtomicInteger(); private WebSocketClient client; private ConnectionState connectionState = ConnectionState.CLOSED; @@ -62,11 +62,12 @@ public class TeslaEventEndpoint implements WebSocketListener, WebSocketPingPongL private EventHandler eventHandler; private final Gson gson = new Gson(); - public TeslaEventEndpoint(WebSocketFactory webSocketFactory) { + public TeslaEventEndpoint(ThingUID uid, WebSocketFactory webSocketFactory) { try { - this.endpointId = "TeslaEventEndpoint-" + INSTANCE_COUNTER.incrementAndGet(); + this.endpointId = "TeslaEventEndpoint-" + uid.getAsString(); - client = webSocketFactory.createWebSocketClient(endpointId); + String name = ThingWebClientUtil.buildWebClientConsumerName(uid, null); + client = webSocketFactory.createWebSocketClient(name); this.client.setConnectTimeout(TIMEOUT_MILLISECONDS); this.client.setMaxIdleTimeout(IDLE_TIMEOUT_MILLISECONDS); } catch (Exception e) { diff --git a/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaVehicleHandler.java b/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaVehicleHandler.java index 6a2b51c4a64..8903a961020 100644 --- a/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaVehicleHandler.java +++ b/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaVehicleHandler.java @@ -193,7 +193,7 @@ public class TeslaVehicleHandler extends BaseThingHandler { if (enableEvents) { if (eventThread == null) { - eventThread = new Thread(eventRunnable, "openHAB-Tesla-Events-" + getThing().getUID()); + eventThread = new Thread(eventRunnable, "OH-binding-" + getThing().getUID() + "-events"); eventThread.start(); } } @@ -1130,7 +1130,7 @@ public class TeslaVehicleHandler extends BaseThingHandler { @Override public void run() { - eventEndpoint = new TeslaEventEndpoint(webSocketFactory); + eventEndpoint = new TeslaEventEndpoint(getThing().getUID(), webSocketFactory); eventEndpoint.addEventHandler(new TeslaEventEndpoint.EventHandler() { @Override public void handleEvent(Event event) {