Search in sources :

Example 16 with UnresolvedAddressException

use of java.nio.channels.UnresolvedAddressException in project j2objc by google.

the class AsynchronousSocketChannelTest method test_connect_unresolvedAddress.

public void test_connect_unresolvedAddress() throws Exception {
    AsynchronousSocketChannel asc = AsynchronousSocketChannel.open();
    try {
        asc.connect(new InetSocketAddress("unresolvedname", 31415));
        fail();
    } catch (UnresolvedAddressException expected) {
    }
    assertNull(asc.getRemoteAddress());
    assertTrue(asc.isOpen());
    asc.close();
}
Also used : AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) InetSocketAddress(java.net.InetSocketAddress) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException)

Example 17 with UnresolvedAddressException

use of java.nio.channels.UnresolvedAddressException in project j2objc by google.

the class AsynchronousServerSocketChannelTest method test_bind_unresolvedAddress.

public void test_bind_unresolvedAddress() throws Throwable {
    AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
    try {
        assc.bind(new InetSocketAddress("unresolvedname", 31415));
        fail();
    } catch (UnresolvedAddressException expected) {
    }
    assertNull(assc.getLocalAddress());
    assertTrue(assc.isOpen());
    assc.close();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException)

Example 18 with UnresolvedAddressException

use of java.nio.channels.UnresolvedAddressException in project jstorm by alibaba.

the class KafkaConsumer method fetchMessages.

public ByteBufferMessageSet fetchMessages(int partition, long offset) throws IOException {
    String topic = config.topic;
    FetchRequest req = new FetchRequestBuilder().clientId(config.clientId).addFetch(topic, partition, offset, config.fetchMaxBytes).maxWait(config.fetchWaitMaxMs).build();
    FetchResponse fetchResponse = null;
    SimpleConsumer simpleConsumer = null;
    try {
        simpleConsumer = findLeaderConsumer(partition);
        if (simpleConsumer == null) {
            // LOG.error(message);
            return null;
        }
        fetchResponse = simpleConsumer.fetch(req);
    } catch (Exception e) {
        if (e instanceof ConnectException || e instanceof SocketTimeoutException || e instanceof IOException || e instanceof UnresolvedAddressException) {
            LOG.warn("Network error when fetching messages:", e);
            if (simpleConsumer != null) {
                String host = simpleConsumer.host();
                int port = simpleConsumer.port();
                simpleConsumer = null;
                throw new KafkaException("Network error when fetching messages: " + host + ":" + port + " , " + e.getMessage(), e);
            }
        } else {
            throw new RuntimeException(e);
        }
    }
    if (fetchResponse.hasError()) {
        short code = fetchResponse.errorCode(topic, partition);
        if (code == ErrorMapping.OffsetOutOfRangeCode() && config.resetOffsetIfOutOfRange) {
            long startOffset = getOffset(topic, partition, config.startOffsetTime);
            offset = startOffset;
        }
        if (leaderBroker != null) {
            LOG.error("fetch data from kafka topic[" + config.topic + "] host[" + leaderBroker.host() + ":" + leaderBroker.port() + "] partition[" + partition + "] error:" + code);
        } else {
        }
        return null;
    } else {
        ByteBufferMessageSet msgs = fetchResponse.messageSet(topic, partition);
        return msgs;
    }
}
Also used : FetchResponse(kafka.javaapi.FetchResponse) IOException(java.io.IOException) ByteBufferMessageSet(kafka.javaapi.message.ByteBufferMessageSet) KafkaException(kafka.common.KafkaException) IOException(java.io.IOException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) SocketTimeoutException(java.net.SocketTimeoutException) FetchRequestBuilder(kafka.api.FetchRequestBuilder) FetchRequest(kafka.api.FetchRequest) KafkaException(kafka.common.KafkaException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) SimpleConsumer(kafka.javaapi.consumer.SimpleConsumer) ConnectException(java.net.ConnectException)

Example 19 with UnresolvedAddressException

use of java.nio.channels.UnresolvedAddressException in project storm by apache.

the class KafkaUtils method fetchMessages.

