org.apache.druid.java.util.common.lifecycle.Lifecycle类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(11.3k)|赞(0)|评价(0)|浏览(141)

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

Lifecycle介绍

[英]A manager of object Lifecycles.

This object has methods for registering objects that should be started and stopped. The Lifecycle allows for three stages: Stage.INIT, Stage.NORMAL, and Stage.LAST.

Things added at Stage.INIT will be started first (in the order that they are added to the Lifecycle instance) and then things added at Stage.NORMAL, and finally, Stage.LAST will be started.

The close operation goes in reverse order, starting with the last thing added at Stage.LAST and working backwards.

There are two sets of methods to add things to the Lifecycle. One set that will just add instances and enforce that start() has not been called yet. The other set will add instances and, if the lifecycle is already started, start them.
[中]对象生命周期的管理器。
此对象具有注册应启动和停止的对象的方法。生命周期允许三个阶段:阶段。初始,阶段。正常,和阶段。最后的
在舞台上添加的东西。INIT将首先启动(按照它们添加到生命周期实例的顺序),然后在阶段中添加内容。正常,最后,阶段。最后一步将开始。
关闭操作以相反的顺序进行,从阶段中添加的最后一项开始。最后一步是向后工作。
有两组方法可以向生命周期添加内容。一个只会添加实例并强制执行start()的集合尚未调用。另一组将添加实例,如果生命周期已经启动,则启动它们。

代码示例

代码示例来源:origin: apache/incubator-druid

final Lifecycle leaderLifecycle = new Lifecycle("task-master");
if (leaderLifecycleRef.getAndSet(leaderLifecycle) != null) {
 log.makeAlert("TaskMaster set a new Lifecycle without the old one being cleared!  Race condition")
leaderLifecycle.addManagedInstance(taskRunner);
leaderLifecycle.addManagedInstance(taskQueue);
leaderLifecycle.addManagedInstance(supervisorManager);
leaderLifecycle.addManagedInstance(overlordHelperManager);
leaderLifecycle.addHandler(
  new Lifecycle.Handler()
leaderLifecycle.start();

代码示例来源:origin: apache/incubator-druid

@Override
 public void stop()
 {
  metamxLifecycle.stop();
 }
});

代码示例来源:origin: apache/incubator-druid

/**
 * Adds a handler to the Lifecycle at the Stage.NORMAL stage and starts it if the lifecycle has already been started.
 *
 * @param handler The hander to add to the lifecycle
 *
 * @throws Exception {@link Lifecycle#addMaybeStartHandler(Handler, Stage)}
 */
public void addMaybeStartHandler(Handler handler) throws Exception
{
 addMaybeStartHandler(handler, Stage.NORMAL);
}

代码示例来源:origin: apache/incubator-druid

public static Lifecycle asMmxLifecycle(Lifecycle lifecycle)
 {
  final Lifecycle metamxLifecycle = new Lifecycle("http-client");
  try {
   lifecycle.addMaybeStartHandler(new Lifecycle.Handler()
   {
    @Override
    public void start() throws Exception
    {
     metamxLifecycle.start();
    }

    @Override
    public void stop()
    {
     metamxLifecycle.stop();
    }
   });
  }
  catch (Exception e) {
   throw Throwables.propagate(e);
  }

  return metamxLifecycle;
 }
}

代码示例来源:origin: apache/incubator-druid

