Weblogic Cluster and Singleton Service.

Weblogic Cluster and Singleton Service.

Need to have only one object for an application, that we can achieve to make the entity as a singleton entity. Creating a singleton class is implementing a singleton design pattern that is a simple and easy thing. Up to till now I created a single object per JVM.

When I come to know about cluster environment.

In a multiple JVM’s environments, each of them will have their own copy of the singleton object which can lead to multiple issues especially in a clustered environment where the access to the resource needs to be restricted and synchronized.

Weblogic has the concept of Singleton Service — where only instance runs within the cluster and all clients will look up to the same instance.

In my case, I have Weblogic server with a clustered environment, which has a two managed server MS1 and MS2. My application contains one thread which is responsible to read a mail from the mailbox and put this record into a database. When I deployed this application on Weblogic server.

The thread which is responsible to read mail and put this into a database. This is not actually the best solution this encounter a problem in some cases two thread MS1 and MS2 are read the same record at a time and put this into a database and creates a duplicate entry. To resolve this issue I implemented the SingletonService interface. This interface is located in below package


This interface contains two methods which you must need to implement

  1. activate()
  2. deactivate()

What is SingletonService?

A singleton service is a service that can only run on a single managed server in a cluster at a time. Weblogic Server lets you automatically monitor and migrate singleton services from one managed server to other.

How do we implement this?

Steps to implement and configure singleton service.

Step-1:- Implement weblogic.cluster.singleton.SingletonService interface.

package org.teliet.singletontest;
import weblogic.cluster.singleton.SingletonService;  

 public class MySingletonServiceClass implements 

    public void activate() {
      System.out.println("This is activate");
       // for testing purpose I put here SOP 
    public void deactivate() {
     System.out.println("This is deactivate");
       // for testing purpose I put here SOP   

Step-2:- Add the configuration in weblogic-application.xml

open your .ear → META-INF → weblogic-application.xml and add below configurations

 <class-name> org.teliet.singletontest.MySingletonServiceClass
</class-name> <name>singletonserviceclass</name> </singleton-service> ... </weblogic-application>

Note:The <class-name> and <name> elements are required.

Step-3:- Now configure SingletonService migration behavior.

Open Weblogic admin portal console which looks like this

Now we need to configure the cluster for the Singleton Service. In the WebLogic Administration Console, navigate to your cluster, and then to the “Migration” tab. You will need to have migration set up in some way, I used “Consensus” to avoid using a database for this example, but your production model may have different needs entirely.

Now you need to navigate to the cluster’s “Singleton Services” tab and create a new Singleton Service.

You will want to use the fully qualified class name for the singleton:

Now Redeploy the application. To test the Singleton Services changes navigate to the

Clusters → Cluster1 → SingletonServicesName → then click on Control Tab

Now Preferred Server is MS1 whenever MS1 is down this will automatically migrate to MS2. You can also the same thing just using logs. In MS1-studout logs you will get those logs “This is activate”, In MS2 nothing will show. When a condition is reverse you will find the logs in MS2-studout.

Singleton Service use cases

  1. Timer Task or Job scheduler in a cluster.
  2. Singleton service to handle other unclustered services.
  3. Single source for properties for clustered applications.

This is just an idea. Dig deeper find more & let me know as well.

For more stories.

Let’s connect on Stackoverflow, LinkedIn, Facebook& Twitter.