Search in sources :

Example 1 with CircuitBreaker

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();
    }
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) CircuitBreaker(org.qi4j.library.circuitbreaker.CircuitBreaker) ServiceCircuitBreaker(org.qi4j.library.circuitbreaker.service.ServiceCircuitBreaker) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) MBeanServer(javax.management.MBeanServer)

Example 2 with CircuitBreaker

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;
    }
}
Also used : CircuitBreaker(org.qi4j.library.circuitbreaker.CircuitBreaker)

Example 3 with CircuitBreaker

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;
    }
}
Also used : CircuitBreaker(org.qi4j.library.circuitbreaker.CircuitBreaker) SQLException(java.sql.SQLException) ServiceImporterException(org.qi4j.api.service.ServiceImporterException) ServiceImporterException(org.qi4j.api.service.ServiceImporterException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) IOException(java.io.IOException) SQLException(java.sql.SQLException) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) DataSource(javax.sql.DataSource)

Aggregations

CircuitBreaker (org.qi4j.library.circuitbreaker.CircuitBreaker)3 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 MBeanServer (javax.management.MBeanServer)1 DataSource (javax.sql.DataSource)1 ServiceImporterException (org.qi4j.api.service.ServiceImporterException)1 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)1 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)1 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)1 SingletonAssembler (org.qi4j.bootstrap.SingletonAssembler)1 ServiceCircuitBreaker (org.qi4j.library.circuitbreaker.service.ServiceCircuitBreaker)1