Search in sources :

Example 1 with Receiver

use of org.qi4j.io.Receiver in project qi4j-sdk by Qi4j.

the class DataSourceConfigurationManagerServiceTest method init.

public void init(@Service @IdentifiedBy("testds") DataSource dataSource, @Service @IdentifiedBy("testds2") ServiceReference<DataSource> dataSource2) throws SQLException, PropertyVetoException {
    Databases databases = new Databases(dataSource);
    // Insert some data and print it out
    databases.update("insert into test values ('id1','foo')");
    databases.query("select * from test").transferTo(Outputs.withReceiver(new Receiver<ResultSet, SQLException>() {

        @Override
        public void receive(ResultSet item) throws SQLException {
            System.out.println(item.getString("id"));
        }
    }));
    Databases databases2 = new Databases(dataSource2.get());
    // Insert some data and print it out
    databases2.update("insert into test values ('id2','bar')");
    databases2.query("select * from test").transferTo(Outputs.withReceiver(new Receiver<ResultSet, SQLException>() {

        @Override
        public void receive(ResultSet item) throws SQLException {
            System.out.println(item.getString("id"));
        }
    }));
    // Trip the CB
    dataSource2.metaInfo(CircuitBreaker.class).trip();
    // This should now fail
    try {
        databases2.query("select * from test").transferTo(Outputs.withReceiver(new Receiver<ResultSet, SQLException>() {

            @Override
            public void receive(ResultSet item) throws SQLException {
                System.out.println(item.getString("id"));
            }
        }));
        Assert.fail();
    } catch (Throwable e) {
    // Correct
    }
    // Turn the CB back on
    dataSource2.metaInfo(CircuitBreaker.class).turnOn();
    // This should now work
    databases2.query("select * from test").transferTo(Outputs.withReceiver(new Receiver<ResultSet, SQLException>() {

        @Override
        public void receive(ResultSet item) throws SQLException {
            System.out.println(item.getString("id"));
        }
    }));
}
Also used : CircuitBreaker(org.qi4j.library.circuitbreaker.CircuitBreaker) Databases(org.qi4j.library.sql.common.Databases) ResultSet(java.sql.ResultSet) Receiver(org.qi4j.io.Receiver)

Aggregations

ResultSet (java.sql.ResultSet)1 Receiver (org.qi4j.io.Receiver)1 CircuitBreaker (org.qi4j.library.circuitbreaker.CircuitBreaker)1 Databases (org.qi4j.library.sql.common.Databases)1