private Emitter parametrizedEmmiter(String uriPattern) throws Exception
{
 final Properties props = new Properties();
 props.setProperty("org.apache.druid.java.util.emitter.type", "parametrized");
 props.setProperty("org.apache.druid.java.util.emitter.recipientBaseUrlPattern", uriPattern);
 lifecycle = new Lifecycle();
 Emitter emitter = Emitters.create(props, httpClient, lifecycle);
 assertEquals(ParametrizedUriEmitter.class, emitter.getClass());
 lifecycle.start();
 return emitter;
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testHttpSilentServerWithRequestTimeout() throws Throwable
{
 final Lifecycle lifecycle = new Lifecycle();
 try {
  final HttpClientConfig config = HttpClientConfig.builder().withReadTimeout(new Duration(86400L * 365)).build();
  final HttpClient client = HttpClientInit.createClient(config, lifecycle);
  final ListenableFuture<StatusResponseHolder> future = client
    .go(
      new Request(HttpMethod.GET, new URL(StringUtils.format("http://localhost:%d/", silentServerSocket.getLocalPort()))),
      new StatusResponseHandler(StandardCharsets.UTF_8),
      new Duration(100L)
    );
  Throwable e = null;
  try {
   future.get();
  }
  catch (ExecutionException e1) {
   e = e1.getCause();
  }
  Assert.assertTrue("ReadTimeoutException thrown by 'get'", e instanceof ReadTimeoutException);
 }
 finally {
  lifecycle.stop();
 }
}

代码示例来源:origin: apache/incubator-druid

@Override
public void run()
{
 Injector injector = DruidTestModuleFactory.getInjector();
 IntegrationTestingConfig config = injector.getInstance(IntegrationTestingConfig.class);
 HttpClient client = injector.getInstance(Key.get(HttpClient.class, TestClient.class));
 waitUntilInstanceReady(client, config.getCoordinatorUrl());
 waitUntilInstanceReady(client, config.getIndexerUrl());
 waitUntilInstanceReady(client, config.getBrokerUrl());
 String routerHost = config.getRouterUrl();
 if (null != routerHost) {
  waitUntilInstanceReady(client, config.getRouterUrl());
 }
 Lifecycle lifecycle = injector.getInstance(Lifecycle.class);
 try {
  lifecycle.start();
  runTests();
 }
 catch (Exception e) {
  LOG.error(e, "");
  throw Throwables.propagate(e);
 }
 finally {
  lifecycle.stop();
 }
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testSanity() throws Exception
{
 Lifecycle lifecycle = new Lifecycle();
 List<Integer> startOrder = new ArrayList<>();
 List<Integer> stopOrder = new ArrayList<>();
 lifecycle.addManagedInstance(new ObjectToBeLifecycled(0, startOrder, stopOrder));
 lifecycle.addManagedInstance(new ObjectToBeLifecycled(1, startOrder, stopOrder), Lifecycle.Stage.NORMAL);
 lifecycle.addManagedInstance(new ObjectToBeLifecycled(2, startOrder, stopOrder), Lifecycle.Stage.NORMAL);
 lifecycle.addManagedInstance(new ObjectToBeLifecycled(3, startOrder, stopOrder), Lifecycle.Stage.LAST);
 lifecycle.addStartCloseInstance(new ObjectToBeLifecycled(4, startOrder, stopOrder));
 lifecycle.addManagedInstance(new ObjectToBeLifecycled(5, startOrder, stopOrder));
 lifecycle.addStartCloseInstance(new ObjectToBeLifecycled(6, startOrder, stopOrder), Lifecycle.Stage.LAST);
 lifecycle.addManagedInstance(new ObjectToBeLifecycled(7, startOrder, stopOrder));
 lifecycle.addStartCloseInstance(new ObjectToBeLifecycled(8, startOrder, stopOrder), Lifecycle.Stage.INIT);
 final List<Integer> expectedOrder = Arrays.asList(8, 0, 1, 2, 4, 5, 7, 3, 6);
 lifecycle.start();
 Assert.assertEquals(9, startOrder.size());
 Assert.assertEquals(0, stopOrder.size());
 Assert.assertEquals(expectedOrder, startOrder);
 lifecycle.stop();
 Assert.assertEquals(9, startOrder.size());
 Assert.assertEquals(9, stopOrder.size());
 Assert.assertEquals(Lists.reverse(expectedOrder), stopOrder);
}

代码示例来源:origin: apache/incubator-druid

/**
 * Adds a handler to the Lifecycle at the Stage.NORMAL stage. If the lifecycle has already been started, it throws
 * an {@link ISE}
 *
 * @param handler The hander to add to the lifecycle
 *
 * @throws ISE {@link Lifecycle#addHandler(Handler, Stage)}
 */
public void addHandler(Handler handler)
{
 addHandler(handler, Stage.NORMAL);
}

代码示例来源:origin: apache/incubator-druid

Lifecycle lifecycle = new Lifecycle();
lifecycle.addHandler(stoppingHandler);
lifecycle.start();
new Thread(lifecycle::stop).start(); // will stop at stoppingHandler.stop()
reachedStop.await();
 lifecycle.addHandler(dummyHandler);
 Assert.fail("Expected exception");
 lifecycle.addMaybeStartHandler(dummyHandler);
 Assert.fail("Expected exception");

代码示例来源:origin: apache/incubator-druid

@Override
public void start() throws Exception
{
 metamxLifecycle.start();
}

代码示例来源:origin: apache/incubator-druid

log.error(e, "Unhandled error in Curator Framework");
 try {
  lifecycle.stop();
lifecycle.addHandler(
  new Lifecycle.Handler()

代码示例来源:origin: apache/incubator-druid

public void setLifecycle(Lifecycle lifecycle)
{
 synchronized (instances) {
  this.lifecycle = lifecycle;
  for (Object instance : instances) {
   lifecycle.addManagedInstance(instance, stage);
  }
 }
}

代码示例来源:origin: apache/incubator-druid

private HttpPostEmitter sizeBasedEmitterGeneralizedCreation(int size)
{
 Properties props = new Properties();
 props.setProperty("org.apache.druid.java.util.emitter.type", "http");
 props.setProperty("org.apache.druid.java.util.emitter.recipientBaseUrl", TARGET_URL);
 props.setProperty("org.apache.druid.java.util.emitter.flushMillis", String.valueOf(Long.MAX_VALUE));
 props.setProperty("org.apache.druid.java.util.emitter.flushCount", String.valueOf(size));
 Lifecycle lifecycle = new Lifecycle();
 Emitter emitter = Emitters.create(props, httpClient, jsonMapper, lifecycle);
 Assert.assertTrue(StringUtils.format(
   "HttpPostEmitter emitter should be created, but found %s",
   emitter.getClass().getName()
 ), emitter instanceof HttpPostEmitter);
 emitter.start();
 return (HttpPostEmitter) emitter;
}

代码示例来源:origin: apache/incubator-druid

512
);
lifecycle.addMaybeStartHandler(
  new Lifecycle.Handler()
return lifecycle.addMaybeStartManagedInstance(
  new NettyHttpClient(
    new ResourcePool<>(

代码示例来源:origin: apache/incubator-druid

@Override
 public synchronized T get()
 {
  if (value == null) {
   final T retVal = unscoped.get();
   synchronized (instances) {
    if (lifecycle == null) {
     instances.add(retVal);
    } else {
     try {
      lifecycle.addMaybeStartManagedInstance(retVal, stage);
     }
     catch (Exception e) {
      log.warn(e, "Caught exception when trying to create a[%s]", key);
      return null;
     }
    }
   }
   value = retVal;
  }
  return value;
 }
};

代码示例来源:origin: apache/incubator-druid

@Override
public void start() throws Exception
{
 lifecycle.addMaybeStartManagedInstance(
   new ObjectToBeLifecycled(1, startOrder, stopOrder), Lifecycle.Stage.NORMAL
 );
 lifecycle.addMaybeStartManagedInstance(
   new ObjectToBeLifecycled(2, startOrder, stopOrder), Lifecycle.Stage.INIT
 );
 lifecycle.addMaybeStartManagedInstance(
   new ObjectToBeLifecycled(3, startOrder, stopOrder), Lifecycle.Stage.LAST
 );
 lifecycle.addMaybeStartStartCloseInstance(new ObjectToBeLifecycled(4, startOrder, stopOrder));
 lifecycle.addMaybeStartManagedInstance(new ObjectToBeLifecycled(5, startOrder, stopOrder));
 lifecycle.addMaybeStartStartCloseInstance(
   new ObjectToBeLifecycled(6, startOrder, stopOrder), Lifecycle.Stage.LAST
 );
 lifecycle.addMaybeStartManagedInstance(new ObjectToBeLifecycled(7, startOrder, stopOrder));
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testHttpConnectionClosingServer() throws Throwable
{
 final Lifecycle lifecycle = new Lifecycle();
 try {
  final HttpClientConfig config = HttpClientConfig.builder().build();
  final HttpClient client = HttpClientInit.createClient(config, lifecycle);
  final ListenableFuture<StatusResponseHolder> response = client
    .go(
      new Request(HttpMethod.GET, new URL(StringUtils.format("http://localhost:%d/", closingServerSocket.getLocalPort()))),
      new StatusResponseHandler(StandardCharsets.UTF_8)
    );
  Throwable e = null;
  try {
   response.get();
  }
  catch (ExecutionException e1) {
   e = e1.getCause();
   e1.printStackTrace();
  }
  Assert.assertTrue("ChannelException thrown by 'get'", isChannelClosedException(e));
 }
 finally {
  lifecycle.stop();
 }
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testBasicInjection() throws Exception
{
 final CaffeineCacheConfig config = new CaffeineCacheConfig();
 Injector injector = Initialization.makeInjectorWithModules(
   GuiceInjectors.makeStartupInjector(), ImmutableList.of(
     binder -> {
      binder.bindConstant().annotatedWith(Names.named("serviceName")).to("druid/test/redis");
      binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
      binder.bindConstant().annotatedWith(Names.named("tlsServicePort")).to(-1);
      binder.bind(CaffeineCacheConfig.class).toInstance(config);
      binder.bind(Cache.class).toProvider(CaffeineCacheProviderWithConfig.class).in(ManageLifecycle.class);
     }
   )
 );
 final Lifecycle lifecycle = injector.getInstance(Lifecycle.class);
 lifecycle.start();
 try {
  Cache cache = injector.getInstance(Cache.class);
  Assert.assertEquals(CaffeineCache.class, cache.getClass());
 }
 finally {
  lifecycle.stop();
 }
}

代码示例来源:origin: apache/incubator-druid

/**
 * Adds a Closeable instance to the lifecycle at {@link Stage#NORMAL} stage, doesn't try to call any "start" method on
 * it, use {@link #addStartCloseInstance(Object)} instead if you need the latter behaviour.
 */
public <T extends Closeable> T addCloseableInstance(T o)
{
 addHandler(new CloseableHandler(o));
 return o;
}

相关文章