Injecting Resource Dependency into a Class Non-Injected EJB 3.0 Resource Reference Annotations

Enhanced Support for Using WebLogic JMS with EJBs and Servlets 4-5

4.1.3 Declaring JMS Destinations and Connection Factories using Annotations

WebLogic Server 10.0 and higher releases support the EJB 3.0 programming model which uses annotations to configure metadata, eliminating the need for deployment descriptors. You can declare JMS objects using the Resources annotation as described in “Standard JDK Annotations Used By EJB 3.0” in Programming WebLogic Enterprise JavaBeans, Version 3.0 for Oracle WebLogic Server.

4.1.3.1 Injecting Resource Dependency into a Class

If you apply Resource to a class, the resource is made available in the compenv context. The following is an example of how to inject a WebLogic JMS destination and connection factory resource in a JEE application, including EJBs, MDBs, and servlets. The following is a Wrapped JMS Pooling Annotation example: Example 4–1 Wrapped JMS Pooling Annotation Example . . . The name= or type= are not always required, mappedName= is usually sufficient. Resourcename=ReplyQueue, type=javax.jms.Queue.class, mappedName=jmsReplyQueue Destination rq; . . . Resourcename=ReplyConnectionFactory, type=javax.jms.ConnectionFactory.class, mappedName = jmsConnectionFactory ConnectionFactory cf; . . .

4.1.3.2 Non-Injected EJB 3.0 Resource Reference Annotations

Injected resource dependencies are resolved when the host EJB or Servlet is instantiated. This is sometimes undesirable because: ■ The injection may prevent applications from deploying successfully if the container attempts to resolve references during deployment. ■ You might want to defer reference resolution until the application is first invoked. One way to setup a non-injected resource reference is to place a Resources annotation above the class definition. An application can resolve such references at runtime by looking up the reference in the bean context. As a best practice, the bean or servlet should also cache the result in order to avoid the overhead of repeated lookups. For example: Example 4–2 Non-Injected Resource Example . . . Resources { Resourcename=targetCFRef, mappedName=TargetCFJNDIName, 4-6 Programming JMS for Oracle WebLogic Server type=javax.jms.ConnectionFactory.class, Resourcename=targetDestRef, mappedName=TargetDestJNDIName, type=javax.jms.Destination.class } StatelessmappedName=StatelessBean public class MyStatelessBean implements MyStateless { Resource private SessionContext sctx; inject the bean context private ConnectionFactory targetCF; private Destination targetDest; public void completeWorkOrder { Lookup the JMS resources and cache for re-use. Note that a java:compenv prefix isnt needed for EJB3.0 bean contexts. if targetCF == null targetCF = javax.jms.ConnectionFactorysctx.lookuptargetCFRef; if targetDest == null targetDest = javax.jms.Destinationsctx.lookuptargetDestRef; . . . For a full example, see Chapter 4.6.4, EJB 3.0 Wrapper Without Injection.

4.1.4 Avoid Transactional XA Interfaces