Programming WebLogic JMS

     Previous  Next    Open TOC in new window  Open Index in new window  View as PDF - New Window  Get Adobe Reader - New Window
Content starts here

Recovering from a Server Failure

These sections discuss how WebLogic JMS client applications reconnect or recover from a server/network failure, and how to migrate JMS data after a server failure.

 


Automatic JMS Client Failover

With the automatic JMS client reconnect feature, if a server or network failure occurs, some JMS client objects will transparently failover to use a another server instance, as long as one is available. For example, if a fatal server failure occurs, JMS clients automatically attempt to reconnect to the server when it becomes available.

A network connection failure could be due to transient reasons (a temporary blip in the network connection) or non-transient reasons (a server bounce or network failure). In such cases, some JMS client objects will attempt to automatically operate with another server instance in a cluster, or possibly with the host server.

By default, JMS producer session objects automatically attempt to reconnect to an available server instance without any manual configuration or modifications to existing client code. If you do not want your JMS producers to be automatically reconnected, then you must explicitly disable this feature either programmatically or administratively.

In addition, JMS consumer session objects can also be configured to automatically attempt to reconnect to an available server, but due to their potentially asynchronous nature, you must explicitly enable this capability using the Administration Console or public WebLogic JMS APIs.

For more information, refer to the following sections:

Automatic Failover for JMS Producers

In most cases, JMS producer applications will transparently failover to another server instance if one is available. The following WebLogic JMS producer-oriented objects will attempt to automatically reconnect to an available sever instance without any manual configuration or modification to existing client code:

If you do not want your JMS clients to be automatically reconnected, then you must explicitly disable this feature either programatically or administratively, as described in Explicitly Disabling Automatic Failover on JMS Clients.

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 Listing 14-1.

Listing 14-1 Sample JMS Client Code for Message Production
0.   Context ctx = create WebLogic JNDI context with credentials etc.
1. ConnectionFactory cf = ctx.lookup(JNDI name of connection factory)
2. Destination dest = ctx.lookup(JNDI name of destination)
// the following operations recover from network failures
3. Connection con = cf.createConnection()
4. Session sess = con.createSession(no transactions, ack mode)
5. MessageProducer prod = sess.createProducer(dest)
6. Loop over:
7. Message msg = sess.createMessage()
8. prod.send(msg)
9. 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 Configuring Automatic Failover for JMS Consumers.

Re-usable ConnectionFactory Objects

