aboutsummaryrefslogtreecommitdiff
path: root/juick-notifications/src/main/java/com/juick/components/CleanUp.java
blob: 9714e59730114353ceda617d6f6f4d33d0382cac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.juick.components;

import com.juick.components.service.TokenService;
import com.notnoop.apns.ApnsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * Created by vitalyster on 22.11.2016.
 */
@Component
public class CleanUp {

    private static Logger logger = LoggerFactory.getLogger(CleanUp.class);

    @Inject
    ApnsService apns;
    @Inject
    TokenService tokenService;

    @Inject
    Notifications push;

    @Scheduled(fixedRate = 600000)
    public void cleanupTokens() {
        logger.info("initializing apns tokens cleanup");
        Collection<String> devices = apns.getInactiveDevices().keySet();
        int count = devices.size();
        if (count > 0) {
            logger.info("{} tokens to delete", count);
            tokenService.deleteTokens("apns", devices.stream().collect(Collectors.toList()));
        } else {
            logger.debug("No APNS tokens to delete");
        }
        logger.debug("initializing GCM tokens cleanup: {} tokens", push.getInvalidGCMTokens().size());
        tokenService.deleteTokens("gcm", push.getInvalidGCMTokens().stream().collect(Collectors.toList()));
        push.cleanupGCMTokens();
        logger.debug("initializing MPNS tokens cleanup: {} tokens", push.getInvalidMPNSTokens().size());
        tokenService.deleteTokens("mpns", push.getInvalidMPNSTokens().stream().collect(Collectors.toList()));
        push.cleanupMPNSTokens();
    }
}