-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Introduce a ReceiveMessageAdvice
#3265
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
garyrussell
merged 2 commits into
spring-projects:master
from
artembilan:ReceiveMessageAdvice
Apr 28, 2020
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
72 changes: 72 additions & 0 deletions
72
...egration-core/src/main/java/org/springframework/integration/aop/ReceiveMessageAdvice.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,72 @@ | ||
/* | ||
* Copyright 2020 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.aop; | ||
|
||
import org.aopalliance.intercept.MethodInterceptor; | ||
import org.aopalliance.intercept.MethodInvocation; | ||
|
||
import org.springframework.integration.core.MessageSource; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.messaging.PollableChannel; | ||
|
||
/** | ||
* An AOP advice to perform hooks before and/or after a {@code receive()} contract is called. | ||
* | ||
* @author Artem Bilan | ||
* | ||
* @since 5.3 | ||
*/ | ||
@FunctionalInterface | ||
public interface ReceiveMessageAdvice extends MethodInterceptor { | ||
|
||
/** | ||
* Subclasses can decide whether to {@link MethodInvocation#proceed()} or not. | ||
* @param source the source of the message to receive. | ||
* @return true to proceed (default). | ||
*/ | ||
default boolean beforeReceive(Object source) { | ||
return true; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
default Object invoke(MethodInvocation invocation) throws Throwable { | ||
Object target = invocation.getThis(); | ||
if (!(target instanceof MessageSource) && !(target instanceof PollableChannel)) { | ||
return invocation.proceed(); | ||
} | ||
|
||
Message<?> result = null; | ||
if (beforeReceive(target)) { | ||
result = (Message<?>) invocation.proceed(); | ||
} | ||
return afterReceive(result, target); | ||
} | ||
|
||
/** | ||
* Subclasses can take actions based on the result of the {@link MethodInvocation#proceed()}; e.g. | ||
* adjust the {@code trigger}. The message can also be replaced with a new one. | ||
* @param result the received message. | ||
* @param source the source of the message to receive. | ||
* @return a message to continue to process the result, null to discard whatever | ||
* the {@link MethodInvocation#proceed()} returned. | ||
*/ | ||
@Nullable | ||
Message<?> afterReceive(@Nullable Message<?> result, Object source); | ||
|
||
} |
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
80 changes: 80 additions & 0 deletions
80
...c/main/java/org/springframework/integration/aop/SimpleActiveIdleReceiveMessageAdvice.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,80 @@ | ||
/* | ||
* Copyright 2020 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.aop; | ||
|
||
import java.time.Duration; | ||
|
||
import org.springframework.integration.util.DynamicPeriodicTrigger; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
A simple advice that polls at one rate when messages exist and another when | ||
* there are no messages. | ||
* | ||
* @author Gary Russell | ||
* @author Artem Bilan | ||
* | ||
* @since 5.3 | ||
* | ||
* @see DynamicPeriodicTrigger | ||
*/ | ||
public class SimpleActiveIdleReceiveMessageAdvice implements ReceiveMessageAdvice { | ||
|
||
private final DynamicPeriodicTrigger trigger; | ||
|
||
private volatile Duration idlePollPeriod; | ||
|
||
private volatile Duration activePollPeriod; | ||
|
||
public SimpleActiveIdleReceiveMessageAdvice(DynamicPeriodicTrigger trigger) { | ||
Assert.notNull(trigger, "'trigger' must not be null"); | ||
this.trigger = trigger; | ||
this.idlePollPeriod = trigger.getDuration(); | ||
this.activePollPeriod = trigger.getDuration(); | ||
} | ||
|
||
/** | ||
* Set the poll period when messages are not returned. Defaults to the | ||
* trigger's period. | ||
* @param idlePollPeriod the period in milliseconds. | ||
*/ | ||
public void setIdlePollPeriod(long idlePollPeriod) { | ||
this.idlePollPeriod = Duration.ofMillis(idlePollPeriod); | ||
} | ||
|
||
/** | ||
* Set the poll period when messages are returned. Defaults to the | ||
* trigger's period. | ||
* @param activePollPeriod the period in milliseconds. | ||
*/ | ||
public void setActivePollPeriod(long activePollPeriod) { | ||
this.activePollPeriod = Duration.ofMillis(activePollPeriod); | ||
} | ||
|
||
@Override | ||
public Message<?> afterReceive(Message<?> result, Object source) { | ||
if (result == null) { | ||
this.trigger.setDuration(this.idlePollPeriod); | ||
} | ||
else { | ||
this.trigger.setDuration(this.activePollPeriod); | ||
} | ||
return result; | ||
} | ||
|
||
} |
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.
Wouldn't something like
Advisable<T>
be better thanObject
?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.
Well, in most cases it is really about both
MessageSource
andPollableChannel
, so even with generic argument it still going to be anObject
😄I thought about a
Supplier
for both those interfaces, but this is not what we are going to use with thereceive(timeout)
.Therefore I'm OK to stick with an
Object
for asource
argument.