Since Weblogic Server 8.1, a ConnectionFactory object looked up via JNDI (see Step 1 in Listing 14-1 and Listing 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.

Re-usable Destination Objects

A Destination object (queue or topic) looked up via JNDI (see Step 2 in Listing 14-1 and Listing 14-2) is re-usable after a server or network failure without requiring another lookup. The same principle applies to producers that send to a distributed destinations, since the client looks up the distributed destination in JNDI, and not the unavailable distributed member.

A network failure could be between the client JVM and the WebLogic Server instance it is connected to, or between that WebLogic Server instance and the WebLogic Server instance that actually hosts the destination. The Destination object will also be robust after restarting the WebLogic Server instance hosting the destination.

Note: For information on how consumers of distributed destinations behave with automatic JMS client reconnect, see Consumers of Distributed Destinations.

Reconnected Connection Objects

The JMS Connection object is used to map one-to-one to a physical network connection between the client JVM and a remote WebLogic Server instance. With the JMS client reconnect feature, the JMS Connection object that the client gets from the ConnectionFactory.createConnection() method (see Step 3 in Listing 14-1 and Listing 14-2) maps in a one-to-one-at-a-time fashion to the physical network connection. One consequence is that while the JMS client continues to use the same Connection object, it could be actually communicating with a different WebLogic Server instance after an implicit failover.

If there is a network disconnect and a subsequent implicit refresh of the connection, all objects derived from the connection (such as javax.jms.Session and javax.jms.MessageProducer objects) are also implicitly refreshed. During the refresh, any synchronous operation on the connection or its derived objects that go to the server (such as producer.send() or connection.createSession()), may block for a period of time before giving up on the connection refresh. This time is configured using the Administration Console or the setReconnectBlockingMillis(long) API in the weblogic.jms.extension.WLConnection interface.

The reconnect feature keeps trying to reconnect to the Weblogic Server instance’s ConnectionFactory object in the background until the application calls connection.close(). The ReconnectBlockingMillis parameter is the time-out for a synchronous caller trying to use the connection when the connection in being retried in the background.

If a synchronous call does time out without seeing a refreshed connection, it then behaves in exactly the same way (that is, throws the same Exceptions) as without the implicit reconnect (that is, it will behave as if it was called on a stale connection without the reconnect feature).

The caller can then decide to simply retry the synchronous call (with a potentially lower quality of service, like duplicate messages), or decide to call connection.close(), which will terminate the background retries for that connection.

Special Cases for Reconnected Connections

There are special cases that can occur when producer connections are refreshed:

Reconnected Session Objects

As described in Reconnected Connection Objects on page 14-4, JMS Session objects are refreshed when their associated JMS connection gets refreshed (see Step 4 in Listing 14-1 and Listing 14-2). Session states, such as acknowledge mode and transaction mode, are preserved across each refresh occurrence. The same session object can be used for calls, like createMessageProducer(), after a refresh.

Special Cases for Reconnected Sessions

These sections discuss special cases that can occur when Sessions are reconnected.

Reconnected MessageProducer Objects

As described in Reconnected Connection Objects on page 14-4, JMS MessageProducer objects are refreshed when their associated JMS connection gets refreshed (see Step 5 in Listing 14-1). If producers are non-anonymous, that is, they are specific to a Destination object (standalone or distributed destination), then the producer’s destination is also implicitly refreshed, as described in Re-usable Destination Objects. If a producer is anonymous, that is not specific to a Destination object, then the possibly-stale Destination object specified on the producer’s send() operation is implicitly refreshed.

Special Case for Reconnected MessageProducers and Distributed Destinations

It is possible that a producer can send a message at the same time that a distributed destination member becomes unavailable. If WebLogic JMS can determine that the distributed destination member is not available, or was not available when the message was sent, the system will retry sending the message to another distributed member. If there is no way to determine if the message made it through the connection all the way to the distributed member before it went down, the system will not attempt to resend the message because doing so may create a duplicate message. In that case, WebLogic JMS will throw an exception. It is up to the application to catch that exception and decide whether or not to resend the message.

Configuring Automatic Failover for JMS Consumers

JMS MessageConsumer objects that are part of a JMS Connection (via a JMS Session) can be refreshed during a JMS Connection refresh (see Step 5 in Listing 14-2). However, due to the stateful nature of JMS Consumers, as well as their potential asynchronous nature, you must explicitly enable this capability using either the weblogic.jms.extension.WLConnection API or the Administration Console.

Explicitly enabling automatic refresh of consumers also refreshes Connections with a configured Client ID for a durable subscriber, as described in Consumer Connections with a ClientID for Durable Subscriptions. However, refreshed consumers does not include QueueBrowser clients, which are never refreshed, as described in Limitations for Automatic JMS Client Failover.

Sample Consumer Client Code

When Message Consumer refresh is explicitly activated, in the event of a network failure, the WebLogic JMS client code for message consumption will attempt to reconnect during Steps 3-8 in Listing 14-2.

Listing 14-2 Sample JMS Client Code for Message Consumption
0.  Context ctx = create WebLogic JNDI context with credentials etc.
1. ConnectionFactory cf = ctx.lookup(JNDI name of connection factory)
2. Destination dest = ctx.lookup(JNDI name of destination)
// the following operations recover from network failures
3. Connection con = cf.createConnection()
(weblogic.jms.extensions.WLConnection)con).setReconnectPolicy("all")
4. Session sess = con.createSession(no transactions, auto ack)
5. MessageConsumer cons = sess.createConsumer(dest, message selector)
- also for async consumers : cons.setMessageListener(onMessage impl)
6. con.start()
7. Loop over:
for sync consumers: Message msg = consumer.receive()
for async consumers (in different thread): onMessage() invoked
8. con.close(), ctx.close()

