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 PublicationHystrixCommand extends HystrixCommand<String> {
14

    
15
    RestTemplate restTemplate;
16
    private String baseAddress;
17

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

    
24
    @Override
25
    protected String run() throws Exception {
26
        String url = baseAddress + "/publications/count";
27

    
28
        UriComponents uriComponents = UriComponentsBuilder
29
                .fromHttpUrl(url)
30
                .queryParam("page", 0)
31
                .queryParam("size", 0)
32
                .queryParam("format", "json")
33
                .build().encode();
34

    
35
        ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
36
        Map metadata = (Map) (rs.getBody());
37
        return String.valueOf(metadata.get("total"));
38
    }
39

    
40
    @Override
41
    protected String getFallback() {
42
        return null;
43
    }
44

    
45
}
(7-7/9)