org.jooby.funzy.Try类的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(6.5k)|赞(0)|评价(0)|浏览(137)

本文整理了Java中org.jooby.funzy.Try类的一些代码示例,展示了Try类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Try类的具体详情如下:
包路径:org.jooby.funzy.Try
类名称:Try

Try介绍

[英]Functional try and try-with-resources implementation.
[中]功能性的尝试和资源实现的尝试。

代码示例

代码示例来源:origin: jooby-project/jooby

@Override
public void writeSuccess() {
 Try.run(success::invoke)
   .onFailure(cause -> log.error("Error while invoking success callback", cause));
}

代码示例来源:origin: jooby-project/jooby

public boolean exists(final Predicate<Path> filter) {
 try (Stream<Path> walk = Try.apply(() -> Files.walk(root)).orElse(Stream.empty())) {
  return walk
    .skip(1)
    .anyMatch(filter);
 }
}

代码示例来源:origin: jooby-project/jooby

/**
  * Execute the given nodejs callback and automatically releaseNow v8 and nodejs resources.
  *
  * @param basedir Base dir where to deploy a library.
  * @param callback Nodejs callback.
  */
 public static void run(final File basedir, final Throwing.Consumer<Nodejs> callback) {
  Nodejs node = new Nodejs(basedir);
  Try.run(() -> callback.accept(node))
    .onComplete(node::release)
    .throwException();
 }
}

代码示例来源:origin: jooby-project/jooby

private static void copy(final InputStream in, final OutputStream out) {
 Try.of(in, out)
   .run(ByteStreams::copy)
   .throwException();
}

代码示例来源:origin: jooby-project/jooby

public void release() {
 Try.run(mem::release)
   .onComplete(() -> releaseNow(v8));
}

代码示例来源:origin: jooby-project/jooby

log.debug("{}", uri);
Path outdir = this.basedir.toPath().resolve("node_modules").resolve(library.replace("/", "."));
Optional<Path> basedir = Try.apply(() -> Paths.get(uri)).toOptional();
String libroot = Route.normalize("/" + library);
try (Library lib = loadLibrary(uri)) {
        .toArray(new StandardCopyOption[this.coptions.size()]);
      Try.run(() -> Files.copy(it, output, coptions))
        .onFailure(x -> log.error("can't copy {}", it, x));

代码示例来源:origin: jooby-project/jooby

public void end() {
 try {
  if (rollbackOnly) {
   rollback();
  } else {
   commit();
  }
 } finally {
  if (readOnly) {
   setConnectionReadOnly(false);
  }
  String sessionId = oid(session);
  log.debug("closing session: {}", sessionId);
  Try.run(session::close)
    .onFailure(x -> log.error("session.close() resulted in exception: {}", sessionId, x))
    .onSuccess(() -> log.debug("session closed: {}", sessionId));
  unbind(session.getSessionFactory());
 }
}

代码示例来源:origin: jooby-project/jooby

Try.run(() -> {
  method.setAccessible(true);
  method.invoke(owner);
 }).unwrap(InvocationTargetException.class)
   .throwException();
});

代码示例来源:origin: jooby-project/jooby