Note that the connection factory does not refresh MessageConsumer objects by default. For this to occur programmatically, your client application code must call the WebLogic WLConnection extension, with the setReconnectPolicy set to “all”, as shown in Step 3 in Listing 14-2.

Configuring Automatic Client Refresh Options

The JMS client reconnect API includes the following configuration parameters, which allow you to make some choices that affect the behavior of the reconnect feature for consumers.

Table 14-1 Automatic JMS Client Reconnect Options
Console Label/MBean Attribute
Value
Description
Reconnect Policy
ReconnectPolicy
  • None
  • Producer (default)
  • All
Determines which JMS client objects are implicitly refreshed upon a network disconnect or server reboot. It only affects the implicit refresh of Connections, Sessions, Producers, and Consumers derived from this Connection Factory. This attribute does not affect Destination or ConnectionFactory objects in the JMS client, since those objects are always refreshed implicitly. Nor does it affect the QueueBrowser object in the JMS client, since that object is never refreshed.
Reconnect Blocking Time
ReconnectBlockingTimeMillis
6000
Determines how long any synchronous JMS calls, such as producer.send(), consumer.receive(), and session.createBrowser() will block the calling thread before giving up on a JMS client reconnect in progress.
TotalReconnectPeriodMillis
-1
Determines how long JMS clients should keep retrying to connect after either the initial network disconnect or the last synchronous JMS call attempt (whichever occurs most recently), before giving up retrying.

For instructions on configuring client parameters on a connection factory using the Administration Console, see “ Configure connection factory client parameters” in the Administration Console Online Help. For more information about these parameters, see ClientParamsBean in the WebLogic Server MBean Reference.

Common Cases for Reconnected Consumers

This section describes the common scenarios when refreshing synchronous and asynchronous consumers.

Synchronous Consumers

Synchronous consumers use MessageConsumer.receive(), MessageConsumer.receive(timeout), and MessageConsume.receiveNoWait() methods to consume messages. The first two methods are already expected to be potentially block the application code, while the third method is not expected to block the application code. To retain these semantics, the following rules describe interaction of the reconnect feature with the synchronous consumer calls:

If these methods eventually reach their respective timeout/wait periods, they all will throw the same Exceptions. as without reconnect. If a reconnect succeeds while these methods are blocked/called, these methods will continue returning messages, but with a potentially lowered quality-of-service and with generally similar semantics of receiving messages (like Redelivered messages), as after a recover. The application is notified of this possibility by a Connection ExceptionListener callback with LostServerException. In addition, for non-AUTO_ACK acknowledge modes, the first acknowledge call after a refresh will throw a LostServerException to notify the application of this possibility.

Asynchronous Consumers

In the context of a reconnect, the behavior for asynchronous consumers will be governed by the setting on the Total Reconnect Period property. The JMS Consumer’s registered message listener’s onMessage() will continue to be invoked if the reconnect framework is able to successfully re-establish a connection within the Total Reconnect Period setting after a connection failure. If the user explicitly calls a close() on the JMS Connection (or on the JMS Session corresponding to the asynchronous Consumer), then the reconnect framework will not invoke any further onMessages for that Consumer. The onMessage() should expect post-recover behavior (like Redelivered messages) if the Connection ExceptionListener’s onException is invoked with a LostServerException.

Special Cases for Reconnected Consumers

These sections discuss special cases that can occur when consumers are refreshed.

Consumers of Distributed Destinations

Previous to release 9.2, consumers of distributed destinations (DDs) were pinned to a particular destination member of the DD for the life of the pinned consumer. This applies to queue consumers of distributed queues, and non-durable subscribers of distributed topics (durable subscribers are not supported distributed topics).

With MessageConsumer reconnect, DD consumers are also refreshed; however, the refreshed consumer is almost never on the same destination member as the stale consumer. Therefore, even though the application is using the same DD consumer across a refresh, it is effectively not pinned to the same destination member across a refresh.

Message-Driven EJBs

Message-driven EJBs (MDBs) are a special sub-case of asynchronous consumers that have their own behavior requirements and their own refresh framework. As such, MDBs are not expected to participate in MessageConsumer refreshes, and are not expected to be affected in any other way by the JMS client reconnect framework.

