Programming Context Propagation: Main Steps Programming Context Propagation in a Client

11-2 Developing Applications for Oracle WebLogic Server For the complete API documentation about context propagation, see the weblogic.workarea Javadocs.

11.2 Programming Context Propagation: Main Steps

The following procedure describes the high-level steps to use context propagation in your application. 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. 1. Update your client application to create the WorkContextMap and WorkContext objects and then add user data to the context. See Section 11.3, Programming Context Propagation in a Client .

2. If your client application is standalone rather than running in a Java EE

component deployed to WebLogic Server, ensure that its CLASSPATH includes the Java EE application client, also called the thin client. See Programming Stand-alone Clients for Oracle WebLogic Server.

3. Update your application EJB, Web Service, servlet, and so on to also create a

WorkContextMap and then get the context and user data that you added from the client application. See Section 11.4, Programming Context Propagation in an Application .

11.3 Programming Context Propagation in a Client

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. Table 11–1 Interfaces and classes of the WebLogic Context Propagation API Interface or Class Description WorkContextMap Interface Main context propagation interface used to tag applications with data and propagate that information via application requests. WorkContextMaps is part of the client or applications JNDI environment and can be accessed through JNDI by looking up the name java:compWorkContextMap. WorkContext Interface 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 AsciiWorkContext, long contexts LongWorkContext, Serializable context SerializableWorkContext, and String contexts StringWorkContext. WorkContext has one subinterface, PrimitiveWorkContext, used to specifically marshal and unmarshal a single primitive data item. WorkContextOutputInpu t Interfaces Interfaces representing primitive streams used for marshaling and unmarshaling, respectively, WorkContext implementations. PropagationMode Interface Defines the propagation properties of WorkContexts. Specifies whether the WorkContext is propagated locally, across threads, across RMI invocations, across JMS queues and topics, or across SOAP messages. If not specified, default is to propagate data across remote and local calls in the same thread. PrimitiveContextFactor y Class Convenience class for creating WorkContexts that contain only primitive data. Programming Context Propagation 11-3 For the complete API documentation about context propagation, see the weblogic.workarea Javadocs. 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 codesayHellocode operation of the WorkArea Web service. public class Main { public final static String SESSION_ID= session_id_key; public static void mainString[] args throws ServiceException, RemoteException, NamingException, PropertyReadOnlyException{ WorkAreaService service = new WorkAreaService_Implargs[0] + ?WSDL; WorkAreaPortType port = service.getWorkAreaPort; WorkContextMap map = WorkContextMapnew InitialContext.lookupjava:compWorkContextMap; WorkContext stringContext = PrimitiveContextFactory.createA String Context; Put a string context map.putSESSION_ID, stringContext, PropagationMode.SOAP; try { String result = null; result = port.sayHelloHi 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; Note: See Getting Started With JAX-WS Web Services for Oracle WebLogic Server for information on creating Web Services and client applications that invoke them. 11-4 Developing Applications for Oracle WebLogic Server ■ The following code shows how to create a WorkContextMap by doing a JNDI lookup of the context propagation-specific JNDI name java:compWorkContextMap: WorkContextMap map = WorkContextMap new InitialContext.lookupjava:compWorkContextMap; ■ The following code shows how to create a WorkContext by using the PrimitiveContextFactory. In this example, the WorkContext consists of the simple String value A String Context. This String value is the user data that is passed to the invoked Web Service. WorkContext stringContext = PrimitiveContextFactory.createA String Context; ■ Finally, the following code shows how to add the context data, along with the key SESSION_ID, to the WorkContextMap and associate it with the current thread. The PropagationMode.SOAP constant specifies that the propagation happens over SOAP messages; this is because the client is invoking a Web Service. map.putSESSION_ID, stringContext, PropagationMode.SOAP;

11.4 Programming Context Propagation in an Application