18-2 Programming Advanced Features of JAX-WS Web Services for Oracle WebLogic Server
2.
Add a variable of type WebServiceContext that will have the context injected into it.
3.
Using the Web service context, get the HttpSession object.
4.
Save objects in the HttpSession using the setAttribute method and retrieve saved object using getAttribute. Objects are identified by a string value you assign.
The following snippet shows its usage:
Example 18–1 Accessing HTTP Session on the Server
WebService public class ShoppingCart {
Resource Step 1 private WebServiceContext wsContext; Step 2
public int addToCartItem item { Find the HttpSession
MessageContext mc = wsContext.getMessageContext; Step 3 HttpSession session =
javax.servlet.http.HttpServletRequestmc.getMessageContext.SERVLET_
REQUEST.getSession; if session == null
throw new WebServiceExceptionNo HTTP Session found; Get the cart object from the HttpSession or create a new one
ListItem cart = ListItemsession.getAttributemyCart; Step 4 if cart == null
cart = new ArrayListItem; Add the item to the cart note that Item is a class defined
in the WSDL cart.additem;
Save the updated cart in the HTTPSession since we use the same myCart name, the old cart object will be replaced
session.setAttributemyCart, cart; return the number of items in the stateful cart
return cart.size; }
}
18.3 Enabling HTTP Session on the Client
The client-side code is quite simple. All you need to do is set the SESSION_ MAINTAIN_PROPERTY on the request context. This tells the client to pass back the
HTTP Cookies that it receives from the Web service. The cookie contains a session ID that allows the server to match the Web service invocation with the correct
HttpSession, providing access to any saved stateful objects.
Example 18–2 Enabling HTTP Session on the Client
ShoppingCart proxy = new CartService.getCartPort; BindingProviderproxy.getRequestContext.putBindingProvider.SESSION_MAINTAIN_
PROPERTY, true; Create a new Item object with a part number of 123456 and an item
count of 4. Item item = new Item123456, 4;
After first call, well print 1 the return value is the number of objects in the Cart object
System.out.printlnproxy.addToCartitem; After the second call, well print 2, since weve added another
Item to the stateful, saved Cart object.
Programming Stateful JAX-WS Web Services Using HTTP Session 18-3
System.out.printlnproxy.addToCartitem;
18.4 Developing Stateful Services in a Cluster Using Session State Replication
In a high-availability environment, a JAX-WS Web service may be replicated across multiple server instances in a cluster. A stateful JAX-WS Web service is supported in
this environment through the use of the WebLogic Server HTTP Session State Replication feature. For more information, see HTTP Session State Replication in
Using Clusters for Oracle WebLogic Server.
There are a variety of techniques and configuration requirements for setting up a clustered environment using session state replication for example, supported servers
and load balancers, and so on. From the JAX-WS programming perspective, the only new consideration is that the objects you store in the HttpSession using the
HttpSession.setAttribute method as in
Example 18–1 must be Serializable. If they are
Serializable, then these stateful objects become available to the Web service on all replicated Web service instances in the cluster, providing both load balancing and
failover capabilities for JAX-WS stateful Web services.
18.5 A Note About the JAX-WS RI Stateful Extension