[英]What is the best way to determine Tomcat has started with node.js
我正在使用node.js构建应用程序,并且该应用程序与Tomcat服务器通信。 在启动tomcat服务器时,我不确定Tomcat是否已准备好并且已经启动,现在我在Windows和Mac上使用CURL和WGET,超时时间为2秒,以继续检查localhost:8080是否启动。
有没有一种更好的方法可以不依赖CURL和WGET?
建议的方法是在tomcat应用程序上创建心跳服务(即启动时发送OK的简单服务),并每隔x秒轮询一次。
心跳服务对于在应用程序运行时进行监视是必不可少的,并且有时即使应用程序已经在侦听端口,应用程序也没有准备就绪(因为正在进行一些繁重的初始化工作)。
但是,还有其他方法,如果您在同一台服务器上,则可以拖尾catalina.out,直到收到“服务器启动”行。
您可以设置您的tomcat应用程序来通知服务器它已经启动(尽管这意味着tomcat需要知道node.js服务器的url),或者可以设置某种可以注册的消息队列(例如ApacheMq等)当tomcat启动时,这还将允许两个服务之间的推送消息。
您可以实现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);
}
});
好了,您可以从Node.JS应用程序发出请求:
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
});
});
说明文件:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.