aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-03-27 05:23:00 +0300
committerGravatar Vitaly Takmazov2018-03-27 05:45:37 +0300
commit7bd9d90ce7716a4224442967a6e06f023a2b66a2 (patch)
tree21255cc8cd2a9071b02c86496953bc381fbaa279 /juick-server/src/main/java/com
parent83dae6eb77c1e9e720c726af47451bcfea2bd14e (diff)
server: event handling refactoring
Diffstat (limited to 'juick-server/src/main/java/com')
-rw-r--r--juick-server/src/main/java/com/juick/server/TelegramBotManager.java28
-rw-r--r--juick-server/src/main/java/com/juick/server/XMPPBot.java10
-rw-r--r--juick-server/src/main/java/com/juick/server/XMPPConnection.java1
-rw-r--r--juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java10
-rw-r--r--juick-server/src/main/java/com/juick/server/configuration/EventsConfiguration.java45
5 files changed, 34 insertions, 60 deletions
diff --git a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
index 6987fa27..a44892a4 100644
--- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
+++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
@@ -46,6 +46,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
+import org.springframework.context.annotation.Scope;
+import org.springframework.context.annotation.ScopedProxyMode;
+import org.springframework.context.event.EventListener;
+import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
@@ -64,6 +68,7 @@ import static com.juick.formatters.PlainTextFormatter.formatUrl;
* Created by vt on 12/05/16.
*/
@Component
+@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class TelegramBotManager {
private static final Logger logger = LoggerFactory.getLogger(TelegramBotManager.class);
@@ -299,7 +304,10 @@ public class TelegramBotManager {
}
});
}
- public void processMessage(com.juick.Message jmsg) {
+ @EventListener
+ @Async
+ public void processMessageEvent(MessageEvent messageEvent) {
+ com.juick.Message jmsg = messageEvent.getMessage();
String msgUrl = formatUrl(jmsg);
if (jmsg.getMid() > 0 && jmsg.getRid() == 0) {
String msg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(jmsg, true));
@@ -318,15 +326,23 @@ public class TelegramBotManager {
subscriptionService.getUsersSubscribedToComments(op, jmsg)
).forEach(c -> telegramNotify(c, fmsg, MessageUtils.attachmentUrl(jmsg)));
}
- };
- public void processLike(User liker, com.juick.Message message) {
+ }
+ @EventListener
+ @Async
+ public void processLikeEvent(LikeEvent likeEvent) {
+ User liker = likeEvent.getUser();
+ com.juick.Message message = likeEvent.getMessage();
telegramService.getTelegramIdentifiers(Collections.singletonList(message.getUser()))
.forEach(c -> telegramNotify(c, String.format("%s recommends your post",
MessageUtils.getMarkdownUser(liker)),null));
- };
- public void processSubscribe(User subscriber, User target) {
+ }
+ @EventListener
+ @Async
+ public void processSubscribe(SubscribeEvent subscribeEvent) {
+ User subscriber = subscribeEvent.getUser();
+ User target = subscribeEvent.getToUser();
telegramService.getTelegramIdentifiers(Collections.singletonList(target))
.forEach(c -> telegramNotify(c, String.format("%s subscribed to your blog",
MessageUtils.getMarkdownUser(subscriber)), null));
- };
+ }
}
diff --git a/juick-server/src/main/java/com/juick/server/XMPPBot.java b/juick-server/src/main/java/com/juick/server/XMPPBot.java
index 41c98856..c304473c 100644
--- a/juick-server/src/main/java/com/juick/server/XMPPBot.java
+++ b/juick-server/src/main/java/com/juick/server/XMPPBot.java
@@ -37,6 +37,10 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationListener;
+import org.springframework.context.annotation.Scope;
+import org.springframework.context.annotation.ScopedProxyMode;
+import org.springframework.context.event.EventListener;
+import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.stanza.model.*;
@@ -58,6 +62,7 @@ import java.util.stream.Collectors;
* @author ugnich
*/
@Component
+@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class XMPPBot implements StanzaListener, AutoCloseable {
private static final Logger logger = LoggerFactory.getLogger(XMPPBot.class);
@@ -334,7 +339,10 @@ public class XMPPBot implements StanzaListener, AutoCloseable {
return true;
}
- public void processMessage(com.juick.Message msg) {
+ @EventListener
+ @Async
+ public void processMessageEvent(MessageEvent event) {
+ com.juick.Message msg = event.getMessage();
if (msg.getRid() == 0 && msg.getMid() > 0) {
String notify = "New message posted.\n#" + msg.getMid() + " https://juick.com/" + msg.getMid();
userService.getJIDsbyUID(msg.getUser().getUid())
diff --git a/juick-server/src/main/java/com/juick/server/XMPPConnection.java b/juick-server/src/main/java/com/juick/server/XMPPConnection.java
index b69156a0..16f7b869 100644
--- a/juick-server/src/main/java/com/juick/server/XMPPConnection.java
+++ b/juick-server/src/main/java/com/juick/server/XMPPConnection.java
@@ -82,7 +82,6 @@ public class XMPPConnection implements AutoCloseable {
private static final Logger logger = LoggerFactory.getLogger(XMPPConnection.class);
private ExternalComponent router;
- @Inject
private XMPPBot bot;
@Value("${componentname:localhost}")
diff --git a/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java b/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java
index 2dd74b19..78838117 100644
--- a/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java
+++ b/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java
@@ -22,10 +22,7 @@ import com.juick.configuration.DataConfiguration;
import com.juick.server.WebsocketManager;
import com.juick.server.api.rss.MessagesView;
import com.juick.server.api.rss.RepliesView;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.ComponentScan;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.context.annotation.Import;
+import org.springframework.context.annotation.*;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@@ -54,12 +51,11 @@ import java.util.Collections;
@EnableSwagger2
@EnableScheduling
@EnableWebSocket
-@Import({ApiSecurityConfig.class, BaseWebConfiguration.class, DataConfiguration.class, StorageConfiguration.class,
-EventsConfiguration.class})
+@Import({ApiSecurityConfig.class, BaseWebConfiguration.class, DataConfiguration.class, StorageConfiguration.class})
@ComponentScan(basePackages = "com.juick.server")
public class ApiAppConfiguration implements WebMvcConfigurer, WebSocketConfigurer {
@Inject
- WebsocketManager websocketManager;
+ private WebsocketManager websocketManager;
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
diff --git a/juick-server/src/main/java/com/juick/server/configuration/EventsConfiguration.java b/juick-server/src/main/java/com/juick/server/configuration/EventsConfiguration.java
deleted file mode 100644
index 63c774fa..00000000
--- a/juick-server/src/main/java/com/juick/server/configuration/EventsConfiguration.java
+++ /dev/null
@@ -1,45 +0,0 @@
-package com.juick.server.configuration;
-
-import com.juick.server.TelegramBotManager;
-import com.juick.server.XMPPBot;
-import com.juick.server.component.LikeEvent;
-import com.juick.server.component.MessageEvent;
-import com.juick.server.component.SubscribeEvent;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.context.ApplicationListener;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-import javax.inject.Inject;
-
-@Configuration
-public class EventsConfiguration {
- private static Logger logger = LoggerFactory.getLogger(EventsConfiguration.class);
- @Inject
- private TelegramBotManager telegramBotManager;
- @Inject
- private XMPPBot bot;
- @Bean
- ApplicationListener<MessageEvent> messageListener() {
- return event -> {
- logger.info("MessageEvent: {}", event.getMessage());
- telegramBotManager.processMessage(event.getMessage());
- bot.processMessage(event.getMessage());
- };
- }
- @Bean
- ApplicationListener<LikeEvent> likeListener() {
- return event -> {
- logger.info("LikeEvent: {} -> {}", event.getUser().getName(), event.getMessage());
- telegramBotManager.processLike(event.getUser(), event.getMessage());
- };
- }
- @Bean
- ApplicationListener<SubscribeEvent> subscribeListener() {
- return event -> {
- logger.info("SubscribeEvent: {} -> {}", event.getUser().getName(), event.getToUser().getName());
- telegramBotManager.processSubscribe(event.getUser(), event.getToUser());
- };
- }
-}