From 1c24852d77792f7c2f420558285e2e0cd163d8c3 Mon Sep 17 00:00:00 2001 From: Stefan Feuerhahn Date: Tue, 6 Nov 2018 14:42:45 +0100 Subject: [PATCH] fixed correct shutdown of server thread pools --- .../openmuc/jositransport/ServerThread.java | 4 ++- .../openiec61850/ServerAssociation.java | 4 ++- .../internal/NamedDefaultThreadFactory.java | 30 +++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/openmuc/openiec61850/internal/NamedDefaultThreadFactory.java diff --git a/src/main/java/org/openmuc/jositransport/ServerThread.java b/src/main/java/org/openmuc/jositransport/ServerThread.java index 27aff2b..aa11ce0 100644 --- a/src/main/java/org/openmuc/jositransport/ServerThread.java +++ b/src/main/java/org/openmuc/jositransport/ServerThread.java @@ -16,6 +16,8 @@ */ package org.openmuc.jositransport; +import org.openmuc.openiec61850.internal.NamedDefaultThreadFactory; + import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; @@ -88,7 +90,7 @@ final class ServerThread extends Thread { @Override public void run() { - ExecutorService executor = Executors.newCachedThreadPool(); + ExecutorService executor = Executors.newCachedThreadPool(new NamedDefaultThreadFactory("openiec61850-osi-server")); try { Socket clientSocket = null; diff --git a/src/main/java/org/openmuc/openiec61850/ServerAssociation.java b/src/main/java/org/openmuc/openiec61850/ServerAssociation.java index e8da14c..da33a12 100644 --- a/src/main/java/org/openmuc/openiec61850/ServerAssociation.java +++ b/src/main/java/org/openmuc/openiec61850/ServerAssociation.java @@ -40,6 +40,7 @@ import org.openmuc.jasn1.ber.types.string.BerVisibleString; import org.openmuc.josistack.AcseAssociation; import org.openmuc.josistack.ByteBufferInputStream; import org.openmuc.josistack.DecodingException; +import org.openmuc.openiec61850.internal.NamedDefaultThreadFactory; import org.openmuc.openiec61850.internal.mms.asn1.AccessResult; import org.openmuc.openiec61850.internal.mms.asn1.ConfirmedErrorPDU; import org.openmuc.openiec61850.internal.mms.asn1.ConfirmedRequestPDU; @@ -118,7 +119,7 @@ final class ServerAssociation { public ServerAssociation(ServerSap serverSap) { this.serverSap = serverSap; serverModel = serverSap.serverModel; - executor = Executors.newScheduledThreadPool(2); + executor = Executors.newScheduledThreadPool(2, new NamedDefaultThreadFactory("openiec61850-server-connection")); } public void handleNewAssociation(AcseAssociation acseAssociation, ByteBuffer associationRequest) { @@ -1635,6 +1636,7 @@ final class ServerAssociation { void close() { cleanUpConnection(); + executor.shutdown(); if (acseAssociation != null) { acseAssociation.disconnect(); } diff --git a/src/main/java/org/openmuc/openiec61850/internal/NamedDefaultThreadFactory.java b/src/main/java/org/openmuc/openiec61850/internal/NamedDefaultThreadFactory.java new file mode 100644 index 0000000..dbbe307 --- /dev/null +++ b/src/main/java/org/openmuc/openiec61850/internal/NamedDefaultThreadFactory.java @@ -0,0 +1,30 @@ +package org.openmuc.openiec61850.internal; + +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicInteger; + +public class NamedDefaultThreadFactory implements ThreadFactory { + + private static final AtomicInteger factoryCounter = new AtomicInteger(1); + private final AtomicInteger threadCounter = new AtomicInteger(1); + private final String namePrefix; + private ThreadFactory backingDefaultThreadFactory = Executors.defaultThreadFactory(); + + public NamedDefaultThreadFactory(String namePrefix) { + this.namePrefix = namePrefix; + } + + @Override + public Thread newThread(Runnable r) { + Thread thread = backingDefaultThreadFactory.newThread(r); + String threadName = + namePrefix + + "-" + + factoryCounter.getAndIncrement() + + "-thread-" + + threadCounter.getAndIncrement(); + thread.setName(threadName); + return thread; + } +}