File workdir = new File(Try.apply(() -> conf.getString("application.tmpdir"))
  .orElse(System.getProperty("java.io.tmpdir")));
     Float h = new Float(params.getDouble(3));
     V8Function callback = (V8Function) params.get(4);
     Try.run(() -> {
      try (FileReader in = new FileReader(svgPath);
        OutputStream out = new FileOutputStream(pngPath)) {
       .onSuccess(() -> callback.call(null, null))
       .onFailure(x -> {
        log.debug("png-fallback resulted in exception", x);
        callback.call(null, toV8Array(v8, Arrays.asList(x.getMessage())));
 sha1files.forEach(it -> Try.run(() -> Files.delete(it)));

代码示例来源:origin: jooby-project/jooby

private Jooby hackInjector(final Jooby app) {
 Try.run(() -> {
  Field field = Jooby.class.getDeclaredField("injector");
  field.setAccessible(true);
  Injector injector = proxyInjector(getClass().getClassLoader(), registry);
  field.set(app, injector);
  registry.put(Key.get(Injector.class), injector);
 }).throwException();
 return app;
}

代码示例来源:origin: jooby-project/jooby

@Override
public void tryRun() throws Throwable {
 if (dep == null) {
  return;
 }
 Try.run(() -> {
  Optional<Method> shutdown = Arrays.stream(dep.getClass().getMethods())
    .filter(m -> m.getName().startsWith("shutdown")
      && m.getParameterCount() == 0
      && Modifier.isPublic(m.getModifiers()))
    .findFirst();
  if (shutdown.isPresent()) {
   log.debug("stopping {}", dep);
   shutdown.get().invoke(dep);
  } else {
   log.debug("no shutdown method found for: {}", dep);
  }
 }).unwrap(InvocationTargetException.class)
   .onComplete(() -> this.dep = null)
   .throwException();
}

代码示例来源:origin: jooby-project/jooby

private static List<URI> expandPath(final ClassLoader loader, final String pattern) {
 List<URI> result = new ArrayList<>();
 File file = new File(pattern);
 if (file.exists()) {
  result.add(file.toURI());
 }
 Try.run(() -> Collections.list(loader.getResources(pattern))
   .stream()
   .map(it -> Try.apply(it::toURI).get())
   .forEach(result::add));
 return result;
}

代码示例来源:origin: jooby-project/jooby

/**
 * Release nodejs and v8 resources.
 */
public void release() {
 Try.run(scope::release);
 node.release();
}

代码示例来源:origin: org.jooby/funzy

/**
 * Run the given action if and only if this is a failure.
 *
 * @param action Failure action/listener.
 * @return This try.
 */
@Override public Value<V> onFailure(final Consumer<? super Throwable> action) {
 super.onFailure(action);
 return this;
}

代码示例来源:origin: jooby-project/jooby

@Override
public void send(final ByteBuffer buffer) throws Exception {
 Try.of(Channels.newChannel(rsp.getOutputStream()))
   .run(channel -> channel.write(buffer))
   .onSuccess(() -> committed = true);
}

代码示例来源:origin: org.jooby/funzy

/**
 * Always run the given action, works like a finally clause.
 *
 * @param action Finally action.
 * @return This try result.
 */
@Override public Value<V> onComplete(Throwing.Runnable action) {
 return (Value<V>) super.onComplete(action);
}

代码示例来源:origin: org.jooby/funzy

@Override public <X extends Throwable> Value<V> unwrap(Class<? extends X> type) {
 return (Value<V>) super.unwrap(type);
}

代码示例来源:origin: org.jooby/funzy

/**
 * Run the given action if and only if this is a success.
 *
 * @param action Success listener.
 * @return This try.
 */
@Override public Value<V> onSuccess(final Runnable action) {
 super.onSuccess(action);
 return this;
}

代码示例来源:origin: jooby-project/jooby

buckets.forEach(n -> Try.apply(() -> r.require(n, Bucket.class).close())
   .onFailure(x -> log.debug("bucket {} close operation resulted in exception", n, x))
   .orElse(false));
 Try.run(cluster::disconnect)
   .onFailure(x -> log.debug("disconnect operation resulted in exception", x));
 Try.run(cenv::shutdown)
   .onFailure(x -> log.debug("environment shutdown resulted in exception", x));
});

代码示例来源:origin: jooby-project/jooby

@Override
default void handle(final Request req, final Response rsp, final Chain chain) throws Throwable {
 Sse sse = req.require(Sse.class);
 String path = req.path();
 rsp.send(new Deferred(deferred -> {
  try {
   sse.handshake(req, () -> {
    Try.run(() -> handle(req, sse))
      .onSuccess(() -> deferred.resolve(null))
      .onFailure(ex -> {
       deferred.reject(ex);
       Logger log = LoggerFactory.getLogger(Sse.class);
       log.error("execution of {} resulted in error", path, ex);
      });
   });
  } catch (Exception ex) {
   deferred.reject(ex);
  }
 }));
}

相关文章