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

43 lines
1.5 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 UsageStatsTotalHystrixCommand extends HystrixCommand<Integer> {
RestTemplate restTemplate ;
String usagestatsEvents;
public UsageStatsTotalHystrixCommand(String usagestatsEvents,RestTemplate restTemplate) {
super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
this.usagestatsEvents = usagestatsEvents;
this.restTemplate = restTemplate;
}
@Override
protected Integer run() {
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(usagestatsEvents)
.build().encode();
ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class);
Map metadata = (Map) ((Map)rs.getBody()).get("totals");
// String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
// JSONObject resultSet = new JSONObject(rs);
// JSONObject totals = resultSet.getJSONObject("totals");
return (Integer) metadata.get("events");
}
@Override
protected Integer getFallback() {
return null;
}
}