uoa-repository-manager-service/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/PublicationHystrixCommand.java

45 lines
1.4 KiB
Java

package eu.dnetlib.repo.manager.service.customHystrixCommands;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.Map;
public class PublicationHystrixCommand extends HystrixCommand<String> {
RestTemplate restTemplate;
private String baseAddress;
public PublicationHystrixCommand(String baseAddress, RestTemplate restTemplate) {
super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
this.baseAddress = baseAddress;
this.restTemplate = restTemplate;
}
@Override
protected String run() throws Exception {
String url = baseAddress + "/publications/count";
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(url)
.queryParam("page", 0)
.queryParam("size", 0)
.queryParam("format", "json")
.build().encode();
ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map) (rs.getBody());
return String.valueOf(metadata.get("total"));
}
@Override
protected String getFallback() {
return null;
}
}