Search in sources :

Example 26 with Cancellable

use of org.apache.twill.common.Cancellable in project cdap by caskdata.

the class AbstractHttpHandlerDelegator method wrapContentConsumer.

/**
 * Returns a new instance of {@link BodyConsumer} that wraps around the given {@link HttpContentConsumer}
 * and {@link DelayedHttpServiceResponder}.
 *
 * IMPORTANT: This method will also capture the context associated with the current thread, hence after
 * this method is called, no other methods on this class should be called from the current thread.
 *
 * This method is called from handler class generated by {@link HttpHandlerGenerator}.
 */
@SuppressWarnings("unused")
protected final BodyConsumer wrapContentConsumer(HttpContentConsumer consumer, DelayedHttpServiceResponder responder, TransactionControl defaultTxControl) {
    Preconditions.checkState(!responder.hasBufferedResponse(), "HttpContentConsumer may not be used after a response has already been sent.");
    // Close the provided responder since a new one will be created for the BodyConsumerAdapter to use.
    responder.close();
    ServiceTaskExecutor taskExecutor = context.getServiceTaskExecutor();
    Cancellable contextReleaser = context.capture();
    return new BodyConsumerAdapter(new DelayedHttpServiceResponder(responder, (contentProducer, txServiceContext) -> new BodyProducerAdapter(contentProducer, txServiceContext, contextReleaser, defaultTxControl)), consumer, taskExecutor, contextReleaser, defaultTxControl);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) BodyConsumer(co.cask.http.BodyConsumer) TransactionControl(co.cask.cdap.api.annotation.TransactionControl) HttpHandler(co.cask.http.HttpHandler) HttpContentProducer(co.cask.cdap.api.service.http.HttpContentProducer) ThrowingRunnable(co.cask.cdap.internal.app.runtime.ThrowingRunnable) HttpContentConsumer(co.cask.cdap.api.service.http.HttpContentConsumer) BodyProducer(co.cask.http.BodyProducer) HttpServiceRequest(co.cask.cdap.api.service.http.HttpServiceRequest) HttpResponder(co.cask.http.HttpResponder) Preconditions(com.google.common.base.Preconditions) Cancellable(org.apache.twill.common.Cancellable) CombineClassLoader(co.cask.cdap.common.lang.CombineClassLoader) HandlerContext(co.cask.http.HandlerContext) Cancellable(org.apache.twill.common.Cancellable)

Example 27 with Cancellable

use of org.apache.twill.common.Cancellable in project cdap by caskdata.

the class AppFabricServer method startHttpService.

private Cancellable startHttpService(final NettyHttpService httpService) throws Exception {
    httpService.start();
    String announceAddress = cConf.get(Constants.Service.MASTER_SERVICES_ANNOUNCE_ADDRESS, httpService.getBindAddress().getHostName());
    int announcePort = cConf.getInt(Constants.AppFabric.SERVER_ANNOUNCE_PORT, httpService.getBindAddress().getPort());
    final InetSocketAddress socketAddress = new InetSocketAddress(announceAddress, announcePort);
    LOG.info("AppFabric HTTP Service announced at {}", socketAddress);
    // Tag the discoverable's payload to mark it as supporting ssl.
    byte[] sslPayload = sslEnabled ? Constants.Security.SSL_URI_SCHEME.getBytes() : Bytes.EMPTY_BYTE_ARRAY;
    // TODO accept a list of services, and start them here
    // When it is running, register it with service discovery
    final List<Cancellable> cancellables = new ArrayList<>();
    for (final String serviceName : servicesNames) {
        cancellables.add(discoveryService.register(ResolvingDiscoverable.of(new Discoverable(serviceName, socketAddress, sslPayload))));
    }
    return new Cancellable() {

        @Override
        public void cancel() {
            LOG.debug("Stopping AppFabric HTTP service.");
            for (Cancellable cancellable : cancellables) {
                if (cancellable != null) {
                    cancellable.cancel();
                }
            }
            try {
                httpService.stop();
            } catch (Exception e) {
                LOG.warn("Exception raised when stopping AppFabric HTTP service", e);
            }
            LOG.info("AppFabric HTTP service stopped.");
        }
    };
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) ResolvingDiscoverable(co.cask.cdap.common.discovery.ResolvingDiscoverable) InetSocketAddress(java.net.InetSocketAddress) Cancellable(org.apache.twill.common.Cancellable) ArrayList(java.util.ArrayList)

Aggregations

Cancellable (org.apache.twill.common.Cancellable)27 CountDownLatch (java.util.concurrent.CountDownLatch)7 Test (org.junit.Test)6 InetSocketAddress (java.net.InetSocketAddress)5 Discoverable (org.apache.twill.discovery.Discoverable)5 HttpContentProducer (co.cask.cdap.api.service.http.HttpContentProducer)3 ResolvingDiscoverable (co.cask.cdap.common.discovery.ResolvingDiscoverable)3 CombineClassLoader (co.cask.cdap.common.lang.CombineClassLoader)3 NotificationFeedNotFoundException (co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException)3 ArrayList (java.util.ArrayList)3 ZKClientService (org.apache.twill.zookeeper.ZKClientService)3 MetricsContext (co.cask.cdap.api.metrics.MetricsContext)2 HttpServiceContext (co.cask.cdap.api.service.http.HttpServiceContext)2 SparkRunnerClassLoader (co.cask.cdap.app.runtime.spark.classloader.SparkRunnerClassLoader)2 FilterClassLoader (co.cask.cdap.common.lang.FilterClassLoader)2 StreamPropertyListener (co.cask.cdap.data.stream.StreamPropertyListener)2 NotificationContext (co.cask.cdap.notifications.service.NotificationContext)2 StreamId (co.cask.cdap.proto.id.StreamId)2 BodyProducer (co.cask.http.BodyProducer)2 ImmutableSet (com.google.common.collect.ImmutableSet)2