Chapter 5 Client Design
219
5.3.2 Locating and Accessing a Service
Locating a service differs depending on the client. Client applications that run in
J2EE environments use the JNDI
InitialContext.lookup
method to locate a ser
vice, whereas a J2SE client can use the
javax.xml.rpc.ServiceFactory
class or an
implementation specific stub to locate a service. Clients use the
javax.xml.rpc.Service
interface API to access a Web service. A stub implements
the service interface.
Code Example 5.1 shows how an application in a J2EE environment might
use a stub to access a service. The application locates the service using a JNDI
InitialContext.lookup
call. The JNDI call returns an
OpcOrderTrackingService
object, which is a stub.
Context ic = new InitialContext();
OpcOrderTrackingService opcOrderTrackingSvc =
(OpcOrderTrackingService) ic.lookup(
"java:comp/env/service/OpcOrderTrackingService");
OrderTrackingIntf port =
opcOrderTrackingSvc.getOrderTrackingIntfPort();
OrderDetails od = port.getOrderDetails(orderId);
Code Example 5.1
Accessing a Service with a Stub in a J2EE Environment
Because it depends on the generated stub classes, the client code in Code
Example 5.1 is not the recommended strategy for using stubs. Although this
example works without problems, JAX RPC gives you a neutral way to access a
service and obtain the same results. By using the JAX RPC
javax.xml.rpc.Service
interface method
getPort
, you can access a Web service
in the same manner regardless of whether you use stubs or dynamic proxies. The
getPort
method returns either an instance of a generated stub implementation
class or a dynamic proxy, and the client can then uses this returned instance to
invoke operations on the service endpoint.
The
getPort
method removes the dependencies on generated service specific
implementation classes. When this method is invoked, the JAX RPC runtime
selects a port and protocol binding for communicating with the port, then config
ures the returned stub that represents the service endpoint interface. Furthermore,
since the J2EE platform allows the deployment descriptor to specify multiple
New Page 1