14 Programming Context Propagation
This chapter includes the following sections:
- Understanding Context Propagation
Context propagation allows programmers to associate information with an application which is then carried along with every request. Furthermore, downstream components can add or modify this information so that it can be carried back to the originator. - Programming Context Propagation: Main Steps
You can associate information to a request on a client, retrieve that information on the server, and then retrieve the value updated by the server instance using context propagation. - Programming Context Propagation in a Client
You can program context propagation to get “associated” user information when a client invokes an application. - Programming Context Propagation in an Application
You can program context propagation to get the user data and other associated information when the applications are invoked.
Understanding Context Propagation
Context propagation allows programmers to associate information with an application which is then carried along with every request. Furthermore, downstream components can add or modify this information so that it can be carried back to the originator.
Context propagation attaches information to a request through a WorkContext
. This information follows the request to any process that supports context propagation through a PropagationMode
. Context propagation is also known as work areas, work contexts, or application transactions.
Common use-cases for context propagation are any type of application in which information, usually related to the request, needs to be carried outside the application or to another application, rather than the information being an integral part of the application. Examples of these use cases include diagnostics monitoring, application transactions, and application load-balancing. The ability of context propagation to tie information to a request greatly simplifies managing such data, in contrast to maintaining a map of request data in each application and then implementing custom code to transmit such information between applications or threads.
However, context propagation can occur within an application. For example, if an application submits work through a Work Manager, part of the processing occurs in different threads. Context propagation uses a PropagationMode
to carry information to other threads.
Programming context propagation has two parts: first you code the client application to create a WorkContextMap
and WorkContext
, and then add user data to the context, and then you code the invoked application itself to get and possibly use this data. The invoked application can be of any type: EJB, Web service, servlet, JMS topic or queue, and so on. See Programming Context Propagation: Main Steps for details.
The WebLogic context propagation APIs are in the weblogic.workarea
package. The following table describes the main interfaces and classes.
Table 14-1 Interfaces and classes of the WebLogic Context Propagation API
Interface or Class | Description |
---|---|
|
Main context propagation interface used to tag applications with data and propagate that information via application requests. |
|
Interface used for marshaling and unmarshaling the user data that is passed along with an application. This interface has four implementing classes for marshaling and unmarshaling the following types of data: simple 8-bit ASCII contexts (
|
|
Interfaces representing primitive streams used for marshaling and unmarshaling, respectively, |
|
Defines the propagation properties of |
|
Convenience class for creating |
For the complete API documentation about context propagation, see the weblogic.workarea Javadocs
.
Parent topic: Programming Context Propagation
Programming Context Propagation: Main Steps
You can associate information to a request on a client, retrieve that information on the server, and then retrieve the value updated by the server instance using context propagation.
The following procedure describes the high-level steps to use context propagation with WebLogic Server. This example demonstrates how to associate information to a request on a client, how to retrieve that information on the server, and then how to retrieve the value updated by the server instance. It is assumed in the procedure that you have already set up your iterative development environment and have an existing client and application that you want to update to use context propagation by using the weblogic.workarea
API.
Parent topic: Programming Context Propagation
Programming Context Propagation in a Client
You can program context propagation to get “associated” user information when a client invokes an application.
The following sample Java code shows a standalone Java client that invokes a Web service; the example also shows how to use the weblogic.workarea.*
context propagation APIs to associate user information with the invoke. The code relevant to context propagation is shown in bold and explained after the example.
For the complete API documentation about context propagation, see the weblogic.workarea Javadocs
.
Note:
See Developing JAX-WS Web Services for Oracle WebLogic Server for information on creating Web services and client applications that invoke them.
package examples.workarea.client; import java.rmi.RemoteException; import javax.xml.rpc.ServiceException; import javax.xml.rpc.Stub; import javax.naming.InitialContext; import javax.naming.NamingException;import weblogic.workarea.WorkContextMap;
import weblogic.workarea.WorkContext;
import weblogic.workarea.PrimitiveContextFactory;
import weblogic.workarea.PropagationMode;
import weblogic.workarea.PropertyReadOnlyException;
/** * This is a simple standalone client application that invokes the * the <code>sayHello</code> operation of your WorkArea Web service. * */ public class Main { public final static String SESSION_ID= "session_id_key"; public static void main(String[] args) throws ServiceException, RemoteException, NamingException, PropertyReadOnlyException{ YourWorkAreaService service = new YourWorkAreaService(args[0] + "?WSDL"); YourWorkAreaPortType port = service.getWorkAreaPort();WorkContextMap map = (WorkContextMap)new InitialContext().lookup("java:comp/WorkContextMap");
WorkContext stringContext = PrimitiveContextFactory.create("A String Context");
// Put a string context
map.put(SESSION_ID, stringContext, PropagationMode.SOAP);
try { String result = null; result = port.sayHello("Hi there!"); System.out.println( "Got result: " + result ); } catch (RemoteException e) { throw e; } } }
In the preceding example:
-
The following code shows how to import the needed
weblogic.workarea.*
classes, interfaces, and exceptions:import weblogic.workarea.WorkContextMap;
import weblogic.workarea.WorkContext;
import weblogic.workarea.PrimitiveContextFactory;
import weblogic.workarea.PropagationMode;
import weblogic.workarea.PropertyReadOnlyException;
-
Substitute your implementation of the
WorkArea
service and port for your Web service forYourWorkAreaService
andYourWorkAreaPortType
. -
The following code shows how to create a
WorkContextMap
by doing a JNDI lookup of the context propagation-specific JNDI namejava:comp/WorkContextMap
:WorkContextMap map = (WorkContextMap)
new InitialContext().lookup("java:comp/WorkContextMap"); -
The following code shows how to create a
WorkContext
by using thePrimitiveContextFactory
. In this example, theWorkContext
consists of the simple String valueA String Context
. This String value is the user data that is passed to the invoked Web service.WorkContext stringContext = PrimitiveContextFactory.create("A String Context");
-
The following code saves the
stringContext
under theSESSION_ID
key in theWorkContextMap
. Specifying the propagation mode ofSOAP
causes the propagation of thestringContext
along anySOAP
message sent to servers supporting context propagation.map.put(SESSION_ID, stringContext, PropagationMode.SOAP);
Parent topic: Programming Context Propagation
Programming Context Propagation in an Application
You can program context propagation to get the user data and other associated information when the applications are invoked.
The following sample Java code shows a simple Java Web service (JWS) file that implements a Web service. The JWS file also includes context propagation code to get the user data that is associated with the invoke of the Web service. The code relevant to context propagation is shown in bold and explained after the example.
For the complete API documentation about context propagation, see the weblogic.workarea Javadocs
.
Note:
See Developing JAX-WS Web Services for Oracle WebLogic Server for information on creating Web services and client applications that invoke them.
package examples.workarea; import javax.naming.InitialContext; // Import the Context Propagation classesimport weblogic.workarea;
import weblogic.workarea.WorkContextMap;
import weblogic.workarea.WorkContext;
import javax.jws.WebMethod; import javax.jws.WebService; import weblogic.jws.WLHttpTransport; @WebService(name="WorkAreaPortType", serviceName="WorkAreaService", targetNamespace="http://example.org") @WLHttpTransport(contextPath="workarea", serviceUri="WorkAreaService", portName="WorkAreaPort") /** * This JWS file forms the basis of simple WebLogic * Web service with a single operation: sayHello * */ public class WorkContextAwareWebService { public final static String SESSION_ID = "session_id_key"; @WebMethod() public String sayHello(String message) { try {WorkContextMap map = (WorkContextMap) new InitialContext().lookup("java:comp/WorkContextMap");
WorkContext localwc = map.get(SESSION_ID);
WorkContext modifiedLocalWC = PrimitiveContextFactory.create(localwc.get() + " could be replaced by a new value...");
map.put(SESSION_ID, newLocalWC, PropagationMode.SOAP); System.out.println("local context: " + localwc); System.out.println("sayHello: " + message); return "The server received message: " + message + ", with SESSION_ID: " + localwc; } catch (Throwable t) { return "error"; } } }
In the preceding example:
-
The following code shows how to import the needed context propagation APIs; in this case, only the WorkContextMap and WorkContext interfaces are needed:
import weblogic.workarea.WorkContextMap; import weblogic.workarea.WorkContext;
-
The following code shows how to create a
WorkContextMap
by doing a JNDI lookup of the context propagation-specific JNDI namejava:comp/WorkContextMap
:WorkContextMap map = (WorkContextMap) new InitialContext().lookup("java:comp/WorkContextMap");
-
The propagation mode is
SOAP
only, meaning that propagation occurs both to the server with the request and to the client with the response. The following code shows how the server instance could modify thestringContext
:WorkContext modifiedLocalWC = PrimitiveContextFactory.create(localwc.get() + " could be replaced by a new value...");
-
The following code replaces the work context with an updated value. When retrieving
SESSION_ID
on the client after the server returns the response, the value updated by the server is now present on the client.map.put(SESSION_ID, newLocalWC, PropagationMode.SOAP);
Parent topic: Programming Context Propagation