Consumer Connections with a ClientID for Durable Subscriptions

Durable subscriptions on standalone topics will not notice any difference due to the client reconnect feature if the topic is still available across a disconnect. The JMS client reconnect framework implicitly refreshes the durable subscriber on that topic and continue from where it was interrupted. Note that if your Reconnect Policy is set to All, JMS Connections with a ClientID will also refresh automatically, thus allowing durable subscriptions (which are scoped by ClientID) to refresh automatically. Connections with a ClientID set will not reconnect for any other Reconnect Policy setting.

Notes: If a JMS Connection has a ClientID specified at the time of a network/server failure, then reconnecting that client make take significantly longer than your other clients. For example, in a cluster the JMS server must wait for the WebLogic Server “heartbeat” notification that is broadcast from other members of the cluster, as explained in “ Failover and Replication in a Cluster” in Using WebLogic Server Clusters.
Note: WebLogic JMS does not support durable subscriptions on distributed topics, so there is no issue of failover to another distributed topic member during a refresh.
Non-Durable Subscriptions and Possible Missed Messages

For consumers that are non-durable subscribers of topics, though the consumption apparently continues successfully across a refresh from an application perspective, it is possible for messages to have been published to the topic and dropped (e.g., for lack of consumers) while the reconnect was happening. Such missed messages can occur with either synchronous or asynchronous non-durable subscribers.

Duplicate Messages

Due to the nature of the consumer refresh feature, there is a possibility of redelivered messages without the client application code calling recover explicitly because a consumer refresh effectively does an implicit equivalent of a recover upon a refresh. This is the main reason why implicit Consumer refresh is not on by default. The semantics of never redelivering a successfully acknowledged message still hold true.

There is also an unlikely case when non-durable subscribers of distributed topics can receive duplicate messages that are not marked redelivered (e.g., when failover happens faster than messages are discarded in topics). This is a consequence of a non-durable subscriber refresh for the distributed topic not being pinned to a topic member across a refresh.

Variations Due to Acknowledge Modes

There will be no difference in the reconnect behaviors of Consumers due to different acknowledge modes. However, the first acknowledge call after a refresh for non-AUTO_ACK modes will throw a LostServerException as described earlier to notify user of potential lowered quality of service.

Consumers May Not Reconnect with Migrated JMS Destinations In a Cluster

Consumers will not always reconnect after a JMS server (and its destinations) is migrated to another server in a cluster. If consumers do not get migrated with the destinations, either an exception is thrown or onException will occur to inform the application that the consumer is no longer valid. As a workaround, an application can refresh the consumer either in the exception handler or through onException.

Explicitly Disabling Automatic Failover on JMS Clients

If you do not want your JMS clients to be automatically reconnected, then you must explicitly disable this feature either programatically or administratively.

Programmatically

If you do not want your JMS clients to be automatically reconnected, then your applications should call the following code:

   ConnectionFactory cf = (javax.jms.ConnectionFactory)ctx.lookup
(JNDI name of connection factory)
javax.jms.Connection con = cf.createConnection();
((weblogic.jms.extensions.WLConnection)con).setReconnectPolicy("none")

For more information about the setReconnectPolicy method, refer to the weblogic.jms.extension.WLConnection API.

Administratively

Administrators that do not want JMS clients to automatically reconnect should use the following steps to disable the Reconnect Policy on the JMS connection factory:

  1. Follow the directions for navigating to the JMS Connection Factory: Configuration: Client pages, see Configure connection factory client parameters in the Administration Console Online Help.
  2. In the Reconnect Policy field, select None to disable the JMS client reconnect feature on this connection factory.
  3. For more information about the Reconnect Policy field, see JMS Connection Factory: Configuration: Client in the Administration Console Online Help.

  4. Click Save.

For more information about the other JMS connection factory client parameters, see ClientParamsBean in the WebLogic Server MBean Reference.

Limitations for Automatic JMS Client Failover

Implicit failover of the following JMS objects is not supported WebLogic Server 9.2:

Best Practices for JMS Clients Using Automatic Failover

