org.jooby.Env.onStop()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(154)

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

Env.onStop介绍

暂无

代码示例

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

@Inject
public FileMonitor(final Injector injector, final Env env,
  final WatchService watcher, final Set<FileEventOptions> optionList) {
 this.injector = injector;
 this.watcher = watcher;
 this.optionList = optionList;
 // start monitor:
 ExecutorService monitor = Executors.newSingleThreadExecutor(task -> {
  Thread thread = new Thread(task, "file-watcher");
  thread.setDaemon(true);
  return thread;
 });
 env.onStop(monitor::shutdown);
 monitor.execute(this);
}

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

private void after(final Env env, final Binder binder, final Config config,
  final Object service) {
 after.forEach(it -> {
  try {
   Object dep = it.apply(service, config);
   requireNonNull(dep, "A nonnull value is required.");
   Class type = dep.getClass();
   binder.bind(type).toInstance(dep);
   env.onStop(new AwsShutdownSupport(dep));
  } catch (ClassCastException ex) {
   log.debug("ignoring callback {}", it);
  }
 });
}

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

@Override
public void configure(final Env env, final Config config, final Binder binder) {
 HttpHost[] httpHosts = Arrays.stream(hosts).map(HttpHost::create).toArray(HttpHost[]::new);
 RestClient restClient = RestClient.builder(httpHosts).build();
 binder.bind(RestClient.class).toInstance(restClient);
 env.onStop(restClient::close);
}

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

@Override
public void configure(final Env env, final Config conf, final Binder binder) {
 com.hazelcast.config.Config config = new com.hazelcast.config.Config();
 config.setProperties(toProperties(conf.getConfig("hazelcast")));
 if (configurer != null) {
  configurer.accept(config, conf);
 }
 HazelcastInstance hcast = Hazelcast.newHazelcastInstance(config);
 binder.bind(com.hazelcast.config.Config.class).toInstance(config);
 binder.bind(HazelcastInstance.class).toInstance(hcast);
 env.onStop(hcast::shutdown);
}

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

@Override
 public void configure(final Env env, final Config config, final Binder binder) {
  Key<DataSource> dskey = Key.get(DataSource.class, Names.named(name));
  DataSource ds = env.get(dskey)
    .orElseThrow(() -> new NoSuchElementException("DataSource missing: " + dskey));
  Database db = Database.fromDataSource(ds);

  env.serviceKey().generate(Database.class, name, k -> binder.bind(k).toInstance(db));

  // close on shutdown
  env.onStop(db::close);
 }
}

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

@Override public void configure(Env env, Config conf, Binder binder) {
  EventBus eventbus = factory.apply(conf);
  binder.bind(EventBus.class).toInstance(eventbus);

  List<Object> subscribers = new ArrayList<>(initialSubscribers.size());
  /** Register subscribers: */
  env.onStart(registry -> {
   initialSubscribers.forEach(candidate -> {
      Object subscriber = candidate;
      if (subscriber instanceof Class) {
       subscriber = registry.require((Class) subscriber);
      }
      subscribers.add(subscriber);
      eventbus.register(subscriber);
     }
   );
   // free initial subscribers
   initialSubscribers.clear();
  });
  /** Unregister subscribers: */
  env.onStop(() -> subscribers.forEach(eventbus::unregister));
 }
}

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

@SuppressWarnings({"unchecked", "rawtypes" })
@Override
public void configure(final Env env, final Config conf, final Binder binder) throws Throwable {
 String db = database(conf, this.db);
 Properties props = props(neo4j(conf, this.db));
 ServiceKey keys = env.serviceKey();
 IDBAccess dbaccess = dbaccess(conf, this.db, db, props, keys, binder);
 Arrays.asList(props.getProperty(SERVER_ROOT_URI), props.getProperty(DATABASE_DIR))
   .stream()
   .filter(Objects::nonNull)
   .findFirst()
   .ifPresent(it -> log.info("Starting neo4j: {}", it));
 Class dbaccessType = dbaccess.getClass();
 keys.generate(IDBAccess.class, this.db, k -> binder.bind(k).toInstance(dbaccess));
 keys.generate(dbaccessType, this.db, k -> binder.bind(k).toInstance(dbaccess));
 env.onStop(dbaccess::close);
}

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

