com.google.common.util.concurrent.Service.startAsync()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.3k)|赞(0)|评价(0)|浏览(133)

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

Service.startAsync介绍

[英]If the service state is State#NEW, this initiates service startup and returns immediately. A stopped service may not be restarted.
[中]如果服务状态为状态#NEW,则会启动服务并立即返回。已停止的服务可能无法重新启动。

代码示例

代码示例来源:origin: Graylog2/graylog2-server

@Override
public void doLaunch(final MessageInput input) throws MisfireException {
  generatorService = new AbstractExecutionThreadService() {
    @Override
    protected void run() throws Exception {
      while (isRunning()) {
        if (isThrottled()) {
          blockUntilUnthrottled();
        }
        final RawMessage rawMessage = GeneratorTransport.this.produceRawMessage(input);
        if (rawMessage != null) {
          input.processRawMessage(rawMessage);
        }
      }
    }
  };
  generatorService.startAsync();
}

代码示例来源:origin: google/guava

/** @since 15.0 */
@CanIgnoreReturnValue
@Override
public final Service startAsync() {
 delegate.startAsync();
 return this;
}

代码示例来源:origin: google/guava

/** @since 15.0 */
@CanIgnoreReturnValue
@Override
public final Service startAsync() {
 delegate.startAsync();
 return this;
}

代码示例来源:origin: google/j2objc

/** @since 15.0 */
@CanIgnoreReturnValue
@Override
public final Service startAsync() {
 delegate.startAsync();
 return this;
}

代码示例来源:origin: google/j2objc

/** @since 15.0 */
@CanIgnoreReturnValue
@Override
public final Service startAsync() {
 delegate.startAsync();
 return this;
}

代码示例来源:origin: wildfly/wildfly

/** @since 15.0 */
@CanIgnoreReturnValue
@Override
public final Service startAsync() {
 delegate.startAsync();
 return this;
}

代码示例来源:origin: wildfly/wildfly

/** @since 15.0 */
@CanIgnoreReturnValue
@Override
public final Service startAsync() {
 delegate.startAsync();
 return this;
}

代码示例来源:origin: google/guava

@Override
 protected void doStart() {
  b.startAsync();
  super.doStart();
 }
};

代码示例来源:origin: google/guava

/**
 * Initiates service {@linkplain Service#startAsync startup} on all the services being managed. It
 * is only valid to call this method if all of the services are {@linkplain State#NEW new}.
 *
 * @return this
 * @throws IllegalStateException if any of the Services are not {@link State#NEW new} when the
 *     method is called.
 */
@CanIgnoreReturnValue
public ServiceManager startAsync() {
 for (Service service : services) {
  State state = service.state();
  checkState(state == NEW, "Service %s is %s, cannot start it.", service, state);
 }
 for (Service service : services) {
  try {
   state.tryStartTiming(service);
   service.startAsync();
  } catch (IllegalStateException e) {
   // This can happen if the service has already been started or stopped (e.g. by another
   // service or listener). Our contract says it is safe to call this method if
   // all services were NEW when it was called, and this has already been verified above, so we
   // don't propagate the exception.
   logger.log(Level.WARNING, "Unable to start Service " + service, e);
  }
 }
 return this;
}

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

@Override
protected void startUp() throws Exception {
 LOGGER.info("Starting the " + StreamingJobConfigurationManager.class.getSimpleName());
 // submit command to fetch job specs
 this.fetchJobSpecExecutor.execute(new Runnable() {
  @Override
  public void run() {
   try {
    while(true) {
     fetchJobSpecs();
    }
   } catch (InterruptedException e) {
    LOGGER.info("Fetch thread interrupted... will exit");
   } catch (ExecutionException e) {
    LOGGER.error("Failed to fetch job specs", e);
    throw new RuntimeException("Failed to fetch specs", e);
   }
  }
 });
 // if the instance consumer is a service then need to start it to consume job specs
 // IMPORTANT: StreamingKafkaSpecConsumer needs to be launched after a fetching thread is created.
 //            This is because StreamingKafkaSpecConsumer will invoke addListener(new JobSpecListener()) during startup,
 //            which will push job specs into a blocking queue _jobSpecQueue. A fetching thread will help to consume the
 //            blocking queue to prevent a hanging issue.
 if (this.specConsumer instanceof Service) {
  ((Service) this.specConsumer).startAsync().awaitRunning();
 }
}

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

/**
 * Start any services required by the application launcher then start the application launcher
 */
private void startAppLauncherAndServices() {
 // other services such as the job configuration manager have a dependency on the job catalog, so it has be be
 // started first
 if (this.jobCatalog instanceof Service) {
  ((Service) this.jobCatalog).startAsync().awaitRunning();
 }
 this.applicationLauncher.start();
}

代码示例来源:origin: google/j2objc

/**
 * Initiates service {@linkplain Service#startAsync startup} on all the services being managed. It
 * is only valid to call this method if all of the services are {@linkplain State#NEW new}.
 *
 * @return this
 * @throws IllegalStateException if any of the Services are not {@link State#NEW new} when the
 *     method is called.
 */
@CanIgnoreReturnValue
public ServiceManager startAsync() {
 for (Service service : services) {
  State state = service.state();
  checkState(state == NEW, "Service %s is %s, cannot start it.", service, state);
 }
 for (Service service : services) {
  try {
   state.tryStartTiming(service);
   service.startAsync();
  } catch (IllegalStateException e) {
   // This can happen if the service has already been started or stopped (e.g. by another
   // service or listener). Our contract says it is safe to call this method if
   // all services were NEW when it was called, and this has already been verified above, so we
   // don't propagate the exception.
   logger.log(Level.WARNING, "Unable to start Service " + service, e);
  }
 }
 return this;
}

