gpt4 book ai didi

java - SqsListener 没有为本地堆栈队列运行?

转载 作者:行者123 更新时间:2023-11-30 10:08:20 26 4
gpt4 key购买 nike

我是 spring boot 的新手,正在尝试使用 sqs 监听器轮询本地堆栈中的测试队列。我可以将消息推送到我的本地堆栈队列中。但是,然后我想轮询同一个队列并记录消息的内容。但是,我没有通过 sqs lister 将任何消息记录到控制台?

application-local.properties

cloud.aws.region=us-east-1
cloud.local.sqs=http://localstack:4576
cloud.local.s3=http://localstack:4572

app.sqs.maxmessages=1
app.sqs.input=http://localstack:4576/queue/test-queue
app.sqs.output=http://localstack:4576/queue/test-queue

应用程序 Controller

@Log4j2
@RestController
public class AppController {

private AmazonS3 s3;

private SQSOutput output;

@Autowired
public AppController(AmazonS3 s3, SQSOutput output) {

this.s3 = s3;
this.output = output;
}

@RequestMapping("/send")
public Map<String, String> sendMessage() {
output.send("Test Message!");

Map<String, String> response = new HashMap<>();
response.put("message", "Message sent!");
return response;
}

@SqsListener(value = "${app.sqs.input}", deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
public void getMessage(String message) {

log.info("Received message: " + message);
}

}

SetupBean

@Component
public class SetupBeans {

@Value("${cloud.aws.region}")
private String region;

@Value("${cloud.local.s3}")
private String localCloudS3;

@Value("${cloud.local.sqs}")
private String localCloudSQS;

@Value("${app.sqs.output}")
private String outputUrl;

@Value("${app.sqs.maxmessages}")
private int maxMessages;

@Bean
@Primary
private AWSCredentialsProvider credProvider() {
return DefaultAWSCredentialsProviderChain.getInstance();
}

@Bean
@Primary
public AmazonS3 amazonS3() {
AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder
.EndpointConfiguration(localCloudS3, region);
return AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(endpointConfiguration)
.build();
}

@Bean
@Primary
private AmazonSQSAsync amazonSQSAsync() {
AmazonSQSAsyncClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder
.EndpointConfiguration(localCloudSQS, region);
return AmazonSQSAsyncClientBuilder.standard()
.withEndpointConfiguration(endpointConfiguration)
.build();
}

@Bean
private QueueMessagingTemplate queueMessagingTemplate() {

return new QueueMessagingTemplate(amazonSQSAsync());
}

@Bean
public QueueMessageHandlerFactory queueMessageHandlerFactory() {
QueueMessageHandlerFactory factory = new QueueMessageHandlerFactory();
MappingJackson2MessageConverter messageConverter = new MappingJackson2MessageConverter();

// set strict content type match to false
messageConverter.setStrictContentTypeMatch(false);
factory.setArgumentResolvers(Collections.<HandlerMethodArgumentResolver>singletonList(new PayloadArgumentResolver(messageConverter)));
return factory;
}

@Bean
public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(AmazonSQSAsync amazonSQS){
SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
factory.setAmazonSqs(amazonSQS);
factory.setMaxNumberOfMessages(maxMessages);
return factory;
}

@Bean
private QueueMessageChannel getQueueMessageChannel() {
return new QueueMessageChannel(amazonSQSAsync(), outputUrl);
}

@Bean
public SQSOutput getSQSOutput() {
return new SQSOutput(queueMessagingTemplate(), getQueueMessageChannel());
}
}

最佳答案

我正在使用 org.springframework.cloud:spring-cloud-aws-messaging 但需要使用 org.springframework.cloud:spring-cloud-starter-aws-messaging.

关于java - SqsListener 没有为本地堆栈队列运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53780563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com