-
Notifications
You must be signed in to change notification settings - Fork 1.1k
GH-8625: Add Duration support for <poller>
#8627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright 2023 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 | ||
* | ||
* https://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 org.springframework.integration.config; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.springframework.beans.factory.FactoryBean; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.scheduling.support.PeriodicTrigger; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* The {@link FactoryBean} to produce a {@link PeriodicTrigger} | ||
* based on parsing string values for its options. | ||
* This class is mostly driver by the XML configuration requirements for | ||
* {@link Duration} value representations for the respective attributes. | ||
* | ||
* @author Artem Bilan | ||
* | ||
* @since 6.2 | ||
*/ | ||
public class PeriodicTriggerFactoryBean implements FactoryBean<PeriodicTrigger> { | ||
|
||
@Nullable | ||
private String fixedDelayValue; | ||
|
||
@Nullable | ||
private String fixedRateValue; | ||
|
||
@Nullable | ||
private String initialDelayValue; | ||
|
||
@Nullable | ||
private TimeUnit timeUnit; | ||
|
||
public void setFixedDelayValue(String fixedDelayValue) { | ||
this.fixedDelayValue = fixedDelayValue; | ||
} | ||
|
||
public void setFixedRateValue(String fixedRateValue) { | ||
this.fixedRateValue = fixedRateValue; | ||
} | ||
|
||
public void setInitialDelayValue(String initialDelayValue) { | ||
this.initialDelayValue = initialDelayValue; | ||
} | ||
|
||
public void setTimeUnit(TimeUnit timeUnit) { | ||
this.timeUnit = timeUnit; | ||
} | ||
|
||
@Override | ||
public PeriodicTrigger getObject() { | ||
boolean hasFixedDelay = StringUtils.hasText(this.fixedDelayValue); | ||
boolean hasFixedRate = StringUtils.hasText(this.fixedRateValue); | ||
|
||
Assert.isTrue(hasFixedDelay ^ hasFixedRate, | ||
"One of the 'fixedDelayValue' or 'fixedRateValue' property must be provided but not both."); | ||
|
||
TimeUnit timeUnitToUse = this.timeUnit; | ||
if (timeUnitToUse == null) { | ||
timeUnitToUse = TimeUnit.MILLISECONDS; | ||
} | ||
|
||
Duration duration = toDuration(hasFixedDelay ? this.fixedDelayValue : this.fixedRateValue, timeUnitToUse); | ||
|
||
PeriodicTrigger periodicTrigger = new PeriodicTrigger(duration); | ||
periodicTrigger.setFixedRate(hasFixedRate); | ||
if (StringUtils.hasText(this.initialDelayValue)) { | ||
periodicTrigger.setInitialDelay(toDuration(this.initialDelayValue, timeUnitToUse)); | ||
} | ||
return periodicTrigger; | ||
} | ||
|
||
@Override | ||
public Class<?> getObjectType() { | ||
return PeriodicTrigger.class; | ||
} | ||
|
||
private static Duration toDuration(String value, TimeUnit timeUnit) { | ||
if (isDurationString(value)) { | ||
return Duration.parse(value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assert that timeUnit is not provided in this case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having |
||
} | ||
return toDuration(Long.parseLong(value), timeUnit); | ||
} | ||
|
||
private static boolean isDurationString(String value) { | ||
return (value.length() > 1 && (isP(value.charAt(0)) || isP(value.charAt(1)))); | ||
} | ||
|
||
private static boolean isP(char ch) { | ||
return (ch == 'P' || ch == 'p'); | ||
} | ||
|
||
private static Duration toDuration(long value, TimeUnit timeUnit) { | ||
return Duration.of(value, timeUnit.toChronoUnit()); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
message.history.tracked.components=input, publishedChannel, annotationTestService* | ||
poller.maxMessagesPerPoll=10 | ||
poller.interval=100 | ||
poller.interval=PT0.1S | ||
poller.receiveTimeout=10000 | ||
global.wireTap.pattern=input |
Uh oh!
There was an error while loading. Please reload this page.