-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...core/src/main/java/org/springframework/integration/config/PeriodicTriggerFactoryBean.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 driven 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); | ||
} | ||
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()); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...test/java/org/springframework/integration/configuration/EnableIntegrationTests.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Having
time-unit
does not cause any ambiguity, so ignoring it as it is also done in theScheduledAnnotationBeanPostProcessor
should be totally OK.