BindException: Address already in use

3,182 views
Skip to first unread message

Ziemo

unread,
Oct 29, 2014, 5:34:45 PM10/29/14
to ve...@googlegroups.com
Hi!

I've just started to develop in vert.x and I face some issue with http server. I try to start 2 verticles, let's say I type in one terminal prompt:
vertx run Verticle1.java

and in the second:
vertx run Verticle2.java

after that, in the second terminal prompt I received that kind of error:
Exception in Java verticle
java.net.BindException: Address already in use: bind
        at sun.nio.ch.Net.bind0(Native Method)
        at sun.nio.ch.Net.bind(Net.java:436)
        at sun.nio.ch.Net.bind(Net.java:428)
        at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214)
        at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
        at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:125)
        at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:476)
        at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1021)
        at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:455)
        at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:440)
        at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:844)
        at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:195)
        at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:339)
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:370)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
        at java.lang.Thread.run(Thread.java:745)

I use Windows 8.1 and Vert.x 2.1.4

Thanks for your help!

Jez P

unread,
Oct 29, 2014, 5:52:56 PM10/29/14
to ve...@googlegroups.com
What do verticle1 and verticle 2 do, Do they both open http servers listening on the same port? If so, that's exactly what you'd expect. Two processes can't nab the same port (as far as I'm aware). 
Message has been deleted
Message has been deleted
Message has been deleted

Ziemo

unread,
Oct 29, 2014, 6:07:04 PM10/29/14
to ve...@googlegroups.com
Yes, they are on the same port. But I can imagine that address exception in normal TCP case, but if I understand good - Vert.x use Round-Robin for simulating deployment of more verticles on the same port. Because if it's not possible, for what I should use Vert.x if I got all HTTP requests on the same front dispatcher, runned on exacly the same thread?

Alexander Lehmann

unread,
Oct 29, 2014, 7:05:10 PM10/29/14
to ve...@googlegroups.com
If you are running two vertx run command, the different jvms will try to bind to the same port, which will fail.

This doesn't work in any other language either. You will have to bind the port in one verticle and jvm and distribute the requests via the eventbus if you want to access different instances.

Ziemo

unread,
Oct 29, 2014, 7:47:27 PM10/29/14
to ve...@googlegroups.com
OK, so that dispatcher will be only one for whole HTTP/REST application run on same JVM? 

And what in that case with that Round-Robin which (like I thought) is involved in 2 verticles deployed on same port ? Is it not truth? 
I'm asking because the manual on the page has only limited information on that.

Ziemo

unread,
Oct 29, 2014, 8:06:14 PM10/29/14
to ve...@googlegroups.com
When I asked about 2 verticles on the same port I refer to that post: https://groups.google.com/d/msg/vertx/I6qdw8GXVoQ/RWfzJli3f8gJ. But I don't understand how can I do that. is it possible by this:
container.deployVerticle("Verticle1.java");
container.deployVerticle("Verticle2.java");

Ziemo

unread,
Oct 29, 2014, 8:09:33 PM10/29/14
to ve...@googlegroups.com
Because that case deploy only first of them. This is my code:
public class Verticle1 extends Verticle {


    @Override
    public void start() {
        RouteMatcher routeMatcher = new RouteMatcher();
        routeMatcher.get("/test1", new Handler<HttpServerRequest>() {
            @Override
            public void handle(HttpServerRequest httpServerRequest) {
                httpServerRequest.response().end("TEST 1");
            }
        });
        vertx.createHttpServer().requestHandler(routeMatcher).listen(1234);
    }
}


public class Verticle2 extends Verticle {
    @Override
    public void start() {
        RouteMatcher routeMatcher = new RouteMatcher();
        routeMatcher.get("/test2", new Handler<HttpServerRequest>() {
            @Override
            public void handle(HttpServerRequest httpServerRequest) {
                httpServerRequest.response().end("TEST 2");
            }
        });
        vertx.createHttpServer().requestHandler(routeMatcher).listen(1234);
    }
}

Tim Fox

unread,
Oct 30, 2014, 3:34:23 AM10/30/14
to ve...@googlegroups.com
Yes, if you run the verticles in the *Same JVM* with the same host/port Vert.x will notice this and only start one real server and distribute requests round robin between your server instances.

In the example you posted you were starting two verticles in *Different JVMs* hence you did not see that behaviour.
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ziemo

unread,
Oct 30, 2014, 5:18:55 AM10/30/14
to ve...@googlegroups.com
I know it could be a silly question - but how can I reach that? Because like I said I've tried to use:

