-
1. Re: How to create and destroy Schedulers programatically?
sverker Oct 22, 2003 10:55 AM (in response to sverker)The obvious answer is MBeanServer.unregisterMBean(). Can't understand how I could miss it when I was searching specifically for that....
-
2. Re: How to create and destroy Schedulers programatically?
brian.duguid Oct 22, 2003 3:04 PM (in response to sverker)Can you tell me how you solved this dilema? I think I am having the same problem: http://www.jboss.org/modules/bb/index.html?module=bb&op=viewtopic&t=
-
3. Re: How to create and destroy Schedulers programatically?
sverker Oct 23, 2003 3:21 AM (in response to sverker)Certainly!
I did like this:
public void startService() throws Exception {
logger.info("Setting up a scheduler that will initialize LimeManagement in 1 minute from now");
ObjectName schedulerName = new ObjectName( "com.limetransit.sms:service=Scheduler,schedule=startup" );
try {
MBeanServer server = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);
// first check if there is already an instance, and if so destroy it
if(getServer().isRegistered( schedulerName ))
server.unregisterMBean(schedulerName);
// create a new instance
ObjectInstance scheduler = server.createMBean(
"org.jboss.varia.scheduler.Scheduler",
schedulerName
);
// Prepare attributes
AttributeList attributes = new AttributeList();
attributes.add(new Attribute("StartAtStartup", new Boolean(true)));
attributes.add(new Attribute("SchedulableMBean", "com.limetransit.sms:service=LimeManagement"));
attributes.add(new Attribute("SchedulableMBeanMethod", "scheduledStart"));
// set start date to 1 minute from now
long startDate = (new Date()).getTime() + 60000;
attributes.add(new Attribute("InitialStartDate", "" + startDate));
// dummy, it just has to be here
attributes.add(new Attribute("SchedulePeriod", new Long(60000)));
// just invoke once
attributes.add(new Attribute("InitialRepetitions", new Long(1)));
// Set the attributes
server.setAttributes( scheduler.getObjectName(), attributes );
// Start the MBean
server.invoke(
scheduler.getObjectName(),
"start",
new Object[] {},
new String[] {}
);
} catch(Exception e) {
logger.error("Failed to create Scheduler instance", e);
server.unregisterMBean(schedulerName);
}
}
/**
* This method is called from a scheduler initiated by the startService method.
* It's a workaround for lack of control of the deploy order
*
* @jmx.managed-operation
*/
public void scheduledStart() throws Exception {
logger.info("Starting LimeManagement tasks");
status = STATUS_STARTING;
try {
// Do the stuff
logger.info("MBean started");
status = STATUS_RUNNING;
} catch(Exception e) {
status = STATUS_ERROR;
logger.info("Starting MBean failed due to " + e.getMessage());
throw new Exception(e);
} finally {
MBeanServer server = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);
ObjectName schedulerName = new ObjectName( "com.limetransit.sms:service=Scheduler,schedule=startup" );
if(server.isRegistered( schedulerName ))
server.unregisterMBean(schedulerName);
}
}