[teleinfo] Fix memory leak (#9724)
* Use set instead list to avoid duplicate listeners * Remove from listeners when bridge is not ONLINE * Unsubscribe from bridge handler events when thing handler is disposed Signed-off-by: Olivier Marceau <hollysaiqs@marceau.ovh>
This commit is contained in:
parent
3e7014dafb
commit
4e73b05ff0
|
@ -12,10 +12,10 @@
|
||||||
*/
|
*/
|
||||||
package org.openhab.binding.teleinfo.internal.handler;
|
package org.openhab.binding.teleinfo.internal.handler;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.CopyOnWriteArraySet;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.openhab.binding.teleinfo.internal.TeleinfoDiscoveryService;
|
import org.openhab.binding.teleinfo.internal.TeleinfoDiscoveryService;
|
||||||
|
@ -32,7 +32,7 @@ import org.openhab.core.thing.binding.ThingHandlerService;
|
||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
public abstract class TeleinfoAbstractControllerHandler extends BaseBridgeHandler {
|
public abstract class TeleinfoAbstractControllerHandler extends BaseBridgeHandler {
|
||||||
|
|
||||||
private List<TeleinfoControllerHandlerListener> listeners = Collections.synchronizedList(new ArrayList<>());
|
private Set<TeleinfoControllerHandlerListener> listeners = new CopyOnWriteArraySet<>();
|
||||||
|
|
||||||
public TeleinfoAbstractControllerHandler(Bridge bridge) {
|
public TeleinfoAbstractControllerHandler(Bridge bridge) {
|
||||||
super(bridge);
|
super(bridge);
|
||||||
|
|
|
@ -65,20 +65,33 @@ public abstract class TeleinfoAbstractElectricityMeterHandler extends BaseThingH
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
|
public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
|
||||||
|
TeleinfoAbstractControllerHandler controllerHandler = getControllerHandler();
|
||||||
if (bridgeStatusInfo.getStatus() != ThingStatus.ONLINE) {
|
if (bridgeStatusInfo.getStatus() != ThingStatus.ONLINE) {
|
||||||
|
if (controllerHandler != null) {
|
||||||
|
controllerHandler.removeListener(this);
|
||||||
|
}
|
||||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, ERROR_OFFLINE_CONTROLLER_OFFLINE);
|
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, ERROR_OFFLINE_CONTROLLER_OFFLINE);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Bridge bridge = getBridge();
|
|
||||||
if (bridge != null) {
|
|
||||||
TeleinfoAbstractControllerHandler controllerHandler = (TeleinfoAbstractControllerHandler) bridge
|
|
||||||
.getHandler();
|
|
||||||
if (controllerHandler != null) {
|
if (controllerHandler != null) {
|
||||||
controllerHandler.addListener(this);
|
controllerHandler.addListener(this);
|
||||||
updateStatus(ThingStatus.ONLINE);
|
updateStatus(ThingStatus.ONLINE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose() {
|
||||||
|
TeleinfoAbstractControllerHandler controllerHandler = getControllerHandler();
|
||||||
|
if (controllerHandler != null) {
|
||||||
|
controllerHandler.removeListener(this);
|
||||||
|
}
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
private @Nullable TeleinfoAbstractControllerHandler getControllerHandler() {
|
||||||
|
Bridge bridge = getBridge();
|
||||||
|
return bridge != null ? (TeleinfoAbstractControllerHandler) bridge.getHandler() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue