aboutsummaryrefslogtreecommitdiff
path: root/juick-notifications/src/main/java/com/juick/components/CleanUp.java
blob: c78b3f77e8215a8b71660d8fde0d8f42c9969894 (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
49
50
51
52
package com.juick.components;

import com.juick.TokensList;
import com.notnoop.apns.ApnsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Collection;

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

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

    @Inject
    ApnsService apns;
    @Inject
    RestTemplate rest;

    @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);
            TokensList list = new TokensList();
            list.setType("apns");
            list.setTokens(new ArrayList<>(devices));
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            rest.exchange("http://api.juick.com/notifications",
                    HttpMethod.DELETE, new HttpEntity<>(list, headers), new ParameterizedTypeReference<Void>() {
                    });
        } else {
            logger.info("No APNS tokens to delete");
        }
    }
}