代码示例来源:origin: google/guava

public void testCustomScheduler_deadlock() throws InterruptedException, BrokenBarrierException {
 final CyclicBarrier inGetNextSchedule = new CyclicBarrier(2);
 // This will flakily deadlock, so run it multiple times to increase the flake likelihood
 for (int i = 0; i < 1000; i++) {
  Service service =
    new AbstractScheduledService() {
     @Override
     protected void runOneIteration() {}
     @Override
     protected Scheduler scheduler() {
      return new CustomScheduler() {
       @Override
       protected Schedule getNextSchedule() throws Exception {
        if (state() != State.STARTING) {
         inGetNextSchedule.await();
         Thread.yield();
         throw new RuntimeException("boom");
        }
        return new Schedule(0, TimeUnit.NANOSECONDS);
       }
      };
     }
    };
  service.startAsync().awaitRunning();
  inGetNextSchedule.await();
  service.stopAsync();
 }
}

代码示例来源:origin: wildfly/wildfly

/**
 * Initiates service {@linkplain Service#startAsync startup} on all the services being managed. It
 * is only valid to call this method if all of the services are {@linkplain State#NEW new}.
 *
 * @return this
 * @throws IllegalStateException if any of the Services are not {@link State#NEW new} when the
 *     method is called.
 */
@CanIgnoreReturnValue
public ServiceManager startAsync() {
 for (Service service : services) {
  State state = service.state();
  checkState(state == NEW, "Service %s is %s, cannot start it.", service, state);
 }
 for (Service service : services) {
  try {
   state.tryStartTiming(service);
   service.startAsync();
  } catch (IllegalStateException e) {
   // This can happen if the service has already been started or stopped (e.g. by another
   // service or listener). Our contract says it is safe to call this method if
   // all services were NEW when it was called, and this has already been verified above, so we
   // don't propagate the exception.
   logger.log(Level.WARNING, "Unable to start Service " + service, e);
  }
 }
 return this;
}

代码示例来源:origin: google/guava

public void testTimeout() {
 // Create a service whose executor will never run its commands
 Service service =
   new AbstractExecutionThreadService() {
    @Override
    protected void run() throws Exception {}
    @Override
    protected ScheduledExecutorService executor() {
     return TestingExecutors.noOpScheduledExecutor();
    }
    @Override
    protected String serviceName() {
     return "Foo";
    }
   };
 try {
  service.startAsync().awaitRunning(1, TimeUnit.MILLISECONDS);
  fail("Expected timeout");
 } catch (TimeoutException e) {
  assertThat(e)
    .hasMessageThat()
    .isEqualTo("Timed out waiting for Foo [STARTING] to reach the RUNNING state.");
 }
}

代码示例来源:origin: google/guava

public void testTimeout() throws Exception {
 // Create a service whose executor will never run its commands
 Service service =
   new TestService() {
    @Override
    protected Executor executor() {
     return new Executor() {
      @Override
      public void execute(Runnable command) {}
     };
    }
    @Override
    protected String serviceName() {
     return "Foo";
    }
   };
 try {
  service.startAsync().awaitRunning(1, TimeUnit.MILLISECONDS);
  fail("Expected timeout");
 } catch (TimeoutException e) {
  assertThat(e)
    .hasMessageThat()
    .isEqualTo("Timed out waiting for Foo [STARTING] to reach the RUNNING state.");
 }
}

代码示例来源:origin: google/guava

public void testTimeout() {
 // Create a service whose executor will never run its commands
 Service service =
   new AbstractScheduledService() {
    @Override
    protected Scheduler scheduler() {
     return Scheduler.newFixedDelaySchedule(0, 1, TimeUnit.NANOSECONDS);
    }
    @Override
    protected ScheduledExecutorService executor() {
     return TestingExecutors.noOpScheduledExecutor();
    }
    @Override
    protected void runOneIteration() throws Exception {}
    @Override
    protected String serviceName() {
     return "Foo";
    }
   };
 try {
  service.startAsync().awaitRunning(1, TimeUnit.MILLISECONDS);
  fail("Expected timeout");
 } catch (TimeoutException e) {
  assertThat(e)
    .hasMessageThat()
    .isEqualTo("Timed out waiting for Foo [STARTING] to reach the RUNNING state.");
 }
}

代码示例来源:origin: torodb/stampede

stampedeService.startAsync();
stampedeService.awaitTerminated();

代码示例来源:origin: at.bestsolution.efxclipse.eclipse/com.google.guava

/**
 * @since 15.0
 */
@Override public final Service startAsync() {
 delegate.startAsync();
 return this;
}

代码示例来源:origin: com.walterjwhite.infrastructure.google-guice.modules/property

private static void doStartService(final Class<? extends Service> serviceClass) {
 try {
  final Service service = GuiceHelper.getGuiceInjector().getInstance(serviceClass);
  if (!service.isRunning()) service.startAsync();
  else LOGGER.debug("service was already started, perhaps because it is an eager singleton?");
 } catch (ConfigurationException e) {
  LOGGER.warn("Service may NOT be a Guice service, check configuration", e);
 }
}

相关文章