From 024533a558e4827564041bdf5040e51370eece80 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 28 Mar 2018 23:33:44 +0300 Subject: server: refactor event listeners --- .../com/juick/server/NotificationListener.java | 15 ++++++++++ .../java/com/juick/server/TelegramBotManager.java | 21 ++++---------- .../src/main/java/com/juick/server/XMPPBot.java | 32 ++++++++++++---------- .../main/java/com/juick/server/XMPPConnection.java | 2 +- 4 files changed, 40 insertions(+), 30 deletions(-) create mode 100644 juick-server/src/main/java/com/juick/server/NotificationListener.java (limited to 'juick-server') diff --git a/juick-server/src/main/java/com/juick/server/NotificationListener.java b/juick-server/src/main/java/com/juick/server/NotificationListener.java new file mode 100644 index 00000000..d5cdc14f --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/NotificationListener.java @@ -0,0 +1,15 @@ +package com.juick.server; + +import com.juick.server.component.LikeEvent; +import com.juick.server.component.MessageEvent; +import com.juick.server.component.SubscribeEvent; +import org.springframework.context.event.EventListener; + +public interface NotificationListener { + @EventListener + void processMessageEvent(MessageEvent messageEvent); + @EventListener + void processSubscribeEvent(SubscribeEvent subscribeEvent); + @EventListener + void processLikeEvent(LikeEvent likeEvent); +} 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 1aa7273d..1ea71ebb 100644 --- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java +++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java @@ -45,16 +45,10 @@ 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.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; -import javax.annotation.Nonnull; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.io.IOException; @@ -68,8 +62,7 @@ import static com.juick.formatters.PlainTextFormatter.formatUrl; * Created by vt on 12/05/16. */ @Component -@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) -public class TelegramBotManager { +public class TelegramBotManager implements NotificationListener { private static final Logger logger = LoggerFactory.getLogger(TelegramBotManager.class); private TelegramBot bot; @@ -304,8 +297,7 @@ public class TelegramBotManager { } }); } - @EventListener - @Async + @Override public void processMessageEvent(MessageEvent messageEvent) { com.juick.Message jmsg = messageEvent.getMessage(); String msgUrl = formatUrl(jmsg); @@ -332,18 +324,17 @@ public class TelegramBotManager { chats.stream().filter(u -> telegramService.getUser(u) == 0).forEach(c -> telegramNotify(c, msg, MessageUtils.attachmentUrl(jmsg))); } } - @EventListener - @Async + @Override public void processLikeEvent(LikeEvent likeEvent) { User liker = likeEvent.getUser(); com.juick.Message message = likeEvent.getMessage(); + logger.info("Like received in tg listener"); telegramService.getTelegramIdentifiers(Collections.singletonList(message.getUser())) .forEach(c -> telegramNotify(c, String.format("%s recommends your [post](%s)", MessageUtils.getMarkdownUser(liker), formatUrl(message)),null)); } - @EventListener - @Async - public void processSubscribe(SubscribeEvent subscribeEvent) { + @Override + public void processSubscribeEvent(SubscribeEvent subscribeEvent) { User subscriber = subscribeEvent.getUser(); User target = subscribeEvent.getToUser(); telegramService.getTelegramIdentifiers(Collections.singletonList(target)) 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 5f9f83e6..6805b733 100644 --- a/juick-server/src/main/java/com/juick/server/XMPPBot.java +++ b/juick-server/src/main/java/com/juick/server/XMPPBot.java @@ -19,14 +19,14 @@ package com.juick.server; import com.juick.Tag; import com.juick.User; +import com.juick.formatters.PlainTextFormatter; import com.juick.server.component.LikeEvent; import com.juick.server.component.MessageEvent; import com.juick.server.component.SubscribeEvent; -import com.juick.server.util.TagUtils; -import com.juick.server.xmpp.s2s.StanzaListener; -import com.juick.formatters.PlainTextFormatter; import com.juick.server.helpers.TagStats; +import com.juick.server.util.TagUtils; import com.juick.server.xmpp.helpers.annotation.UserCommand; +import com.juick.server.xmpp.s2s.StanzaListener; import com.juick.service.*; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; @@ -36,11 +36,6 @@ 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.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.*; @@ -49,6 +44,7 @@ import rocks.xmpp.core.stanza.model.client.ClientPresence; import rocks.xmpp.core.stanza.model.errors.Condition; import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; import javax.inject.Inject; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -62,8 +58,7 @@ import java.util.stream.Collectors; * @author ugnich */ @Component -@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) -public class XMPPBot implements StanzaListener, AutoCloseable { +public class XMPPBot implements StanzaListener, NotificationListener { private static final Logger logger = LoggerFactory.getLogger(XMPPBot.class); @@ -319,8 +314,7 @@ public class XMPPBot implements StanzaListener, AutoCloseable { return true; } - @EventListener - @Async + @Override public void processMessageEvent(MessageEvent event) { com.juick.Message msg = event.getMessage(); if (msg.getRid() == 0 && msg.getMid() > 0) { @@ -346,6 +340,16 @@ public class XMPPBot implements StanzaListener, AutoCloseable { } } + @Override + public void processSubscribeEvent(SubscribeEvent subscribeEvent) { + + } + + @Override + public void processLikeEvent(LikeEvent likeEvent) { + + } + @UserCommand(pattern = "^ping$", patternFlags = Pattern.CASE_INSENSITIVE, help = "PING - returns you a PONG") public String commandPing(User user, Jid from, String[] input) { @@ -764,8 +768,8 @@ public class XMPPBot implements StanzaListener, AutoCloseable { }); } - @Override - public void close() throws Exception { + @PreDestroy + public void close() { broadcastPresence(Presence.Type.UNAVAILABLE); } } 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 2176317f..e4d2db04 100644 --- a/juick-server/src/main/java/com/juick/server/XMPPConnection.java +++ b/juick-server/src/main/java/com/juick/server/XMPPConnection.java @@ -287,7 +287,7 @@ public class XMPPConnection implements AutoCloseable { void route(Stanza stanza) { try { String xml = stanzaToString(stanza); - logger.debug("stream router (out): {}", xml); + logger.info("stream router (out): {}", xml); xmpp.sendOut(stanza); } catch (XMLStreamException | JAXBException e) { logger.error("JAXB exception", e); -- cgit v1.2.3