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 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() { }); } else { logger.info("No APNS tokens to delete"); } } }