Search in sources :

Example 61 with BindException

use of java.net.BindException in project guice by google.

the class CheckedProviderTest method testProviderMethodWithManyExceptions.

public void testProviderMethodWithManyExceptions() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                install(ThrowingProviderBinder.forModule(this));
            }

            @SuppressWarnings("unused")
            @CheckedProvides(RemoteProvider.class)
            String foo() throws InterruptedException, RuntimeException, RemoteException, AccessException, TooManyListenersException, BindException, SubBindException {
                return null;
            }
        });
        fail();
    } catch (CreationException ce) {
        // The only two that should fail are Interrupted & TooManyListeners.. the rest are OK.
        List<Message> errors = ImmutableList.copyOf(ce.getErrorMessages());
        assertEquals(InterruptedException.class.getName() + " is not compatible with the exceptions ([" + RemoteException.class + ", " + BindException.class + "]) declared in the CheckedProvider interface (" + RemoteProvider.class.getName() + ")", errors.get(0).getMessage());
        assertEquals(TooManyListenersException.class.getName() + " is not compatible with the exceptions ([" + RemoteException.class + ", " + BindException.class + "]) declared in the CheckedProvider interface (" + RemoteProvider.class.getName() + ")", errors.get(1).getMessage());
        assertEquals(2, errors.size());
    }
}
Also used : BindException(java.net.BindException) CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule) TooManyListenersException(java.util.TooManyListenersException) AccessException(java.rmi.AccessException) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) RemoteException(java.rmi.RemoteException)

Example 62 with BindException

use of java.net.BindException in project guice by google.

the class CheckedProviderMethodsModuleTest method testWithThrownException.

public void testWithThrownException() {
    TestModule testModule = new TestModule();
    Injector injector = Guice.createInjector(testModule);
    RpcProvider<Float> provider = injector.getInstance(Key.get(rpcProviderOfFloat));
    try {
        provider.get();
        fail();
    } catch (RemoteException e) {
        fail();
    } catch (BindException e) {
    // good
    }
}
Also used : Injector(com.google.inject.Injector) BindException(java.net.BindException) RemoteException(java.rmi.RemoteException)

Example 63 with BindException

use of java.net.BindException in project guice by google.

the class CheckedProviderTest method tExceptionsThrown.

private void tExceptionsThrown(Injector injector) throws Exception {
    RemoteProvider<Foo> remoteProvider = injector.getInstance(Key.get(remoteProviderOfFoo));
    mockRemoteProvider.throwOnNextGet(new BindException("kaboom!"));
    MockFoo.nextToThrow = new BindException("kaboom!");
    try {
        remoteProvider.get();
        fail();
    } catch (BindException expected) {
        assertEquals("kaboom!", expected.getMessage());
    }
}
Also used : BindException(java.net.BindException)

Example 64 with BindException

use of java.net.BindException in project voldemort by voldemort.

the class SocketServer method run.

