|
| 1 | +/* |
| 2 | + * Copyright 2025-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.awspring.cloud.sqs.integration; |
| 18 | + |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.concurrent.CompletableFuture; |
| 21 | +import java.util.concurrent.ExecutionException; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | +import java.util.concurrent.TimeoutException; |
| 24 | + |
| 25 | +import io.awspring.cloud.sqs.listener.SqsHeaders; |
| 26 | +import io.awspring.cloud.sqs.operations.SendResult; |
| 27 | +import io.awspring.cloud.sqs.operations.SqsAsyncOperations; |
| 28 | + |
| 29 | +import org.springframework.expression.EvaluationContext; |
| 30 | +import org.springframework.expression.Expression; |
| 31 | +import org.springframework.expression.common.LiteralExpression; |
| 32 | +import org.springframework.integration.MessageTimeoutException; |
| 33 | +import org.springframework.integration.expression.ExpressionUtils; |
| 34 | +import org.springframework.integration.expression.ValueExpression; |
| 35 | +import org.springframework.integration.handler.AbstractMessageProducingHandler; |
| 36 | +import org.springframework.messaging.Message; |
| 37 | +import org.springframework.util.Assert; |
| 38 | + |
| 39 | +/** |
| 40 | + * The {@link AbstractMessageProducingHandler} implementation for the Amazon SQS. |
| 41 | + * All the logic based on the {@link SqsAsyncOperations#sendAsync(String, Message)} |
| 42 | + * or {@link SqsAsyncOperations#sendManyAsync(String, Collection)} if the request message's payload |
| 43 | + * is a collection of {@link Message} instances. |
| 44 | + * <p> |
| 45 | + * All the SQS-specific message attributes have to be provided in the respective message headers |
| 46 | + * via {@link SqsHeaders.MessageSystemAttributes} constant values or with the {@link SqsAsyncOperations}. |
| 47 | + * <p> |
| 48 | + * This {@link AbstractMessageProducingHandler} produces a reply only in the {@link #isAsync()} mode. |
| 49 | + * For a single request message the {@link SendResult} is converted to the reply message with respective headers. |
| 50 | + * The {@link SendResult.Batch} is sent as a reply message's payload as is. |
| 51 | + * |
| 52 | + * @author Artem Bilan |
| 53 | + * |
| 54 | + * @since 4.0 |
| 55 | + * |
| 56 | + * @see SqsAsyncOperations#sendAsync |
| 57 | + * @see SqsAsyncOperations#sendManyAsync |
| 58 | + * @see SqsHeaders.MessageSystemAttributes |
| 59 | + */ |
| 60 | +public class SqsMessageHandler extends AbstractMessageProducingHandler { |
| 61 | + |
| 62 | + public static final long DEFAULT_SEND_TIMEOUT = 10000; |
| 63 | + |
| 64 | + private final SqsAsyncOperations sqsAsyncOperations; |
| 65 | + |
| 66 | + private Expression queueExpression; |
| 67 | + |
| 68 | + private EvaluationContext evaluationContext; |
| 69 | + |
| 70 | + private Expression sendTimeoutExpression = new ValueExpression<>(DEFAULT_SEND_TIMEOUT); |
| 71 | + |
| 72 | + public SqsMessageHandler(SqsAsyncOperations sqsAsyncOperations) { |
| 73 | + this.sqsAsyncOperations = sqsAsyncOperations; |
| 74 | + } |
| 75 | + |
| 76 | + public void setQueue(String queue) { |
| 77 | + setQueueExpression(new LiteralExpression(queue)); |
| 78 | + } |
| 79 | + |
| 80 | + public void setQueueExpressionString(String queueExpression) { |
| 81 | + setQueueExpression(EXPRESSION_PARSER.parseExpression(queueExpression)); |
| 82 | + } |
| 83 | + |
| 84 | + public void setQueueExpression(Expression queueExpression) { |
| 85 | + this.queueExpression = queueExpression; |
| 86 | + } |
| 87 | + |
| 88 | + public void setSendTimeout(long sendTimeout) { |
| 89 | + setSendTimeoutExpression(new ValueExpression<>(sendTimeout)); |
| 90 | + } |
| 91 | + |
| 92 | + public void setSendTimeoutExpressionString(String sendTimeoutExpression) { |
| 93 | + setSendTimeoutExpression(EXPRESSION_PARSER.parseExpression(sendTimeoutExpression)); |
| 94 | + } |
| 95 | + |
| 96 | + public void setSendTimeoutExpression(Expression sendTimeoutExpression) { |
| 97 | + Assert.notNull(sendTimeoutExpression, "'sendTimeoutExpression' must not be null"); |
| 98 | + this.sendTimeoutExpression = sendTimeoutExpression; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + protected void onInit() { |
| 103 | + Assert.notNull(this.queueExpression, "The SQS queue must be provided."); |
| 104 | + super.onInit(); |
| 105 | + this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory()); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + protected boolean shouldCopyRequestHeaders() { |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + @SuppressWarnings("unchecked") |
| 115 | + protected void handleMessageInternal(Message<?> message) { |
| 116 | + String queueName = this.queueExpression.getValue(this.evaluationContext, message, String.class); |
| 117 | + Assert.hasText(queueName, "The 'queueExpression' must not evaluate to empty String."); |
| 118 | + CompletableFuture<?> resultFuture; |
| 119 | + if (message.getPayload() instanceof Collection<?> collection) { |
| 120 | + Assert.notEmpty(collection, "The payload with a collection of messages must not be empty."); |
| 121 | + Object next = collection.iterator().next(); |
| 122 | + Assert.isInstanceOf(Message.class, next, |
| 123 | + "The payload with a collection of messages must contain 'Message' instances only."); |
| 124 | + Collection<Message<Object>> messages = (Collection<Message<Object>>) collection; |
| 125 | + |
| 126 | + resultFuture = this.sqsAsyncOperations.sendManyAsync(queueName, messages) |
| 127 | + .thenApply((batchResult) -> getMessageBuilderFactory().withPayload(batchResult).build()); |
| 128 | + } |
| 129 | + else { |
| 130 | + resultFuture = this.sqsAsyncOperations.sendAsync(queueName, message) |
| 131 | + .thenApply((sendResult) -> |
| 132 | + getMessageBuilderFactory() |
| 133 | + .fromMessage(sendResult.message()) |
| 134 | + .setHeader(SqsHeaders.SQS_QUEUE_NAME_HEADER, sendResult.endpoint()) |
| 135 | + .setHeader(SqsHeaders.MessageSystemAttributes.MESSAGE_ID, sendResult.messageId()) |
| 136 | + .copyHeaders(sendResult.additionalInformation()) |
| 137 | + .build()); |
| 138 | + } |
| 139 | + |
| 140 | + if (isAsync()) { |
| 141 | + sendOutputs(resultFuture, message); |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + Long sendTimeout = this.sendTimeoutExpression.getValue(this.evaluationContext, message, Long.class); |
| 146 | + if (sendTimeout == null || sendTimeout < 0) { |
| 147 | + try { |
| 148 | + resultFuture.get(); |
| 149 | + } |
| 150 | + catch (InterruptedException | ExecutionException ex) { |
| 151 | + throw new IllegalStateException(ex); |
| 152 | + } |
| 153 | + } |
| 154 | + else { |
| 155 | + try { |
| 156 | + resultFuture.get(sendTimeout, TimeUnit.MILLISECONDS); |
| 157 | + } |
| 158 | + catch (TimeoutException te) { |
| 159 | + throw new MessageTimeoutException(message, "Timeout waiting for response from Amazon SQS", te); |
| 160 | + } |
| 161 | + catch (InterruptedException ex) { |
| 162 | + Thread.currentThread().interrupt(); |
| 163 | + throw new IllegalStateException(ex); |
| 164 | + } |
| 165 | + catch (ExecutionException ex) { |
| 166 | + throw new IllegalStateException(ex); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments