Search in sources :

Example 1 with ChannelFutureListener

use of org.jboss.netty.channel.ChannelFutureListener in project weave by continuuity.

the class SimpleKafkaClient method preparePublish.

@Override
public PreparePublish preparePublish(final String topic, final Compression compression) {
    final Map<Integer, MessageSetEncoder> encoders = Maps.newHashMap();
    return new PreparePublish() {

        @Override
        public PreparePublish add(byte[] payload, Object partitionKey) {
            return add(ByteBuffer.wrap(payload), partitionKey);
        }

        @Override
        public PreparePublish add(ByteBuffer payload, Object partitionKey) {
            // TODO: Partition
            int partition = 0;
            MessageSetEncoder encoder = encoders.get(partition);
            if (encoder == null) {
                encoder = getEncoder(compression);
                encoders.put(partition, encoder);
            }
            encoder.add(ChannelBuffers.wrappedBuffer(payload));
            return this;
        }

        @Override
        public ListenableFuture<?> publish() {
            List<ListenableFuture<?>> futures = Lists.newArrayListWithCapacity(encoders.size());
            for (Map.Entry<Integer, MessageSetEncoder> entry : encoders.entrySet()) {
                futures.add(doPublish(topic, entry.getKey(), entry.getValue().finish()));
            }
            encoders.clear();
            return Futures.allAsList(futures);
        }

        private ListenableFuture<?> doPublish(String topic, int partition, ChannelBuffer messageSet) {
            final KafkaRequest request = KafkaRequest.createProduce(topic, partition, messageSet);
            final SettableFuture<?> result = SettableFuture.create();
            final ConnectionPool.ConnectResult connection = connectionPool.connect(getTopicBroker(topic, partition).getAddress());
            connection.getChannelFuture().addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    try {
                        future.getChannel().write(request).addListener(getPublishChannelFutureListener(result, null, connection));
                    } catch (Exception e) {
                        result.setException(e);
                    }
                }
            });
            return result;
        }
    };
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) PreparePublish(com.continuuity.weave.kafka.client.PreparePublish) ByteBuffer(java.nio.ByteBuffer) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener) FetchException(com.continuuity.weave.kafka.client.FetchException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Map(java.util.Map)

Example 2 with ChannelFutureListener

use of org.jboss.netty.channel.ChannelFutureListener in project weave by continuuity.

the class ConnectionPool method connect.

ConnectResult connect(InetSocketAddress address) {
    Queue<ChannelFuture> channelFutures = connections.get(address);
    if (channelFutures == null) {
        channelFutures = new ConcurrentLinkedQueue<ChannelFuture>();
        Queue<ChannelFuture> result = connections.putIfAbsent(address, channelFutures);
        channelFutures = result == null ? channelFutures : result;
    }
    ChannelFuture channelFuture = channelFutures.poll();
    while (channelFuture != null) {
        if (channelFuture.isSuccess() && channelFuture.getChannel().isConnected()) {
            return new SimpleConnectResult(address, channelFuture);
        }
        channelFuture = channelFutures.poll();
    }
    channelFuture = bootstrap.connect(address);
    channelFuture.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                channelGroup.add(future.getChannel());
            }
        }
    });
    return new SimpleConnectResult(address, channelFuture);
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener)

Example 3 with ChannelFutureListener

use of org.jboss.netty.channel.ChannelFutureListener in project crate by crate.

the class Messages method sendRowDescription.

/**
     * RowDescription (B)
     * <p>
     * | 'T' | int32 len | int16 numCols
     * <p>
     * For each field:
     * <p>
     * | string name | int32 table_oid | int16 attr_num | int32 oid | int16 typlen | int32 type_modifier | int16 format_code
     * <p>
     * See https://www.postgresql.org/docs/current/static/protocol-message-formats.html
     */
static void sendRowDescription(Channel channel, Collection<Field> columns, @Nullable FormatCodes.FormatCode[] formatCodes) {
    int length = 4 + 2;
    int columnSize = 4 + 2 + 4 + 2 + 4 + 2;
    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(// use 10 as an estimate for columnName length
    length + (columns.size() * (10 + columnSize)));
    buffer.writeByte('T');
    // will be set at the end
    buffer.writeInt(0);
    buffer.writeShort(columns.size());
    int idx = 0;
    for (Field column : columns) {
        byte[] nameBytes = column.path().outputName().getBytes(StandardCharsets.UTF_8);
        length += nameBytes.length + 1;
        length += columnSize;
        writeCString(buffer, nameBytes);
        // table_oid
        buffer.writeInt(0);
        // attr_num
        buffer.writeShort(0);
        PGType pgType = PGTypes.get(column.valueType());
        buffer.writeInt(pgType.oid());
        buffer.writeShort(pgType.typeLen());
        buffer.writeInt(pgType.typeMod());
        buffer.writeShort(FormatCodes.getFormatCode(formatCodes, idx).ordinal());
        idx++;
    }
    buffer.setInt(1, length);
    ChannelFuture channelFuture = channel.write(buffer);
    if (LOGGER.isTraceEnabled()) {
        channelFuture.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                LOGGER.trace("sentRowDescription");
            }
        });
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) Field(io.crate.analyze.symbol.Field) PGType(io.crate.protocols.postgres.types.PGType) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 4 with ChannelFutureListener

