3 Replies Latest reply on Oct 23, 2003 3:21 AM by Sverker Abrahamsson

    How to create and destroy Schedulers programatically?

    Sverker Abrahamsson Novice

      Hi,
      how can I programatically create and destroy a Scheduler? Creating it should be possible to do with MBeanServer but I have not found any way to destroy it later when it is not used any longer so that the next time my app is deployed, it will try to create the Scheduler and fail because there is already one.

      The reason why I want to do this is that I have an MBean that have to carry out some tasks after my application has loaded but no matter how I set the depends, it keeps on getting started before my EJB's has finished being initialized.

      Therefore I would like to create a Scheduler that calls a method on my MBean 1 minute after the startService() method has been called to give enough time for the EJB's to finish their initializement.

      How can I destroy an MBean that has been created by MBeanServer?

        • 1. Re: How to create and destroy Schedulers programatically?
          Sverker Abrahamsson Novice

          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 Newbie

            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 Abrahamsson Novice

              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);
              }
              }