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 server to another. This can be done by implementing the “weblogic.cluster.singleton.SingletonService” interface and by using activate () and deactivate () methods in a class.
For Application level demo you have download a simple application “MySingletonServiceEar.ear” which has a class in it called “MySingletonService.class” which implements the “weblogic.cluster.singleton.SingletonService” interface and has both activate () and deactivate () methods in it which is under APP-INF -> classes. And in “weblogic-application.xml” has the singleton service name and class name which necessary for this configuration because here we are configuring a singleton services from application side.
Steps To Follow:
1. Create 2 managed Servers say MS-1 and MS-2 and assign a Machine & a Cluster to these Managed Servers.
2. We would be using NodeManagers (Consensus) for clustered singleton services migration, hence we have to configure following things.
Console Path: Clusters —> Cluster —> Configuration [tab] —> Migration [sub-tab]
Candidate Machines For Migratable Servers: Machine
Migration Basis: Consensus
3. Now you just have to deploy “MySingletonServiceEar.ear” targeted on Cluster which we had made.
4. Start both the managed servers with the help of nodemanager.
5. If you want to check on which server is Singleton Service might be running then you can confirm this from the managed servers .out file, as from console we did not make any Singleton Services. In my case following was the output.
MS-1.out file was having the active () method called.
But MS-2.out file was not having the active () method.
This states that the service is running only on one server.
6. Now shutdown MS-1 and then check the managed server’s .out files.
You will notice that the MS-1.out would have now called deactivate () method and has got shutdown.
However MS-2.out file will have the active () method as when MS-1 was brought down and during that time our service had migrated to the MS-2.
This states that the service has got migrated properly and is still running only on one server.
NOTE: Here service can be running on any server for the first time, as we did not mention the Preferred Server for the service anywhere in application level.