@Override
public void run() {
    logger.info("Starting voldemort socket server (" + serverName + ") on port " + port);
    try {
        serverSocket = new ServerSocket();
        serverSocket.bind(new InetSocketAddress(port));
        serverSocket.setReceiveBufferSize(this.socketBufferSize);
        startedStatusQueue.put(SUCCESS);
        while (!isInterrupted() && !serverSocket.isClosed()) {
            final Socket socket = serverSocket.accept();
            configureSocket(socket);
            long sessionId = this.sessionIdSequence.getAndIncrement();
            this.threadPool.execute(new SocketServerSession(activeSessions, socket, handlerFactory, sessionId));
        }
    } catch (BindException e) {
        logger.error("Could not bind to port " + port + ".");
        startedStatusQueue.offer(e);
        throw new VoldemortException(e);
    } catch (SocketException e) {
        startedStatusQueue.offer(e);
        // If we have been manually shutdown, ignore
        if (!isInterrupted())
            logger.error("Error in server: ", e);
    } catch (IOException e) {
        startedStatusQueue.offer(e);
        throw new VoldemortException(e);
    } catch (Throwable t) {
        logger.error(t);
        startedStatusQueue.offer(t);
        if (t instanceof Error)
            throw (Error) t;
        else if (t instanceof RuntimeException)
            throw (RuntimeException) t;
        throw new VoldemortException(t);
    } finally {
        if (serverSocket != null) {
            try {
                serverSocket.close();
            } catch (IOException e) {
                logger.warn("Error while shutting down server.", e);
            }
        }
    }
}
Also used : SocketException(java.net.SocketException) InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) VoldemortException(voldemort.VoldemortException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 65 with BindException

use of java.net.BindException in project voldemort by voldemort.

the class ServerTestUtils method startVoldemortServer.

/**
     * Starts a Voldemort server for testing purposes.
     *
     * Unless the ports passed in via cluster are guaranteed to be available,
     * this method is susceptible to BindExceptions in VoldemortServer.start().
     * (And, there is no good way of guaranteeing that ports will be available,
     * so...)
     *
     * The method {@link ServerTestUtils#startVoldemortCluster} should be used
     * in preference to this method.}
     *
     * @param socketStoreFactory
     * @param config
     * @param cluster
     * @param testConnection
     * @return
     */
public static VoldemortServer startVoldemortServer(SocketStoreFactory socketStoreFactory, VoldemortConfig config, Cluster cluster, boolean testConnection) throws BindException {
    // TODO: Some tests that use this method fail intermittently with the
    // following output:
    //
    // A successor version version() to this version() exists for key
    // cluster.xml
    // voldemort.versioning.ObsoleteVersionException: A successor version
    // version() to this version() exists for key cluster.xml"
    //
    // Need to trace through the constructor VoldemortServer(VoldemortConfig
    // config, Cluster cluster) to understand how this error is possible,
    // and why it only happens intermittently.
    final int MAX_NUMBER_OF_ATTEMPTS = 120;
    // 5 minutes
    final long MAX_RETRY_TIME_IN_MS = 5 * Time.MS_PER_MINUTE;
    VoldemortException lastVE = null;
    long startTime = System.currentTimeMillis(), currentTime = System.currentTimeMillis();
    for (int i = 1; (i <= MAX_NUMBER_OF_ATTEMPTS) && (currentTime - startTime < MAX_RETRY_TIME_IN_MS); i++, currentTime = System.currentTimeMillis()) {
        VoldemortServer server = null;
        boolean success = false;
        try {
            if (cluster != null) {
                server = new VoldemortServer(config, cluster);
            } else {
                server = new VoldemortServer(config);
            }
            server.start();
            if (testConnection)
                ServerTestUtils.waitForServerStart(socketStoreFactory, server.getIdentityNode());
            // wait till server starts or throw exception
            success = true;
            return server;
        } catch (VoldemortException ve) {
            if (ve.getCause() instanceof BindException) {
                ve.printStackTrace();
                trySleep(Math.min(100 * i, 1000));
                lastVE = ve;
            } else {
                throw ve;
            }
        } finally {
            if (!success && server != null) {
                // This is in case new VoldemortServer() worked but ServerTestUtils.waitForServerStart() failed.
                try {
                    server.stop();
                } catch (Exception e) {
                    logger.error("Got an exception while trying to close a VoldemortServer", e);
                }
            }
        }
    }
    throw new BindException(lastVE.getMessage());
}
Also used : BindException(java.net.BindException) VoldemortServer(voldemort.server.VoldemortServer) UnreachableStoreException(voldemort.store.UnreachableStoreException) BindException(java.net.BindException) IOException(java.io.IOException)

Aggregations

BindException (java.net.BindException)104 IOException (java.io.IOException)34 InetSocketAddress (java.net.InetSocketAddress)25 ServerSocket (java.net.ServerSocket)22 Test (org.junit.Test)22 File (java.io.File)12 SocketException (java.net.SocketException)10 Configuration (org.apache.hadoop.conf.Configuration)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 InterruptedIOException (java.io.InterruptedIOException)5 MiniDFSNNTopology (org.apache.hadoop.hdfs.MiniDFSNNTopology)5 InetAddress (java.net.InetAddress)4 UnknownHostException (java.net.UnknownHostException)4 RemoteException (java.rmi.RemoteException)4 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)4 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)4 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)3 Socket (java.net.Socket)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3