0

i would like deploy inside my Java VertX a javascript Verticle. Of course, this is not a problem. The question is, how can i give them a callback ?

My Java Code:

    Vertx.clusteredVertx(vertxOptions, res ->
    {
        if (res.succeeded()) {
            logger.info("Cluster ready, starting verticle deploy");
            /*
             * React JS Server Deploy
             */
            Future< String > reactVerticleFuture = Future.future();

            vertx.executeBlocking(future ->
            {
                vertx.deployVerticle("dist/server.js", options, deployResult ->
                {
                    if (deployResult.succeeded()) {
                        future.complete();
                    } else {
                        future.fail(deployResult.cause());
                    }
                });

            } , reactVerticleFuture.completer());

             CompositeFuture.all(..., reactVerticleFuture).setHandler(ar ->
            {
               /*
                * deploy http listener and health endpoint
                */
            });
        } else {
            logger.error(res.cause().getMessage(), res.cause());
        }
    });

My server.js:

exports.vertxStartAsync = function(startFuture) {

console.log('vertxStartAsync')

var eb = vertx.eventBus()
var consumer = eb.consumer('httpGetWebChannel', function (message) {
})

consumer.completionHandler(function (res, res_err) {
    if (res_err == null) {
        console.log("The handler registration has reached all nodes");

        startFuture.complete()
    } else {
        console.log("Registration failed!");

        startFuture.fail()
    }
});
}

Of course, my server.js is bigger and this need's some time for starting. In my clustered Vertx i get during the start some messages that something blocks my bus.

How can i fix this ?

Thanks Marcel

Marcel
  • 377
  • 5
  • 16
  • If you `server.js` does some blocking stuff on start up you should extract these parts and execute it as a worker verticle or with [`executeBlocking`](http://vertx.io/docs/vertx-core/js/#blocking_code). Regarding the callback... Your `server.js` already subscribed to `httpGetWebChannel` and therefore you could send message from your Java verticle via event bus to your JavaScript verticle. Then reply with `message.reply("how interesting!");` (see [eventbus docs](http://vertx.io/docs/vertx-core/js/#event_bus)). – alexvetter Mar 12 '16 at 16:29
  • That's exactly my problem. the server.js is not listening on the eventbus in time. On my Java Side, i use something like that (from eventbus docs): consumer.completionHandler(function (res, res_err) { if (res_err == null) { console.log("The handler registration has reached all nodes"); } else { console.log("Registration failed!"); } }); But on JS Side, i don't have this feature. – Marcel Mar 12 '16 at 16:42
  • I mean, after some seconds he's listening. but when i've no callback, i switch my traffic directly to the new service and then i get a lot of errors. – Marcel Mar 12 '16 at 17:00
  • Then you probably should use the `AbstractVerticle.start` with the `Future` argument and complete this Future in the callback of the consume. – alexvetter Mar 12 '16 at 18:42
  • Ok, i'll. but how can i send Future complete in the Javascript, after my consumer is started and known in the cluster ? – Marcel Mar 12 '16 at 21:37
  • What is the name in javascript of the future ? – Marcel Mar 12 '16 at 21:56
  • I think this should help: http://vertx.io/docs/vertx-core/js/#_asynchronous_verticle_start_and_stop – alexvetter Mar 12 '16 at 22:17
  • Thanks, i try it in my server.js file and it's not working. Because i deploy my server.js verticle from the java side. – Marcel Mar 12 '16 at 23:12
  • I put my try in my question and change the server.js file/logic. but i guess in a normal world it will work - i use webpack and es6 for "compiling" this js file .... – Marcel Mar 13 '16 at 08:15

1 Answers1

1

Ok, we found a solution ( alexvetter - thanks for your patience).

The problem is that i build my server.js file with webpack and after building, the exports block is inside a function.

This is the working solution:

webpack.config.js

var WrapperPlugin = require('wrapper-webpack-plugin');

... 

plugins: [
        new WrapperPlugin({
            header: 'exports.vertxStartAsync = function(startFuture) {\n',
            footer: '}\n'
        }),
...
]
...

server.js

var eb = vertx.eventBus()
var consumer = eb.consumer('httpGetWebChannel', function (message) {
  ...
})

consumer.completionHandler(function (res, res_err) {
    if (res_err == null) {
        console.log("The handler registration has reached all nodes");

        startFuture.complete()
    } else {
        console.log("Registration failed!");

        startFuture.fail()
    }
});

Thanks

Marcel
  • 377
  • 5
  • 16