views:

66

answers:

2

In a spring servlet xml file, I'm using org.springframework.scheduling.quartz.SchedulerFactoryBean to regularly fire a set of triggers.

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref local="AwesomeTrigger" />
            <ref local="GreatTrigger" />
            <ref local="FantasticTrigger"/>
        </list>
    </property>
</bean>

The issue is that in different environments, I don't want certain triggers firing. Is there a way to include some sort of configuration or variable defined either in my build.properties for the environment or in a spring custom context properties file that assists the bean xml to determine which triggers should be included in the list? That way, for example, AwesomeTrigger would be called in development but not qa.

+1  A: 

I had the same problem.

What I've done :

I overrode the Trigger bean to add a property enabled.

And after I overrode SchedulerFactoryBean.setTriggers and I only set the enabled triggers.

You just need to use these new beans in your xml files and the enabled in your properties file.

Ex :

public void setTriggers(Trigger[] triggers) {
        ArrayList<Trigger> triggersToSchedule = new ArrayList<Trigger>();

        for(Trigger trigger : triggers){
            if(trigger instanceof SimpleTriggerBeanEnabler){
                if(((SimpleTriggerBeanEnabler)(trigger)).isEnabled()){
                    triggersToSchedule.add(trigger);
                }
            }
            else{
                triggersToSchedule.add(trigger);
            }
        }


        super.setTriggers(triggersToSchedule.toArray(new Trigger[triggersToSchedule.size()]));
    }
Mike
+2  A: 

Create a factory bean that returns a list of triggers based on a property

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <bean class="com.yourcompany.YourFactoryBeanImplementationThatReturnsAListOfTriggers">
            <property name="triggerNames" value="${property.from.a.properties.file}" />
        </bean>

    </property>
</bean>

<context:property-placeholder location="classpath:your.properties" />
seanizer