dku.rajkumar
dku.rajkumar

Reputation: 18568

How to call one web service method from other service if both the services are sitting in same project?

Consider there are two web services Test1 and Test2 sitting in the same server in same project. There impl classes are Test1Impl and Test2Impl. Test1Impl contains definition of a method getDetails and Test2Impl contains a method addDetails.

Now I want to call addDetails in Test2Impl from getDetails in Test1Impl. I get following ideas to implement this functionality

1) since these services impl classes are sitting in same project can I call it as a noramal java method like

new Test1Impl().addDetails(request)

this will break web service limitation.

2) I should make web service call to invoke that method. This will become nasty because the It will be hitting the network and making web service call to a service sitting in same server moreover in same project.

ServiceLocator.getTest1Services().addDetails(request);

3) implement same method in addDetails in Test1Impl as a private method.

public class Test1Impl{

public Response addDetails(Request request){
//
//
}
}

public class Test2Impl{

public Response addDetails(Request request){
//
//
}
}

This will be redundant of code and it will decrease the code standard.

Please help me which way to go,even new ideas are welcomed.

Upvotes: 1

Views: 2291

Answers (3)

Mark
Mark

Reputation: 1924

I think you are mixing concerns in your design: you should refactor the business logic away from the web service interface/public exposure/transport logic. Perhaps have a DetailsService business service class (not a web service!) that has addDetails and getDetails methods that the services call as necessary. This way you will have no duplicated code and you are still keeping the read and write operations separate at the web service interface level.

I like to keep my web service classes as small as possible and responsible only for passing information on to my business service layer. This means that if you were to add a GUI to your application that the business logic is already coded and you can just hook your new GUI into the DetailsService.

Upvotes: 1

Cratylus
Cratylus

Reputation: 54074

I would suggest to refactor the code and make a Utilities class for the common functionality among web services.

If this is not possible, I would opt for option 1, i.e. call the method directly instead of using a web service call.

From my point of view it is conceptually better, since both are in the same machine and you don't have to use a network call (even if it goes to loopback address).
Also you don't have to suffer the penalty of marhalling/demarshalling (especially if you use SOAP) since you will call the implementation directly

Upvotes: 1

Matthew Farwell
Matthew Farwell

Reputation: 61705

You've answered your own question. Use the simplest and easiest version. Just call the other web service java directly.

If you're really concerned about 'breaking the web service limitation', whatever that is, then refactor the shared method into a shared helper class.

Upvotes: 1

Related Questions