Reputation: 545
I am trying to run jxcore. but i am not able to.Please help me out as i am new to this.
server.js
var http = require("http");
jxcore.tasks.on('message', function (threadId, param) {
console.log('Main thread received a message from subthread no ' +
threadId + '. Message: ', param);
});
http.createServer(function(req,resp) {
console.log("Listening To Thread " + process.threadId);
resp.writeHead(200,{"Content-Type":"text/html"});
resp.end("Running JXCORE "+process.threadId);
}).listen(3000);
serverjx.js
var method = function() {
try {
process.keepAlive();
require("server");
console.log("Welcome To NodeJS");
return {
someResult: "some result";
};
} catch(e) {
console.log("Error Occured : "+e);
return {"Error":e};
}
}
jxcore.tasks.runOnce(code, {count:1000}, function(obj) {
process.sendToMain({started:true});
console.log("Return Value " + obj);
setTimeout(function() {
console.log("Waiting For TimeOut 5 Sec");
}, 5000);
});
and i am typing on cmd as jx server.js jx mt-keep server.js
I am not seeing thread running. please help
Upvotes: 0
Views: 634
Reputation: 4005
There are couple of structural mistakes here. Plus it is not obvious, what you're trying to do.
Scenario 1 - running only server.js
they both work: jx mt server.js
or jx mt-keep server.js
Scenario 2 - running serverjx.js which loads for each thread server.js
Here probably you try to create an http server on each thread by using jxcore.tasks.runOnce()
. So each of the threads would load server.js and create it's own instance of http server there.
This should be launched this way: jx serverjx.js
(without mt or mt-keep)
Although I don't see the point to do it this way (why not running it as in Scenario 1 since it is the correct approach for a multithreaded http server?), after few fixes the code would look like this:
serverjx.js:
var method = function () {
try {
process.keepAlive();
require("./server");
console.log("Welcome To NodeJS");
return {
someResult: "some result"
};
} catch (e) {
console.log("Error Occured : " + e);
return {"Error": e};
}
};
jxcore.tasks.runOnce(method, {count: 1000});
Please note few things:
require('server')
is wrong if you try to load server.js - you have to call require('./server')
jxcore.tasks.runOnce(code, ...)
is wrong - should be jxcore.tasks.runOnce(method, ...)
jxcore.tasks.runOnce
does not receive callback argument, so I removed this.Another thing is that in your callback you were trying to send an object to main thread (process.sendToMain({started:true})
), but you have located the listener in server.js (jxcore.tasks.on('message')
) which in fact gets loaded into a thread (so it is not the main thread) and the message could not arrive there.
Upvotes: 1