use of com.spotify.apollo.request.RequestHandler in project apollo by spotify.
the class HttpService method boot.
/**
* Boot up a service and wait for it to shut down.
*
* @param service the service to start
* @param instanceListener gets called when a service instance has been created
* @param uncaughtExceptionHandler an exception handler that gets invoked for the current thread
* if any uncaught exceptions are thrown during service startup
* or while waiting for it to shut down.
* @param args arguments to the service
* @throws LoadingException in case of an error starting up
*/
public static void boot(Service service, InstanceListener instanceListener, Thread.UncaughtExceptionHandler uncaughtExceptionHandler, String... args) throws LoadingException {
Objects.requireNonNull(uncaughtExceptionHandler);
Thread.currentThread().setUncaughtExceptionHandler(uncaughtExceptionHandler);
LOG.debug("Trying to create instance of service {} with args {}", service.getServiceName(), args);
try (Service.Instance instance = service.start(args)) {
final RequestHandler requestHandler = HttpServiceModule.requestHandler(instance);
HttpServerModule.server(instance).start(requestHandler);
final String serviceName = service.getServiceName();
final MetaDescriptor metaDescriptor = instance.resolve(MetaDescriptor.class);
final ApolloConfig config = instance.resolve(ApolloConfig.class);
LOG.info("Started {} {} (apollo {}) with backend domain '{}'", serviceName, metaDescriptor.descriptor().version(), metaDescriptor.apolloVersion(), config.backend());
if (instanceListener != null) {
instanceListener.instanceCreated(instance);
}
instance.waitForShutdown();
LOG.info("Starting shutdown of {} ...", serviceName);
} catch (IOException e) {
throw failure(e, "Failed to start service");
} catch (InterruptedException e) {
throw failure(e, "Service interrupted");
} catch (Exception e) {
throw failure(e, "Something went wrong");
}
LOG.info("Shutdown of {} complete", service.getServiceName());
}
use of com.spotify.apollo.request.RequestHandler in project apollo by spotify.
the class HttpServiceModuleTest method shouldDestroyApplicationOnExit.
@Test
public void shouldDestroyApplicationOnExit() throws Exception {
final AtomicBoolean init = new AtomicBoolean();
final AtomicBoolean destroy = new AtomicBoolean();
final App app = new App(init, destroy);
try (Service.Instance i = service(app).start()) {
final RequestHandler handler = HttpServiceModule.requestHandler(i);
assertNotNull(handler);
// not calling environment.close()
} catch (IOException e) {
fail(e.getMessage());
}
assertTrue(init.get());
assertTrue(destroy.get());
}
use of com.spotify.apollo.request.RequestHandler in project apollo by spotify.
the class ApolloEnvironmentModuleTest method shouldGetFunctioningEnvironment.
@Test
public void shouldGetFunctioningEnvironment() throws Exception {
final AtomicReference<Environment> envReference = new AtomicReference<>();
try (Service.Instance i = service.build().start()) {
final ApolloEnvironment environment = ApolloEnvironmentModule.environment(i);
final RequestHandler handler = environment.initialize(new EnvApp(envReference::set));
assertNotNull(handler);
final Environment e = envReference.get();
validateEnv(e);
} catch (IOException e) {
fail(e.getMessage());
}
}
use of com.spotify.apollo.request.RequestHandler in project apollo by spotify.
the class ApolloEnvironmentModuleTest method shouldHandleAppInit.
@Test
public void shouldHandleAppInit() throws Exception {
final AtomicBoolean init = new AtomicBoolean();
final AtomicBoolean destroy = new AtomicBoolean();
try (Service.Instance i = service.build().start()) {
final ApolloEnvironment environment = ApolloEnvironmentModule.environment(i);
final RequestHandler handler = environment.initialize(env -> {
init.set(true);
env.closer().register(() -> destroy.set(true));
});
assertNotNull(handler);
} catch (IOException e) {
fail(e.getMessage());
}
assertTrue(init.get());
assertTrue(destroy.get());
}
use of com.spotify.apollo.request.RequestHandler in project apollo by spotify.
the class ApolloEnvironmentModuleTest method shouldSetUpConfig.
@Test
public void shouldSetUpConfig() throws Exception {
final AtomicReference<Environment> envReference = new AtomicReference<>();
final Map<String, String> env = ImmutableMap.of("APOLLO_APOLLO_DOMAIN", "my-domain");
try (Service.Instance i = service.build().start(ZERO_ARGS, env)) {
final ApolloEnvironment environment = ApolloEnvironmentModule.environment(i);
final RequestHandler handler = environment.initialize(new EnvApp(envReference::set));
assertNotNull(handler);
final Environment e = envReference.get();
assertNotNull(e);
assertNotNull(e.config());
// from ping.conf
assertEquals("baz", e.config().getString("bar"));
assertEquals("my-domain", e.domain());
} catch (IOException e) {
fail(e.getMessage());
}
}
Aggregations