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);
}
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.");
}
};
}
Aggregations