Skip to content

Commit 557fa6b

Browse files
mcruzdevMatheus Cruzm
andauthored
Add SQS sample - listen from multiple queues (#742)
Relates to #670 Co-authored-by: Matheus Cruzm <[email protected]>
1 parent 07a57c8 commit 557fa6b

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2013-2023 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+
package io.awspring.cloud.sqs.sample;
17+
18+
import io.awspring.cloud.sqs.annotation.SqsListener;
19+
import io.awspring.cloud.sqs.operations.SqsTemplate;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
import org.springframework.boot.ApplicationRunner;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import software.amazon.awssdk.services.sqs.model.Message;
26+
27+
import java.util.UUID;
28+
29+
@Configuration
30+
public class SpringSqsListenMultipleQueues {
31+
32+
private static final Logger LOGGER = LoggerFactory.getLogger(SpringSqsListenMultipleQueues.class);
33+
34+
private static final String ORDER_QUEUE = "order-queue";
35+
private static final String WITHDRAWAL_QUEUE = "withdrawal-queue";
36+
37+
@SqsListener(queueNames = { ORDER_QUEUE, WITHDRAWAL_QUEUE })
38+
void listen(Message message) {
39+
LOGGER.info("Received message {}", message);
40+
}
41+
42+
@Bean
43+
public ApplicationRunner sendMessageToQueues(SqsTemplate sqsTemplate) {
44+
return args -> {
45+
sqsTemplate.sendAsync(ORDER_QUEUE, new OrderMessage(UUID.randomUUID(), "[email protected]"));
46+
47+
sqsTemplate.sendAsync(WITHDRAWAL_QUEUE,
48+
new WithdrawalMessage(UUID.randomUUID(), "Mary", "[email protected]"));
49+
};
50+
}
51+
52+
private record WithdrawalMessage(UUID transaction, String customerName, String customerEmail) {
53+
}
54+
55+
private record OrderMessage(UUID orderId, String customerEmail) {
56+
}
57+
58+
}

0 commit comments

Comments
 (0)