Project

General

Profile

1 54840 panagiotis
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 SoftwareHystrixCommand extends HystrixCommand<String> {
14
15
    String baseAddress;
16
    RestTemplate restTemplate;
17
18
    public SoftwareHystrixCommand(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() {
26
        String url = baseAddress + "/software/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
}