BEA recommends the following best practices for JMS clients when using the Automatic JMS Client Reconnect feature:

Use Transactions to Group Message Work

Use transacted sessions (JMS) or user transactions (JTA) to group related or dependent work, including messaging work, so that either all of the work is completed or none of it is. If a server instance goes down and a message is lost in the middle of a transaction, the entire transaction is rolled back and the application does not need to make a decision for each message after a failure.

Tip: Be aware of transaction commit failures after a server reconnect, which may occur if the transaction subsystem cannot reach all the participants involved in the transaction.

JMS Clients Should Always Call the close() Method

As a best practice, your applications should not rely on the JVM’s garbage collection to clean up JMS connections because the JMS automatic reconnect feature keeps a reference to the JMS connection. Therefore, always use connection.close() to clean up your connections. Also consider using a Finally block to ensure that your connection resources are cleaned up. Otherwise, WebLogic Server allocates system resources to keep the connection available.

For more information closing JMS client connections, see Best Practice: Always Close Failed JMS ClientIDs.

 


Programming Considerations for WebLogic Server 9.0 or Earlier Failures

JMS client applications running on Weblogic Server 9.0 or earlier had to reestablish javax.jms objects after a server failure. If you are still running release 9.0 or earlier JMS clients, you may want to program your JMS clients to terminate gracefully in the event of a server failure. For example:

Table 14-2 Programming Considerations for Server Failures
If a WebLogic Server Instance Fails and...
Then...
You are connected to the failed WebLogic Server instance
A JMSException is delivered to the connection exception listener. You must restart the application once the server is restarted or replaced.
A JMS Server is targeted on the failed WebLogic Server instance
A ConsumerClosedException is delivered to the session exception listener. You must re-establish any message consumers that have been lost.

 


Migrating JMS Data to a New Server

WebLogic JMS uses the migration framework implemented in the WebLogic Server core, which allows WebLogic JMS respond properly to migration requests and bring a WebLogic JMS server online and offline in an orderly fashion. This includes both scheduled migrations as well as migrations in response to a WebLogic Server failure.

Once properly configured, a JMS server and all of its destinations can migrate to another WebLogic Server within a cluster.

You can recover JMS data from a failed WebLogic Server by starting a new server and doing one or more of the tasks in Table 14-3.

Note: There are special considerations when you migrate a service from a server instance that has crashed or is unavailable to the Administration Server. If the Administration Server cannot reach the previously active host of the service at the time you perform the migration, see “Migrating a Service When Currently Active Host is Unavailable” in Using WebLogic Server Clusters.

Table 14-3 Migration Task Guide
If Your JMS Application Uses. . .
Perform the Following Task. . .
Persistent messaging—JDBC Store
  • If the JDBC database store physically exists on the failed server, migrate the database to a new server and ensure that the JDBC connection pool URL attribute reflects the appropriate location reference.
  • If the JDBC database does not physically exist on the failed server, access to the database has not been impacted, and no changes are required.
Persistent messaging—File Store
Migrate the file to the new server, ensuring that the pathname within the WebLogic Server home directory is the same as it was on the original server.
Transactions
To facilitate recovery after a crash, WebLogic Server provides the Transaction Recovery Service, which automatically attempts to recover transactions on system startup. The Transaction Recovery Service owns the transaction log for a server.
For detailed instructions on recovering transactions from a failed server, see “ Transaction Recovery After a Server Fails” in Programming WebLogic JTA.

Note: JMS persistent stores can increase the amount of memory required during initialization of WebLogic Server as the number of stored messages increases. When rebooting WebLogic Server, if initialization fails due to insufficient memory, increase the heap size of the Java Virtual Machine (JVM) proportionally to the number of messages that are currently stored in the JMS persistent store and try the reboot again.

For information about starting a new WebLogic Server, see the “ Starting and Stopping Servers: Quick Reference”. For information about recovering a failed server, refer to Avoiding and Recovering From Server Failure in Managing Server Startup and Shutdown.

For more information about defining migratable services, see “ Service Migration” in Using WebLogic Server Clusters.


  Back to Top       Previous  Next