Hi,
Hi,

Jay SenSharma

Jay SenSharma

Many times we want to develop a Service which would be active only on any one of the Node (Cluster Member) of our Clustered environment at Any Point of time …. based on our requirement. WebLogic provides “weblogic.cluster.singleton.SingletonService” interface which has two methods “activate()” and “deactivate”….We need to provide implementation of these two methods according to our requirement.

Here we will see a very basic overview of Creating and deploying a Simple Singleton Service in form of a Simple EAR application.

Step1). Create 2 managed Servers Lets say MS1 and MS2.

Step2). Assign Machine & Cluster to these Managed Servers  better if you choose different Machines for Managed Servers…(For Testing you can keep Same Machines for Both managed Servers)….  To Use Clustered Singleton Services ManagedServers Must be started using NodeManagers so we must create a Machine for Managed Servers.

MS1 —>Machine-A—>Cluster-A

MS1 —>Machine-B—>Cluster-A

Step3). From Admin Console

AdminConsole—>Clusters—>Cluster-A—>Configuration (Tab)—>Migration (SubTab)

Candidate Machines For Migratable Servers: Machine-A   (your machine name)
Migration Basis: Consensus

Save and Activate Changes. Restart the Whole Cluster sothat the above changes gets reflected.

Step4).. Now develop a Simple EAR Application… Create a Directory somewhere in your file system: “C:JavaTestSingletonDemo”

Step5).Now Create your EAR Application Directory “MySingletonServiceEar” inside “C:JavaTestSingletonDemo”.

Step6). Create 3 Directories “APP-INF”, “META-INF” and “SimpleWebApp” inside “C:JavaTestSingletonDemoMySingletonServiceEar” directory.

Step7). Now Inside “C:JavaTestSingletonDemoMySingletonServiceEarAPP-INFclasses” location provide your Singleton class Implementation: “ClusterSingleton.java”

package my.singleton.service;
import weblogic.cluster.singleton.SingletonService;
import weblogic.logging.NonCatalogLogger;
public class ClusterSingleton  implements weblogic.cluster.singleton.SingletonService
{

public ClusterSingleton()
{
System.out.println("nt ClusterSingleton Object Initialized...");
}

public void activate()
{
try
{
System.out.println("nt ClusterSingleton activate() called....ntWrite your Actual Code inside this Method");
}
catch(Exception e)
{
System.out.println("nt ERROR ClusterSinglenton:"+ e);
e.printStackTrace();
}
}

public void deactivate()
{
System.out.println("nt ClusterSingleton called deactivate() ....Write your Resource Cleaning Code Here");
}
}

Step8). Now Compile this class “ClusterSingleton.java” after running the “setWLSEnv.sh” in the Shell/command prompt.

javac -d . ClusterSingleton.java

Step9). Inside “C:JavaTestSingletonDemoMySingletonServiceEarMETA-INF” directory provide “application.xml” file:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.4">
<module>
<web>
<web-uri>SimpleWebApp</web-uri>
<context-root>SimpleWebApp</context-root>
</web>
</module>
</application>

Step10). Inside “C:JavaTestSingletonDemoMySingletonServiceEarMETA-INF” directory provide “weblogic-application.xml” file:

<?xml version="1.0" encoding="UTF-8"?>
<weblogic-application xmlns="http://www.bea.com/ns/weblogic/90">
<singleton-service>
<name>MyTestSingletonService</name>
<class-name>my.singleton.service.ClusterSingleton</class-name>
</singleton-service>
</weblogic-application>

Step11). Inside “C:JavaTestSingletonDemoMySingletonServiceEarSimpleWebApp” directory provide any simple JSP page like “index.jsp”:

<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<html>
<body>
<h1> Check The Any of the Managed Server Out file...Singleton Service is activated or not!!!
</body>
</html>

Step12). Provide “web.xml” file inside “C:JavaTestSingletonDemoMySingletonServiceEarSimpleWebAppWEB-INF” location:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
</web-app>

Step13). Finally Write “build.xml” file to deploy, redeploy and undeploy your application inside “C:JavaTestSingletonDemo” location:

<project name="webservices-hello_world" default="deploy">
<property name="src" value="src" />
<property name="build" value="build" />
<property name="wls.username" value="weblogic" />
<property name="wls.password" value="weblogic" />
<property name="wls.hostname" value="localhost" />
<property name="wls.port" value="7001" />
<property name="deploy.target" value="Cluster-A" /> 
<property name="war.name" value="MySingletonServiceEar" />
<property name="war.deploy.src" value="MySingletonServiceEar" />

<path id="client.class.path">
<pathelement path="${clientclass-dir}"/>
<pathelement path="${java.class.path}"/>
</path>

<taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy"/>

<target name="deploy">
<wldeploy action="deploy" name="${war.name}" source="${war.deploy.src}" user="${wls.username}"
nostage="true" password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}"
targets="${deploy.target}" />
</target>

<target name="redeploy">
<wldeploy action="redeploy" name="${war.name}"  source="${war.deploy.src}" user="${wls.username}"
password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}"
targets="${deploy.target}" />
</target>

<target name="undeploy">
<wldeploy action="undeploy" name="${war.name}" failonerror="false"
user="${wls.username}" password="${wls.password}" verbose="true"
adminurl="t3://${wls.hostname}:${wls.port}" targets="${deploy.target}" />
</target>
</project>

Step14). Run the “setWLSEnv.cmd” in a command/shell prompt and move to  “C:JavaTestSingletonDemo” directory and run the ANT to deploy your application….

Step15). In any of the Managed Server OUT file you will see that the activate() is called….not in Both the Managed Servers…because it’s Singleton Service so at a time it will be activated on any one Member of the Cluster.

.

.

Thanks

Jay SenSharma

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.