How to implement callback mechanism in Rserve? - r

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 ?

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

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:
Also, check the links for further reference. I didn't got who is the author otherwise I would have mentioned it.

Related

Does gRPC resend messages

A question related to the idempotence of serverside code, or the necessity of it. Either for gRPC in general, or specifically to the java implementation.
Is it possible that when we send a message once from a client, that it is handled twice by our service implementation?
Maybe this would be related to retries when service seems unavailable; or could be configured by some policy?
Right now, when you send a message from a client it will be seen (at most) once by the server. If you have idempotent methods, you will soon be able to specify a policy for automatic retries (design doc) but these retry attempts will not be enabled by default. You do not have to worry about the gRPC library sending your RPCs more than once unless you have this configured in your retry policy.
According to grpc-java/5724, the retry logic has already been implemented. The OP does it using a Map, that is not type safe. A better way would be as follows:
NettyChannelBuilder builder = NettyChannelBuilder.forAddress(host, port)
.enableRetry()
.maxRetryAttempts(3);
There are other retry configurations available on the NettyChannelBuilder.
There's also an example here, although it's pretty hard to find.

BizTalk: terminate messages

If I need to build a specialized web app to be able to terminate messages processed by specific send ports, WMI is one option. Are there others? and are there pros/cons to each approach?
You should be able to terminate messages programmatically by referencing the Microsoft.BizTalk.Operations.dll assembly. That will allow you to use the TerminateInstance method of the BizTalkOperations Class, which allows you to reference a remote BizTalk instance (using this constructor) without enabling remote WMI administrative access.
You may also need to reference Microsoft.BizTalk.Pipeline.dll in Visual Studio to get IntelliSense to work.
The BizTalk SDK includes a sample app that you can review, as well, to see how to look up a message instance, which you'll need for the parameter to the TerminateInstance method:
http://msdn.microsoft.com/en-us/library/gg163868
For example:
BizTalkOperations _operations = new BizTalkOperations()
IEnumerable messages = _operations.GetMessages();
foreach (BizTalkMessage msg in messages)
…
Have you considered the "/null" Send Port Adapter? This allows you to send messages to a "null" port, where they effectively disappear. Source code can be found here, although it hasn't been updated since BizTalk 2006 R2.
If this isn't relevant to what you are trying to achieve, maybe some additional information regarding the use case would help.

node.js asynchronous initialization issue

I am creating a node.js module which communicates with a program through XML-RPC. The API for this program changed recently after a certain version. For this reason, when a client is created (createClient) I want to ask the program its version (through XML-RPC) and base my API definitions on that.
The problem with this is that, because I do the above asynchronously, there exists a possibility that the work has not finished before the client is actually used. In other words:
var client = program.createClient();
client.doSomething();
doSomething() will fail because the API definitions have not been set, I imagine because HTTP XML-RPC response has not returned from the program.
What are some ways to remedy this? I want to be able to have a variable named client and work with that, as later I will be calling methods on it to get information (which will be returned via a callback).
Set it up this way:
program.createClient(function (client) {
client.doSomething()
})
Any time there is IO, it must be async. Another approach to this would be with a promise/future/coroutine type thing, but imo, just learning to love the callback is best :)

Passing an Arraylist of Java objects to a servlet from Java program

I would like to pass an arrayList of objects to a servlet from a java program.
Can some one please tell me, how this can be done.
Look at this link they describe the process ind detail
http://www2.sys-con.com/ITSG/virtualcd/java/archives/0309/darby/index.html
Please note that if you are going to serialize objects back and forth that the compiled version must be in sync on both the client and the server or you will get errors. I would recommend converting your objects to either XML or JSON and then reading them from that on the server side. That way if you client and server code get out of sync it will still work.
For the client I would recommend Apache's HttpClient (or whatever they have renamed it to)
Have you considered using a web service framework for this instead of coding a naked servlet? The whole business might be about 10 lines of code using, for example, an Apache CXF JAX-RS service and client. If the objects are complex, you might want to use a full SOAP service.

Get host name without using HttpRequest

I want to run a "background job" in my ASP.NET application (periodically, as separate thread). And I need host name (DNS name or IP) to do my tasks. The problem is that the HttpContext.Current may be not available here (it's NULL).
Is there any way to get a host name in not using HttpContext.Current.Request.Url.Host.
When the host name is available in HttpContext.Request.Url.Host, it is a result of the host name being part of the request sent by the client. As an example, take a request to this page:
GET /questions/2164261/get-host-name-without-using-httprequest HTTP/1.1
Host: stackoverflow.com
...
When running in a background thread, no request context is available, and there really is no concept of a host name at all. Your only alternative is to store the hostname within the code or in configuration.
Slightly off topic: Running scheduled tasks within a web application is asking for trouble, and spawning threads only deals with a few of them. If at all possible, consider running your scheduled jobs from a Windows service, possibly built using a framework like NCron.
probably you can add a class variable in your thread class, and set this variable with request.url.host before you run the thread class.
this method can also apply to the session object.
Keep in mind that it's a bad idea to initiate that "background job" from a web application if you need that background process to run 24/7 independently. Even if you start it in a new thread. Your web app may have no requests for some time. In this case the run time will shut down the process and all its "child" threads. For continuous running you need to run it as a Windows service. Otherwise, the Darren is right, use the System.Net.Dns.GetHostName().
I'm using the same approach as you for scheduling regular tasks and the way I worked around this is to store the machine name for later use when the application gets any kind of web request.
It's a rather dirty hack, but the only way to do this unless you want to hard-code it or retrieve it from an external configuration file, which was too dangerous (unreliable) for my purposes.

Resources