Simple JSON-RPC updated to 1.0.0 8


Simple JSON-RPC has been updated to 1.0.0. This new version includes a brand new event messaging system making it possible for classes to react to events generated by the Simple JSON-RPC framework.

Through simple event messaging, your RPC classes are now able to react to and even interact with servlet configuration, context, request, and response objects. To use this facility your class needs to extend the new JSONRPCEventListener interface and implement the messageReceived method, accepting one argument of the JSONRPCMessageEvent type.

Here is an example of how a class could implement a Simple JSON-RPC event listener:

public void messageReceived(JSONRPCMessageEvent me) {
	switch(me.message().getCode()) {
		case JSONRPCMessage.INIT:
			config = me.message().getServletConfig();
		break;
		case JSONRPCMessage.BEFOREREQUEST:
			request = me.message().getRequest();
			session = request.getSession(true);
		break;
		case JSONRPCMessage.BEFORERESPONSE:
			response = me.message().getHttpResponse();
			response.setContentType("text/html");
		break;
		case JSONRPCMessage.AFTERRESPONSE:
			lastresponse = me.message().getRPCResponse();
		break;
	}
}

This new feature should also make it easier to implement solutions that use sessions. This should also make it possible for classes to react to the output of other classes, including errors.

No related content found.


Leave a comment

8 thoughts on “Simple JSON-RPC updated to 1.0.0

  • Stephen Gaunt

    Thanks a lot. This is great stuff. This is the kind of framework i was searching for. This keeps the presentation layer totally independent of the service when compared to others frameworks like jabsorb. Hope you will keep on improving this.

    I faced one issue. Creating a new instance of the service throws InstantiationException. If I keep the service method as static or if the persist_class property is true then it works.

    • wes Post author

      Thanks for taking the time to try it out and give me some feedback. You wouldn’t happen to have the full error that was being produced by any chance, would you?

  • Stephen Gaunt

    Hi,

    The following is the exception I am getting. The service method i am calling has no arguments. I think the problem is creating a new instance for the service class.

    INFO: looking for methodsig: getUserProfile:0
    java.lang.InstantiationException: java.lang.reflect.Method
    at java.lang.Class.newInstance0(Class.java:340)
    at java.lang.Class.newInstance(Class.java:308)
    at com.werxltd.jsonrpc.RPC.runMethod(RPC.java:545)
    at com.werxltd.jsonrpc.RPC.handleRequest(RPC.java:523)
    at com.werxltd.jsonrpc.RPC.doGet(RPC.java:392)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)

  • Stephen Gaunt

    Just one more thing. If I want to call one service from another (both are running in the same server) using json-rpc, how do I do that? Do I have to use a library like httpclient? Is it a performance overhead to use json-rpc if both services are running in the same server? The reason I want to use json-rpc for communication with various services is so that the services can be developed in various languages like PHP, java etc.

    Thanks.

  • wes Post author

    As for the error: does your class initialize properly if persistence is enabled in the configuration? I will write a test case to see if I can duplicate the error you are experiencing.

    In regards to interoperability. This library was actually devised as a way to communicate from PHP with Java-based solutions which didn’t have external web services (many do now) but provide API calls that a simple object could interact with. That is the story behind the simple HBase query bridge. So what you describe as your primary reason for using JSON-RPC, and this library in particular, is exactly why it was developed in the first place.

    While it may not be ideal, there is some overhead to using http as an RPC medium, I think it is negligible considering what you gain by way of stability and interoperability. Stability in that your RPC app can be hosted on a rock-solid application server like JBoss or (my current favorite) Glassfish. Interoperability in that you are using a well-defined standard, JSON-RPC, which uses a light and easily parseable message dialect, JSON, which has implementations in all kinds of languages. Including C, which I’ve used before and have dreams of implementing a JSON-RPC framework in one day.

  • Stephen Gaunt

    Hi,

    Thanks for your clear answer. It was very helpful. Actually I was looking everywhere for any articles or discussions regarding this. Surprisingly I couldn’t find any.

    Regarding the error, yes it works, if I make the persist_class parameter true.

    • wes Post author

      I found and fixed it in version 1.0.2. It was indeed an issue with creating an instance of the object, I’ve switched it around to use the same method as the persist_class=true used. I’ve also added a unit test to keep this from breaking in the future. Thanks for helping me improve this library!