If you are an XPages developer who is interested in learning about this tool then I first suggest checking out the following resources:
- This email response to a question by Tim Tripcony is sheer brilliance. I had to read and re-read this several times to really understand it, but that speaks all about me and nothing about how Tim explains things. A big thanks to David Leedy for making it available to all.
- Keith Strickland has written a very helpful blog post on JSON-RPC way back in June 2011. It was Keith who first turned me on to the Remote Service control when I found his answer to this Stack Overflow question.
- The Extension Library Book devotes a few pages (351-353) to the JSON-RPC that I found very helpful.
How I and why I used the JSON-RPC control
I needed to run a server based Java method that called a process and returned a result. My requirements stated that I need to try this process twice in a single event, and tell the user if the first try has failed. The way that Java works is to always one return value, so the intermediary messages would never be returned. In order to accomplish what I needed, I knew that clientside javascript was my best bet. In order to call a serverside java method from clientside JS, I decided to use an JSON-PRC call.
2) Give the service a serviceName and/or a pathInfo. I believe that the service may be referred to as either.
3) Create the methods that you need to run. This will involve adding optional arguments and adding SSJS code to run in the script property. The script property doesn’t use the standard script editor that is usually used when you can enter serverside javascript.
4) The finished result
Here is the code where I call my remote service methods:
How to create the JSON-RPC service
1) Pull the tool from the pallet under “Data Access”. It is just called “Remote Service”.2) Give the service a serviceName and/or a pathInfo. I believe that the service may be referred to as either.
3) Create the methods that you need to run. This will involve adding optional arguments and adding SSJS code to run in the script property. The script property doesn’t use the standard script editor that is usually used when you can enter serverside javascript.
4) The finished result
Using the Remote Service
It took me a while to understand that the service doesn’t return a value initially, it returns an object that contains a callback method. The callback method actually returns the value. Thanks to Toby Samples who answered my Stack Overflow question helping me with this.Here is the code where I call my remote service methods:
Code Explanation:
- First the code shows the user a message that the process is starting
- Next, a value needed in one of the messages is pulled from a hidden input
- The RPC method is called, the object returned is assigned to the var “deferred”
- The callback of the object is called to get the result of the ‘first’ method
- If the ‘first’ method returns true, the user gets a success message
- If the ‘first’ method fails, the user gets a ‘retrying’ message, and the second method is called and the resulting object is assigned to a var “insideDeferred”
- If the result of the ‘second’ is true, the user gets a success message
- If the result of the ‘second’ method is false, the user gets a ‘try later’ message.
Nice post. Thanks for the mention.
ReplyDeleteOne thing that stood out. You say Java can only return a single value. Well that's yes.. And no.
I return multiple values from Java often by putting them in a hash map. It's just key, value pairs right?
So for user messages I might return a map with the keys "result", "title", "msg". And appropriate values to use in a dialog box maybe.
Of course that returning a hashMap is multiple messages for one event. You're talking about about multiple events I think. Just wanted to make sure you knew about returning multiple values overall.
ReplyDeleteYes, I needed the same method to return something twice at different times. Not possible as far as I know.
DeleteThat email was in response to a question I had emailed him about the JSON-RPC service. I received it a couple of days before he passed on.
ReplyDeleteI'll never delete that email :-(
Dan
Dan, I thought that might have been from you. Great question and great answer. Even though I only met him once, I miss having him around.
Delete