@SuppressWarnings({"unchecked", "rawtypes"})
private static void guavaServices(final Env env, final Binder binder,
  final Set<Class<Service>> serviceTypes) {
 Consumer<Class> guavaService = klass -> {
  binder.bind(klass).asEagerSingleton();
  serviceTypes.add(klass);
 };
 serviceTypes.forEach(guavaService);
 // lazy service manager
 AtomicReference<ServiceManager> sm = new AtomicReference<>();
 Provider<ServiceManager> smProvider = sm::get;
 binder.bind(ServiceManager.class).toProvider(smProvider);
 // ask Guice for services, create ServiceManager and start services
 env.onStart(r -> {
  List<Service> services = serviceTypes.stream()
    .map(r::require)
    .collect(Collectors.toList());
  sm.set(new ServiceManager(services));
  sm.get().startAsync().awaitHealthy();
 });
 // stop services
 env.onStop(() -> sm.get().stopAsync().awaitStopped());
}

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

@SuppressWarnings("unchecked")
@Override
public void configure(final Env env, final Config conf, final Binder binder) {
 Config $memcached = conf.getConfig("memcached")
   .withFallback(conf.getConfig("memcached"));
 ConnectionFactoryBuilder builder = newConnectionFactoryBuilder($memcached);
 if (configurer != null) {
  configurer.accept(builder, conf);
 }
 List<String> servers = new ArrayList<>();
 Object $servers = conf.getAnyRef("memcached.server");
 if ($servers instanceof List) {
  servers.addAll((Collection<? extends String>) $servers);
 } else {
  servers.add($servers.toString());
 }
 MemcachedClientProvider provider = new MemcachedClientProvider(
   builder,
   AddrUtil.getAddresses(servers),
   $memcached.getDuration("shutdownTimeout", TimeUnit.MILLISECONDS));
 env.onStop(provider::destroy);
 binder
   .bind(MemcachedClient.class)
   .toProvider(provider)
   .asEagerSingleton();
}

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

