Project

General

Profile

1
package eu.dnetlib.repo.manager.service.customHystrixCommands;
2

    
3
import com.netflix.hystrix.HystrixCommand;
4
import com.netflix.hystrix.HystrixCommandGroupKey;
5
import org.springframework.http.HttpMethod;
6
import org.springframework.http.ResponseEntity;
7
import org.springframework.web.client.RestTemplate;
8
import org.springframework.web.util.UriComponents;
9
import org.springframework.web.util.UriComponentsBuilder;
10

    
11
import java.util.Map;
12

    
13
public class UsageStatsTotalHystrixCommand extends HystrixCommand<Integer> {
14

    
15
    RestTemplate restTemplate ;
16
    String usagestatsEvents;
17

    
18
    public UsageStatsTotalHystrixCommand(String usagestatsEvents,RestTemplate restTemplate) {
19
        super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
20
        this.usagestatsEvents = usagestatsEvents;
21
        this.restTemplate = restTemplate;
22
    }
23

    
24
    @Override
25
    protected Integer run() {
26
        UriComponents uriComponents = UriComponentsBuilder
27
                .fromHttpUrl(usagestatsEvents)
28
                .build().encode();
29

    
30
        ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class);
31
        Map metadata = (Map) ((Map)rs.getBody()).get("totals");
32
//        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
33
//        JSONObject resultSet = new JSONObject(rs);
34
//        JSONObject totals = resultSet.getJSONObject("totals");
35
        return (Integer) metadata.get("events");
36
    }
37

    
38
    @Override
39
    protected Integer getFallback() {
40
        return null;
41
    }
42

    
43
}
(9-9/9)