What are the advantages of using a dynamic client with JAX-WS services as opposed to just using generated client classes? What are the disadvantages?
**For my particular case I am using Apache CXF, I'm not sure what other libraries allow "dynamic" clients.
-I thought I didn't need to add this, but... I'm looking for non-obvious(I know...subjective) advantages. I don't need someone else to tell me that an advantage of not using generated classes is that I don't nee开发者_Python百科d to generate classes.
Well, the CXF documentation is pretty clear about the advantages of Dynamic Clients:
CXF supports several alternatives to allow an application to communicate with a service without the SEI and data classes. JAX-WS specified the JAX-WS Dispatch API, as well as the Provider interface for reading and writing XML. This page, however, describes the dynamic client facility of CXF. With dynamic clients, CXF generates SEI and bean classes at runtime, and allows you to invoke operations via APIs that take Objects, or by using reflection to call into full proxies.
In other words, you don't need the definitions of classes as shown in the documentation sample below:
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("echo.wsdl");
Object[] res = client.invoke("echo", "test echo");
System.out.println("Echo response: " + res[0]);
Regarding the disadvantages, they are pretty obvious (and this is the price to pay): you are manipulating strings, you lost strong typing.
The advantage is avoiding generating and including code. In some environments, that's a problem. If there's no barrier in your environment to including generated code, then the dynamic client is a bad idea, being slower and more cumbersome.
The dynamic client is slower because the code (of which I wrote some) must:
- parse the wsdl and schema
- generate code
- compile the code
It is more cumbersome because you don't have bean classes for any complex objects in your data model. You need to use reflection.
Keep in mind that the dynamic client is different from the invocation interface.
The advantage to using a dynamic client is that you don't need to have generated the stubs before run time. This allows you to generically invoke services that you may not know about at run time.
The generated client classes are great if you know precisely what web-service your client code is going to call and that it isn't going to change over the lifetime of your client.
If either of these isn't the case then you will need to think about how your client will handle these situations. The Dispatch API gives you the flexibility to generate the web-service call on the fly without having an apriori knowledge of the service being accessed. This obviously comes at the cost of your code needing to support the configuration options required to build that call.
With all this said, a certain amount of responsibility does lie with the developer/maintainer of the server side interface to not introduce changes that will break client code.
I had a similar conversation with a co-worker the other day. He was using the Spring client, that requires the use of an Interface to compile the client against, but then Spring injects the actual code to make the interface work. It basically boiled down to the oldest of saws between us, things like dynamic proxies usually introduce some sort of performance tax, he's ok with that, I started out life writing device drivers, and so am thoroughly prejudiced in favor of speed. Faster/smaller wins as far as I'm concerned, and since I'm not constrained to such limited environments...heck my Droid phone makes all the systems I worked on, including mainframes, in my first 10 years professionally look puny, I'll come down on the side of speed. The common rejoinder for this is that there are many other bottlenecks that are the "real" problem, and that this issue is insignificant against them....and it may be true...but every little bit helps. If you read the stuff from Steve Souders and his compatriots...users can notice a change of as little as 400 milliseconds...they don't necessarily register that things are slower, but their reaction is different. So since I can't do anything about network speed, database index overhead, etc, then I can at least do the best job I can with the things that I can influence. Whew! Sorry 'bout that!! Stepping off the soapbox now! ;)
DII(Dynamic Invocation Interface) Client: With the DII, a client can call a remote procedure even if the signature of the remote procedure or the name of the service are unknown until runtime.
Because of its flexibility, a DII client can be used in a service broker that dynamically discovers services, configures the remote calls, and executes the calls.
Advantages
- Here we have to create simple java interface describing the operations supported by the web service to access. So no need auto generated stubs to access the service.
- Using endpoint it generates WSDL and Stubs on the fly i.e. at run-time.
Disadvantages
- Even in this style we have to know the web service before the creating client.
- Comparing to Generated Stub(GS) it works slowly because of WSDL and stubs generates at run-time.
精彩评论