use of org.jboss.netty.channel.ChannelFutureListener in project crate by crate.

the class Messages method sendErrorResponse.

/**
     * 'E' | int32 len | char code | str value | \0 | char code | str value | \0 | ... | \0
     * <p>
     * char code / str value -> key-value fields
     * example error fields are: message, detail, hint, error position
     * <p>
     * See https://www.postgresql.org/docs/9.2/static/protocol-error-fields.html for a list of error codes
     */
static void sendErrorResponse(Channel channel, Throwable throwable) {
    final String message = SQLExceptions.messageOf(throwable);
    byte[] msg = message.getBytes(StandardCharsets.UTF_8);
    byte[] severity = "ERROR".getBytes(StandardCharsets.UTF_8);
    byte[] lineNumber = null;
    byte[] fileName = null;
    byte[] methodName = null;
    StackTraceElement[] stackTrace = throwable.getStackTrace();
    if (stackTrace != null && stackTrace.length > 0) {
        StackTraceElement stackTraceElement = stackTrace[0];
        lineNumber = String.valueOf(stackTraceElement.getLineNumber()).getBytes(StandardCharsets.UTF_8);
        if (stackTraceElement.getFileName() != null) {
            fileName = stackTraceElement.getFileName().getBytes(StandardCharsets.UTF_8);
        }
        if (stackTraceElement.getMethodName() != null) {
            methodName = stackTraceElement.getMethodName().getBytes(StandardCharsets.UTF_8);
        }
    }
    // See https://www.postgresql.org/docs/9.2/static/errcodes-appendix.html
    // need to add a throwable -> error code mapping later on
    byte[] errorCode;
    if (throwable instanceof IllegalArgumentException || throwable instanceof UnsupportedOperationException) {
        // feature_not_supported
        errorCode = "0A000".getBytes(StandardCharsets.UTF_8);
    } else {
        // internal_error
        errorCode = "XX000".getBytes(StandardCharsets.UTF_8);
    }
    int length = 4 + 1 + (severity.length + 1) + 1 + (msg.length + 1) + 1 + (errorCode.length + 1) + (fileName != null ? 1 + (fileName.length + 1) : 0) + (lineNumber != null ? 1 + (lineNumber.length + 1) : 0) + (methodName != null ? 1 + (methodName.length + 1) : 0) + 1;
    ChannelBuffer buffer = ChannelBuffers.buffer(length + 1);
    buffer.writeByte('E');
    buffer.writeInt(length);
    buffer.writeByte('S');
    writeCString(buffer, severity);
    buffer.writeByte('M');
    writeCString(buffer, msg);
    buffer.writeByte(('C'));
    writeCString(buffer, errorCode);
    if (fileName != null) {
        buffer.writeByte('F');
        writeCString(buffer, fileName);
    }
    if (lineNumber != null) {
        buffer.writeByte('L');
        writeCString(buffer, lineNumber);
    }
    if (methodName != null) {
        buffer.writeByte('R');
        writeCString(buffer, methodName);
    }
    buffer.writeByte(0);
    ChannelFuture channelFuture = channel.write(buffer);
    if (LOGGER.isTraceEnabled()) {
        channelFuture.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                LOGGER.trace("sentErrorResponse msg={}", message);
            }
        });
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 5 with ChannelFutureListener

use of org.jboss.netty.channel.ChannelFutureListener in project camel by apache.

the class NettyProducer method process.

