Announcement Announcement Module
No announcement yet.
Any way to conditionally declare a quartz cron job/trigger/bean in XML? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Any way to conditionally declare a quartz cron job/trigger/bean in XML?


    I was wondering if there was anyway to conditionally declare a bean in an xml configuration. The goal behind this question is that I only want to enable a quartz configured job based on some value in a system environment variable. Technically speaking, I could pass the value of the system environment var to the job and check within the job whether or not to execute it, but that would be poorly designed since the bean running the job should be completely agnostic to conditions as to when it should be run.

    For instance, I would like to configure my job as follows:
    	<!-- Generate TeamLeader article every night -->
    	<bean id="fwl.service.TrendLeaderArticleService.trigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    	 	<property name="cronExpression" value="0 0/10 * * * ? *" />
    		<property name="jobDetail">
    			<bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    				<property name="name" value="fwl.service.TrendLeaderArticleService" />
    				<property name="group" value="news" />
    				<property name="targetObject" ref="trendLeaderArticleServiceImpl"/>
    				<property name="targetMethod" value="createTrendLeaderArticle"/>
    However, I only want this declaration based on a sys variable. To add complexity to the equation, I need to specify the trigger bean in the quartz FactorySchedulerBean, so if the trigger bean isn't actually defined, I'm not sure what to do.

    Perhaps the solution is to always keep this defined but only inject the trigger bean into the scheduler based on some conditional parameter. But there too, I'm not sure how to do that.

    I can only assume that I'm not the first one that has encountered this type of need, but a search did not reveal any promising ideas how to accomplish something like this.

    Does anyone have any suggestions?



  • #2
    Well, after a good night's sleep, I realized that I was looking at the wrong the wrong way. Guess that's what happens when it is too late a night.

    Instead of trying to conditionally trying to define the trigger, I've gone a more sensible approach, and upon startup, enable or disable the scheduler altogether based on a system environment variable. Makes a lot more sense, and much easier to implement.

    Maybe this will help someone in the future.