I have a web application built using nodejs. The backend is calling several java command lines to answer queries. Those java programs are relying on models that are quite huge (around 50Mo), and for each java command line executed the model needs to be reloaded. In the end, most of the answer time is spent on loading these models in RAM.
What I would like to do is to set-up a java server along with the nodejs one, and make those two servers communicate. The java server would keep the models in RAM and handle all the java processing. The problem is that node is asynchronous and I would like to fire a callback when my java server ended processing the data. Do you know any good way to do this? Would it be possible to send some sort of POST request from node to java on localhost?
EDIT:
Ok since jfriend00 and Pelit Mamani answered me I've made some progress. I decided to go with jfriend00's solution to use socket.io. Here is exactly what I would like to do (here in javascript):
I have a server (the one that I would like to have in Java, here in javascript)
var http = require('http'); console.log('Creating HTTP server'); var server = http.createServer(function(req, res){ }); var io = require('socket.io').listen(server); io.sockets.on('connection', function(socket) { console.log('Client connected.'); // Disconnect listener socket.on('disconnect', function() { console.log('Client disconnected.'); }); socket.on('myEvent', function (data, fn) { console.log(data); console.log('\tSending answer ...'); fn('\tI am the answer'); }); }); server.listen(8080);
And I have a client (in my case it will be my nodejs server):
// Connect to server var io = require('socket.io-client') var socket = io.connect('http://localhost:8080'); socket.on('connect', function () { console.log('Connected!\n\tSending query ...'); socket.emit('myEvent', '\tI am the query', function (data) { console.log(data); }); });
The client send a query, the server answer the query, simple. I'm using netty-socketio to implement the server part in java. Here is what I have right now:
import java.io.UnsupportedEncodingException;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.Transport;
import com.corundumstudio.socketio.listener.*;
public class App {
public static void main(String[] args) throws InterruptedException, UnsupportedEncodingException {
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(8080);
final SocketIOServer server = new SocketIOServer(config);
server.addConnectListener(new ConnectListener() {
@Override
public void onConnect(SocketIOClient client) {
System.out.println("client connected !: "+client.toString());
}
});
server.addEventListener("myEvent", String.class, new DataListener<String>() {
@Override
public void onData(final SocketIOClient client, String data, final AckRequest ackRequest) {
System.out.println("myEvent triggered");
System.out.println("Here is the query from the client: \n"+data);
ackRequest.sendAckData("I am the answer from the Server!");
}
});
server.addDisconnectListener(new DisconnectListener() {
@Override
public void onDisconnect(SocketIOClient client) {
System.out.println("client disconnected !: "+client.toString());
}
});
server.start();
System.out.println("server started");
Thread.sleep(20000);//Integer.MAX_VALUE);
server.stop();
System.out.println("server stopped");
}
}
When running this server with my javascript client, everything is working:
On the client side:
Connected! Sending query ... I am the answer from the Server!
On the server side:
server started client connected !: com.corundumstudio.socketio.transport.NamespaceClient@ecac7898 myEvent triggered Here is the query from the client: I am the query client disconnected !: com.corundumstudio.socketio.transport.NamespaceClient@ecac7898
But I don't think AckRequest should be used to send data like that. I will probably change for another implementation. To be continued...