.ifPresent(e -> bind(binder, null, e.getValue()));
env.onStop(() -> {
 services.forEach(exec -> Try.run(() -> exec.getValue().shutdown()).onFailure(cause -> log.error("shutdown of {} resulted in error", exec.getKey(), cause)));
 services.clear();

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

protected void configure(final Env env, final Config config, final Binder binder,
  final BiConsumer<MongoClientURI, MongoClient> callback) {
 MongoClientOptions.Builder options = options(mongodb(config));
 if (this.options != null) {
  this.options.accept(options, config);
 }
 MongoClientURI uri = new MongoClientURI(config.getString(db), options);
 String database = uri.getDatabase();
 checkArgument(database != null, "Database not found: " + uri);
 MongoClient client = new MongoClient(uri);
 ServiceKey serviceKey = env.serviceKey();
 serviceKey.generate(MongoClientURI.class, database, k -> binder.bind(k).toInstance(uri));
 serviceKey.generate(MongoClient.class, database,
   k -> binder.bind(k).toInstance(client));
 MongoDatabase mongodb = client.getDatabase(database);
 serviceKey.generate(MongoDatabase.class, database,
   k -> binder.bind(k).toInstance(mongodb));
 env.onStop(client::close);
 callback.accept(uri, client);
}

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

@Override
public void configure(final Env env, final Config config, final Binder binder) {
 callbacks.build().forEach(it -> {
  ForwardingCredentialsProvider fcp = new ForwardingCredentialsProvider();
  Object service = it.apply(fcp, config);
  String serviceName;
  if (service instanceof AmazonWebServiceClient) {
   serviceName = ((AmazonWebServiceClient) service).getServiceName();
  } else {
   serviceName = service.getClass().getSimpleName()
     .replace("AWS", "")
     .replace("Amazon", "")
     .replace("JavaClient", "")
     .replace("ServiceClient", "")
     .replace("Client", "")
     .toLowerCase();
  }
  fcp.setProvider(CredentialsFactory.create(config, serviceName));
  Class serviceType = service.getClass();
  Class[] interfaces = serviceType.getInterfaces();
  if (interfaces.length > 0) {
   // pick first
   binder.bind(interfaces[0]).toInstance(service);
  }
  binder.bind(serviceType).toInstance(service);
  env.onStop(new AwsShutdownSupport(service));
  after(env, binder, config, service);
 });
}

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

@Override
public void configure(final Env env, final Config config, final Binder binder) {
 /**
  * Pool
  */
 GenericObjectPoolConfig poolConfig = poolConfig(config, name);
 int timeout = (int) config.getDuration("jedis.timeout", TimeUnit.MILLISECONDS);
 URI uri = URI.create(config.getString(name));
 JedisPool pool = new JedisPool(poolConfig, uri, timeout);
 RedisProvider provider = new RedisProvider(pool, uri, poolConfig);
 env.onStart(provider::start);
 env.onStop(provider::stop);
 Provider<Jedis> jedis = (Provider<Jedis>) () -> pool.getResource();
 ServiceKey serviceKey = env.serviceKey();
 serviceKey.generate(JedisPool.class, name, k -> binder.bind(k).toInstance(pool));
 serviceKey.generate(Jedis.class, name,
   k -> binder.bind(k).toProvider(jedis));
}

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

@Override
public void configure(final Env env, final Config conf, final Binder binder) {
 // empty metric & checks
 MapBinder.newMapBinder(binder, String.class, Metric.class);
 MapBinder.newMapBinder(binder, String.class, HealthCheck.class);
 Router routes = env.router();
 MetricHandler mhandler = new MetricHandler();
 routes.use("GET", this.pattern + "/metrics", mhandler);
 routes.use("GET", this.pattern + "/metrics/:type", mhandler);
 routes.use("GET", this.pattern + "/healthcheck", new HealthCheckHandler());
 Multibinder<Reporter> reporters = Multibinder.newSetBinder(binder, Reporter.class);
 binder.bind(MetricRegistry.class).toInstance(metricRegistry);
 this.reporters.forEach(it -> reporters.addBinding().toInstance(it.apply(metricRegistry, conf)));
 binder.bind(MetricRegistryInitializer.class).asEagerSingleton();
 env.onStop(app -> app.require(MetricRegistryInitializer.class).close());
 binder.bind(HealthCheckRegistry.class).toInstance(healthCheckRegistry);
 binder.bind(HealthCheckRegistryInitializer.class).asEagerSingleton();
 bindings.forEach(it -> it.bind(binder, routes, conf));
 this.routes.forEach(it -> it.accept(routes));
}

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

});
env.onStop(crash::stop);

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

@Override
public void configure(final Env env, final Config conf, final Binder binder) {
 // dump rx.* as system properties
 conf.getConfig("rx")
  .withoutPath("schedulers").entrySet()
  .forEach(
   e -> System.setProperty("rx." + e.getKey(), e.getValue().unwrapped().toString()));
 Map<String, Executor> executors = new HashMap<>();
 super.configure(env, conf, binder, executors::put);
 env.router()
  .map(rx(observable, single, completable));
 /**
  * Side effects of global/evil static state. Hack to turn off some of this errors.
  */
 trySchedulerHook(executors);
 // shutdown schedulers: silent shutdown in dev mode between app reloads
 env.onStop(() -> {
  try {
   Schedulers.shutdown();
  } catch (Throwable ex) {
   log.debug("Schedulers.shutdown() resulted in error", ex);
  }
 });
}

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

env.onStop(consul::destroy);
 env.onStop(() -> agentClient.deregister(registration.getId()));

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

env.onStop(cm::shutdown);

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

env.onStop(ebean::stop);

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

.map(new CassandraMapper());
env.onStop(() -> {
 log.debug("Stopping {}", cstr);
 Try.run(session::close)

相关文章