轮询对象的S3时等待池中的连接超时

时间:2019-01-05 21:06:28

标签: java amazon-s3 streaming spring-integration spring-integration-aws

我正在使用后端服务,该服务使用spring aws集成定期轮询S3存储桶并处理来自S3的被轮询对象。下面是它的实现

@Configuration
@EnableIntegration
@IntegrationComponentScan
@EnableAsync
public class S3PollerConfiguration {

    //private static final Logger log = (Logger) LoggerFactory.getLogger(S3PollerConfiguration.class);

    @Value("${amazonProperties.bucketName}")
    private String bucketName;

    @Bean
    @InboundChannelAdapter(value = "s3FilesChannel", poller = @Poller(fixedDelay = "5"))
    public MessageSource<InputStream> s3InboundStreamingMessageSource() {    
        S3StreamingMessageSource messageSource = new S3StreamingMessageSource(template());
        messageSource.setRemoteDirectory(bucketName);   
        return messageSource;
    }

    @Bean
    public S3RemoteFileTemplate template() {
        return new S3RemoteFileTemplate(new S3SessionFactory(thumbnailGeneratorService.getImagesS3Client()));
    }

    @Bean
    public PollableChannel s3FilesChannel() {
        return new QueueChannel();
    }

    @Bean
    IntegrationFlow fileReadingFlow() throws IOException {
        return IntegrationFlows
                .from(s3InboundStreamingMessageSource(),
                        e -> e.poller(p -> p.fixedDelay(10, TimeUnit.SECONDS)))
                .handle(Message.class, (payload, header) -> processS3Object(payload.getHeaders(), payload.getPayload()))
                .get();
    }
}

我从对象上传的S3中获取消息,并且能够使用作为消息有效负载的一部分接收的输入流来处理它。但是我在这里面临的问题是,收到少量消息后,我收到了“超时等待来自池的连接”异常

2019-01-06 02:19:06.156 ERROR 11322 --- [ask-scheduler-5] o.s.integration.handler.LoggingHandler   : org.springframework.messaging.MessagingException: Failed to execute on session; nested exception is com.amazonaws.SdkClientException: Unable to execute HTTP request: Timeout waiting for connection from pool
    at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:445)
    at org.springframework.integration.file.remote.RemoteFileTemplate.list(RemoteFileTemplate.java:405)
    at org.springframework.integration.file.remote.AbstractRemoteFileStreamingMessageSource.listFiles(AbstractRemoteFileStreamingMessageSource.java:194)
    at org.springframework.integration.file.remote.AbstractRemoteFileStreamingMessageSource.poll(AbstractRemoteFileStreamingMessageSource.java:180)
    at org.springframework.integration.aws.inbound.S3StreamingMessageSource.poll(S3StreamingMessageSource.java:70)
    at org.springframework.integration.file.remote.AbstractRemoteFileStreamingMessageSource.doReceive(AbstractRemoteFileStreamingMessageSource.java:153)
    at org.springframework.integration.endpoint.AbstractMessageSource.receive(AbstractMessageSource.java:155)
    at org.springframework.integration.endpoint.SourcePollingChannelAdapter.receiveMessage(SourcePollingChannelAdapter.java:236)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:250)

我知道问题与未关闭打开的S3Object(如此处https://github.com/aws/aws-sdk-java/issues/1405)有关,因此我实现了关闭作为消息有效负载一部分接收到的S3Object的输入流。但这并不能解决问题,我不断收到例外。有人可以帮我解决此问题吗?

1 个答案:

答案 0 :(得分:1)

您的问题仍然是在配置中将消息传递注释声明与Java DSL混合在一起。

看起来像在fileReadingFlow中关闭了代码InputStream方法中的processS3Object(),但是对InputStream产生的@InboundChannelAdapter(value = "s3FilesChannel", poller = @Poller(fixedDelay = "5"))却不做任何事情。 为什么要把它放在第一位呢?如果不使用该代码,什么使您保留该代码?

S3StreamingMessageSource一直两次被@InboundChannelAdapterIntegrationFlows.from()轮询。

您只需要从@InboundChannelAdapter bean定义中删除该S3StreamingMessageSource,仅此而已。

请阅读更多参考手册,以确定产生此类注释的原因以及在使用Java DSL时不需要它的方式:

https://docs.spring.io/spring-integration/reference/html/configuration.html#_using_the_literal_inboundchanneladapter_literal_annotation

https://docs.spring.io/spring-integration/reference/html/java-dsl.html#java-dsl-inbound-adapters