diff options
author | Vitaly Takmazov | 2018-03-28 23:33:44 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-03-28 23:33:44 +0300 |
commit | 024533a558e4827564041bdf5040e51370eece80 (patch) | |
tree | 694a7f765ac31d6170b2422060badbe036cd61c2 /juick-server | |
parent | f6eb11a23ab645b187c5911ec762a3d6ca58140c (diff) |
server: refactor event listeners
Diffstat (limited to 'juick-server')
4 files changed, 40 insertions, 30 deletions
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); |