如果amazonsqsasync无效,则阻止加载sqslistener和AWSCOConfiguration

gdx19jrr  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(527)

我有一个mysqslistener和一个amazonsqsconfig类(参见下面的课程)
如果由于任何原因凭据不正确,我会得到以下例外:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'simpleMessageListenerContainer' defined in class path resource [org/springframework/cloud/aws/messaging/config/annotation/SqsConfiguration.class]: Invocation of init method failed; nested exception is com.amazonaws.services.sqs.model.AmazonSQSException: The security token included in the request is invalid. (Service: AmazonSQS; Status Code: 403; Error Code: InvalidClientTokenId; Request ID: 729e468d-0c99-56d9-a1db-1ed18e322319)

在这种情况下,我希望停止加载sqslistener和amazonsqsconfig,而不是使整个应用程序崩溃。
有没有办法在加载bean之前测试凭据?
谢谢!

@Slf4j
@Component
@ConditionalOnExpression("${aws.sqs.enabled:false}")
public class MySQSListener {

  @SqsListener(value = "${aws.sqs.queueName}", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
  public void receiveMessage(MyCustomObject customObject, Acknowledgment acknowledgment) {
     //process custom object
  }
}

和sqsconfig:

@Slf4j
@EnableSqs
@Configuration
@ConditionalOnExpression("${aws.sqs.enabled:false}")
public class AmazonSQSConfig {

  @Bean
  public AWSCredentialsProvider awsCredentialsProvider(
      @Value("${aws.sqs.credentials.accessKey:\"UNKNOWN\"}") final String awsAccessKey,
      @Value("${aws.sqs.credentials.secretKey:\"UNKNOWN\"}") final String awsSecretKey) {
    return new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKey, awsSecretKey));
  }

  @Bean
  public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(
      AmazonSQSAsync amazonSQSAsync,
      @Value("${aws.sqs.maxNumberOfMessages:10}") int maxNumberOfMessages,
      @Value("${aws.sqs.waitTimeOut:20}") int waitTimeOut,
      @Value("${aws.sqs.visibilityTimeOut:300}") int visibilityTimeOut) {
    SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
    factory.setAmazonSqs(amazonSQSAsync);
    factory.setMaxNumberOfMessages(maxNumberOfMessages);
    factory.setWaitTimeOut(waitTimeOut);
    factory.setVisibilityTimeout(visibilityTimeOut);
    factory.setAutoStartup(true);
    return factory;
  }

  @Bean
  @Primary
  public AmazonSQSAsync amazonSqsAsync(AWSCredentialsProvider awsCredentialsProvider,
                                       @Value("${aws.sqs.local.endpointOverride:#{null}}")
                                       final String endpointOverride) {
    AmazonSQSAsyncClientBuilder amazonSQSAsyncClientBuilder = AmazonSQSAsyncClientBuilder.standard();
    amazonSQSAsyncClientBuilder.withCredentials(awsCredentialsProvider);
    if (endpointOverride != null) {
      LOG.info(String.format("Found endpoint override in application properties, new endpoint: %s", endpointOverride));
      amazonSQSAsyncClientBuilder.withEndpointConfiguration(
          new AwsClientBuilder.EndpointConfiguration(endpointOverride, Regions.GovCloud.getName()));
    } else {
      amazonSQSAsyncClientBuilder.withRegion(Regions.GovCloud);
    }
    return amazonSQSAsyncClientBuilder.build();
  }

  @Bean
  public QueueMessageHandlerFactory queueMessageHandlerFactory(MessageConverter messageConverter,
                                                               AmazonSQSAsync amazonSQSAsync) {
    QueueMessageHandlerFactory factory = new QueueMessageHandlerFactory();
    factory.setAmazonSqs(amazonSQSAsync);
    AcknowledgmentHandlerMethodArgumentResolver acknowledgmentResolver =
        new AcknowledgmentHandlerMethodArgumentResolver("Acknowledgment");
    PayloadArgumentResolver payloadArgumentResolver = new PayloadArgumentResolver(messageConverter);
    factory.setArgumentResolvers(Arrays.asList(acknowledgmentResolver, payloadArgumentResolver));
    return factory;
  }

  @Bean
  public QueueMessagingTemplate queueMessagingTemplate(AmazonSQSAsync amazonSQSAsync) {
    return new QueueMessagingTemplate(amazonSQSAsync);
  }

  @Bean
  protected MessageConverter messageConverter(ObjectMapper objectMapper) {
    MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
    converter.setObjectMapper(objectMapper);
    converter.setStrictContentTypeMatch(false);
    converter.setSerializedPayloadClass(String.class);
    return converter;
  }
}
hgb9j2n6

hgb9j2n61#

可以通过添加SimpleMessageListenerContainerFactoryBean的条件创建(@conditional)来实现这一点。在该条件实现中,您可以检查此连接。让这些豆子自动连线需要假

相关问题