Search in sources :

Example 1 with Service

use of com.spotify.apollo.core.Service in project zoltar by spotify.

the class ServiceRunner method main.

/**
 * Runs the app locally.
 *
 * <p>$ curl http://localhost:8080/predict/5.8-2.7-5.1-1.9 Lengths seperated by "-"
 */
public static void main(final String... args) throws LoadingException {
    final Service service = HttpService.usingAppInit(ServiceRunner::configure, SERVICE_NAME).build();
    HttpService.boot(service, args);
}
Also used : HttpService(com.spotify.apollo.httpservice.HttpService) Service(com.spotify.apollo.core.Service)

Example 2 with Service

use of com.spotify.apollo.core.Service 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());
}
Also used : MetaDescriptor(com.spotify.apollo.meta.MetaDescriptor) RequestHandler(com.spotify.apollo.request.RequestHandler) ApolloConfig(com.spotify.apollo.environment.ApolloConfig) Service(com.spotify.apollo.core.Service) IOException(java.io.IOException) IOException(java.io.IOException)

Example 3 with Service

use of com.spotify.apollo.core.Service 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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RequestHandler(com.spotify.apollo.request.RequestHandler) Service(com.spotify.apollo.core.Service) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with Service

use of com.spotify.apollo.core.Service in project apollo by spotify.

the class ApolloEnvironmentModuleTest method shouldUseEndpointRunnableFactoryDecorator.

@Test
public void shouldUseEndpointRunnableFactoryDecorator() throws Exception {
    AtomicReference<String> lastResponseRef = new AtomicReference<>();
    final Service service = this.service.withModule(new LastResponseModule(lastResponseRef)).build();
    try (Service.Instance i = service.start()) {
        final ApolloEnvironment environment = ApolloEnvironmentModule.environment(i);
        final RequestHandler handler = environment.initialize(env -> {
            env.routingEngine().registerAutoRoute(Route.sync("GET", "/", ctx -> "hello"));
        });
        assertNotNull(handler);
        final FakeOngoingRequest ongoingRequest = ongoingRequest("http://foo");
        handler.handle(ongoingRequest);
        assertThat(ongoingRequest.getReply(), hasStatus(Status.GONE));
        assertEquals("hello", lastResponseRef.get());
    } catch (IOException e) {
        fail(e.getMessage());
    }
}
Also used : Response(com.spotify.apollo.Response) Request(com.spotify.apollo.Request) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) OngoingRequest(com.spotify.apollo.request.OngoingRequest) RequestHandler(com.spotify.apollo.request.RequestHandler) Assert.assertThat(org.junit.Assert.assertThat) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ByteString(okio.ByteString) Assert.fail(org.junit.Assert.fail) RequestMetadata(com.spotify.apollo.RequestMetadata) Service(com.spotify.apollo.core.Service) Environment(com.spotify.apollo.Environment) Status(com.spotify.apollo.Status) Before(org.junit.Before) Description(org.hamcrest.Description) ImmutableMap(com.google.common.collect.ImmutableMap) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Route(com.spotify.apollo.route.Route) Instant(java.time.Instant) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Consumer(java.util.function.Consumer) MetaModule(com.spotify.apollo.meta.MetaModule) Services(com.spotify.apollo.core.Services) Matcher(org.hamcrest.Matcher) RequestMetadataImpl(com.spotify.apollo.request.RequestMetadataImpl) Optional(java.util.Optional) AppInit(com.spotify.apollo.AppInit) Assert.assertEquals(org.junit.Assert.assertEquals) RequestHandler(com.spotify.apollo.request.RequestHandler) Service(com.spotify.apollo.core.Service) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteString(okio.ByteString) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with Service

use of com.spotify.apollo.core.Service in project apollo by spotify.

the class ExampleService method main.

public static void main(String... args) throws LoadingException {
    final Service service = HttpService.usingAppInit(ExampleService::init, "ping").build();
    HttpService.boot(service, args);
    LOG.info("bye bye");
}
Also used : HttpService(com.spotify.apollo.httpservice.HttpService) Service(com.spotify.apollo.core.Service)

Aggregations

Service (com.spotify.apollo.core.Service)14 Test (org.junit.Test)9 RequestHandler (com.spotify.apollo.request.RequestHandler)6 OngoingRequest (com.spotify.apollo.request.OngoingRequest)5 IOException (java.io.IOException)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Request (okhttp3.Request)3 HttpService (com.spotify.apollo.httpservice.HttpService)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 AppInit (com.spotify.apollo.AppInit)1 Environment (com.spotify.apollo.Environment)1 Request (com.spotify.apollo.Request)1 RequestMetadata (com.spotify.apollo.RequestMetadata)1 Response (com.spotify.apollo.Response)1 Status (com.spotify.apollo.Status)1 Services (com.spotify.apollo.core.Services)1 ApolloConfig (com.spotify.apollo.environment.ApolloConfig)1 MetaDescriptor (com.spotify.apollo.meta.MetaDescriptor)1 MetaModule (com.spotify.apollo.meta.MetaModule)1