Hessian Web Service Protocol - Hello World Example
Join the DZone community and get the full member experience.
Join For Freeyesterday i took a quick look to the binary communication protocol: hessian . with hessian your admins will not have any troubles with port-activation for your (’desktop’) application, if they have to access remote services (db etc.).
the java implemenation from caucho is released under the apache license 2.0. hessian is well integrated into spring and seems to perform well for version 3.2 (one year old!). an orm tool which supports hessian out of the box is cayenne .
example
now to our hello world example. it is a maven project and you can get it from here (public domain of course …).
if you don’t want to use maven you should get the following jars:
but maybe you have the same problems with downloading a none-corrupted hessian.jar from caucho (why that?) - then you will be forced to install maven or netbeans.
the usage is simple: in netbeans you can directly open maven projects with the maven plugin. then start the jetty-server: go into the myservlet class and press shift+f6. you should see in the output window:
… info: logging to stderr via org.mortbay.log.stderrlog
… info: jetty-6.1.16
… info: started socketconnector@0.0.0.0:8080
then go to the client class and press shift+f6. you should see a new window coming up:
change the text and press enter will push the text to the server and get the response text at the bottom of the window updated:
now let us understand what is necessary:
-
an interface which has to be available on the server and on the client side:
public interface communicationservice { string communicate(string str);}
-
an implementation of this interface on the server side:
(here only the ‘implements communicationservice’ is important)public class myservlet extends hessianservlet implements communicationservice { public string communicate(string str) { return "hello world! " + str; } ...}
-
now the server code:
public static void main(string[] args) throws exception { server server = new server(8080); context context = new context(server, "/", context.sessions); context.addservlet(myservlet.class, "/communication-service"); server.start();}
-
and the client code:
string url = "http://localhost:8080/communication-service";hessianproxyfactory factory = new hessianproxyfactory();final communicationservice basic = (communicationservice) factory.create(communicationservice.class, url);...resultlabel.settext("server said : " + basic.communicate(field.gettext()));
- thats it!
looks really like rpc of java but it is language independent - there are implementations for ruby, phyton … and it seems to be more performant than language independent solutions like xml-rpc. are there other advantages or disadvantages?
Opinions expressed by DZone contributors are their own.
Comments