Search in sources :

Example 1 with UnresolvedAddressException

use of java.nio.channels.UnresolvedAddressException in project jdk8u_jdk by JetBrains.

the class Connect method doTest.

void doTest() {
    SctpChannel channel = null;
    SctpServerChannel ssc = null;
    try {
        /* Create a server channel to connect to */
        ssc = SctpServerChannel.open().bind(null);
        Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
        if (addrs.isEmpty())
            debug("addrs should not be empty");
        final SocketAddress peerAddress = (InetSocketAddress) addrs.iterator().next();
        channel = SctpChannel.open();
        /* TEST 0.5 Verify default values for new/unconnected channel */
        check(channel.getRemoteAddresses().isEmpty(), "non empty set for unconnected channel");
        check(channel.association() == null, "non-null association for unconnected channel");
        check(!channel.isConnectionPending(), "should not have a connection pending");
        /* TEST 1: non-blocking connect */
        channel.configureBlocking(false);
        if (channel.connect(peerAddress) != true) {
            debug("non-blocking connect did not immediately succeed");
            check(channel.isConnectionPending(), "should return true for isConnectionPending");
            try {
                channel.connect(peerAddress);
                fail("should have thrown ConnectionPendingException");
            } catch (ConnectionPendingException cpe) {
                pass();
            } catch (IOException ioe) {
                unexpected(ioe);
            }
            channel.configureBlocking(true);
            check(channel.finishConnect(), "finishConnect should have returned true");
        }
        ssc.accept();
        ssc.close();
        /* TEST 1.5 Verify after connect */
        check(!channel.getRemoteAddresses().isEmpty(), "empty set for connected channel");
        check(channel.association() != null, "null association for connected channel");
        check(!channel.isConnectionPending(), "pending connection for connected channel");
        /* TEST 2: Verify AlreadyConnectedException thrown */
        try {
            channel.connect(peerAddress);
            fail("should have thrown AlreadyConnectedException");
        } catch (AlreadyConnectedException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 2.5: Verify AlreadyConnectedException thrown */
        try {
            channel.connect(peerAddress, 5, 5);
            fail("should have thrown AlreadyConnectedException");
        } catch (AlreadyConnectedException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 3: UnresolvedAddressException */
        channel.close();
        channel = SctpChannel.open();
        InetSocketAddress unresolved = InetSocketAddress.createUnresolved("xxyyzzabc", 4567);
        try {
            channel.connect(unresolved);
            fail("should have thrown UnresolvedAddressException");
        } catch (UnresolvedAddressException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 4: UnsupportedAddressTypeException */
        SocketAddress unsupported = new UnsupportedSocketAddress();
        try {
            channel.connect(unsupported);
            fail("should have thrown UnsupportedAddressTypeException");
        } catch (UnsupportedAddressTypeException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 5: ClosedChannelException */
        channel.close();
        final SctpChannel closedChannel = channel;
        testCCE(new Callable<Void>() {

            public Void call() throws IOException {
                closedChannel.connect(peerAddress);
                return null;
            }
        });
        /* TEST 5.5 getRemoteAddresses */
        testCCE(new Callable<Void>() {

            public Void call() throws IOException {
                closedChannel.getRemoteAddresses();
                return null;
            }
        });
        testCCE(new Callable<Void>() {

            public Void call() throws IOException {
                closedChannel.association();
                return null;
            }
        });
        check(!channel.isConnectionPending(), "pending connection for closed channel");
        /* Run some more finishConnect tests */
        /* TEST 6: NoConnectionPendingException */
        channel = SctpChannel.open();
        try {
            channel.finishConnect();
            fail("should have thrown NoConnectionPendingException");
        } catch (NoConnectionPendingException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 7: ClosedChannelException */
        channel.close();
        final SctpChannel cceChannel = channel;
        testCCE(new Callable<Void>() {

            public Void call() throws IOException {
                cceChannel.finishConnect();
                return null;
            }
        });
        /* TEST 8: IOException: Connection refused. Exercises handleSocketError.
             *         Assumption: no sctp socket listening on 3456 */
        SocketAddress addr = new InetSocketAddress("localhost", 3456);
        channel = SctpChannel.open();
        try {
            channel.connect(addr);
            fail("should have thrown ConnectException: Connection refused");
        } catch (IOException ioe) {
            pass();
        }
    } catch (IOException ioe) {
        unexpected(ioe);
    } finally {
        try {
            if (channel != null)
                channel.close();
        } catch (IOException unused) {
        }
        try {
            if (ssc != null)
                ssc.close();
        } catch (IOException unused) {
        }
    }
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) UnsupportedAddressTypeException(java.nio.channels.UnsupportedAddressTypeException) NoConnectionPendingException(java.nio.channels.NoConnectionPendingException) AlreadyConnectedException(java.nio.channels.AlreadyConnectedException) InetSocketAddress(java.net.InetSocketAddress) NoConnectionPendingException(java.nio.channels.NoConnectionPendingException) ConnectionPendingException(java.nio.channels.ConnectionPendingException) SctpServerChannel(com.sun.nio.sctp.SctpServerChannel) IOException(java.io.IOException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 2 with UnresolvedAddressException

use of java.nio.channels.UnresolvedAddressException in project heron by twitter.

the class KafkaUtils method fetchMessages.

/***
   * Fetch messages from kafka
   * @param config
   * @param consumer
   * @param partition
   * @param offset
   * @return
   * @throws TopicOffsetOutOfRangeException
   * @throws FailedFetchException
   * @throws RuntimeException
   */
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);
    // SUPPRESS CHECKSTYLE IllegalCatch
    } 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);
    }
    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 3 with UnresolvedAddressException

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

the class ReplicaStateChecker method call.

@Override
public Void call() throws Exception {
    Thread.currentThread().setName("ReplicaConnector Thread");
    long startTime = System.currentTimeMillis();
    InetSocketAddress replicaAddress = replica.getAddress(asterixReplicationProperties);
    while (true) {
        try (SocketChannel connection = SocketChannel.open()) {
            connection.configureBlocking(true);
            connection.connect(new InetSocketAddress(replicaAddress.getHostString(), replicaAddress.getPort()));
            ByteBuffer buffer = ReplicationProtocol.getGoodbyeBuffer();
            connection.write(buffer);
            replicationManager.updateReplicaState(replica.getId(), ReplicaState.ACTIVE, suspendReplication);
            return null;
        } catch (IOException | UnresolvedAddressException e) {
            Thread.sleep(WAIT_TIME);
            //check if connection to replica timed out
            if (((System.currentTimeMillis() - startTime) / 1000) >= replicationTimeOut) {
                replicationManager.updateReplicaState(replica.getId(), ReplicaState.DEAD, suspendReplication);
                return null;
            }
        }
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ByteBuffer(java.nio.ByteBuffer)

Example 4 with UnresolvedAddressException

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

the class UnresolvedAddressExceptionTest method test_Constructor.

/**
     * @tests {@link java.nio.channels.UnresolvedAddressException#UnresolvedAddressException()}
     */
public void test_Constructor() {
    UnresolvedAddressException e = new UnresolvedAddressException();
    assertNull(e.getMessage());
    assertNull(e.getLocalizedMessage());
    assertNull(e.getCause());
}
Also used : UnresolvedAddressException(java.nio.channels.UnresolvedAddressException)

Example 5 with UnresolvedAddressException

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

the class DatagramChannelTest method testConnect_Unresolved.

/**
     * Test method for 'DatagramChannelImpl.connect(SocketAddress)'
     *
     * @throws IOException
     */
public void testConnect_Unresolved() throws IOException {
    assertFalse(this.channel1.isConnected());
    InetSocketAddress unresolved = new InetSocketAddress("unresolved address", 1080);
    try {
        this.channel1.connect(unresolved);
        //$NON-NLS-1$
        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