From 5461d04f4e551f51db1a1f7f628aed5e3460c092 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 30 Aug 2017 14:34:19 +0200 Subject: [PATCH] [New] StrolchAgent now instantiates executor services for async work --- .../li/strolch/agent/api/StrolchAgent.java | 78 +++++++++++++++++++ .../strolch/agent/api/StrolchComponent.java | 23 ++++++ 2 files changed, 101 insertions(+) diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/api/StrolchAgent.java b/li.strolch.agent/src/main/java/li/strolch/agent/api/StrolchAgent.java index 1932956de..305e00c41 100644 --- a/li.strolch.agent/src/main/java/li/strolch/agent/api/StrolchAgent.java +++ b/li.strolch.agent/src/main/java/li/strolch/agent/api/StrolchAgent.java @@ -21,6 +21,10 @@ import java.io.InputStream; import java.text.MessageFormat; import java.util.Properties; import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,41 +46,110 @@ public class StrolchAgent { private ComponentContainerImpl container; private StrolchConfiguration strolchConfiguration; private StrolchVersion appVersion; + private ExecutorService executor; + private ScheduledExecutorService scheduledExecutor; public StrolchAgent(StrolchVersion appVersion) { this.appVersion = appVersion; } + /** + * Return the {@link StrolchConfiguration} + * + * @return the {@link StrolchConfiguration} + */ public StrolchConfiguration getStrolchConfiguration() { return this.strolchConfiguration; } + /** + * Return the container + * + * @return the container + */ public ComponentContainer getContainer() { return this.container; } + /** + * @return the name of this application as is defined in the configuration + */ public String getApplicationName() { return this.strolchConfiguration.getRuntimeConfiguration().getApplicationName(); } + /** + * Return the {@link ExecutorService} instantiated for this agent + * + * @return the {@link ExecutorService} instantiated for this agent + */ + public ExecutorService getExecutor() { + return this.executor; + } + + /** + * Return the {@link ScheduledExecutorService} instantiated for this agent + * + * @return the {@link ScheduledExecutorService} instantiated for this agent + */ + public ScheduledExecutorService getScheduledExecutor() { + return this.scheduledExecutor; + } + + /** + * Initializes the underlying container and prepares the executor services. Before calling this method, + * {@link #setup(String, File, File, File)} must have ben called + */ public void initialize() { if (this.container == null) throw new RuntimeException("Please call setup first!"); + this.executor = Executors.newCachedThreadPool(); + this.scheduledExecutor = Executors.newScheduledThreadPool(4); this.container.initialize(this.strolchConfiguration); } + /** + * Starts the container + */ public void start() { if (this.container == null) throw new RuntimeException("Please call setup first!"); this.container.start(); } + /** + * Stops the container + */ public void stop() { if (this.container != null) this.container.stop(); } + private void shutdownExecutorService(ExecutorService executor) { + try { + logger.info("Shutting down executor..."); + 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 + */ public void destroy() { + + if (this.executor != null) + shutdownExecutorService(this.executor); + if (this.scheduledExecutor != null) + shutdownExecutorService(this.scheduledExecutor); + if (this.container != null) this.container.destroy(); this.container = null; @@ -139,6 +212,11 @@ public class StrolchAgent { private VersionQueryResult versionQueryResult; + /** + * Returns the version of this agent + * + * @return the version of this agent + */ public VersionQueryResult getVersion() { if (this.versionQueryResult == null) { diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/api/StrolchComponent.java b/li.strolch.agent/src/main/java/li/strolch/agent/api/StrolchComponent.java index bbff19093..724043e8c 100644 --- a/li.strolch.agent/src/main/java/li/strolch/agent/api/StrolchComponent.java +++ b/li.strolch.agent/src/main/java/li/strolch/agent/api/StrolchComponent.java @@ -19,6 +19,8 @@ import java.io.IOException; import java.io.InputStream; import java.text.MessageFormat; import java.util.Properties; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -112,6 +114,24 @@ public class StrolchComponent { return this.configuration; } + /** + * Return the {@link ExecutorService} instantiated for this agent + * + * @return the {@link ExecutorService} instantiated for this agent + */ + protected ExecutorService getExecutorService() { + return this.container.getAgent().getExecutor(); + } + + /** + * Return the {@link ScheduledExecutorService} instantiated for this agent + * + * @return the {@link ScheduledExecutorService} instantiated for this agent + */ + protected ScheduledExecutorService getScheduledExecutor() { + return this.container.getAgent().getScheduledExecutor(); + } + /** * Can be used by sub classes to assert that the component is started and thus ready to use, before any component * methods are used @@ -383,6 +403,9 @@ public class StrolchComponent { return this.version; } + /** + * @return Returns the locator of this agent + */ public Locator getLocator() { return Locator.valueOf(Tags.AGENT, getName()); }