use of org.qi4j.library.circuitbreaker.CircuitBreaker in project qi4j-sdk by Qi4j.
the class CircuitBreakerManagementSample method main.
public static void main(String[] args) throws ActivationException, AssemblyException {
SingletonAssembler assembler = new SingletonAssembler() {
@Override
public // START SNIPPET: jmx
void assemble(ModuleAssembly module) throws AssemblyException {
// END SNIPPET: jmx
CircuitBreaker cb = new CircuitBreaker(3, 250, CircuitBreakers.in(IllegalArgumentException.class));
module.importedServices(TestService.class).setMetaInfo(new TestService(cb));
// START SNIPPET: jmx
// JMX Library
module.importedServices(MBeanServer.class).importedBy(MBeanServerImporter.class);
// CircuitBreakers in JMX
module.services(CircuitBreakerManagement.class).instantiateOnStartup();
}
};
TestService service = assembler.module().<TestService>findService(TestService.class).get();
// Seconds
int interval = 1;
System.out.println("CircuitBreaker JMX Support sample is now started.");
System.out.println();
System.out.println("A Service that randomly output some text or fail is called through a CircuitBreaker every " + interval + " seconds.");
System.out.println("In a few interval the CircuitBreaker will be turned off.");
System.out.println("Connect with a MBean browser (eg. VisualVM + MBean plugin) to use the turnOn operation on the CircuitBreakers.");
System.out.println();
System.out.println("Hit Ctrl-C to stop.");
System.out.println();
while (true) {
try {
Thread.sleep(interval * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
service.helloWorld();
}
}
use of org.qi4j.library.circuitbreaker.CircuitBreaker in project qi4j-sdk by Qi4j.
the class BreakCircuitConcern method invoke.
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
CircuitBreaker circuitBreaker = serviceCircuitBreaker.circuitBreaker();
try {
if (!circuitBreaker.isOn()) {
throw circuitBreaker.lastThrowable();
}
Object result = next.invoke(proxy, method, args);
circuitBreaker.success();
return result;
} catch (Throwable throwable) {
circuitBreaker.throwable(throwable);
throw throwable;
}
}
use of org.qi4j.library.circuitbreaker.CircuitBreaker in project qi4j-sdk by Qi4j.
the class AbstractDataSourceServiceImporterMixin method importService.
@Override
public final synchronized DataSource importService(final ImportedServiceDescriptor importedServiceDescriptor) throws ServiceImporterException {
PooledDataSourceType pool = pools.get(importedServiceDescriptor.identity());
if (pool == null) {
try {
DataSourceConfigurationValue config = getConfiguration(importedServiceDescriptor.identity());
if (!config.enabled().get()) {
// Not started
throw new ServiceImporterException("DataSource not enabled");
}
// Instantiate pool
pool = setupDataSourcePool(config);
pools.put(importedServiceDescriptor.identity(), pool);
LOGGER.info("Starting up DataSource '" + importedServiceDescriptor.identity() + "' for: {}@{}", config.username().get(), config.url().get());
} catch (Exception e) {
throw new ServiceImporterException(e);
}
// Test the pool
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(null);
try {
pool.getConnection().close();
LOGGER.info("Database for DataSource is up!");
} catch (SQLException e) {
LOGGER.warn("Database for DataSource " + importedServiceDescriptor.identity() + " is not currently available");
throw new ServiceImporterException("Database for DataSource " + importedServiceDescriptor.identity() + " is not currently available", e);
} finally {
Thread.currentThread().setContextClassLoader(cl);
}
}
// Check if circuitbreaker is used
final CircuitBreaker circuitBreaker = importedServiceDescriptor.metaInfo(CircuitBreaker.class);
if (circuitBreaker != null) {
DataSource wrappedDataSource = DataSources.wrapWithCircuitBreaker(importedServiceDescriptor.identity(), pool, circuitBreaker);
circuitBreakers.put(pool, circuitBreaker);
return wrappedDataSource;
} else {
return pool;
}
}
Aggregations