Skip to content

Instantly share code, notes, and snippets.

@garyrussell
Last active December 15, 2015 15:45
Show Gist options
  • Save garyrussell/f2f465ec7e9780db415f to your computer and use it in GitHub Desktop.
Save garyrussell/f2f465ec7e9780db415f to your computer and use it in GitHub Desktop.
CompoundTriggerAdvice for Smart Polling with Spring Integration
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example;
import java.util.Date;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import reactor.core.support.Assert;
/**
* @author Gary Russell
* @since 4.3
*
*/
public class CompoundTrigger implements Trigger {
private volatile Trigger primary;
private volatile Trigger override;
/**
* Construct a compound trigger with the supplied primary trigger.
* @param primary the primary trigger.
*/
public CompoundTrigger(Trigger primary) {
setPrimary(primary);
}
/**
* Set the primary trigger.
* @param primary the trigger.
*/
public final void setPrimary(Trigger primary) {
Assert.notNull(primary, "'primary' cannot be null");
this.primary = primary;
}
/**
* Set the override trigger; set to null to revert to using the
* primary trigger.
* @param override the override trigger, or null.
*/
public void setOverride(Trigger override) {
this.override = override;
}
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
if (this.override != null) {
return this.override.nextExecutionTime(triggerContext);
}
else {
return this.primary.nextExecutionTime(triggerContext);
}
}
}
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.util.CompoundTrigger;
import org.springframework.messaging.Message;
import org.springframework.scheduling.Trigger;
import org.springframework.util.Assert;
/**
* An {@link AbstractMessageSourceAdvice} that uses a {@link CompoundTrigger} to adjust
* the poller - when a message is present, the compound trigger's primary trigger is
* used to determine the next poll. When no message is present, the override trigger is
* used.
* <p>
* The poller advised by this class must be configured to use the same
* {@link CompoundTrigger} instance and must <b>not</b> use a task executor.
*
* @author Gary Russell
* @since 4.3
*
*/
public class CompoundTriggerAdvice extends AbstractMessageSourceAdvice {
private final CompoundTrigger compoundTrigger;
private final Trigger override;
public CompoundTriggerAdvice(CompoundTrigger compoundTrigger, Trigger overrideTrigger) {
Assert.notNull(compoundTrigger, "'compoundTrigger' cannot be null");
this.compoundTrigger = compoundTrigger;
this.override = overrideTrigger;
}
@Override
public boolean beforeReceive(MessageSource<?> source) {
return true;
}
@Override
public Message<?> afterReceive(Message<?> result, MessageSource<?> source) {
if (result == null) {
this.compoundTrigger.setOverride(this.override);
}
else {
this.compoundTrigger.setOverride(null);
}
return result;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
<int:inbound-channel-adapter channel="nullChannel" auto-startup="false">
<bean class="example.Source" />
<int:poller trigger="compoundTrigger">
<int:advice-chain>
<bean class="example.CompoundTriggerAdvice">
<constructor-arg ref="compoundTrigger"/>
<constructor-arg ref="secondary"/>
</bean>
</int:advice-chain>
</int:poller>
</int:inbound-channel-adapter>
<bean id="compoundTrigger" class="example.CompoundTrigger">
<constructor-arg ref="primary" />
</bean>
<bean id="primary" class="org.springframework.scheduling.support.CronTrigger">
<constructor-arg value="*/1 * * * * *" />
</bean>
<bean id="secondary" class="org.springframework.scheduling.support.PeriodicTrigger">
<constructor-arg value="10" />
</bean>
</beans>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment