From 98b8a3b76f48d947499f242e56b71f167add781b Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 12 Feb 2018 18:58:57 -0500 Subject: [PATCH] http-service: try to release okhttp resources on shutdown --- .../service/SpringBootWebApplication.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/http-service/src/main/java/net/runelite/http/service/SpringBootWebApplication.java b/http-service/src/main/java/net/runelite/http/service/SpringBootWebApplication.java index 4a2ae288ee..4ff7fd3b1d 100644 --- a/http-service/src/main/java/net/runelite/http/service/SpringBootWebApplication.java +++ b/http-service/src/main/java/net/runelite/http/service/SpringBootWebApplication.java @@ -24,14 +24,21 @@ */ package net.runelite.http.service; +import java.io.IOException; import java.time.Instant; import java.util.HashMap; import java.util.Map; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; import javax.sql.DataSource; +import lombok.extern.slf4j.Slf4j; +import net.runelite.http.api.RuneLiteAPI; import net.runelite.http.service.util.InstantConverter; +import okhttp3.Cache; +import okhttp3.OkHttpClient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; @@ -42,8 +49,46 @@ import org.sql2o.converters.Converter; import org.sql2o.quirks.NoQuirks; @SpringBootApplication +@Slf4j public class SpringBootWebApplication extends SpringBootServletInitializer { + @Bean + protected ServletContextListener listener() + { + return new ServletContextListener() + { + @Override + public void contextInitialized(ServletContextEvent sce) + { + log.info("RuneLite API started"); + } + + @Override + public void contextDestroyed(ServletContextEvent sce) + { + // Destroy okhttp client + OkHttpClient client = RuneLiteAPI.CLIENT; + client.dispatcher().executorService().shutdown(); + client.connectionPool().evictAll(); + try + { + Cache cache = client.cache(); + if (cache != null) + { + cache.close(); + } + } + catch (IOException ex) + { + log.warn(null, ex); + } + + log.info("RuneLite API stopped"); + } + + }; + } + private Context getContext() throws NamingException { Context initCtx = new InitialContext();