Example usage for io.netty.util.concurrent EventExecutorGroup submit

List of usage examples for io.netty.util.concurrent EventExecutorGroup submit

Introduction

In this page you can find the example usage for io.netty.util.concurrent EventExecutorGroup submit.

Prototype

@Override
    <T> Future<T> submit(Callable<T> task);

Source Link

Usage

From source file:org.apache.tinkerpop.gremlin.server.handler.IteratorHandler.java

License:Apache License

@Override
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise)
        throws Exception {
    if (msg instanceof Pair) {
        try {//from  www .ja  v  a  2  s.c  om
            final Pair pair = (Pair) msg;
            final Iterator itty = (Iterator) pair.getValue1();
            final RequestMessage requestMessage = (RequestMessage) pair.getValue0();

            // the batch size can be overriden by the request
            final int resultIterationBatchSize = (Integer) requestMessage.optionalArgs(Tokens.ARGS_BATCH_SIZE)
                    .orElse(settings.resultIterationBatchSize);

            // timer for the total serialization time
            final StopWatch stopWatch = new StopWatch();

            final EventExecutorGroup executorService = ctx.executor();
            final Future<?> iteration = executorService.submit((Callable<Void>) () -> {
                logger.debug("Preparing to iterate results from - {} - in thread [{}]", requestMessage,
                        Thread.currentThread().getName());

                stopWatch.start();

                List<Object> aggregate = new ArrayList<>(resultIterationBatchSize);
                while (itty.hasNext()) {
                    aggregate.add(itty.next());

                    // send back a page of results if batch size is met or if it's the end of the results being
                    // iterated
                    if (aggregate.size() == resultIterationBatchSize || !itty.hasNext()) {
                        final ResponseStatusCode code = itty.hasNext() ? ResponseStatusCode.PARTIAL_CONTENT
                                : ResponseStatusCode.SUCCESS;
                        ctx.writeAndFlush(
                                ResponseMessage.build(requestMessage).code(code).result(aggregate).create());
                        aggregate = new ArrayList<>(resultIterationBatchSize);
                    }

                    stopWatch.split();
                    if (stopWatch.getSplitTime() > settings.serializedResponseTimeout)
                        throw new TimeoutException(
                                "Serialization of the entire response exceeded the serializeResponseTimeout setting");

                    stopWatch.unsplit();
                }

                return null;
            });

            iteration.addListener(f -> {
                stopWatch.stop();

                if (!f.isSuccess()) {
                    final String errorMessage = String.format(
                            "Response iteration and serialization exceeded the configured threshold for request [%s] - %s",
                            msg, f.cause().getMessage());
                    logger.warn(errorMessage);
                    ctx.writeAndFlush(
                            ResponseMessage.build(requestMessage).code(ResponseStatusCode.SERVER_ERROR_TIMEOUT)
                                    .statusMessage(errorMessage).create());
                }
            });
        } finally {
            ReferenceCountUtil.release(msg);
        }

    } else {
        ctx.write(msg, promise);
    }
}