Sample Producer Code Re-usable ConnectionFactory Objects

Recovering from a Server Failure 14-3

14.1.2.1 Sample Producer Code

In the event of a network failure, the WebLogic JMS client code for message production will attempt to reconnect to an available server during Steps 3-8 shown in Example 14–1 . Example 14–1 Sample JMS Client Code for Message Production set exception listener 1. public void onExceptionjavax.jms.JMSException jsme { connection.setExceptionListener handle the exception, which may require checking for duplicates or sending the message again } 2. Context ctx = create WebLogic JNDI context with credentials etc. 3. ConnectionFactory cf = ctx.lookupJNDI name of connection factory 4. Destination dest = ctx.lookupJNDI name of destination the following operations recover from network failures 5. Connection con = cf.createConnection 6. Session sess = con.createSessionno transactions, ack mode 7. MessageProducer prod = sess.createProducerdest 8. Loop over: 9. Message msg = sess.createMessage try block to handle destination availablitiy scenarios 10. try { prod.sendmsg} catch Some Destination Availability Exception e { handle the exception, in most cases, the destination or member is not yet available, so the code should try to resend } end loop done sending messages 11. con.close; ctx.close; The JMS producer will transparently failover to another server instance, if one is available. This keeps the client code as simple as listed above and eliminates the need for client code for retrying across network failures. The WebLogic JMS does not reconnect MessageConsumers by default. For this to automatically occur programmatically, your client application code must call the WebLogic WLConnection extension, with the setReconnectPolicy set to all, as explained in Section 14.1.3, Configuring Automatic Failover for JMS Consumers.

14.1.2.2 Re-usable ConnectionFactory Objects

A ConnectionFactory object looked up via JNDI see Step 1 in Example 14–1 and Example 14–2 is re-usable after a server or network failure without requiring a re-lookup. A network failure could be between the JMS client JVM and the remote WebLogic Server instance it is connected to as part of the JNDI lookup, or between the JMS client JVM and any remote WebLogic Server instance in the same cluster where the JMS client subsequently connects.

14.1.2.3 Re-usable Destination Objects