diff options
Diffstat (limited to 'juick-notifications/src/main/java')
-rw-r--r-- | juick-notifications/src/main/java/com/juick/components/Notifications.java | 89 |
1 files changed, 49 insertions, 40 deletions
diff --git a/juick-notifications/src/main/java/com/juick/components/Notifications.java b/juick-notifications/src/main/java/com/juick/components/Notifications.java index ec6ffe9c..15af4a52 100644 --- a/juick-notifications/src/main/java/com/juick/components/Notifications.java +++ b/juick-notifications/src/main/java/com/juick/components/Notifications.java @@ -28,7 +28,6 @@ import com.turo.pushy.apns.ApnsClient; import com.turo.pushy.apns.PushNotificationResponse; import com.turo.pushy.apns.util.ApnsPayloadBuilder; import com.turo.pushy.apns.util.SimpleApnsPushNotification; -import com.turo.pushy.apns.util.concurrent.PushNotificationFuture; import com.turo.pushy.apns.util.concurrent.PushNotificationResponseListener; import io.netty.util.concurrent.Future; import org.apache.commons.lang3.StringUtils; @@ -234,23 +233,25 @@ public class Notifications extends TextWebSocketHandler implements NotificationC } private void processAPNSResponse(String token, PushNotificationResponse<SimpleApnsPushNotification> pushNotificationResponse) { - if (pushNotificationResponse.isAccepted()) { - logger.info("APNS accepted: {}", token); - } else { - String reason = pushNotificationResponse.getRejectionReason(); - logger.info("APNS rejected: {}", reason); - if (reason.equals("BadDeviceToken")) { - invalidAPNSTokens.add(token); + if (!closeFlag.get()) { + if (pushNotificationResponse.isAccepted()) { + logger.info("APNS accepted: {}", token); + } else { + String reason = pushNotificationResponse.getRejectionReason(); + logger.info("APNS rejected: {}", reason); + if (reason.equals("BadDeviceToken")) { + invalidAPNSTokens.add(token); + } } + Optional<Date> invalidationDate = Optional.ofNullable( + pushNotificationResponse.getTokenInvalidationTimestamp()); + invalidationDate.ifPresent(date -> { + if (date.before(new Date())) { + logger.info("Token invalidated: {}", token); + invalidAPNSTokens.add(token); + } + }); } - Optional<Date> invalidationDate = Optional.ofNullable( - pushNotificationResponse.getTokenInvalidationTimestamp()); - invalidationDate.ifPresent(date -> { - if (date.before(new Date())) { - logger.info("Token invalidated: {}", token); - invalidAPNSTokens.add(token); - } - }); } public void addInvalidGCMToken(String token) { @@ -303,35 +304,41 @@ public class Notifications extends TextWebSocketHandler implements NotificationC private final AtomicBoolean closeFlag = new AtomicBoolean(false); @Override - public void afterConnectionEstablished(WebSocketSession session) throws Exception { - logger.info("WebSocket connected"); - this.session = new ConcurrentWebSocketSessionDecorator(session, 60000, 65535); + public void afterConnectionEstablished(WebSocketSession session) { + if (!closeFlag.get()) { + logger.info("WebSocket connected"); + this.session = new ConcurrentWebSocketSessionDecorator(session, 60000, 65535); + } } @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)); + public void afterConnectionClosed(WebSocketSession session, CloseStatus status) { + if (!closeFlag.get()) { + logger.info("WebSocket disconnected with code {}: {}", status.getCode(), status.getReason()); + 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)); if (!closeFlag.get()) { + 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)); messageReceived(jmsg); } } @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 if (!closeFlag.get()) { - applicationEventPublisher.publishEvent(new DisconnectedEvent(this)); + if (!closeFlag.get()) { + if (session != null && session.isOpen()) { + logger.debug("Sending WebSocket ping"); + session.sendMessage(new PingMessage()); + } else { + applicationEventPublisher.publishEvent(new DisconnectedEvent(this)); + } } } @PreDestroy @@ -341,15 +348,17 @@ public class Notifications extends TextWebSocketHandler implements NotificationC } @Scheduled(fixedRate = 600000) public void cleanupTokens() { - logger.debug("initializing GCM tokens cleanup: {} tokens", getInvalidGCMTokens().size()); - deleteTokens("gcm", new ArrayList<>(getInvalidGCMTokens())); - cleanupGCMTokens(); - logger.debug("initializing MPNS tokens cleanup: {} tokens", getInvalidMPNSTokens().size()); - deleteTokens("mpns", new ArrayList<>(getInvalidMPNSTokens())); - cleanupMPNSTokens(); - logger.debug("initializing APNS tokens cleanup: {} tokens", getInvalidAPNSTokens().size()); - deleteTokens("apns", new ArrayList<>(getInvalidAPNSTokens())); - cleanupMPNSTokens(); + if (!closeFlag.get()) { + logger.debug("initializing GCM tokens cleanup: {} tokens", getInvalidGCMTokens().size()); + deleteTokens("gcm", new ArrayList<>(getInvalidGCMTokens())); + cleanupGCMTokens(); + logger.debug("initializing MPNS tokens cleanup: {} tokens", getInvalidMPNSTokens().size()); + deleteTokens("mpns", new ArrayList<>(getInvalidMPNSTokens())); + cleanupMPNSTokens(); + logger.debug("initializing APNS tokens cleanup: {} tokens", getInvalidAPNSTokens().size()); + deleteTokens("apns", new ArrayList<>(getInvalidAPNSTokens())); + cleanupMPNSTokens(); + } } private void deleteTokens(String type, List<String> devices) { if (devices.size() > 0) { |