[Minor] Using ExecutorPool in StrolchAgent
This commit is contained in:
parent
082758c1be
commit
e4f1a388f7
|
@ -22,9 +22,10 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.lang.management.*;
|
import java.lang.management.*;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
import java.util.*;
|
import java.util.Date;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@ -34,8 +35,7 @@ import li.strolch.agent.impl.ComponentContainerImpl;
|
||||||
import li.strolch.runtime.configuration.ConfigurationParser;
|
import li.strolch.runtime.configuration.ConfigurationParser;
|
||||||
import li.strolch.runtime.configuration.RuntimeConfiguration;
|
import li.strolch.runtime.configuration.RuntimeConfiguration;
|
||||||
import li.strolch.runtime.configuration.StrolchConfiguration;
|
import li.strolch.runtime.configuration.StrolchConfiguration;
|
||||||
import li.strolch.utils.NamedThreadPoolFactory;
|
import li.strolch.utils.ExecutorPool;
|
||||||
import li.strolch.utils.dbc.DBC;
|
|
||||||
import li.strolch.utils.helper.StringHelper;
|
import li.strolch.utils.helper.StringHelper;
|
||||||
import li.strolch.utils.helper.SystemHelper;
|
import li.strolch.utils.helper.SystemHelper;
|
||||||
import li.strolch.utils.iso8601.ISO8601;
|
import li.strolch.utils.iso8601.ISO8601;
|
||||||
|
@ -55,8 +55,7 @@ public class StrolchAgent {
|
||||||
private StrolchConfiguration strolchConfiguration;
|
private StrolchConfiguration strolchConfiguration;
|
||||||
private StrolchVersion appVersion;
|
private StrolchVersion appVersion;
|
||||||
|
|
||||||
private Map<String, ExecutorService> executors;
|
private ExecutorPool executorPool;
|
||||||
private Map<String, ScheduledExecutorService> scheduledExecutors;
|
|
||||||
|
|
||||||
private JsonObject systemState;
|
private JsonObject systemState;
|
||||||
private long systemStateUpdateTime;
|
private long systemStateUpdateTime;
|
||||||
|
@ -100,14 +99,7 @@ public class StrolchAgent {
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized ExecutorService getExecutor(String poolName) {
|
public synchronized ExecutorService getExecutor(String poolName) {
|
||||||
DBC.PRE.assertNotEmpty("poolName must be set!", poolName);
|
return this.executorPool.getExecutor(poolName);
|
||||||
ExecutorService executor = this.executors.get(poolName);
|
|
||||||
if (executor == null) {
|
|
||||||
executor = Executors.newCachedThreadPool(new NamedThreadPoolFactory(poolName));
|
|
||||||
this.executors.put(poolName, executor);
|
|
||||||
}
|
|
||||||
|
|
||||||
return executor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -120,14 +112,7 @@ public class StrolchAgent {
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized ExecutorService getSingleThreadExecutor(String poolName) {
|
public synchronized ExecutorService getSingleThreadExecutor(String poolName) {
|
||||||
DBC.PRE.assertNotEmpty("poolName must be set!", poolName);
|
return this.executorPool.getSingleThreadExecutor(poolName);
|
||||||
ExecutorService executor = this.executors.get(poolName);
|
|
||||||
if (executor == null) {
|
|
||||||
executor = Executors.newSingleThreadExecutor(new NamedThreadPoolFactory(poolName));
|
|
||||||
this.executors.put(poolName, executor);
|
|
||||||
}
|
|
||||||
|
|
||||||
return executor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -140,14 +125,7 @@ public class StrolchAgent {
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized ScheduledExecutorService getScheduledExecutor(String poolName) {
|
public synchronized ScheduledExecutorService getScheduledExecutor(String poolName) {
|
||||||
DBC.PRE.assertNotEmpty("poolName must be set!", poolName);
|
return this.executorPool.getScheduledExecutor(poolName);
|
||||||
ScheduledExecutorService executor = this.scheduledExecutors.get(poolName);
|
|
||||||
if (executor == null) {
|
|
||||||
executor = Executors.newScheduledThreadPool(4, new NamedThreadPoolFactory(poolName));
|
|
||||||
this.scheduledExecutors.put(poolName, executor);
|
|
||||||
}
|
|
||||||
|
|
||||||
return executor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -158,9 +136,7 @@ public class StrolchAgent {
|
||||||
if (this.container == null)
|
if (this.container == null)
|
||||||
throw new RuntimeException("Please call setup first!");
|
throw new RuntimeException("Please call setup first!");
|
||||||
|
|
||||||
this.executors = new HashMap<>();
|
this.executorPool = new ExecutorPool();
|
||||||
this.scheduledExecutors = new HashMap<>();
|
|
||||||
|
|
||||||
this.container.initialize();
|
this.container.initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,36 +157,13 @@ public class StrolchAgent {
|
||||||
this.container.stop();
|
this.container.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T extends ExecutorService> void shutdownExecutorService(Map<String, T> executors) {
|
|
||||||
|
|
||||||
for (String poolName : executors.keySet()) {
|
|
||||||
logger.info("Shutting down executor pool " + poolName);
|
|
||||||
|
|
||||||
T executor = executors.get(poolName);
|
|
||||||
|
|
||||||
try {
|
|
||||||
executor.shutdown();
|
|
||||||
executor.awaitTermination(5, TimeUnit.SECONDS);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
logger.error("Was interrupted while shutting down tasks");
|
|
||||||
} finally {
|
|
||||||
if (!executor.isTerminated()) {
|
|
||||||
logger.error("Tasks not stopped after " + 5 + "s. Shutting down now.");
|
|
||||||
executor.shutdownNow();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destroys the container and the executor services
|
* Destroys the container and the executor services
|
||||||
*/
|
*/
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
|
|
||||||
if (this.executors != null)
|
if (this.executorPool != null)
|
||||||
shutdownExecutorService(this.executors);
|
this.executorPool.destroy();
|
||||||
if (this.scheduledExecutors != null)
|
|
||||||
shutdownExecutorService(this.scheduledExecutors);
|
|
||||||
|
|
||||||
if (this.container != null)
|
if (this.container != null)
|
||||||
this.container.destroy();
|
this.container.destroy();
|
||||||
|
|
Loading…
Reference in New Issue