event bus configuration

485 views
Skip to first unread message

qsys

unread,
Jul 21, 2013, 4:01:42 PM7/21/13
to ve...@googlegroups.com
Hey all, I have some issues with the event bus. I tried the examples from https://github.com/vert-x/vertx-examples/tree/master/src/raw/java, https://github.com/vert-x/vertx-examples/tree/master/src/raw/javascript and https://github.com/darkredz/vertx-sockjs-eventbus-hook and tried to figure out how it exactly works using the vertx docs and http://blog.shinetech.com/2012/05/31/halo-vert-x/.

Things seem to be relatively easy, so I tried to make a MWE myself, and it's not behaving as I expect. Code: see below. I set up an eventbus handler 'fetchdata' in the main Server verticle.

1. In my first trials, I tried to send messages to the handler from the browser (index.html). Messages didn't seem to be sent. The event bus does open, but messages are not sent (nor published). I get this output using the browser debug console:
GET localhost:8080        200 OK                  localhost:8080   953 B    127.0.0.1:8080    2ms
GET jquery-1.10.2.min.js  304 Not Modified        code.jquery.com  32,1 KB  68.232.35.119:80  17ms
GET sockjs-0.3.4.min.js   304 Not Modified        cdn.sockjs.org   33,1 KB  54.240.184.150:80 17ms
GET vertxbus.js           200 OK                  localhost:8080   6,4 KB   127.0.0.1:8080    2ms
GET info                  200 OK                  localhost:8080   77 B     127.0.0.1:8080    1ms
GET websocket             101 Switching Protocols localhost:8080   0 B      127.0.0.1:8080    5ms

When I registered a handler in index.html (using javascript), all messages sent or published were recieved by that (local) handler only.

