How to implement callback mechanism in Rserve?
Asked Answered
K

2

14

i want to know a simple way of implementing callback mechanism in Rserve for a java client . According to Rserve docs :

Rserve provides no callback functionality. Your application could implement callbacks via TCP/IP and the R sockets but it is not a part of Rserve.

This means my java client can call functions on the remote Session through Rconnection reference , but the remote Session cannot call back the java client which has instantiated it . How can i develop such a mechanism . If its through R sockets or a tcp/ip server , does that mean for every connection there will be a socket server open ?

Kooima answered 23/5, 2012 at 7:12 Comment(3)
You can use socketConnection, from R, to connect to your Java client (who should be listening).Alarcon
Hi, I did some research and I know how to implement non-blocking calls from java. Can't implement it because my company don't need that, but if someone interested then ping me.Crossbill
Hi Prezmek, I gave up on doing this through sockets used a message queue to which R server published and java listened to get the callback. If interested you can use www.inside-r.org/packages/cran/Rjms/docs/Rjms to integrate with activeMQKooima
C
2

Ok, so this is how I think it is possible to implement reactive R.

Non-blocking calls from java

You need to fork RServe java client and split method request into two parts in this line [1]. First part write request to the socket and the second waits for response. We need to make waiting optional by for example some boolean flag.

Returning result from R

You will need some kind of active communication to Java. One possibility is to use plain sockets or something on higher level as HTTP. I thought about httpRequest package [2]. So the call from java should look like:

connection.eval(s"""simplePostToHost(
"192.168.12.12","/listener/results/",
try(eval(parse(text="$code")),silent=TRUE),port=8080""")

Listening for result in Java

The request and response should share some kind of unique ID so we know which response is for which request. You should run some service that listen on path /listener/results for incoming results and tells Java that result is ready. It should also enable to reuse RConnection that previously should be marked as "busy". I recommend to use it this part scala Promise[T] .

Hope it helps somebody. I'm probably going to implement it once my company needs it.

[1]https://github.com/s-u/REngine/blob/a74e184c051c2d2e850430cd2d0526656d3a6c48/Rserve/protocol/RTalk.java#L211

[2]https://cran.r-project.org/web/packages/httpRequest/httpRequest.pdf

Crossbill answered 16/7, 2015 at 13:49 Comment(0)
A
1

Here is the answer I found on the http://statweb.stanford.edu/~lpekelis/13_datafest_cart/13_datafest_r_talk.pdf and at http://www.rforge.net/JRI/files/

Start with the instance of R

Rengine re= new Rengine(args, false, new TextConsole());

Here is the code you can see for the call back:
enter image description here
Also, check the links for further reference. I didn't got who is the author otherwise I would have mentioned it.

Allegorist answered 16/7, 2015 at 10:32 Comment(1)
I think that this rs.eval will block the thread, so it's not the correct answerCrossbill

© 2022 - 2024 — McMap. All rights reserved.