diff options
-rw-r--r-- | src/main/java/com/juick/ws/XMPPConnection.java | 6 | ||||
-rw-r--r-- | src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java | 20 |
2 files changed, 25 insertions, 1 deletions
diff --git a/src/main/java/com/juick/ws/XMPPConnection.java b/src/main/java/com/juick/ws/XMPPConnection.java index a6f4e2cb..9ac22d45 100644 --- a/src/main/java/com/juick/ws/XMPPConnection.java +++ b/src/main/java/com/juick/ws/XMPPConnection.java @@ -17,6 +17,8 @@ import javax.inject.Inject; import java.io.IOException; import java.net.Socket; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; @@ -36,12 +38,14 @@ public class XMPPConnection implements Runnable, Stream.StreamListener, Message. MessageSerializer ms; @Inject WebsocketComponent ws; + @Inject + ExecutorService xmppThreadService; @Inject public XMPPConnection(Environment env) { xmppPassword = env.getProperty("xmpp_password"); ms = new MessageSerializer(); - new Thread(this).start(); + xmppThreadService.submit(this); } @Override diff --git a/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java b/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java index 63ff12cb..27bfcf36 100644 --- a/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java +++ b/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java @@ -2,6 +2,7 @@ package com.juick.ws.configuration; import com.juick.ws.WebsocketComponent; import com.juick.ws.XMPPConnection; +import org.springframework.beans.factory.DisposableBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -17,6 +18,8 @@ import org.springframework.web.socket.config.annotation.WebSocketConfigurer; import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; import javax.inject.Inject; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; /** * Created by vitalyster on 28.06.2016. @@ -41,8 +44,25 @@ public class WebsocketConfiguration extends WebMvcConfigurerAdapter implements W dataSource.setUrl(env.getProperty("datasource_url")); return new JdbcTemplate(dataSource); } + @Bean + ExecutorService xmppThreadService() { + return Executors.newSingleThreadExecutor(); + } + @Bean + DestroyBean destroyBean() { + return new DestroyBean(); + } @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(wsHandler(), "/**"); } + class DestroyBean implements DisposableBean { + + @Inject + ExecutorService xmppThreadService; + @Override + public void destroy() throws Exception { + xmppThreadService.shutdownNow(); + } + } } |