public static ByteBufferMessageSet fetchMessages(KafkaConfig config, SimpleConsumer consumer, Partition partition, long offset) throws TopicOffsetOutOfRangeException, FailedFetchException, RuntimeException {
    ByteBufferMessageSet msgs = null;
    String topic = partition.topic;
    int partitionId = partition.partition;
    FetchRequestBuilder builder = new FetchRequestBuilder();
    FetchRequest fetchRequest = builder.addFetch(topic, partitionId, offset, config.fetchSizeBytes).clientId(config.clientId).maxWait(config.fetchMaxWait).minBytes(config.minFetchByte).build();
    FetchResponse fetchResponse;
    try {
        fetchResponse = consumer.fetch(fetchRequest);
    } catch (Exception e) {
        if (e instanceof ConnectException || e instanceof SocketTimeoutException || e instanceof IOException || e instanceof UnresolvedAddressException) {
            LOG.warn("Network error when fetching messages:", e);
            throw new FailedFetchException(e);
        } else {
            throw new RuntimeException(e);
        }
    }
    if (fetchResponse.hasError()) {
        KafkaError error = KafkaError.getError(fetchResponse.errorCode(topic, partitionId));
        if (error.equals(KafkaError.OFFSET_OUT_OF_RANGE) && config.useStartOffsetTimeIfOffsetOutOfRange) {
            String msg = partition + " Got fetch request with offset out of range: [" + offset + "]";
            LOG.warn(msg);
            throw new TopicOffsetOutOfRangeException(msg);
        } else {
            String message = "Error fetching data from [" + partition + "] for topic [" + topic + "]: [" + error + "]";
            LOG.error(message);
            throw new FailedFetchException(message);
        }
    } else {
        msgs = fetchResponse.messageSet(topic, partitionId);
    }
    LOG.debug("Messages fetched. [config = {}], [consumer = {}], [partition = {}], [offset = {}], [msgs = {}]", config, consumer, partition, offset, msgs);
    return msgs;
}
Also used : FetchResponse(kafka.javaapi.FetchResponse) IOException(java.io.IOException) ByteBufferMessageSet(kafka.javaapi.message.ByteBufferMessageSet) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) SocketTimeoutException(java.net.SocketTimeoutException) FetchRequestBuilder(kafka.api.FetchRequestBuilder) FetchRequest(kafka.api.FetchRequest) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConnectException(java.net.ConnectException)

Example 20 with UnresolvedAddressException

use of java.nio.channels.UnresolvedAddressException in project robovm by robovm.

the class SocketChannelTest method testCFII_Unresolved.

public void testCFII_Unresolved() throws IOException {
    statusNotConnected_NotPending();
    InetSocketAddress unresolved = new InetSocketAddress("unresolved address", 1080);
    try {
        this.channel1.connect(unresolved);
        fail("Should throw an UnresolvedAddressException here.");
    } catch (UnresolvedAddressException e) {
    // OK.
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException)

Aggregations

UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)30 IOException (java.io.IOException)15 InetSocketAddress (java.net.InetSocketAddress)15 SocketChannel (java.nio.channels.SocketChannel)8 ConnectException (java.net.ConnectException)5 SocketAddress (java.net.SocketAddress)4 UnsupportedAddressTypeException (java.nio.channels.UnsupportedAddressTypeException)4 Socket (java.net.Socket)3 SocketTimeoutException (java.net.SocketTimeoutException)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 SelectionKey (java.nio.channels.SelectionKey)3 ServerSocketChannel (java.nio.channels.ServerSocketChannel)3 FetchRequest (kafka.api.FetchRequest)3 FetchRequestBuilder (kafka.api.FetchRequestBuilder)3 FetchResponse (kafka.javaapi.FetchResponse)3 ByteBufferMessageSet (kafka.javaapi.message.ByteBufferMessageSet)3 Status (io.grpc.Status)2 Http2Exception (io.netty.handler.codec.http2.Http2Exception)2 ServerSocket (java.net.ServerSocket)2 AsynchronousSocketChannel (java.nio.channels.AsynchronousSocketChannel)2