cancel
Showing results for 
Search instead for 
Did you mean: 

Cronjob query action: prevent from running from config

mtielemans
Champ in-the-making
Champ in-the-making
I have created a custom action, which can be invoked as a rule, or from a query cronjob.

The spring bean that registers the cronjob + query is a java class that extends
CronScheduledQueryBasedTemplateActionDefinition
. I have overridden its
queryTemplate
setters and getters to be able to load my query from the java code, and added a
isEnabled
boolean flag. This is switched to true or false from config.

Now, I want to make sure that on startup, the cron is not scheduled at all if
isEnabled
is false. To that end, I wrote the following method:


   @Override
   public void afterPropertiesSet() throws Exception {
      if(isEnabled) {
         logger.warn("Scheduling "+NAME);
         super.afterPropertiesSet();
      } else {
         logger.warn("Didn't schedule "+NAME+" because it was disabled.");
      }
   }


The superclass's afterPropertiesSet method registers the cron with the scheduler.

Unfortunately, this doesn't work. Corresponding log warnings never even show, suggesting that the method is never invoked. However, the following code works flawlessly:


   @Override
   public List<NodeRef> getNodes() {
      if(isEnabled) {
         logger.warn("getNodes: "+NAME);
         return super.getNodes();
      } else {
         logger.warn("Didn't get nodes for "+NAME+" because it was disabled.");
         return new ArrayList<NodeRef>();
      }
   }


Of course, the cron is still scheduled, so the getNodes method is still invoked at scheduled times. However, it always returns an empty response, so no furthur actions are taken.

I want to be able to make sure the cronjob is never scheduled. Why does my first attempt not work, and what should I try instead?
2 REPLIES 2

kaynezhang
World-Class Innovator
World-Class Innovator
Mehtod afterPropertiesSet should be invoked .
Are you sure neither "Scheduling "+NAME,nor "Didn't schedule "+NAME+" because it was disabled." is printed?
Did you set logger level correctly?
Is isEnabled an boolean property of your class and you configure it in spring xml file?
can you paste your complete source code here?

mtielemans
Champ in-the-making
Champ in-the-making
I had been looking for these entries below the log entry from my init method. I now found that the init method, which sets the <java>isEnabled</java> switch, is called after <java>afterPropertiesSet</java>..

The spring bean config has
init-method=init
. This points to the following piece of code in my bean:
<java>
public final void init() throws IOException {
   intakePath=MyGlobalSettings.getPropertyValue("app.intake.path", intakePath);

   String enabled = MyGlobalSettings.getPropertyValue("app.intake.cronjob.enabled", "1");
   if(enabled.equals("1")) {
      logger.info("Enabled "+NAME);
      setEnabled(true);
   } else {
      logger.warn("Disabled "+NAME);
      setEnabled(false);
   }
   setQueryTemplate("PATH:\""+intakePath+"/*\" -ASPECT:\"{http://www.mark.nl/model/app/1.0}customAspect\"");
}
</java>

From the log:

2013-07-31 12:37:30,446  WARN  [app.actions.CronScheduledQueryBasedAppIntakeActionDefinition] [Thread-2] Scheduling CronScheduledQueryBasedAppIntakeActionDefinition
2013-07-31 12:37:30,448  WARN  [app.actions.CronScheduledQueryBasedAppIntakeActionDefinition] [Thread-2] Disabled CronScheduledQueryBasedAppIntakeActionDefinition


Evidently, the init method is somehow not the first to be called. I was in the illusion that was what its for.

<strong>EDIT</strong>

I moved the logic from init to a zero-args constructor. Without changing any further code, this works flawlessly. Why didn't using the init-method work, and what is it for if not for what I was trying to achieve?


2013-07-31 12:47:38,026  WARN  [app.actions.CronScheduledQueryBasedAppIntakeActionDefinition] [Thread-2] Disabled CronScheduledQueryBasedAppIntakeActionDefinition
2013-07-31 12:47:38,026  DEBUG [app.actions.CronScheduledQueryBasedAppIntakeActionDefinition] [Thread-2] Query='PATH:"/app:company_home/st:sites/cm:app/cm:documentLibrary/cm:AppIntake/*" -ASPECT:"{http://www.mark.nl/model/app/1.0}customAspect"'
2013-07-31 12:47:38,032  WARN  [app.actions.CronScheduledQueryBasedAppIntakeActionDefinition] [Thread-2] Didn't schedule CronScheduledQueryBasedAppIntakeActionDefinition because it was disabled.