A clustered singleton service that calls a configurable
method on a target (m)bean, whenever the current node becomes
the master. Correspondingly, it calls a configurable method
on the target (m)bean, whenever the current node resigns from
being the master.
Optional string arguments may be passed to those methods.
| Method from org.jboss.ha.singleton.HASingletonController Detail: |
public Object getTarget() {
return mSingleton;
}
|
public static Method getTargetMethod(Object target,
String methodName,
Class[] types) throws NoSuchMethodException {
Class clazz = target.getClass();
NoSuchMethodException nsme = null;
while (clazz != null)
{
try
{
Method method = clazz.getDeclaredMethod(methodName, types);
return method;
}
catch (NoSuchMethodException e)
{
// Cache the one from the top level class
if (nsme == null)
{
nsme = e;
}
// Keep searching
clazz = clazz.getSuperclass();
}
}
throw nsme;
}
|
public ObjectName getTargetName() {
return mSingletonMBean;
}
|
public String getTargetStartMethod() {
return mSingletonStartMethod;
}
|
public String getTargetStartMethodArgument() {
return mSingletonStartMethodArgument ;
}
|
public String getTargetStopMethod() {
return mSingletonStopMethod;
}
|
public String getTargetStopMethodArgument() {
return mSingletonStopMethodArgument ;
}
|
protected Object invokeSingletonMBeanMethod(ObjectName target,
String operationName,
Object param) throws InstanceNotFoundException, ReflectionException, MBeanException {
if (target != null && operationName != null)
{
Object[] params;
String[] signature;
if (param != null)
{
params = new Object[] { param };
signature = new String[] { param.getClass().getName() };
log.debug("Calling operation: " + operationName +
"(" + param + "), on target: '" + target + "'");
}
else
{
params = NO_ARGS;
signature = NO_TYPE_NAMES;
log.debug("Calling operation: " + operationName +
"(), on target: '" + target + "'");
}
return server.invoke(target, operationName, params, signature);
}
else
{
log.debug("No configured target mbean or operation to call");
return null;
}
}
|
protected Object invokeSingletonMethod(Object target,
String operationName,
Object param) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
if (target != null && operationName != null)
{
Object[] params;
Class[] types;
if (param != null)
{
params = new Object[] { param };
types = new Class[] { param.getClass() };
log.debug("Calling operation: " + operationName +
"(" + param + "), on target: '" + target + "'");
}
else
{
params = NO_ARGS;
types = NO_TYPES;
log.debug("Calling operation: " + operationName +
"(), on target: '" + target + "'");
}
Method method = getTargetMethod(target, operationName, types);
return method.invoke(target, params);
}
else
{
log.debug("No configured target mbean or operation to call");
return null;
}
}
|
public void setTarget(Object target) {
this.mSingleton = target;
}
|
public void setTargetName(ObjectName targetObjectName) {
this.mSingletonMBean = targetObjectName;
}
|
public void setTargetStartMethod(String targetStartMethod) throws InvalidParameterException {
if (targetStartMethod != null)
mSingletonStartMethod = targetStartMethod;
}
|
public void setTargetStartMethodArgument(String targetStartMethodArgument) {
mSingletonStartMethodArgument = targetStartMethodArgument;
}
|
public void setTargetStopMethod(String targetStopMethod) throws InvalidParameterException {
if (targetStopMethod != null)
mSingletonStopMethod = targetStopMethod;
}
|
public void setTargetStopMethodArgument(String targetStopMethodArgument) {
mSingletonStopMethodArgument = targetStopMethodArgument;
}
|
public void startSingleton() {
super.startSingleton();
try
{
if (mSingleton != null)
{
invokeSingletonMethod(
mSingleton,
mSingletonStartMethod,
mSingletonStartMethodArgument
);
}
else if (mSingletonMBean != null)
{
invokeSingletonMBeanMethod(
mSingletonMBean,
mSingletonStartMethod,
mSingletonStartMethodArgument
);
}
else
{
log.warn("No singleton configured; cannot start");
}
}
catch (Exception e)
{
log.error("Controlled Singleton failed to become master", e);
}
}
Call the target start method |
public void stopSingleton() {
super.stopSingleton();
try
{
if (mSingleton != null)
{
invokeSingletonMethod(
mSingleton,
mSingletonStopMethod,
mSingletonStopMethodArgument
);
}
else if (mSingletonMBean != null)
{
invokeSingletonMBeanMethod(
mSingletonMBean,
mSingletonStopMethod,
mSingletonStopMethodArgument
);
}
else
{
log.warn("No singleton configured; cannot start");
}
}
catch (Exception e)
{
log.error("Controlled Singleton failed to resign from master position", e);
}
}
Call the target stop method |