diff options
Diffstat (limited to 'juick-api/src/main/java/com/juick/api')
4 files changed, 15 insertions, 64 deletions
diff --git a/juick-api/src/main/java/com/juick/api/ApiServer.java b/juick-api/src/main/java/com/juick/api/ApiServer.java index f4dd4d992..a8e2db85d 100644 --- a/juick-api/src/main/java/com/juick/api/ApiServer.java +++ b/juick-api/src/main/java/com/juick/api/ApiServer.java @@ -17,13 +17,10 @@ package com.juick.api; import com.juick.User; -import com.juick.server.component.DisconnectedEvent; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.ApplicationListener; -import org.springframework.scheduling.annotation.Scheduled; import org.springframework.web.socket.client.WebSocketConnectionManager; import rocks.xmpp.addr.Jid; import rocks.xmpp.core.XmppException; @@ -34,7 +31,6 @@ import rocks.xmpp.core.stanza.model.Message; import rocks.xmpp.extensions.component.accept.ExternalComponent; import rocks.xmpp.extensions.oob.model.x.OobX; -import javax.annotation.Nonnull; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.net.URI; @@ -43,7 +39,7 @@ import java.net.URISyntaxException; /** * @author Ugnich Anton */ -public class ApiServer implements ApplicationListener<DisconnectedEvent>, AutoCloseable { +public class ApiServer implements AutoCloseable { private static Logger logger = LoggerFactory.getLogger(ApiServer.class); private ExternalComponent xmpp; @@ -58,8 +54,6 @@ public class ApiServer implements ApplicationListener<DisconnectedEvent>, AutoCl private int xmppPort; @Value("${xmpp_disabled:false}") private boolean isXmppDisabled; - @Inject - private WebSocketConnectionManager webSocketConnectionManager; @PostConstruct public void init() { @@ -114,11 +108,4 @@ public class ApiServer implements ApplicationListener<DisconnectedEvent>, AutoCl logger.warn("attachment error", e1); } } - - @Override - public void onApplicationEvent(@Nonnull DisconnectedEvent event) { - logger.info("retrying..."); - webSocketConnectionManager.stop(); - webSocketConnectionManager.start(); - } } diff --git a/juick-api/src/main/java/com/juick/api/TelegramBotManager.java b/juick-api/src/main/java/com/juick/api/TelegramBotManager.java index 0516af5fd..1611c23c6 100644 --- a/juick-api/src/main/java/com/juick/api/TelegramBotManager.java +++ b/juick-api/src/main/java/com/juick/api/TelegramBotManager.java @@ -19,6 +19,7 @@ package com.juick.api; import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.server.component.DisconnectedEvent; +import com.juick.server.component.MessageEvent; import com.juick.service.MessagesService; import com.juick.service.SubscriptionService; import com.juick.service.TelegramService; @@ -35,6 +36,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ApplicationListener; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.web.socket.CloseStatus; @@ -55,7 +57,7 @@ import static com.juick.formatters.PlainTextFormatter.formatUrl; * Created by vt on 12/05/16. */ @Component -public class TelegramBotManager extends TextWebSocketHandler { +public class TelegramBotManager implements ApplicationListener<MessageEvent> { private static final Logger logger = LoggerFactory.getLogger(TelegramBotManager.class); private TelegramBot bot; @@ -68,12 +70,6 @@ public class TelegramBotManager extends TextWebSocketHandler { private MessagesService messagesService; @Inject private SubscriptionService subscriptionService; - @Inject - private ObjectMapper jsonMapper; - @Inject - private ApplicationEventPublisher applicationEventPublisher; - - private WebSocketSession session; public static final String MSG_LINK = "🔗"; @@ -94,35 +90,11 @@ public class TelegramBotManager extends TextWebSocketHandler { } } - @Override - public void afterConnectionEstablished(WebSocketSession session) throws Exception { - logger.info("WebSocket connected"); - this.session = session; - } - @Override - public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { - logger.info("WebSocket disconnected with code {}: {}", status.getCode(), status.getReason()); - applicationEventPublisher.publishEvent(new DisconnectedEvent(this)); - } - - @Scheduled(fixedRate = 30000, initialDelay = 30000) - public void ping() throws IOException { - if (session != null && session.isOpen()) { - logger.debug("Sending WebSocket ping"); - session.sendMessage(new PingMessage()); - } else { - applicationEventPublisher.publishEvent(new DisconnectedEvent(this)); - } - } @Override - protected void handleTextMessage(WebSocketSession session, TextMessage text) throws Exception { - com.juick.Message jmsg = jsonMapper.readValue(text.asBytes(), com.juick.Message.class); - - if (logger.isInfoEnabled()) // prevent writeValueAsString execution if logger disabled - logger.info("got jmsg: {}", jsonMapper.writeValueAsString(jmsg)); - + public void onApplicationEvent(MessageEvent event) { + com.juick.Message jmsg = event.getMessage(); String msgUrl = formatUrl(jmsg); if (jmsg.getRid() == 0) { String msg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(jmsg)); diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java b/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java index f51528bf2..69a2438d9 100644 --- a/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java @@ -19,6 +19,8 @@ package com.juick.api.configuration; import com.juick.api.ApiServer; import com.juick.api.TelegramBotManager; +import com.juick.server.component.JuickServerComponent; +import com.juick.server.component.JuickServerReconnectManager; import com.juick.server.configuration.BaseWebConfiguration; import com.juick.service.ImagesService; import com.juick.service.ImagesServiceImpl; @@ -44,36 +46,24 @@ import java.util.Collections; * Created by aalexeev on 11/12/16. */ @Configuration -@EnableScheduling @EnableAsync @EnableWebMvc @EnableSwagger2 @PropertySource("classpath:juick.conf") @ComponentScan(basePackages = "com.juick.api") public class ApiAppConfiguration extends BaseWebConfiguration { - @Inject - TelegramBotManager telegramBotManager; - - @Value("${websocket_url:ws://localhost:8080/ws/}") - private String WS_URI; - @Bean - public WebSocketConnectionManager connectionManager() { - WebSocketConnectionManager manager = new WebSocketConnectionManager(client(), telegramBotManager, WS_URI); - manager.setAutoStartup(true); - return manager; + public JuickServerComponent juickServerComponent() { + return new JuickServerComponent(); } - @Bean - public StandardWebSocketClient client() { - return new StandardWebSocketClient(); + public JuickServerReconnectManager juickServerReconnectManager() { + return new JuickServerReconnectManager(); } - @Bean public ApiServer apiServer() { return new ApiServer(); } - @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java b/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java index 59c699261..3380df10c 100644 --- a/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java @@ -18,6 +18,7 @@ package com.juick.api.configuration; import com.juick.configuration.DataConfiguration; +import com.juick.server.configuration.JuickServerComponentConfiguration; import com.juick.server.configuration.StorageConfiguration; import org.apache.commons.codec.CharEncoding; import org.springframework.web.filter.CharacterEncodingFilter; @@ -35,7 +36,8 @@ public class ApiInitializer extends AbstractAnnotationConfigDispatcherServletIni return new Class<?>[]{ ApiSecurityConfig.class, DataConfiguration.class, - StorageConfiguration.class + StorageConfiguration.class, + JuickServerComponentConfiguration.class }; } |