diff options
author | Vitaly Takmazov | 2017-11-02 16:02:17 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-11-02 16:12:40 +0300 |
commit | ee20020d9c576a48173c13b68b03d349c0ec3e47 (patch) | |
tree | 7c59dcac74cde4e5b819a245dc0fbbb315020f10 /juick-crosspost | |
parent | 61c6a3fe338a5cbbef55081f8ac26011c053125d (diff) |
server-web: base websocket component with autoreconnect
Diffstat (limited to 'juick-crosspost')
3 files changed, 20 insertions, 25 deletions
diff --git a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java index eae12e6c..76a909ad 100644 --- a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java +++ b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java @@ -17,6 +17,8 @@ package com.juick.components.configuration; +import com.juick.server.component.JuickServerComponent; +import com.juick.server.component.JuickServerReconnectManager; import com.juick.server.configuration.BaseWebConfiguration; import com.juick.service.Crosspost; import org.springframework.beans.factory.annotation.Value; @@ -29,8 +31,6 @@ import org.springframework.http.client.InterceptingClientHttpRequestFactory; import org.springframework.http.client.support.BasicAuthorizationInterceptor; import org.springframework.web.client.RestTemplate; import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.socket.client.WebSocketConnectionManager; -import org.springframework.web.socket.client.standard.StandardWebSocketClient; import java.util.Collections; import java.util.List; @@ -47,7 +47,14 @@ public class CrosspostAppConfiguration extends BaseWebConfiguration { private String apiUser; @Value("${api_password:secret}") private String apiSecret; - + @Bean + public JuickServerComponent juickServerComponent() { + return new JuickServerComponent(); + } + @Bean + public JuickServerReconnectManager juickServerReconnectManager() { + return new JuickServerReconnectManager(); + } @Bean public RestTemplate rest() { RestTemplate rest = new RestTemplate(); @@ -57,22 +64,10 @@ public class CrosspostAppConfiguration extends BaseWebConfiguration { rest.setRequestFactory(new InterceptingClientHttpRequestFactory(rest.getRequestFactory(), interceptors)); return rest; } - private static final String WS_URI = "wss://ws.juick.com/"; @Bean public Crosspost crosspost() { return new Crosspost(); } - @Bean - public WebSocketConnectionManager connectionManager() { - WebSocketConnectionManager manager = new WebSocketConnectionManager(client(), crosspost(), WS_URI); - manager.setAutoStartup(true); - return manager; - } - - @Bean - public StandardWebSocketClient client() { - return new StandardWebSocketClient(); - } } diff --git a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostInitializer.java b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostInitializer.java index 6a95c1f0..88280480 100644 --- a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostInitializer.java +++ b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostInitializer.java @@ -17,6 +17,7 @@ package com.juick.components.configuration; +import com.juick.server.configuration.JuickServerComponentConfiguration; import org.apache.commons.codec.CharEncoding; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; @@ -30,7 +31,10 @@ public class CrosspostInitializer extends AbstractAnnotationConfigDispatcherServ @Override protected Class<?>[] getRootConfigClasses() { - return new Class<?>[]{ CrosspostAppConfiguration.class }; + return new Class<?>[]{ + CrosspostAppConfiguration.class, + JuickServerComponentConfiguration.class + }; } @Override diff --git a/juick-crosspost/src/main/java/com/juick/service/Crosspost.java b/juick-crosspost/src/main/java/com/juick/service/Crosspost.java index a8458439..6bcf7c81 100644 --- a/juick-crosspost/src/main/java/com/juick/service/Crosspost.java +++ b/juick-crosspost/src/main/java/com/juick/service/Crosspost.java @@ -16,8 +16,8 @@ */ package com.juick.service; -import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.Message; +import com.juick.server.component.MessageEvent; import com.juick.util.MessageUtils; import org.apache.commons.codec.CharEncoding; import org.apache.commons.io.IOUtils; @@ -26,11 +26,9 @@ import org.apache.commons.lang3.tuple.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationListener; import org.springframework.social.twitter.api.Twitter; import org.springframework.social.twitter.api.impl.TwitterTemplate; -import org.springframework.web.socket.TextMessage; -import org.springframework.web.socket.WebSocketSession; -import org.springframework.web.socket.handler.TextWebSocketHandler; import javax.inject.Inject; import javax.net.ssl.HttpsURLConnection; @@ -42,7 +40,7 @@ import java.nio.charset.StandardCharsets; /** * @author Ugnich Anton */ -public class Crosspost extends TextWebSocketHandler { +public class Crosspost implements ApplicationListener<MessageEvent> { final static String FBURL = "https://graph.facebook.com/me/feed"; final static String VKURL = "https://api.vk.com/method/wall.post"; @@ -55,12 +53,10 @@ public class Crosspost extends TextWebSocketHandler { private String twitter_consumer_key; @Value("${twitter_consumer_secret:}") private String twitter_consumer_secret; - @Inject - private ObjectMapper jsonMapper; @Override - protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { - Message msg = jsonMapper.readValue(message.asBytes(), Message.class); + public void onApplicationEvent(MessageEvent event) { + Message msg = event.getMessage(); if (msg.getMid() > 0 && msg.getRid() == 0) { if (StringUtils.isNotEmpty(crosspostService.getTwitterName(msg.getUser().getUid()))) { if (msg.getTags().stream().noneMatch(t -> t.getName().equals("notwitter"))) { |