2. So I went to a more basic example, and tested with a 'sender-verticle'. When I ran both verticles ('vertx run Server.java' and 'vertx run Sender.java'), again, the messages sent or published by the sender where not handled by the handler in the server (or by the handler on the client side). However, when I ran both verticles in cluster-mode ('vertx run Server.java -cluster and 'vertx run Sender.java -cluster'), it did work:
$ vertx run Server.java -cluster
Starting clustering...
No cluster-host specified so using address 192.168.11.2
fetch data: ping!
fetch data: publishing
fetch data: ping!
fetch data: publishing
fetch data: ping!
fetch data: publishing

$ vertx run Sender.java -cluster
Starting clustering...
No cluster-host specified so using address 192.168.11.2
Received reply: pong!
Received reply: pong!
Received reply: pong!


As far as I understood, the -cluster option shouldn't be necessary when the verticles run on the same machine (although I start to doubt that). In any case, The problem sending messages on the event bus from (or to) the client (index.html) seems pretty much the same problem as the -cluster option, although I'm not really sure. Anyway, I must be doing something wrong or overlooking something... Or maybe I need some (hazelcast?) config, but in the examples, there's no config either, so I suppose it's not neccesary.

I'm using vertx-2.0.0-final, openJDK RE (IcedTea7 2.1.7) (7u3-2.1.7-1) and openJDK 64-Bit Server VM (build 22.30-b10, mixed mode), if that may be important.

Any ideas? Thx a lot, Kurt



--
Sever.java:
import org.vertx.java.core.Handler;
import org.vertx.java.core.eventbus.Message;
import org.vertx.java.core.http.HttpServer;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.core.json.JsonArray;
import org.vertx.java.platform.Verticle;


public class Server extends Verticle {
    public void start() {
        HttpServer server = vertx.createHttpServer();
        server.requestHandler(new Handler<HttpServerRequest>() {
            public void handle(HttpServerRequest req) {
                if (req.path().equals("/")) {
                    req.response().sendFile("web/index.html");
                }
                if (req.path().endsWith("vertxbus.js")) {
                    req.response().sendFile("web/vertxbus.js");
                }
            }
        });
        JsonObject config = new JsonObject().putString("prefix", "/eventbus");
        vertx.createSockJSServer(server).bridge(
                config,
                new JsonArray(),
                new JsonArray()
                );
        vertx.eventBus().registerHandler("fetchdata", new Handler<Message>() {
            public void handle(Message object) {
                System.out.println("fetch data: " + object.body());
                object.reply("pong!");
            }
        });
        server.listen(8080);
    }
}


I had more verticles here, i.e. a seperate for the handler, but to I put the together since I couldn't get it working properly (nor with something similar in javascript).
--
Sender.java - send and publish a message every two seconds:
import org.vertx.java.core.Handler;
import org.vertx.java.core.eventbus.Message;
import org.vertx.java.platform.Verticle;

public class Sender extends Verticle {

    public void start() {
        vertx.setPeriodic(2000, new Handler<Long>() {
            @Override
            public void handle(Long timerID) {
                vertx.eventBus().send("fetchdata", "ping!", new Handler<Message<String>>() {
                    @Override
                    public void handle(Message<String> reply) {
                        System.out.println("Received reply: " + reply.body());
                    }
                });
                vertx.eventBus().publish("fetchdata", "publishing");
            }
        });
    }
}

--
web/index.html (I also tried with replacing 'localhost' with '127.0.0.1' and '192.168.11.2', the address in the local network):
<!DOCTYPE html>

<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://cdn.sockjs.org/sockjs-0.3.4.min.js"></script>
<script src="vertxbus.js"></script>
</head>
<body>
<div id="content">
    blabla<br />
</div>

<script>
var eb=null;

openConn = function() {
    if(!eb) {
        eb = new vertx.EventBus("http://localhost/eventbus");
        eb.onopen = function() {
            $('#content').append('open connection<br />');
        }
        eb.onclose = function() {
            alert('close connection');
            eb=null;
        }
    }
}
$(document).ready(function() {
    openConn();
    setTimeout(function() {
        if (eb) {
            $('#content').append('ready... ');
            eb.send('fetchdata', 'sending from client', function(reply) {
                $('#content').append('recieved <br />' + reply );
            });
        }

    }, 2000)
});
</script>
</body>
</html>




Tim Fox

unread,
Jul 22, 2013, 3:37:15 AM7/22/13
to ve...@googlegroups.com
send only sends messages to *one* handler - please see docs
>
> 2. So I went to a more basic example, and tested with a 'sender-verticle'.
> When I ran both verticles ('vertx run Server.java' and 'vertx run
> Sender.java'), again, the messages sent or published by the sender where
> not handled by the handler in the server (or by the handler on the client
> side). However, when I ran both verticles in cluster-mode ('vertx run
> Server.java -cluster and 'vertx run Sender.java -cluster'), it did work:
> $ vertx run Server.java -cluster
> Starting clustering...
> No cluster-host specified so using address 192.168.11.2
> fetch data: ping!
> fetch data: publishing
> fetch data: ping!
> fetch data: publishing
> fetch data: ping!
> fetch data: publishing
>
> $ vertx run Sender.java -cluster
> Starting clustering...
> No cluster-host specified so using address 192.168.11.2
> Received reply: pong!
> Received reply: pong!
> Received reply: pong!
>
> As far as I understood, the -cluster option shouldn't be necessary when the
> verticles run on the same machine (although I start to doubt that).

Cluster is necessary for any verticles running in separate jvms to
communicate.
> In any
> case, The problem sending messages on the event bus from (or to) the client
> (index.html) seems pretty much the same problem as the -cluster option,
> although I'm not really sure. Anyway, I must be doing something wrong or
> overlooking something... Or maybe I need some (hazelcast?) config, but in
> the examples, there's no config either, so I suppose it's not neccesary.


>
> I'm using vertx-2.0.0-final, openJDK RE (IcedTea7 2.1.7) (7u3-2.1.7-1) and
> openJDK 64-Bit Server VM (build 22.30-b10, mixed mode), if that may be
> important.
>
> Any ideas? Thx a lot, Kurt

I'm struggling to see what the issue is here. Perhaps a _simpler_
example would make it clearer./

Also.. there is an eventbusbridge example in the vertx-examples
repository which seems to do most of what you want to do (and works) -
have you tried that?

Tim Fox

unread,
Jul 22, 2013, 3:51:09 AM7/22/13
to ve...@googlegroups.com
Some more observations:

You also seem to be mixing send and publish - not sure why.... and on
the client you are potentially starting sending before the event bus is
open (see the eventbusbridge example)




On 21/07/13 21:01, qsys wrote:
Message has been deleted

qsys

unread,
Jul 22, 2013, 7:28:26 AM7/22/13
to ve...@googlegroups.com
I actually have only 1 handler and I used both send and publish to test both methods. I started from the eventbusbridge example which did not entirly did what I wanted (there is no eventbus-handler on the server-side). That's when I started to test the things with the 'Sender.java'-verticle. Maybe this will clarify some things:

1. I tried the example and left out the eventbus handler on the client and I added a handler on the server. That's what I wanted.I didn't need a handler on index.html. 'index.html' is still a simplified version of the example in eventbusbridge. It didn't work.

2. I added a handler in the 'index.html'-page (like in the example), and that handler did recieve the messages (like in the example). But that was when both publisher and handler where in index.html.

3. I checked the 'examples eventbus_pointtopoint' and 'eventbus_pubsub'. These work when run in cluster-mode, as it should be, as pointed out to me. The code of Sender.java is similar to the code used in the 'eventbus_pointtopoint' example. It works but both handler and publisher are on the server-side, both running in cluster-mode. Any messages sent or published (!) from the client are not handled by the server and vice verca.

The examples work, but they send or from client to client, or from server to server, not from client to server or vice versa. Publishing a message on the browser is not handled by the server and messages published on the server are not handled on the client. The eventbus bridge is open, as can be seen on the debugger of the browser and also, the 'onopen' event in the browser has occured - I did not mention that because I thought that was obvious. However, it seems to me that the client is somehow not registered in the cluster, so there are like two seperated event busses with the same name, which are not connected (yet). To reproduce my basic problem:

1. start the Server-verticle
2. go to http://localhost:8080/index.html

'index.html' will send a message to the event bus after 2 seconds. There is a handler in the 'Server.java'-verticle, so the message should be handled by that verticle. It's not. Publishing instead of sending will have the (no-)effect.
To test that the handler on the server works: start the 'Sender.java' (and 'Server.java' both in cluster mode) and the handler will handle both sent and published messages on the fetchdata-channel of localhost:8080/eventbus.
To test that sending (or publishing) on the client works, add a handler in the javascript and see it working properly.

Both server (verticles) and client (browser) are running on the same machine.

Thx, Kurt

Op maandag 22 juli 2013 09:51:09 UTC+2 schreef Tim Fox het volgende:

Tim Fox

unread,
Jul 22, 2013, 9:46:23 AM7/22/13
to ve...@googlegroups.com
On 22/07/13 12:19, qsys wrote:
> I actually have only 1 handler and I used both send and publish to test
> both methods. I started from the eventbusbridge example which didn't work.

If the example didn't work can you please file a github issue? We test
all examples before each release, so it certainly should work.

> That's when I started to test the things with the 'Sender.java'-verticle.
> Maybe this will clarify some things:
>
> 1. I tried the example and left out the handler first and added a handler
> on the server. That's what I wanted.I didn't need a handler on index.html.
> 'index.html' is still a simplified version of the example in
> eventbusbridge. It didn't work.
>
> 2. I added a handler in the 'index.html'-page (like in the example), and
> that handler did recieve the messages. But that was when both publisher and
> handler where in index.html.
>
> 3. I checked the 'examples eventbus_pointtopoint' and 'eventbus_pubsub'.
> These work when run in cluster-mode. The code of Sender.java is similar to
> the code used in the 'eventbus_pointtopoint' example. Again, it works but
> both handler and publisher are on the server-side, both running in
> cluster-mode. Any messages sent or published (!) from the client are not
> handled by the server and vice verca.
>
> The examples work, but they send or from client to client, or from server
> to server, not from client to server or vice versa. Publishing a message
> from the browser is not handled by the server and messages published on the
> server are not handled on the client. The eventbus bridge is open, as can
> be seen on the debugger of the browser and also, the 'onopen' event in the
> browser has occured - I did not mention that because I thought that was
> obvious. However, it seems to me that the client is somehow not registered
> in the cluster, so there are like two seperated event busses with the same
> name, which are not connected (yet). To reproduce my basic problem:
>
> 1. start the Server-verticle
> 2. go to http://localhost:8080/index.html
>
> 'index.html' will send a message to the event bus after 2 seconds. There is
> a handler in the 'Server.java'-verticle, so the message should be handled
> by that verticle. It's not. Publishing instead of sending will have the
> (no-)effect.
> To test that the handler on the server works: start the 'Sender.java' (and
> 'Server.java' both in cluster mode) and the handler will handle both sent
> and published messages on the fetchdata-channel of localhost:8080/eventbus.
> To test that sending (or publishing) on the client works, add a handler in
> the javascript and see it working properly.
>
> For clarity: I use the terms server and client, but both are on the same
> machine.
>
> Thx, Kurt
>
>
> Op maandag 22 juli 2013 09:51:09 UTC+2 schreef Tim Fox het volgende:

Tim Fox

unread,
Jul 22, 2013, 9:48:48 AM7/22/13
to ve...@googlegroups.com
On 22/07/13 12:28, qsys wrote:
> I actually have only 1 handler and I used both send and publish to test
> both methods. I started from the eventbusbridge example which did not
> entirly did what I wanted (there is no eventbus-handler on the
> server-side). That's when I started to test the things with the
> 'Sender.java'-verticle. Maybe this will clarify some things:
>
> 1. I tried the example and left out the eventbus handler on the client and
> I added a handler on the server. That's what I wanted.I didn't need a
> handler on index.html. 'index.html' is still a simplified version of the
> example in eventbusbridge. It didn't work.

Before going any further, please can you show the _exact_ changes you
made to the eventbusbridge example which 'didn't work', and the exact
command lines you used to run it.

qsys

unread,
Jul 22, 2013, 11:42:53 AM7/22/13
to ve...@googlegroups.com
Hey, thx for you patience. I tried to reproduce all the things I tried, which I failed, because... I found what was wrong:
I permitted a JsonArray() with no elements (so no type of messages were allowed on the server, I guess). I suppose I mixed the code from the example with snippets from 'http://vertx.io/core_manual_java.html#setting-up-the-bridge'. Problem solved and thanks again for your patience :).
Thx, Kurt


Op maandag 22 juli 2013 15:48:48 UTC+2 schreef Tim Fox het volgende:
Reply all
Reply to author
Forward
0 new messages