|
1 | 1 | /*
|
2 |
| - * Copyright 2016 the original author or authors. |
| 2 | + * Copyright 2016-2018 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
27 | 27 | import org.springframework.integration.expression.FunctionExpression;
|
28 | 28 | import org.springframework.integration.handler.DelayHandler;
|
29 | 29 | import org.springframework.integration.store.MessageGroupStore;
|
| 30 | +import org.springframework.integration.transaction.TransactionInterceptorBuilder; |
30 | 31 | import org.springframework.messaging.Message;
|
| 32 | +import org.springframework.messaging.MessageChannel; |
| 33 | +import org.springframework.messaging.MessageHandler; |
| 34 | +import org.springframework.messaging.support.ErrorMessage; |
| 35 | +import org.springframework.transaction.PlatformTransactionManager; |
| 36 | +import org.springframework.transaction.interceptor.DefaultTransactionAttribute; |
| 37 | +import org.springframework.transaction.interceptor.TransactionInterceptor; |
31 | 38 | import org.springframework.util.Assert;
|
32 | 39 |
|
33 | 40 | /**
|
34 | 41 | * A {@link ConsumerEndpointSpec} for a {@link DelayHandler}.
|
35 | 42 | *
|
36 | 43 | * @author Artem Bilan
|
| 44 | + * @author Gary Russell |
37 | 45 | *
|
38 | 46 | * @since 5.0
|
39 | 47 | */
|
40 | 48 | public final class DelayerEndpointSpec extends ConsumerEndpointSpec<DelayerEndpointSpec, DelayHandler> {
|
41 | 49 |
|
42 |
| - private final List<Advice> delayedAdvice = new LinkedList<Advice>(); |
| 50 | + private final List<Advice> delayedAdvice = new LinkedList<>(); |
43 | 51 |
|
44 | 52 | DelayerEndpointSpec(DelayHandler delayHandler) {
|
45 | 53 | super(delayHandler);
|
@@ -97,6 +105,105 @@ public DelayerEndpointSpec delayExpression(String delayExpression) {
|
97 | 105 | return this;
|
98 | 106 | }
|
99 | 107 |
|
| 108 | + /** |
| 109 | + * Set a message channel to which an {@link ErrorMessage} will be sent if sending the |
| 110 | + * released message fails. If the error flow returns normally, the release is complete. |
| 111 | + * If the error flow throws an exception, the release will be re-attempted. |
| 112 | + * If there is a transaction advice on the release task, the error flow is called |
| 113 | + * within the transaction. |
| 114 | + * @param channel the channel. |
| 115 | + * @return the endpoint spec. |
| 116 | + * @see #maxAttempts(int) |
| 117 | + * @see #retryDelay(long) |
| 118 | + * @since 5.0.8 |
| 119 | + */ |
| 120 | + public DelayerEndpointSpec delayedMessageErrorChannel(MessageChannel channel) { |
| 121 | + this.handler.setDelayedMessageErrorChannel(channel); |
| 122 | + return this; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Set a message channel name to which an {@link ErrorMessage} will be sent if sending |
| 127 | + * the released message fails. If the error flow returns normally, the release is |
| 128 | + * complete. If the error flow throws an exception, the release will be re-attempted. |
| 129 | + * If there is a transaction advice on the release task, the error flow is called |
| 130 | + * within the transaction. |
| 131 | + * @param channel the channel name. |
| 132 | + * @return the endpoint spec. |
| 133 | + * @see #maxAttempts(int) |
| 134 | + * @see #retryDelay(long) |
| 135 | + * @since 5.0.8 |
| 136 | + */ |
| 137 | + public DelayerEndpointSpec delayedMessageErrorChannel(String channel) { |
| 138 | + this.handler.setDelayedMessageErrorChannelName(channel); |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Set the maximum number of release attempts for when message release fails. |
| 144 | + * Default {@value DelayHandler#DEFAULT_MAX_ATTEMPTS}. |
| 145 | + * @param maxAttempts the max attempts. |
| 146 | + * @return the endpoint spec. |
| 147 | + * @see #retryDelay(long) |
| 148 | + * @since 5.0.8 |
| 149 | + */ |
| 150 | + public DelayerEndpointSpec maxAttempts(int maxAttempts) { |
| 151 | + this.handler.setMaxAttempts(maxAttempts); |
| 152 | + return this; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Set an additional delay to apply when retrying after a release failure. |
| 157 | + * Default {@value DelayHandler#DEFAULT_RETRY_DELAY}. |
| 158 | + * @param retryDelay the retry delay. |
| 159 | + * @return the endpoint spec. |
| 160 | + * @see #maxAttempts(int) |
| 161 | + * @since 5.0.8 |
| 162 | + */ |
| 163 | + public DelayerEndpointSpec retryDelay(long retryDelay) { |
| 164 | + this.handler.setRetryDelay(retryDelay); |
| 165 | + return this; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Specify a {@link TransactionInterceptor} {@link Advice} with default |
| 170 | + * {@link PlatformTransactionManager} and {@link DefaultTransactionAttribute} for the |
| 171 | + * {@link MessageHandler}. |
| 172 | + * @return the spec. |
| 173 | + * @since 5.0.8 |
| 174 | + */ |
| 175 | + public DelayerEndpointSpec transactionalRelease() { |
| 176 | + TransactionInterceptor transactionInterceptor = new TransactionInterceptorBuilder().build(); |
| 177 | + this.componentsToRegister.put(transactionInterceptor, null); |
| 178 | + return delayedAdvice(transactionInterceptor); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Specify a {@link TransactionInterceptor} {@link Advice} for the {@link MessageHandler}. |
| 183 | + * @param transactionInterceptor the {@link TransactionInterceptor} to use. |
| 184 | + * @return the spec. |
| 185 | + * @see TransactionInterceptorBuilder |
| 186 | + * @since 5.0.8 |
| 187 | + */ |
| 188 | + public DelayerEndpointSpec transactionalRelease(TransactionInterceptor transactionInterceptor) { |
| 189 | + return delayedAdvice(transactionInterceptor); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Specify a {@link TransactionInterceptor} {@link Advice} with the provided |
| 194 | + * {@code PlatformTransactionManager} and default {@link DefaultTransactionAttribute} |
| 195 | + * for the {@link MessageHandler}. |
| 196 | + * @param transactionManager the {@link PlatformTransactionManager} to use. |
| 197 | + * @return the spec. |
| 198 | + * @since 5.0.8 |
| 199 | + */ |
| 200 | + public DelayerEndpointSpec transactionalRelease(PlatformTransactionManager transactionManager) { |
| 201 | + return transactionalRelease( |
| 202 | + new TransactionInterceptorBuilder() |
| 203 | + .transactionManager(transactionManager) |
| 204 | + .build()); |
| 205 | + } |
| 206 | + |
100 | 207 | /**
|
101 | 208 | * Specify the function to determine delay value against {@link Message}.
|
102 | 209 | * Typically used with a Java 8 Lambda expression:
|
|
0 commit comments