Monday, July 14, 2008

Remote Method call - EJB/HTTPClient/Web Service?

This is not a new thing i am talking about,but i have used this for first time. Until now i have been used to EJB for remote method invocations. I use it for some bussiness logic which is on another distributed server. And i am not ashamed of telling that i have not used Web services :(.

This was a need in my project to call a method of another application which can be on same machine or another. First i thought i need to call a EJB and its all mess because of heavy loads for a simple method call. But when i saw the implementation, i got confused how i will cuse it! But my senior told me to check using HTTPClient from apache commons.

Its very simple to call a method on servlet using HTTPClient. Check the code below. We will just use PostMethod class for parameters to be passed and execute the request. So as per the parameters,the servlet will do its work.
PostMethod postMethod = new PostMethod("http://some.blah.com");
postMethod.setParameter("some_id", "1");
postMethod.setParameter("param1", "any data");
postMethod.setParameter("param2", "any data");
...
int status = httpClient.executeMethod(postMethod); //status will be your response...
Its so simple. But though its not a call which returns something. I mean you cna see i am just passign some information to some method to invoke some processing which is independent and i don't need its response.