What is the best way to determine Tomcat started with node.js

I am creating an application using node.js and this application interacts with the Tomcat server. While the tomcat server is starting up, I'm not sure if Tomcat is ready and has appeared or not, now I use CURL and WGET on Windows and Mac with a 2 second timeout to check if localhost: 8080 has appeared.

Is there a better way to do this without relying on CURL and WGET?

+5
source share
3 answers

The proposed method is to create a heartbeat service in the tomcat application (IE is a simple service that sends OK when it is) and poll it every x seconds.
Service maintenance is important for monitoring while the application is running, as well as when the application is not ready, even if it is already listening on the port (because some intensive initialization is taking place).

, , .out , , .
tomcat, ( , tomcat URL- node.js) , , - (, ApacheMq ), , tomcat , .

+2

httpWatcher ( - fs.watch). http ( html ) , 200 ( ). - :

var request = require('request');

var watch = function(uri) {
  var options;
  var callback;

  if ('object' == typeof arguments[1]) {
    options = arguments[1];
    callback = arguments[2];
  } else {
    options = {};
    callback = arguments[1];
  }

  if (options.interval === undefined) options.interval = 2000;
  if (options.maxRuns === undefined) options.maxRuns = 10;
  var runCount = 0;

  var intervalId = setInterval(function() {
    runCount++;
    if(runCount > options.maxRuns) {
        clearInterval(intervalId);
        callback(null, false);
    }

    request(uri, function (error, response, body) {
          if (!error && response.statusCode == 200) {
            clearInterval(intervalId);
            callback(null, true);
          }
        });
  }, options.interval);
}

:

watch('http://blah.asdfasdfasdfasdfasdfs.com/', function(err, isGood) {
    if (!err) {
        console.log(isGood);
    }
});

...

watch('http://www.google.com/', {interval:1000,maxRuns:3}, 
  function(err, isGood) {
    if (!err) {
        console.log(isGood);
    }
});
+2

Well, you can make requests from the Node.JS app:

var http = require("http");

var options = {
    host: "example.com",
    port: 80,
    path: "/foo.html"
};

http.get(options, function(resp){

    var data = "";

    resp.on("data", function(chunk){
        data += chunk;
    });

    resp.on("end", function() {
        console.log(data);
        // do something with data
    });

}).on("error", function(e){
    // HANDLE ERRORS
    console.log("Got error: " + e.message);

}).on("socket", function(socket) {
    // ADD TIMEOUT
    socket.setTimeout(2000);  
    socket.on("timeout", function() {
        req.abort();
        // or make the request one more time
    });
});

Documentation:

http://nodejs.org/docs/v0.4.11/api/http.html#http.request

+1
source

All Articles