[tesla] Normalize thread names and avoid a possible IAE (#14351)

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2023-02-19 20:55:16 +01:00 committed by GitHub
parent 43232dae2e
commit e0a3ca2f9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 7 deletions

View File

@ -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) {

View File

@ -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) {