From 6f52c96b612a8655b0d5dc4ede16af969506fec3 Mon Sep 17 00:00:00 2001 From: samypr100 <3933065+samypr100@users.noreply.github.com> Date: Sun, 1 Sep 2024 15:33:48 -0400 Subject: [PATCH] use an executor instead --- src/main/java/edu/jhuapl/trinity/App.java | 2 +- .../javafx/handlers/RestEventHandler.java | 82 +++++++++++-------- .../trinity/messages/TrinityHttpServer.java | 32 +++----- .../edu/jhuapl/trinity/fxml/Data.fxml | 2 +- 4 files changed, 64 insertions(+), 54 deletions(-) diff --git a/src/main/java/edu/jhuapl/trinity/App.java b/src/main/java/edu/jhuapl/trinity/App.java index 0fffc17..c76ddeb 100644 --- a/src/main/java/edu/jhuapl/trinity/App.java +++ b/src/main/java/edu/jhuapl/trinity/App.java @@ -476,7 +476,7 @@ public void start(Stage stage) throws IOException { if(enableHttp) { reh.startHttpService(); } - + setupMissionTimer(scene); //add helper tools and overlays circleSpinner = new CircleProgressIndicator(); diff --git a/src/main/java/edu/jhuapl/trinity/javafx/handlers/RestEventHandler.java b/src/main/java/edu/jhuapl/trinity/javafx/handlers/RestEventHandler.java index 6f71def..5923650 100644 --- a/src/main/java/edu/jhuapl/trinity/javafx/handlers/RestEventHandler.java +++ b/src/main/java/edu/jhuapl/trinity/javafx/handlers/RestEventHandler.java @@ -25,58 +25,76 @@ import edu.jhuapl.trinity.messages.TrinityHttpServer; import javafx.application.Platform; import javafx.event.EventHandler; +import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.text.Font; -import javafx.scene.Scene; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.logging.Logger; /** * @author Sean Phillips */ public class RestEventHandler implements EventHandler { - Scene scene; - TrinityHttpServer trinityHttpServer = null; - Thread serverThread = null; - + + private static final Logger LOGGER = Logger.getLogger(RestEventHandler.class.getName()); + + private final Scene scene; + private ExecutorService executor; + private Future serverFuture; + + public RestEventHandler(Scene scene) { this.scene = scene; + this.executor = Executors.newSingleThreadExecutor(); } - + public void startHttpService() { - if(null == trinityHttpServer && null == serverThread) { - System.out.println("Creating Trinity HTTP Server..."); - trinityHttpServer = new TrinityHttpServer(scene); - serverThread = new Thread(trinityHttpServer, "Trinity HTTP Server"); - serverThread.setDaemon(true); - serverThread.start(); - Platform.runLater(() -> { - scene.getRoot().fireEvent( - new CommandTerminalEvent("Creating Trinity HTTP Server...", - new Font("Consolas", 20), Color.GREEN)); - }); + if (serverFuture != null && !serverFuture.isDone()) { + LOGGER.warning("Trinity HTTP Server is already running."); + return; } + + LOGGER.info("Creating Trinity HTTP Server..."); + serverFuture = executor.submit(new TrinityHttpServer(scene)); + + Platform.runLater(() -> { + scene.getRoot().fireEvent( + new CommandTerminalEvent("Creating Trinity HTTP Server...", + new Font("Consolas", 20), Color.GREEN)); + }); } + public void stopHttpService() { - if(null != trinityHttpServer && null != serverThread) { - Platform.runLater(() -> { - scene.getRoot().fireEvent( - new CommandTerminalEvent("Stopping Trinity HTTP Server...", - new Font("Consolas", 20), Color.YELLOW)); - }); - trinityHttpServer.stop(); //will kill future inside the runnable. - trinityHttpServer = null; - serverThread = null; + if (serverFuture == null || serverFuture.isDone()) { + LOGGER.warning("Trinity HTTP Server is not running."); + return; } + Platform.runLater(() -> { + scene.getRoot().fireEvent( + new CommandTerminalEvent("Stopping Trinity HTTP Server...", + new Font("Consolas", 20), Color.YELLOW)); + }); + + // Cancel the running task and interrupt if running + serverFuture.cancel(true); + // Disable new tasks from being submitted + executor.shutdown(); + // Cancel currently executing tasks (if any) + executor.shutdownNow(); + + // Reset the executor service for future use + executor = Executors.newSingleThreadExecutor(); } @Override public void handle(RestEvent t) { - if(t.getEventType() == RestEvent.START_RESTSERVER_THREAD) { - if(null == trinityHttpServer && null == serverThread) - startHttpService(); - } else if(t.getEventType() == RestEvent.TERMINATE_RESTSERVER_THREAD) { - if(null != trinityHttpServer && null != serverThread) - stopHttpService(); + if (t.getEventType() == RestEvent.START_RESTSERVER_THREAD) { + startHttpService(); + } else if (t.getEventType() == RestEvent.TERMINATE_RESTSERVER_THREAD) { + stopHttpService(); } } } diff --git a/src/main/java/edu/jhuapl/trinity/messages/TrinityHttpServer.java b/src/main/java/edu/jhuapl/trinity/messages/TrinityHttpServer.java index ded0b6c..436a9c0 100644 --- a/src/main/java/edu/jhuapl/trinity/messages/TrinityHttpServer.java +++ b/src/main/java/edu/jhuapl/trinity/messages/TrinityHttpServer.java @@ -32,28 +32,20 @@ public class TrinityHttpServer implements Runnable { private static final Logger LOGGER = Logger.getLogger(TrinityHttpServer.class.getName()); - private static final String HTTP_HOST = "0.0.0.0"; - private static final int HTTP_PORT = 8080; + private static final String DEFAULT_HTTP_HOST = "0.0.0.0"; + private static final int DEFAULT_HTTP_PORT = 8080; private final Scene scene; - ChannelFuture future; - EventLoopGroup parentGroup; - EventLoopGroup childGroup; - ServerBootstrap bootstrap; - + public TrinityHttpServer(Scene scene) { this.scene = scene; } - public void stop() { - future.cancel(true); - childGroup.shutdownGracefully(); - parentGroup.shutdownGracefully(); - } + @Override public void run() { - parentGroup = new NioEventLoopGroup(); - childGroup = new NioEventLoopGroup(); + EventLoopGroup parentGroup = new NioEventLoopGroup(); + EventLoopGroup childGroup = new NioEventLoopGroup(); try { - bootstrap = new ServerBootstrap() + ServerBootstrap bootstrap = new ServerBootstrap() .group(parentGroup, childGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer() { @@ -63,11 +55,11 @@ protected void initChannel(SocketChannel ch) { ch.pipeline().addLast(new HttpObjectAggregator(Integer.MAX_VALUE)); ch.pipeline().addLast(new TrinityServerHandler(scene)); } - }); - future = bootstrap.bind(HTTP_HOST, HTTP_PORT).sync(); + }); + ChannelFuture future = bootstrap.bind(DEFAULT_HTTP_HOST, DEFAULT_HTTP_PORT).sync(); future.channel().closeFuture().sync(); } catch (InterruptedException ex) { - LOGGER.log(Level.SEVERE, null, ex); + LOGGER.log(Level.WARNING, "Trinity HTTP Server Stopped."); } finally { childGroup.shutdownGracefully(); parentGroup.shutdownGracefully(); @@ -79,7 +71,7 @@ public static class TrinityServerHandler extends SimpleChannelInboundHandler -