container.deployVerticle("Verticle1.java");
container.deployVerticle("Verticle2.java");

But in this case it neither work, nor throw any exception. But  the behavior is similar to my first issue, when I tried to deploy 2 verticles separately from the console - only first of then deployed correctly.

Tim Fox

unread,
Oct 30, 2014, 5:28:24 AM10/30/14
to ve...@googlegroups.com
On 30/10/14 09:18, Ziemo wrote:
I know it could be a silly question - but how can I reach that? Because like I said I've tried to use:

container.deployVerticle("Verticle1.java");
container.deployVerticle("Verticle2.java");

But in this case it neither work,

If you can provide a runnable reproducer that demonstrates the issue, someone will take a look.

Ziemo

unread,
Oct 30, 2014, 5:38:32 AM10/30/14
to ve...@googlegroups.com
It's very simple:

public class Startup extends Verticle {
    public void start() {
        container.deployVerticle("Hello2.java");
        container.deployVerticle("HelloWorld.java");
    }
}

Tim Fox

unread,
Oct 30, 2014, 5:42:16 AM10/30/14
to ve...@googlegroups.com
That's not a runnable reproducer.

Please provide something in gist.github.com (or as a github project) that I can copy and directly run to see the issue.

Also please provide the exact instructions for running it :)

Ziemo

unread,
Oct 30, 2014, 7:43:39 AM10/30/14
to ve...@googlegroups.com
Sorry, this the link to the github project: https://github.com/ziemo87/vertx_startup

For run type in console
vertx run Startup.java

Mumuney Abdlquadri

unread,
Oct 30, 2014, 7:50:52 AM10/30/14
to ve...@googlegroups.com
If I may ask why are you deploying two verticles that do different things on the same port?

Tim Fox

unread,
Oct 30, 2014, 7:51:44 AM10/30/14
to ve...@googlegroups.com
Can you put this in the form of a test case please?

Ziemo

unread,
Oct 30, 2014, 8:06:47 AM10/30/14
to ve...@googlegroups.com
Yes, but I can do that after my work today :)

In case of question - It's because I thought in case of vert.x you should deploy each Rest service on different "server" (in this case vert.x managed server) . 

Maybe I misunderstanding something with application scalability with vert.x (?). But also I find it a bit inconvenient to write whole dispatcher (which is Java coded) in one Verticle and then call other Rest services from it. So I thought to deploy all Rest services in separate service-server verticle.

If I'm wrong please point me out.

Alexander Lehmann

unread,
Oct 30, 2014, 2:20:25 PM10/30/14
to ve...@googlegroups.com
I am not sure if I understand what you want to do, but I assume you want to separate the different rest services in different classes (and verticles).

This is possible of course, but the approach to have the different services listen on the same port doesn't work. In any server, the listener has to start listening in a single process and if requests have to be distributed between different processes, these processes all have to have the same socket open, in apache this means that either threads or processes are used and every process is forked from the original master process and then they have to some kind of synchronization to accept and process requests.
Java programs usually do not fork at all and use threads for everything, in the case of async processing, there are not even threads to process the requests as in the case of vert.x

If you have a http listener, you will have only one httpserver running in one jvm, so that the requests are processed asynchronous and this will not have any scaling issues (if you have that, you will have to run 2 or more listeners on different ports and do the load balancing in some other way, but its really not necessary).
You can load off the actual work to other verticles (including worker verticles if you are doing things synchronously), via the eventbus or to different classes if you want to run everything inside the event loop, but still structure your program in different classes.

I think I do not understand what you mean by "deploy all REST services in separate verticles". You don't have to start optimizing your implementation before you are in fact getting scaling issues and you will not likely get them in vert.x if you program the code in the correct way.

Ziemo

unread,
Nov 3, 2014, 7:16:26 PM11/3/14
to ve...@googlegroups.com
OK, I think I'm understand that. So please confirm if I'm right. You have ONLY ONE Verticle class which can started HttpServer at port 80. But you can scale it through many cores if you like (here is round-robin implemented). But still it's one and only one class, and you can't define more classes on same HttpServer port.

I asked that question because I wonder what if I have many REST services on same HttpServer with same port 80. In that case routing is located in same Verticle class. This could be a bit messy while reading. In contrast RestEasy, for example provides a way to define @Path  for each class and it's much more readable that way.

Is there a way to do the same thing in vertx?

Tim Fox

