Skip to content

Commit a45e735

Browse files
authored
Add manual container instantiation sample (#788)
* docs: add manual ack sample * feat: add pr suggestions and comments * refactor: change method name * fix: change beans naming * refactor: apply pr suggestions * refactor: use specified class instead Object
1 parent fcc79c4 commit a45e735

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2013-2021 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.listener.SqsMessageListenerContainer;
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.SqsAsyncClient;
26+
27+
import java.util.UUID;
28+
29+
@Configuration
30+
public class SqsManualContainerInstantiationSample {
31+
32+
public static final String NEW_USER_QUEUE = "new-user-queue";
33+
34+
private static final Logger LOGGER = LoggerFactory.getLogger(SqsManualContainerInstantiationSample.class);
35+
36+
@Bean
37+
public ApplicationRunner sendMessageToQueueManualContainerInstantiation(SqsTemplate sqsTemplate) {
38+
LOGGER.info("Sending message");
39+
return args -> sqsTemplate.send(to -> to.queue(NEW_USER_QUEUE)
40+
.payload(new User(UUID.randomUUID(), "John"))
41+
);
42+
}
43+
44+
@Bean
45+
public SqsTemplate sqsTemplateManualContainerInstantiation(SqsAsyncClient sqsAsyncClient) {
46+
return SqsTemplate.builder()
47+
.sqsAsyncClient(sqsAsyncClient)
48+
.build();
49+
}
50+
51+
@Bean
52+
SqsMessageListenerContainer<User> sqsMessageListenerContainer(SqsAsyncClient sqsAsyncClient) {
53+
return new SqsMessageListenerContainer.Builder<User>()
54+
.sqsAsyncClient(sqsAsyncClient)
55+
.queueNames(NEW_USER_QUEUE)
56+
.messageListener((message) -> {
57+
LOGGER.info("Received message {}", message);
58+
})
59+
.build();
60+
}
61+
62+
public record User(UUID id, String name) {
63+
}
64+
}
65+

0 commit comments

Comments
 (0)