Compare commits

...

1 Commits

Author SHA1 Message Date
ikalyvas 7c5e9430cb Logging Circuit breaker 2019-02-28 12:23:08 +02:00
13 changed files with 469 additions and 252 deletions

View File

@ -21,7 +21,13 @@ public abstract class AbstractBatchLogger {
public AbstractBatchLogger(Environment environment) { public AbstractBatchLogger(Environment environment) {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(() -> this.outputData(), Long.parseLong(environment.getProperty("http-logger.initial-delay")), Long.parseLong(environment.getProperty("http-logger.delay")), TimeUnit.SECONDS); executor.scheduleAtFixedRate(() -> {
try {
this.outputData();
} catch (Exception e) {
}
}, Long.parseLong(environment.getProperty("http-logger.initial-delay")), Long.parseLong(environment.getProperty("http-logger.delay")), TimeUnit.SECONDS);
} }
public abstract LoggingOutputType logOutputType(); public abstract LoggingOutputType logOutputType();
@ -53,5 +59,5 @@ public abstract class AbstractBatchLogger {
} else return null; } else return null;
} }
public abstract void outputData(); public abstract void outputData() throws Exception;
} }

View File

@ -15,7 +15,6 @@ import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import types.LoggingOutputType; import types.LoggingOutputType;
@ -31,257 +30,252 @@ import java.util.Map;
@Service("logger") @Service("logger")
public class HttpRemoteLogger extends AbstractBatchLogger implements Logger { public class HttpRemoteLogger extends AbstractBatchLogger implements Logger {
private RestTemplate rest; private RestTemplate rest;
private HttpHeaders headers; private HttpHeaders headers;
private Environment environment; private Environment environment;
@Autowired @Autowired
public HttpRemoteLogger(Environment environment) { public HttpRemoteLogger(Environment environment, RestTemplate loggerClient) {
super(environment); super(environment);
this.rest = new RestTemplate(); this.rest = loggerClient;
this.headers = new HttpHeaders(); this.headers = new HttpHeaders();
this.environment = environment; this.environment = environment;
headers.add("Content-Type", "application/json"); headers.add("Content-Type", "application/json");
headers.add("Accept", "*/*"); headers.add("Accept", "*/*");
} }
@Override @Override
public void outputData() { public void outputData() throws Exception {
try { String log = this.tranformLog();
String log = this.tranformLog(); if (log != null) {
if (log != null) { HttpEntity<String> requestEntity = new HttpEntity(log, headers);
HttpEntity<String> requestEntity = new HttpEntity(log, headers); rest.exchange(this.environment.getProperty("http-logger.server-address"), HttpMethod.POST, requestEntity, String.class);
ResponseEntity<String> Object = rest.exchange(this.environment.getProperty("http-logger.server-address"), HttpMethod.POST, requestEntity, String.class); }
} }
} catch (Exception ex) {
ex.printStackTrace();
}
} @Override
public LoggingOutputType logOutputType() {
return LoggingOutputType.JSON;
}
@Override @Override
public LoggingOutputType logOutputType() { public <T> void debug(T model) {
return LoggingOutputType.JSON; LoggingModel<T> loggingModel = new SimpleLoggingModel<>();
} loggingModel.setData(model);
loggingModel.setType(LoggingType.DEBUG);
this.put(String.valueOf(model.hashCode()), loggingModel);
}
@Override @Override
public <T> void debug(T model) { public <T> void debug(T model, String message) {
LoggingModel<T> loggingModel = new SimpleLoggingModel<>(); LoggingModel<T> loggingModel = new SimpleLoggingModel<>();
loggingModel.setData(model); loggingModel.setData(model);
loggingModel.setType(LoggingType.DEBUG); loggingModel.setMessage(message);
this.put(String.valueOf(model.hashCode()), loggingModel); loggingModel.setType(LoggingType.DEBUG);
} this.put(String.valueOf(model.hashCode()), loggingModel);
}
@Override @Override
public <T> void debug(T model, String message) { public <T> void debug(String message) {
LoggingModel<T> loggingModel = new SimpleLoggingModel<>(); LoggingModel<T> loggingModel = new SimpleLoggingModel<>();
loggingModel.setData(model); loggingModel.setMessage(message);
loggingModel.setMessage(message); loggingModel.setType(LoggingType.DEBUG);
loggingModel.setType(LoggingType.DEBUG); this.put(String.valueOf(message.hashCode()), loggingModel);
this.put(String.valueOf(model.hashCode()), loggingModel); }
}
@Override @Override
public <T> void debug(String message) { public <T extends Exception> void debug(T exception) {
LoggingModel<T> loggingModel = new SimpleLoggingModel<>(); ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
loggingModel.setMessage(message); Map<String, String> map = new HashMap<>();
loggingModel.setType(LoggingType.DEBUG); ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
this.put(String.valueOf(message.hashCode()), loggingModel); try {
} String json = ow.writeValueAsString(exception);
map.put("exception", json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
exceptionLoggingModel.setData(map);
exceptionLoggingModel.setType(LoggingType.DEBUG);
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
}
@Override @Override
public <T extends Exception> void debug(T exception) { public <T extends Exception> void debug(T exception, String message) {
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel(); ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
try { try {
String json = ow.writeValueAsString(exception); String json = ow.writeValueAsString(exception);
map.put("exception", json); map.put("exception", json);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
e.printStackTrace(); e.printStackTrace();
} }
exceptionLoggingModel.setData(map);
exceptionLoggingModel.setType(LoggingType.DEBUG);
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
}
@Override exceptionLoggingModel.setData(map);
public <T extends Exception> void debug(T exception, String message) { exceptionLoggingModel.setMessage(message);
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel(); exceptionLoggingModel.setType(LoggingType.DEBUG);
Map<String, String> map = new HashMap<>(); this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); }
try {
String json = ow.writeValueAsString(exception);
map.put("exception", json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
exceptionLoggingModel.setData(map); @Override
exceptionLoggingModel.setMessage(message); public <T> void warn(T model) {
exceptionLoggingModel.setType(LoggingType.DEBUG); LoggingModel<T> loggingModel = new SimpleLoggingModel<>();
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel); loggingModel.setData(model);
} loggingModel.setType(LoggingType.WARNING);
this.put(String.valueOf(model.hashCode()), loggingModel);
}
@Override @Override
public <T> void warn(T model) { public <T> void warn(T model, String message) {
LoggingModel<T> loggingModel = new SimpleLoggingModel<>(); LoggingModel<T> loggingModel = new SimpleLoggingModel<>();
loggingModel.setData(model); loggingModel.setData(model);
loggingModel.setType(LoggingType.WARNING); loggingModel.setMessage(message);
this.put(String.valueOf(model.hashCode()), loggingModel); loggingModel.setType(LoggingType.WARNING);
} this.put(String.valueOf(model.hashCode()), loggingModel);
}
@Override @Override
public <T> void warn(T model, String message) { public <T> void warn(String message) {
LoggingModel<T> loggingModel = new SimpleLoggingModel<>(); LoggingModel<T> loggingModel = new SimpleLoggingModel<>();
loggingModel.setData(model); loggingModel.setMessage(message);
loggingModel.setMessage(message); loggingModel.setType(LoggingType.WARNING);
loggingModel.setType(LoggingType.WARNING); this.put(String.valueOf(message.hashCode()), loggingModel);
this.put(String.valueOf(model.hashCode()), loggingModel); }
}
@Override @Override
public <T> void warn(String message) { public <T extends Exception> void warn(T exception) {
LoggingModel<T> loggingModel = new SimpleLoggingModel<>(); ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
loggingModel.setMessage(message); Map<String, String> map = new HashMap<>();
loggingModel.setType(LoggingType.WARNING); ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
this.put(String.valueOf(message.hashCode()), loggingModel); try {
} String json = ow.writeValueAsString(exception);
map.put("exception", json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
exceptionLoggingModel.setData(map);
exceptionLoggingModel.setType(LoggingType.WARNING);
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
}
@Override @Override
public <T extends Exception> void warn(T exception) { public <T extends Exception> void warn(T exception, String message) {
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel(); ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
try { try {
String json = ow.writeValueAsString(exception); String json = ow.writeValueAsString(exception);
map.put("exception", json); map.put("exception", json);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
e.printStackTrace(); e.printStackTrace();
} }
exceptionLoggingModel.setData(map); exceptionLoggingModel.setData(map);
exceptionLoggingModel.setType(LoggingType.WARNING); exceptionLoggingModel.setMessage(message);
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel); exceptionLoggingModel.setType(LoggingType.WARNING);
} this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
}
@Override @Override
public <T extends Exception> void warn(T exception, String message) { public <T> void info(String message) {
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel(); SimpleAuditModel<T> simpleAuditModel = new SimpleAuditModel<>();
Map<String, String> map = new HashMap<>(); simpleAuditModel.setMessage(message);
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); simpleAuditModel.setType(LoggingType.INFO);
try { this.put(String.valueOf(simpleAuditModel.hashCode()), simpleAuditModel);
String json = ow.writeValueAsString(exception); }
map.put("exception", json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
exceptionLoggingModel.setData(map);
exceptionLoggingModel.setMessage(message);
exceptionLoggingModel.setType(LoggingType.WARNING);
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
}
@Override @Override
public <T> void info(String message) { public <T> void info(T model) {
SimpleAuditModel<T> simpleAuditModel = new SimpleAuditModel<>(); SimpleAuditModel<T> simpleAuditModel = new SimpleAuditModel<>();
simpleAuditModel.setMessage(message); simpleAuditModel.setData(model);
simpleAuditModel.setType(LoggingType.INFO); simpleAuditModel.setType(LoggingType.INFO);
this.put(String.valueOf(simpleAuditModel.hashCode()), simpleAuditModel); this.put(String.valueOf(model.hashCode()), simpleAuditModel);
} }
@Override @Override
public <T> void info(T model) { public <T> void info(T model, String message) {
SimpleAuditModel<T> simpleAuditModel = new SimpleAuditModel<>(); SimpleAuditModel<T> simpleAuditModel = new SimpleAuditModel<>();
simpleAuditModel.setData(model); simpleAuditModel.setData(model);
simpleAuditModel.setType(LoggingType.INFO); simpleAuditModel.setMessage(message);
this.put(String.valueOf(model.hashCode()), simpleAuditModel); simpleAuditModel.setType(LoggingType.INFO);
} this.put(String.valueOf(model.hashCode()), simpleAuditModel);
}
@Override @Override
public <T> void info(T model, String message) { public <T extends RuntimeException> void info(T exception) {
SimpleAuditModel<T> simpleAuditModel = new SimpleAuditModel<>(); ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
simpleAuditModel.setData(model); Map<String, String> map = new HashMap<>();
simpleAuditModel.setMessage(message); ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
simpleAuditModel.setType(LoggingType.INFO); try {
this.put(String.valueOf(model.hashCode()), simpleAuditModel); String json = ow.writeValueAsString(exception);
} map.put("exception", json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
exceptionLoggingModel.setData(map);
exceptionLoggingModel.setType(LoggingType.INFO);
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
}
@Override @Override
public <T extends RuntimeException> void info(T exception) { public <T extends RuntimeException> void info(T exception, String message) {
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel(); ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
try { try {
String json = ow.writeValueAsString(exception); String json = ow.writeValueAsString(exception);
map.put("exception", json); map.put("exception", json);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
e.printStackTrace(); e.printStackTrace();
} }
exceptionLoggingModel.setData(map); exceptionLoggingModel.setData(map);
exceptionLoggingModel.setType(LoggingType.INFO); exceptionLoggingModel.setMessage(message);
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel); exceptionLoggingModel.setType(LoggingType.INFO);
} this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
}
@Override @Override
public <T extends RuntimeException> void info(T exception, String message) { public <E extends Exception, P> void error(ApiExceptionLoggingModel<E, P> model) {
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel(); this.put(String.valueOf(model.hashCode()), model);
Map<String, String> map = new HashMap<>(); }
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
try {
String json = ow.writeValueAsString(exception);
map.put("exception", json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
exceptionLoggingModel.setData(map);
exceptionLoggingModel.setMessage(message);
exceptionLoggingModel.setType(LoggingType.INFO);
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
}
@Override @Override
public <E extends Exception, P> void error(ApiExceptionLoggingModel<E, P> model) { public <T> void error(T model) {
this.put(String.valueOf(model.hashCode()), model); LoggingModel<T> loggingModel = new SimpleLoggingModel<>();
} loggingModel.setType(LoggingType.DEBUG);
loggingModel.setData(model);
this.put(String.valueOf(model.hashCode()), loggingModel);
}
@Override @Override
public <T> void error(T model) { public <T extends Exception> void error(T exception) {
LoggingModel<T> loggingModel = new SimpleLoggingModel<>(); ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
loggingModel.setType(LoggingType.DEBUG); Map<String, String> map = new HashMap<>();
loggingModel.setData(model); ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
this.put(String.valueOf(model.hashCode()), loggingModel); try {
} String json = ow.writeValueAsString(exception);
map.put("exception", json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
exceptionLoggingModel.setData(map);
exceptionLoggingModel.setType(LoggingType.ERROR);
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
}
@Override @Override
public <T extends Exception> void error(T exception) { public <T extends Exception> void error(T exception, String message) {
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel(); ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
try { try {
String json = ow.writeValueAsString(exception); String json = ow.writeValueAsString(exception);
map.put("exception", json); map.put("exception", json);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
e.printStackTrace(); e.printStackTrace();
} }
exceptionLoggingModel.setData(map); exceptionLoggingModel.setData(map);
exceptionLoggingModel.setType(LoggingType.ERROR); exceptionLoggingModel.setMessage(message);
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel); exceptionLoggingModel.setType(LoggingType.ERROR);
} this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
}
@Override
public <T extends Exception> void error(T exception, String message) {
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
Map<String, String> map = new HashMap<>();
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
try {
String json = ow.writeValueAsString(exception);
map.put("exception", json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
exceptionLoggingModel.setData(map);
exceptionLoggingModel.setMessage(message);
exceptionLoggingModel.setType(LoggingType.ERROR);
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
}
} }

View File

@ -1,20 +1,13 @@
package eu.eudat.configurations; package eu.eudat.configurations.database.elasticsearch;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import java.net.InetAddress;
/** /**
* Created by ikalyvas on 7/5/2018. * Created by ikalyvas on 7/5/2018.
*/ */

View File

@ -1,4 +1,4 @@
package eu.eudat.configurations; package eu.eudat.configurations.database.sql;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -7,7 +7,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.*; import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

View File

@ -1,4 +1,4 @@
package eu.eudat.configurations; package eu.eudat.configurations.database.sql;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;

View File

@ -48,7 +48,7 @@ public class DynamicProjectConfigurationDevelImpl implements DynamicProjectConfi
JAXBContext jaxbContext = JAXBContext.newInstance(Configuration.class); JAXBContext jaxbContext = JAXBContext.newInstance(Configuration.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
is = new URL("file:///"+current + "/web/src/main/resources/ProjectConfiguration.xml").openStream(); is = new URL("file:///"+current + "/dmp-backend/web/src/main/resources/ProjectConfiguration.xml").openStream();
this.configuration = (Configuration) jaxbUnmarshaller.unmarshal(is); this.configuration = (Configuration) jaxbUnmarshaller.unmarshal(is);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -69,14 +69,14 @@ public class DynamicProjectConfigurationDevelImpl implements DynamicProjectConfi
Configuration configuration = this.getConfiguration(); Configuration configuration = this.getConfiguration();
List<DynamicField> fields = new LinkedList<>(); List<DynamicField> fields = new LinkedList<>();
List<Property> properties = configuration.getConfigurationProperties(); List<Property> properties = configuration.getConfigurationProperties();
properties.stream().forEach(item -> { properties.forEach(item -> {
DynamicField dynamicField = new DynamicField(); DynamicField dynamicField = new DynamicField();
dynamicField.setId(item.getId()); dynamicField.setId(item.getId());
dynamicField.setName(item.getName()); dynamicField.setName(item.getName());
dynamicField.setQueryProperty(item.getQueryProperty()); dynamicField.setQueryProperty(item.getQueryProperty());
dynamicField.setRequired(item.getRequired()); dynamicField.setRequired(item.getRequired());
List<Dependency> dependencies = new LinkedList<>(); List<Dependency> dependencies = new LinkedList<>();
item.getDependencies().stream().forEach(dependency -> { item.getDependencies().forEach(dependency -> {
Dependency modelDependency = new Dependency(); Dependency modelDependency = new Dependency();
modelDependency.setId(dependency.getId()); modelDependency.setId(dependency.getId());
modelDependency.setQueryProperty(dependency.getQueryProperty()); modelDependency.setQueryProperty(dependency.getQueryProperty());

View File

@ -0,0 +1,48 @@
package eu.eudat.configurations.http;
import eu.eudat.logic.handlers.CircuitBreakerHttpClientInterceptor;
import eu.eudat.logic.utilities.circuitbreaker.Circuit;
import eu.eudat.logic.utilities.circuitbreaker.CircuitBreaker;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ikalyvas on 2/25/2019.
*/
@Configuration
public class HttpClientConfigurations {
@Bean
public RestTemplate loggerClient(CircuitBreaker circuitBreaker) {
RestTemplate restTemplate = new RestTemplate();
List<ClientHttpRequestInterceptor> interceptors
= restTemplate.getInterceptors();
if (CollectionUtils.isEmpty(interceptors)) {
interceptors = new ArrayList<>();
}
interceptors.add(new CircuitBreakerHttpClientInterceptor(circuitBreaker));
restTemplate.setInterceptors(interceptors);
return restTemplate;
}
@Bean
public CircuitBreaker circuitBreaker(){
CircuitBreaker circuitBreaker = new CircuitBreaker();
circuitBreaker.addCircuitResolver(
new Circuit.CircuitBuilder()
.url("http://localhost:31311")
.method("POST")
.build(),
httpRequest -> {
System.out.println(httpRequest);
});
return circuitBreaker;
}
}

View File

@ -1,13 +1,10 @@
package eu.eudat.configurations; package eu.eudat.configurations.misc;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** /**
* Created by ikalyvas on 9/26/2018. * Created by ikalyvas on 9/26/2018.

View File

@ -1,4 +1,4 @@
package eu.eudat.configurations; package eu.eudat.configurations.web;
import eu.eudat.controllers.interceptors.RequestInterceptor; import eu.eudat.controllers.interceptors.RequestInterceptor;
import eu.eudat.logic.handlers.PrincipalArgumentResolver; import eu.eudat.logic.handlers.PrincipalArgumentResolver;

View File

@ -0,0 +1,27 @@
package eu.eudat.logic.handlers;
import eu.eudat.logic.utilities.circuitbreaker.CircuitBreaker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
/**
* Created by ikalyvas on 2/25/2019.
*/
public class CircuitBreakerHttpClientInterceptor implements ClientHttpRequestInterceptor {
private CircuitBreaker circuitBreaker;
@Autowired
public CircuitBreakerHttpClientInterceptor(CircuitBreaker circuitBreaker) {
this.circuitBreaker = circuitBreaker;
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) {
System.out.println(request);
return this.circuitBreaker.handle(request, () -> execution.execute(request, body));
}
}

View File

@ -0,0 +1,98 @@
package eu.eudat.logic.utilities.circuitbreaker;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Created by ikalyvas on 2/25/2019.
*/
public final class Circuit {
private final String url;
private final String method;
private Integer numberOfFails;
private CircuitState state;
private final Integer MAX_NUMBER_OF_FAILS = 5;
public String getUrl() {
return url;
}
public String getMethod() {
return method;
}
public Integer getNumberOfFails() {
return numberOfFails;
}
public Circuit(String url, String method, Integer numberOfFails) {
this.url = url;
this.method = method;
this.numberOfFails = numberOfFails;
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(this::halfOpenCircuit, 0, 20, TimeUnit.SECONDS);
}
public enum CircuitState {
CLOSED, HALF_OPENED, OPENED
}
public static class CircuitBuilder {
private String url;
private String method;
public CircuitBuilder url(String url) {
this.url = url;
return this;
}
public CircuitBuilder method(String method) {
this.method = method;
return this;
}
public Circuit build() {
return new Circuit(url, method, 0);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Circuit circuit = (Circuit) o;
if (!url.equals(circuit.url)) return false;
return method.equals(circuit.method);
}
@Override
public int hashCode() {
int result = url.hashCode();
result = 31 * result + method.hashCode();
return result;
}
private void halfOpenCircuit() {
if (this.state == CircuitState.CLOSED) {
this.state = CircuitState.HALF_OPENED;
}
}
public void addFailure() {
this.numberOfFails++;
if(this.state == CircuitState.OPENED && this.numberOfFails > MAX_NUMBER_OF_FAILS){
this.state = CircuitState.CLOSED;
}
else if(this.state == CircuitState.HALF_OPENED){
this.state = CircuitState.CLOSED;
}
}
public boolean isOpen(){
return this.state == CircuitState.OPENED;
}
}

View File

@ -0,0 +1,47 @@
package eu.eudat.logic.utilities.circuitbreaker;
import eu.eudat.logic.utilities.interfaces.CheckedSupplier;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientResponseException;
import java.util.*;
import java.util.function.Consumer;
/**
* Created by ikalyvas on 2/25/2019.
*/
@Component
public class CircuitBreaker {
private Set<Circuit> circuits = new HashSet<>();
private Map<Circuit, Consumer<HttpRequest>> resolvers = new HashMap<>();
public <R> R handle(HttpRequest request, CheckedSupplier<R> function) throws RestClientResponseException {
Optional<Circuit> circuitOptional = this.circuits.stream()
.filter(circuit ->
circuit.getMethod().equals(request.getMethod().name()) && circuit.getUrl().equals(request.getURI().toString()))
.findFirst();
if (circuitOptional.isPresent() && circuitOptional.get().isOpen()) {
Consumer<HttpRequest> resolver = resolvers.get(circuitOptional.get());
if (resolver != null) {
resolver.accept(request);
return null;
} else throw new RuntimeException("Resolver not found for " + request);
} else {
try {
return function.get();
} catch (Exception ex) {
if (circuitOptional.isPresent()) circuitOptional.get().addFailure();
else
circuits.add(new Circuit.CircuitBuilder().method(request.getMethod().name()).url(request.getURI().toString()).build());
throw new RestClientResponseException(StringUtils.EMPTY, 400, StringUtils.EMPTY, null, null, null);
}
}
}
public CircuitBreaker addCircuitResolver(Circuit circuit, Consumer<HttpRequest> requestConsumer) {
this.resolvers.put(circuit, requestConsumer);
return this;
}
}

View File

@ -0,0 +1,8 @@
package eu.eudat.logic.utilities.interfaces;
/**
* Created by ikalyvas on 2/25/2019.
*/
public interface CheckedSupplier<T> {
T get() throws Exception;
}