unread,
Nov 4, 2014, 2:26:11 AM11/4/14
to ve...@googlegroups.com
On 04/11/14 00:16, Ziemo wrote:
OK, I think I'm understand that. So please confirm if I'm right. You have ONLY ONE Verticle class which can started HttpServer at port 80.

No, you can have as many as you want in the same Vert.x instance.

Ziemo

unread,
Nov 4, 2014, 5:00:37 AM11/4/14
to ve...@googlegroups.com
OK, so it's a bit frustrating. Because I can't find out in manual how can I manage multiple verticles with the same HTTP server and port (and RouteMatcher use).

I added test case to my github project (https://github.com/ziemo87/vertx_startup)

But when I test only testVerticle1() method or only testVerticle2() it works.
So maby I start those tests wrong. This exception occurs while I start 2 test methods:
SEVERE: Exception in Java verticle
java.net.ConnectException: Connection refused: localhost/127.0.0.1:1234

Neverteless when I deploy module and enter:
localhost:1234/verticle1
I get regular response

but on:
localhost:1234/verticle2
I get no response.

What I'm doing wrong? Because I've read second time whole manual but I not got it.

I will be very glad for your help!

Tim Fox

unread,
Nov 4, 2014, 5:08:06 AM11/4/14
to ve...@googlegroups.com
Can you provide a simple runnable test case as I asked before? :)

Ziemo

unread,
Nov 4, 2014, 5:18:51 AM11/4/14
to ve...@googlegroups.com
OK, please tell me what do you mean by that :)

On Github I added all 3 classes to getstarted package. There is also test in test.java.integration.java.IntegrationTest.

Just in case I copy-pasted my test into Gist if you mean that by simple :)

otherwise - please clarify - I'm sorry :)
It's very simple:

For more options, visit <a
...

Tim Fox

unread,
Nov 4, 2014, 5:21:58 AM11/4/14
to ve...@googlegroups.com
Sorry I am just confused about what test you want me to run as there are several tests in that project....

Ziemo

unread,
Nov 4, 2014, 6:22:32 AM11/4/14
to ve...@googlegroups.com
In test folder you have only java.getstarted.integration.java.IntegrationTest. 
But for clarity I've cleaned up all not used tests from test.resources.
...

Tim Fox

unread,
Nov 5, 2014, 6:00:19 AM11/5/14
to ve...@googlegroups.com
I had a quick look at the code. There are a few issues with your code:

1. On both tests you are deploying the verticle but not waiting for deploy to complete. So by the time you create your client and send a request there's a good chance it's not started yet. Remember, almost everything in Vert.x is asynchronous. If you want to wait for deployment you specify a resultHandler in the call to deployVerticle. There are examples of this in the docs and the example tests.
2. In your Startup verticle you are deploying two other verticles but you are not waiting for them to start before the start method returns. https://github.com/ziemo87/vertx_startup/blob/master/src/main/java/getstarted/Startup.java#L10
You should use the version of start that takes a Future. Again look at the example tests for examples of this.
3. In Verticle1 and Verticle2 you start a webserver but again you do no wait for them to start (same issue as 2)

HTH
--

Ziemo

unread,
Nov 7, 2014, 10:57:31 AM11/7/14
to ve...@googlegroups.com
I've fixed all things what you mentioned as far as I understand. But the problem persist -> second HTTP server is not "merged" to the first one. In fact what I discovered now that my HTTP servers start with different identifiers:
[HTTP-SERV] [DEPLOY] [SUCESS] in Verticle1 --> org.vertx.java.core.http.impl.DefaultHttpServer@3465bcf4
[HTTP-SERV] [DEPLOY] [SUCESS] in Verticle2 --> org.vertx.java.core.http.impl.DefaultHttpServer@725edbc0

I read core-manual, Java-API manual,  GitHub examples, examples settled in maven archetype without any answer. 

Can you look one more time on my code?

I want to build simple, REST application with more than one Verticle with routing contained in, so my case can't be very specific.

Thanks in advance for your time!  

...

Tim Fox

unread,
Nov 8, 2014, 3:17:52 AM11/8/14
to ve...@googlegroups.com

Ziemo

unread,
Nov 15, 2014, 11:35:42 AM11/15/14
to ve...@googlegroups.com
Tim, many thanks for your help and for your contribution in my code on github! Sorry for the delay in response, but I have been on vacation :) 
I will left my startup project on github if anyone may have the same problem in the future :)
...
Reply all
Reply to author
Forward
0 new messages