4 Replies Latest reply on Jul 1, 2014 1:27 AM by pierce.currid@citi.com

    Creating a Cron Task plugin

    pierce.currid@citi.com

      Hi,

      Looking through the Jive codebase, there is a CronTask class, along with a CronTaskDefinitionBean class.

      Does anyone have any pointers for creating a Cron job plugin that can hit a service using Cron Task within Jive?

       

      Thanks

      Pierce

        • Re: Creating a Cron Task plugin

          Hi Pierce Currid,

           

          This is very straightforward to achieve from a plugin using Jive and Spring.

           

          1. Create you own bean, with for example a method called "run" that does what you want to do as a task. 

          2. Put your bean definition into your plugin spring.xml.

          3. Now create a bean task definition in your plugin spring.xml

           

          For example:

           

              <bean id="myBean" class="com.xxx.MyBean">

               ...

              </bean>

           

              <bean id="doSomethingTask" class="com.jivesoftware.base.taskmanager.AutoStartFixedPeriodTask">

                  <property name="allNodes" value="false"/>

                  <property name="beanName" value="myBean"/>

                  <property name="methodName" value="run"/>

                  <property name="period" value="1"/>

                  <property name="timeUnit" ref="MINUTES"/>

              </bean>

           

          Make sure that you set the methodName property to the method name in your bean that you want to run (for example "run").

           

          There are several variations on the AutoStartFixedPeriodTask that can be configured.  For example AutoStartCronTask can be defined like this;

           

              <bean id="doSomethingTask" class="com.jivesoftware.base.taskmanager.AutoStartCronTask">

                  <property name="allNodes" value="false"/>

                  <property name="beanName" value="myBean"/>

                  <property name="methodName" value="run"/>

                <!-- every 24 hours at 2am -->

                  <property name="cronExpression" value="0 0 2 * * ?"/>

              </bean>

           

          There are others like AutoStartOneTimeTask.  Have a look at /core/src/main/resources/spring-taskContext.xml for some more examples.

            • Re: Creating a Cron Task plugin
              pierce.currid@citi.com

              Hi Jason,

              Thanks for the speedy and detailed response.

              I'll take a look at this now.

              So I don't have to merge my task to to the existing list of Tasks within Jive?

                • Re: Re: Creating a Cron Task plugin
                  So I don't have to merge my task to to the existing list of Tasks within Jive?

                  Nope.  You just need to define it in your plugin within /src/main/plugin/spring.xml.  Installing the plugin will do the rest.

                  I recommend that you define a Jive system property for turning on/off your task just in case you need to stop the task but don't want to uninstall the plugin or perform a restart of Jive.

                   

                  for example:

                      public static final String RUN_PROP = "task.should.run";

                      public boolean isRunEnabled() {

                          return JiveGlobals.getJiveBooleanProperty(RUN_PROP, false);

                      }

                   

                      public void run() throws Exception {

                           if (isRunEnabled()) {

                            ...

                            }

                      }

                    • Re: Creating a Cron Task plugin
                      pierce.currid@citi.com

                      Hi Jason,

                      Quick question - in the bean configuration for AutoStartCronTask, there is property called 'allNodes', that is set to false for all of the Jive CronTasks within spring-taskContext.xml. I notice you have also set it to false.

                      Does this mean it only runs once if its a clustered Jive environment?

                      Thanks Again

                      Pierce