diff --git a/spring-integration-core/src/main/java/org/springframework/integration/handler/DelayHandler.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/DelayHandler.java index 199f862381..51c0b0f177 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/handler/DelayHandler.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/DelayHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -140,9 +140,10 @@ public class DelayHandler extends AbstractReplyProducingMessageHandler implement } /** - * Specify the {@link Expression} that should be checked for a delay period - * (in milliseconds) or a Date to delay until. If this property is set, the - * result of the expression evaluation will take precedence over this handler's default delay. + * Specify the {@link Expression} that should be checked for a delay period (in + * milliseconds) or a Date to delay until. If this property is set, the result of the + * expression evaluation (if not null) will take precedence over this handler's + * default delay. * * @param delayExpression The delay expression. */ @@ -150,6 +151,19 @@ public class DelayHandler extends AbstractReplyProducingMessageHandler implement this.delayExpression = delayExpression; } + /** + * Specify the {@code Expression} that should be checked for a delay period (in + * milliseconds) or a Date to delay until. If this property is set, the result of the + * expression evaluation (if not null) will take precedence over this handler's + * default delay. + * + * @param delayExpression The delay expression. + * @since 5.0 + */ + public void setDelayExpressionString(String delayExpression) { + this.delayExpression = EXPRESSION_PARSER.parseExpression(delayExpression); + } + /** * Specify whether {@code Exceptions} thrown by {@link #delayExpression} evaluation should be * ignored (only logged). In this case case the delayer will fall back to the diff --git a/src/reference/asciidoc/delayer.adoc b/src/reference/asciidoc/delayer.adoc index 51f3c6bd38..2ab2a19999 100644 --- a/src/reference/asciidoc/delayer.adoc +++ b/src/reference/asciidoc/delayer.adoc @@ -12,7 +12,7 @@ On the contrary, in the typical case a thread pool will be used for the actual e Below you will find several examples of configuring a Delayer. [[delayer-namespace]] -==== Configuring Delayer +==== Configuring a Delayer The `` element is used to delay the Message flow between two Message Channels. As with the other endpoints, you can provide the 'input-channel' and 'output-channel' attributes, but the delayer also has 'default-delay' and 'expression' attributes (and 'expression' sub-element) that are used to determine the number of milliseconds that each Message should be delayed. @@ -34,6 +34,38 @@ In the example above, the 3 second delay would only apply when the expression ev If you only want to apply a delay to Messages that have a valid result of the expression evaluation, then you can use a 'default-delay' of 0 (the default). For any Message that has a delay of 0 (or less), the Message will be sent immediately, on the calling Thread. +The java configuration equivalent of the second example is: + +[source, java] +---- +@ServiceActivator(inputChannel = "input") +@Bean +public DelayHandler delayer() { + DelayHandler handler = new DelayHandler("delayer.messageGroupId"); + handler.setDefaultDelay(3_000L); + handler.setDelayExpressionString("headers['delay']"); + handler.setOutputChannelName("output"); + return handler; +} +---- + +and with the Java DSL: + +[source, java] +---- +@Bean +public IntegrationFlow flow() { + return IntegrationFlows.from("input") + .delay("delayer.messageGroupId", d -> d + .defaultDelay(3_000L) + .delayExpression("headers['delay']")) + .channel("output") + .get(); +} +---- + +NOTE: The XML parser uses a message group id `.messageGroupId`. + TIP: The delay handler supports expression evaluation results that represent an interval in milliseconds (any Object whose `toString()` method produces a value that can be parsed into a Long) as well as `java.util.Date` instances representing an absolute time. In the first case, the milliseconds will be counted from the current time (e.g. a value of 5000 would delay the Message for at least 5 seconds from the time it is received by the Delayer). @@ -87,7 +119,7 @@ By default it uses an `org.springframework.scheduling.support.TaskUtils$LoggingE You might want to consider using an `org.springframework.integration.channel.MessagePublishingErrorHandler`, which sends an `ErrorMessage` into an `error-channel`, either from the failed Message's header or into the default `error-channel`. [[delayer-message-store]] -==== Delayer and Message Store +==== Delayer and a Message Store The `DelayHandler` persists delayed Messages into the Message Group in the provided `MessageStore`. (The 'groupId' is based on required 'id' attribute of `` element.) A delayed message is removed from the `MessageStore` by the scheduled task just before the `DelayHandler` sends the Message to the `output-channel`.