public boolean process(final Exchange exchange, AsyncCallback callback) {
    if (!isRunAllowed()) {
        if (exchange.getException() == null) {
            exchange.setException(new RejectedExecutionException());
        }
        callback.done(true);
        return true;
    }
    Object body;
    try {
        body = getRequestBody(exchange);
        if (body == null) {
            noReplyLogger.log("No payload to send for exchange: " + exchange);
            callback.done(true);
            return true;
        }
    } catch (Exception e) {
        exchange.setException(e);
        callback.done(true);
        return true;
    }
    // set the exchange encoding property
    if (getConfiguration().getCharsetName() != null) {
        exchange.setProperty(Exchange.CHARSET_NAME, IOHelper.normalizeCharset(getConfiguration().getCharsetName()));
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("Pool[active={}, idle={}]", pool.getNumActive(), pool.getNumIdle());
    }
    // get a channel from the pool
    Channel existing;
    try {
        existing = pool.borrowObject();
        if (existing != null) {
            LOG.trace("Got channel from pool {}", existing);
        }
    } catch (Exception e) {
        exchange.setException(e);
        callback.done(true);
        return true;
    }
    // we must have a channel
    if (existing == null) {
        exchange.setException(new CamelExchangeException("Cannot get channel from pool", exchange));
        callback.done(true);
        return true;
    }
    if (exchange.getIn().getHeader(NettyConstants.NETTY_REQUEST_TIMEOUT) != null) {
        long timeoutInMs = exchange.getIn().getHeader(NettyConstants.NETTY_REQUEST_TIMEOUT, Long.class);
        ChannelHandler oldHandler = existing.getPipeline().get("timeout");
        ReadTimeoutHandler newHandler = new ReadTimeoutHandler(getEndpoint().getTimer(), timeoutInMs, TimeUnit.MILLISECONDS);
        if (oldHandler == null) {
            existing.getPipeline().addBefore("handler", "timeout", newHandler);
        } else {
            existing.getPipeline().replace(oldHandler, "timeout", newHandler);
        }
    }
    // need to declare as final
    final Channel channel = existing;
    final AsyncCallback producerCallback = new NettyProducerCallback(channel, callback);
    // setup state as attachment on the channel, so we can access the state later when needed
    channel.setAttachment(new NettyCamelState(producerCallback, exchange));
    InetSocketAddress remoteAddress = null;
    if (!isTcp()) {
        // Need to specify the remoteAddress for udp connection
        remoteAddress = new InetSocketAddress(configuration.getHost(), configuration.getPort());
    }
    // write body
    NettyHelper.writeBodyAsync(LOG, channel, remoteAddress, body, exchange, new ChannelFutureListener() {

        public void operationComplete(ChannelFuture channelFuture) throws Exception {
            LOG.trace("Operation complete {}", channelFuture);
            if (!channelFuture.isSuccess()) {
                // no success then exit, (any exception has been handled by ClientChannelHandler#exceptionCaught)
                return;
            }
            // if we do not expect any reply then signal callback to continue routing
            if (!configuration.isSync()) {
                try {
                    // should channel be closed after complete?
                    Boolean close;
                    if (ExchangeHelper.isOutCapable(exchange)) {
                        close = exchange.getOut().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
                    } else {
                        close = exchange.getIn().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
                    }
                    // should we disconnect, the header can override the configuration
                    boolean disconnect = getConfiguration().isDisconnect();
                    if (close != null) {
                        disconnect = close;
                    }
                    if (disconnect) {
                        if (LOG.isTraceEnabled()) {
                            LOG.trace("Closing channel when complete at address: {}", getEndpoint().getConfiguration().getAddress());
                        }
                        NettyHelper.close(channel);
                    }
                } finally {
                    // signal callback to continue routing
                    producerCallback.done(false);
                }
            }
        }
    });
    // continue routing asynchronously
    return false;
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) SucceededChannelFuture(org.jboss.netty.channel.SucceededChannelFuture) CamelExchangeException(org.apache.camel.CamelExchangeException) InetSocketAddress(java.net.InetSocketAddress) Channel(org.jboss.netty.channel.Channel) AsyncCallback(org.apache.camel.AsyncCallback) ChannelHandler(org.jboss.netty.channel.ChannelHandler) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CamelException(org.apache.camel.CamelException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ConnectException(java.net.ConnectException) CamelExchangeException(org.apache.camel.CamelExchangeException) ReadTimeoutHandler(org.jboss.netty.handler.timeout.ReadTimeoutHandler)

Aggregations

ChannelFuture (org.jboss.netty.channel.ChannelFuture)22 ChannelFutureListener (org.jboss.netty.channel.ChannelFutureListener)22 Channel (org.jboss.netty.channel.Channel)10 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)6 ConnectException (java.net.ConnectException)5 InetSocketAddress (java.net.InetSocketAddress)4 URL (java.net.URL)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 CamelException (org.apache.camel.CamelException)3 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)3 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)3 FetchException (com.continuuity.weave.kafka.client.FetchException)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 Map (java.util.Map)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 CamelExchangeException (org.apache.camel.CamelExchangeException)2 ChannelException (org.jboss.netty.channel.ChannelException)2 ChannelHandlerContext (org.jboss.netty.channel.ChannelHandlerContext)2