Dependency Injection EJB 3.0 Wrapper Without Injection

4-16 Programming JMS for Oracle WebLogic Server

4.6.3 Dependency Injection

The following code fragment sends a message if you have used dependency injection to a variable. Example 4–4 Sending a Message using Dependency Injection package test; Example injected annotation. import javax.annotation.Resource; import javax.ejb.; import javax.jms.; StatelessmappedName=StatelessBean public class MyStatelessBean implements MyStateless { ResourcemappedName=myDestJNDIName private Destination dest; ResourcemappedName=weblogic.jms.XAConnectionFactory private ConnectionFactory connectionFactory; public void completeWorkOrder { Connection con = null; Session session = null; MessageProducer sender = null; try { System.out.printlncompleteWorkOrder called; con = connectionFactory.createConnection; session = con.createSessiontrue, Session.AUTO_ACKNOWLEDGE; sender = session.createProducernull; Message message = session.createTextMessagework order complete; sender.senddest, message; } catchException e { throw new EJBExceptionException sending message: + e, e; } finally { try { if con = null con.close; } catchException e { e.printStackTrace; } } } }

4.6.4 EJB 3.0 Wrapper Without Injection

This example demonstrates EJB 3.0 annotations for an MDB that references resources that are not injected. The references are resolved at runtime when the MDB is invoked instead of when the MDB instances are instantiated. Note: When using a wrapped JMS connection factory, which is obtained by using the resource-ref feature and looked up by using the java:compenvjms JNDI tree context, then the EJB must not use the javax.jms XA transactional XA interfaces. Enhanced Support for Using WebLogic JMS with EJBs and Servlets 4-17 Example 4–5 Non-Injected MDB Example package test; import javax.annotation.Resources; import javax.annotation.Resource; import javax.naming.; import javax.ejb.; import javax.jms.; import javax.ejb.ActivationConfigProperty; MessageDriven name = MyMDB, mappedName = JNDINameOfMDBSourceDest, activationConfig = { the JMS interface type for the MDB destination, either javax.jms.Topic or javax.jms.Queue ActivationConfigProperty propertyName = destinationType, propertyValue = javax.jms.Queue, optionally specify a connection factory theres no need to specify a connection factory if the source destination is a WebLogic JMS destination ActivationConfigProperty propertyName = connectionFactoryJndiName, propertyValue = JNDINameOfMDBSourceCF, } resources that are not injected Resources { Resourcename=targetCFRef, mappedName=TargetCFJNDIName, type=javax.jms.ConnectionFactory.class, Resourcename=targetDestRef, mappedName=TargetDestJNDIName, type=javax.jms.Destination.class } public class MyMDB implements MessageListener { inject a reference to the MDB context Resource private MessageDrivenContext mdctx; cache targetCF and targetDest for re-use performance private ConnectionFactory targetCF; private Destination targetDest; TransactionAttributeTransactionAttributeType.REQUIRED public void onMessageMessage message { Connection jmsConnection = null; try { 4-18 Programming JMS for Oracle WebLogic Server System.out.printlnMy MDB got message: + message; if targetCF == null targetCF = javax.jms.ConnectionFactorymdctx.lookuptargetCFRef; if targetDest == null targetDest = javax.jms.Destinationmdctx.lookuptargetDestRef; jmsConnection = targetCF.createConnection; Session s = jmsConnection.createSessionfalse, Session.AUTO_ACKNOWLEDGE; MessageProducer mp = s.createProducernull; if message.getJMSReplyTo = null mp.sendmessage.getJMSReplyTo, s.createTextMessageMy Reply; else mp.sendtargetDest, message; } catch JMSException e { throw new EJBExceptione; } finally { Return JMS resources to the resource reference pool for later re-use. Closing a connection automatically also closes its sessions, etc. try { if jmsConnection = null jmsConnection.close; } catch JMSException ignored {}; } } } 5 Developing a Basic JMS Application 5-1 5 Developing a Basic JMS Application The following sections provide information on the steps required to develop a basic JMS application: 1. Section 5.1, Importing Required Packages 2. Section 5.2, Setting Up a JMS Application 3. Section 5.3, Sending Messages 4. Section 5.4, Receiving Messages 5. Section 5.5, Acknowledging Received Messages 6. Section 5.6, Releasing Object Resources In addition to the application development steps defined in the previous figure, you can also optionally perform any of the following steps during your design development: ■ Manage connection and session processing ■ Create destinations dynamically ■ Create durable subscriptions ■ Manage message processing by setting and browsing message header and property fields, filtering messages, andor processing messages concurrently ■ Use JMS within transactions, described in Chapter 12, Using Transactions with WebLogic JMS.

5.1 Importing Required Packages

The following table lists the packages that are commonly used by WebLogic JMS applications. Note: For more information about the JMS classes described in this section, access the JMS Javadoc supplied on the Sun Microsystems Java Web site at http:www.java.sun.comproductsjmsdocs.html .