示例#1
0
 public void filter() throws IOException {
   // START SNIPPET: filter
   File source = new File("source.txt");
   File destination = new File("destination.txt");
   Inputs.text(source).transferTo(Outputs.text(destination));
   // END SNIPPET: filter
 }
  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"));
                  }
                }));
  }