Programming Context Propagation in an Application

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

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. package examples.workarea; import javax.naming.InitialContext; Import the Context Propagation classes import weblogic.workarea.WorkContextMap; import weblogic.workarea.WorkContext; import javax.jws.WebMethod; import javax.jws.WebService; import weblogic.jws.WLHttpTransport; WebServicename=WorkAreaPortType, serviceName=WorkAreaService, targetNamespace=http:example.org WLHttpTransportcontextPath=workarea, serviceUri=WorkAreaService, portName=WorkAreaPort This JWS file forms the basis of simple WebLogic Web Service with a single operation: sayHello public class WorkAreaImpl { public final static String SESSION_ID = session_id_key; WebMethod 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. Programming Context Propagation 11-5 public String sayHelloString message { try { WorkContextMap map = WorkContextMap new InitialContext.lookupjava:compWorkContextMap; WorkContext localwc = map.getSESSION_ID; System.out.printlnlocal context: + localwc; System.out.printlnsayHello: + message; return Here is the message: + message + ; } 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 name java:compWorkContextMap: WorkContextMap map = WorkContextMap new InitialContext.lookupjava:compWorkContextMap; ■ The following code shows how to get contexts user data from the current WorkContextMap using a key; in this case, the key is the same one that the client application set when it invoked the Web Service: SESSION_ID: WorkContext localwc = map.getSESSION_ID; 11-6 Developing Applications for Oracle WebLogic Server 12 Programming JavaMail with WebLogic Server 12-1 12 Programming JavaMail with WebLogic Server The following sections contains information on additional WebLogic Server programming topics: ■ Section 12.1, Overview of Using JavaMail with WebLogic Server Applications