software.amazon.awssdk.services.sqs.SqsAsyncClient Java Examples

The following examples show how to use software.amazon.awssdk.services.sqs.SqsAsyncClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AsyncSqsClientFactory.java    From dynein with Apache License 2.0 7 votes vote down vote up
private SqsAsyncClient getAsyncSQSClient(AsyncSqsClientConfiguration config) {
  if (config.getType().equals(AsyncSqsClientConfiguration.Type.PRODUCTION)) {
    SqsAsyncClientBuilder builder =
        SqsAsyncClient.builder()
            .region(Region.of(config.getRegion()))
            .endpointOverride(config.getEndpoint());

    OverrideConfiguration overrideConfig = config.getOverrideConfiguration();

    if (overrideConfig != null) {

      RetryPolicy retry = buildRetryPolicy(overrideConfig.getRetryPolicyConfiguration());
      ClientOverrideConfiguration clientOverrideConfiguration =
          ClientOverrideConfiguration.builder()
              .apiCallAttemptTimeout(Duration.ofMillis(overrideConfig.getApiCallAttemptTimeout()))
              .apiCallTimeout(Duration.ofMillis(overrideConfig.getApiCallTimeout()))
              .retryPolicy(retry)
              .build();

      builder = builder.overrideConfiguration(clientOverrideConfiguration);
    }
    return builder.build();
  } else {
    throw new IllegalArgumentException("Doesn't support this Type " + config.getType());
  }
}
 
Example #2
Source File: AsyncSqsClientFactory.java    From dynein with Apache License 2.0 7 votes vote down vote up
private AsyncSqsClient createClient(@NonNull final AsyncSqsClientConfiguration configuration) {
  if (configuration.getType().equals(AsyncSqsClientConfiguration.Type.MOCK)) {
    return new MockAsyncSqsClient();
  } else {
    SqsAsyncClient asyncSqsClient = getAsyncSQSClient(configuration);
    return new AsyncSqsClientImpl(
        asyncSqsClient,
        metrics,
        MoreExecutors.getExitingExecutorService(
            new ThreadPoolExecutor(
                configuration.getConsumerConcurrency(),
                configuration.getConsumerConcurrency(),
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(),
                new ThreadFactoryBuilder().setNameFormat("conveyor-executor-%d").build())),
        configuration.getMaxUrlCacheSize(),
        configuration.getReceiveWaitSeconds(),
        configuration.getBulkheadMaxWaitMillis(),
        configuration.getConsumerConcurrency());
  }
}
 
Example #3
Source File: AWSQueueUtils.java    From para with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a client instance for AWS SQS.
 * @return a client that talks to SQS
 */
public static SqsAsyncClient getClient() {
	if (sqsClient != null) {
		return sqsClient;
	}
	if (Config.getConfigBoolean("aws_sqs_local", false)) {
		sqsClient = SqsAsyncClient.builder().endpointOverride(URI.create(LOCAL_ENDPOINT)).
				credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("x", "x"))).build();
	} else {
		sqsClient = SqsAsyncClient.create();
	}

	Para.addDestroyListener(new DestroyListener() {
		public void onDestroy() {
			sqsClient.close();
		}
	});
	return sqsClient;
}
 
Example #4
Source File: AsyncSqsClientFactoryTest.java    From dynein with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsyncWithOverrideConfig() throws Exception {
  AsyncSqsClientConfiguration config =
      new AsyncSqsClientConfiguration(AsyncSqsClientConfiguration.Type.PRODUCTION);

  RetryPolicyConfiguration retryPolicyConfiguration = new RetryPolicyConfiguration();
  retryPolicyConfiguration.setCondition(RetryPolicyConfiguration.Condition.DEFAULT);
  retryPolicyConfiguration.setBackOff(RetryPolicyConfiguration.BackOff.EQUAL_JITTER);

  OverrideConfiguration overrideConfiguration = new OverrideConfiguration();
  overrideConfiguration.setApiCallAttemptTimeout(300);
  overrideConfiguration.setApiCallTimeout(25 * 1000);
  overrideConfiguration.setRetryPolicyConfiguration(retryPolicyConfiguration);

  config.setOverrideConfiguration(overrideConfiguration);

  AsyncSqsClient asyncClient = factory.create(config);
  assertTrue(asyncClient instanceof AsyncSqsClientImpl);

  SqsAsyncClient sqs = ((AsyncSqsClientImpl) asyncClient).getClient();
  assertNotNull(sqs);
}
 
Example #5
Source File: SqsMessageQueueReceiverEndpointIntegrationTest.java    From synapse with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    new SqsClientHelper(asyncClient).createChannelIfNotExists(SQS_INTEGRATION_TEST_CHANNEL);

    AwsProperties awsProperties = new AwsProperties();
    awsProperties.setRegion(Region.EU_CENTRAL_1.id());

    delegateAsyncClient = SqsAsyncClient.builder()
            .credentialsProvider(StaticCredentialsProvider.create(
                    AwsBasicCredentials.create("foobar", "foobar")))
            .overrideConfiguration(ClientOverrideConfiguration.builder()
                    .apiCallAttemptTimeout(Duration.ofMillis(200))
                    .retryPolicy(new SqsAutoConfiguration(awsProperties)
                            .sqsRetryPolicy()).build())
            .endpointOverride(URI.create("http://localhost:8080/"))
            .build();
}
 
Example #6
Source File: SqsMessageQueueReceiverEndpoint.java    From synapse with Apache License 2.0 6 votes vote down vote up
public SqsMessageQueueReceiverEndpoint(final @Nonnull String channelName,
                                       final @Nonnull MessageInterceptorRegistry interceptorRegistry,
                                       final @Nonnull SqsAsyncClient sqsAsyncClient,
                                       final @Nonnull ExecutorService executorService,
                                       final @Nullable ApplicationEventPublisher eventPublisher) {
    super(channelName, interceptorRegistry, eventPublisher);
    this.sqsAsyncClient = sqsAsyncClient;
    this.executorService = executorService;
    try {
        this.queueUrl = sqsAsyncClient.getQueueUrl(GetQueueUrlRequest
                .builder()
                .queueName(channelName)
                .overrideConfiguration(AwsRequestOverrideConfiguration.builder()
                        .apiCallAttemptTimeout(ofMillis(2000))
                        .build())
                .build())
                .get()
                .queueUrl();
    } catch (Exception e) {
        stopped.complete(null);
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example #7
Source File: SqsAutoConfiguration.java    From synapse with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(name = "sqsMessageQueueSenderEndpointFactory")
public MessageSenderEndpointFactory sqsMessageQueueSenderEndpointFactory(final MessageInterceptorRegistry registry,
                                                                         final SqsAsyncClient sqsAsyncClient,
                                                                         final @Value("${spring.application.name:Synapse Service}") String messageSenderName) {
    return new SqsMessageSenderEndpointFactory(registry, sqsAsyncClient);
}
 
Example #8
Source File: SqsMessageSenderEndpointFactoryTest.java    From synapse with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRegisterMessageInterceptor() {
    final SqsAsyncClient sqsAsyncClient = mock(SqsAsyncClient.class);
    when(sqsAsyncClient.getQueueUrl(any(GetQueueUrlRequest.class))).thenReturn(completedFuture(GetQueueUrlResponse.builder().queueUrl("http://example.com").build()));

    final MessageInterceptorRegistry registry = new MessageInterceptorRegistry();
    final MessageInterceptor interceptor = mock(MessageInterceptor.class);
    registry.register(MessageInterceptorRegistration.allChannelsWith(interceptor));

    final SqsMessageSenderEndpointFactory factory = new SqsMessageSenderEndpointFactory(registry, sqsAsyncClient);

    final MessageSenderEndpoint sender = factory.create("foo-stream", MessageFormat.V1);

    assertThat(sender.getInterceptorChain().getInterceptors(), contains(interceptor));
}
 
Example #9
Source File: SqsMessageSenderEndpointFactoryTest.java    From synapse with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotMatchMessageLogSelectors() {
    final MessageInterceptorRegistry interceptorRegistry = mock(MessageInterceptorRegistry.class);
    final SqsAsyncClient sqsAsyncClient = mock(SqsAsyncClient.class);

    final SqsMessageSenderEndpointFactory factory = new SqsMessageSenderEndpointFactory(new MessageInterceptorRegistry(), sqsAsyncClient);

    assertThat(factory.matches(MessageLog.class), is(false));
}
 
Example #10
Source File: SqsMessageSenderEndpointFactoryTest.java    From synapse with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldMatchMessageQueueSelectors() {
    final MessageInterceptorRegistry interceptorRegistry = mock(MessageInterceptorRegistry.class);
    final SqsAsyncClient sqsAsyncClient = mock(SqsAsyncClient.class);

    final SqsMessageSenderEndpointFactory factory = new SqsMessageSenderEndpointFactory(new MessageInterceptorRegistry(), sqsAsyncClient);

    assertThat(factory.matches(MessageQueue.class), is(true));
    assertThat(factory.matches(Sqs.class), is(true));
}
 
Example #11
Source File: SqsMessageSenderEndpointFactoryTest.java    From synapse with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBuildSqsMessageSender() {
    final SqsAsyncClient sqsAsyncClient = mock(SqsAsyncClient.class);
    when(sqsAsyncClient.getQueueUrl(any(GetQueueUrlRequest.class))).thenReturn(completedFuture(GetQueueUrlResponse.builder().queueUrl("http://example.com").build()));

    final SqsMessageSenderEndpointFactory factory = new SqsMessageSenderEndpointFactory(new MessageInterceptorRegistry(), sqsAsyncClient);

    final MessageSenderEndpoint sender = factory.create("foo-stream", MessageFormat.V1);
    assertThat(sender.getChannelName(), is("foo-stream"));
    assertThat(sender, is(instanceOf(SqsMessageSender.class)));
}
 
Example #12
Source File: SqsTestStreamSource.java    From synapse with Apache License 2.0 5 votes vote down vote up
public SqsTestStreamSource(String channelName, String inputFile) {
    this.inputFile = inputFile;
    final SqsAsyncClient sqsAsyncClient = new SqsTestConfiguration().sqsAsyncClient();
    final URL queueUrl = new SqsClientHelper(sqsAsyncClient).getQueueUrl(channelName);
    final MessageInterceptorRegistry interceptorRegistry = new MessageInterceptorRegistry();
    interceptorRegistry.register(matchingSenderChannelsWith(channelName, (message) -> {
        LOG.info("Sent message {}", message.getKey());
        return message;
    }));
    messageSender = new SqsMessageSender(
            channelName,
            queueUrl.toString(),
            interceptorRegistry,
            new TextMessageTranslator(), sqsAsyncClient);
}
 
Example #13
Source File: SqsMessageSender.java    From synapse with Apache License 2.0 5 votes vote down vote up
public SqsMessageSender(final String channelName,
                        final String queueUrl,
                        final MessageInterceptorRegistry interceptorRegistry,
                        final MessageTranslator<TextMessage> messageTranslator,
                        final SqsAsyncClient sqsAsyncClient) {
    super(channelName, interceptorRegistry, messageTranslator);
    this.queueUrl = queueUrl;
    this.sqsAsyncClient = sqsAsyncClient;
}
 
Example #14
Source File: SqsMessageQueueReceiverEndpointFactory.java    From synapse with Apache License 2.0 5 votes vote down vote up
public SqsMessageQueueReceiverEndpointFactory(final MessageInterceptorRegistry registry,
                                              final SqsAsyncClient sqsAsyncClient,
                                              final ApplicationEventPublisher eventPublisher) {
    this.registry = registry;
    this.sqsAsyncClient = sqsAsyncClient;
    this.eventPublisher = eventPublisher;
}
 
Example #15
Source File: SqsAutoConfiguration.java    From synapse with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(name = "messageQueueReceiverEndpointFactory")
public MessageQueueReceiverEndpointFactory messageQueueReceiverEndpointFactory(final MessageInterceptorRegistry registry,
                                                                               final SqsAsyncClient sqsAsyncClient,
                                                                               final ApplicationEventPublisher eventPublisher) {

    return new SqsMessageQueueReceiverEndpointFactory(registry, sqsAsyncClient, eventPublisher);
}
 
Example #16
Source File: SqsAutoConfiguration.java    From synapse with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(SqsAsyncClient.class)
public SqsAsyncClient sqsAsyncClient(final AwsCredentialsProvider credentialsProvider, final RetryPolicy sqsRetryPolicy) {
    return SqsAsyncClient.builder()
            .credentialsProvider(credentialsProvider)
            .region(Region.of(awsProperties.getRegion()))
            .overrideConfiguration(ClientOverrideConfiguration.builder()
                    .apiCallAttemptTimeout(Duration.ofSeconds(5))
                    .retryPolicy(sqsRetryPolicy).build())
            .build();
}
 
Example #17
Source File: SqsRecorder.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public RuntimeValue<SqsAsyncClient> buildAsyncClient(RuntimeValue<? extends AwsClientBuilder> builder,
        BeanContainer beanContainer,
        ShutdownContext shutdown) {
    SqsClientProducer producer = beanContainer.instance(SqsClientProducer.class);
    producer.setAsyncConfiguredBuilder((SqsAsyncClientBuilder) builder.getValue());
    shutdown.addShutdownTask(producer::destroy);
    return new RuntimeValue<>(producer.asyncClient());
}
 
Example #18
Source File: SqsRecorder.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public RuntimeValue<AwsClientBuilder> createAsyncBuilder(SqsConfig config,
        RuntimeValue<SdkAsyncHttpClient.Builder> transport) {

    SqsAsyncClientBuilder builder = SqsAsyncClient.builder();

    if (transport != null) {
        builder.httpClientBuilder(transport.getValue());
    }
    return new RuntimeValue<>(builder);
}
 
Example #19
Source File: AsyncSqsClientFactoryTest.java    From dynein with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsync() throws Exception {
  AsyncSqsClient asyncClient = factory.create(ASYNC_CONFIG);
  assertTrue(asyncClient instanceof AsyncSqsClientImpl);

  SqsAsyncClient sqs = ((AsyncSqsClientImpl) asyncClient).getClient();
  assertNotNull(sqs);
}
 
Example #20
Source File: AsyncSqsClientImplTest.java    From dynein with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
  this.awsAsyncSqsClient = Mockito.mock(SqsAsyncClient.class);
  metrics = new NoOpConveyorMetrics();
  executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
  asyncClient = new AsyncSqsClientImpl(awsAsyncSqsClient, metrics, executor);
}
 
Example #21
Source File: AsyncSqsClientImpl.java    From dynein with Apache License 2.0 5 votes vote down vote up
AsyncSqsClientImpl(
    @NonNull final SqsAsyncClient client,
    @NonNull final AsyncConveyorMetrics metrics,
    @NonNull final ExecutorService executor,
    long maxCacheSize,
    int receiveWaitTimeoutSeconds,
    int bulkheadMaxWaitMillis,
    int consumerConcurrency) {

  this.client = client;
  this.metrics = metrics;
  this.urlCache = initUrlCache(maxCacheSize);
  this.executor = executor;
  this.receiveWaitTimeoutSeconds = receiveWaitTimeoutSeconds;
  this.bulkhead =
      Bulkhead.of(
          "conveyor-async",
          BulkheadConfig.custom()
              .maxConcurrentCalls(consumerConcurrency)
              .maxWaitTimeDuration(Duration.ofMillis(bulkheadMaxWaitMillis))
              .build());
  this.bulkhead
      .getEventPublisher()
      .onCallPermitted(event -> metrics.consumePermitted())
      .onCallFinished(event -> metrics.consumeFinished())
      .onCallRejected(event -> metrics.consumeRejected())
      .onEvent(event -> metrics.bulkheadMetrics(this.bulkhead.getMetrics()));
}
 
Example #22
Source File: AsyncSqsClientImpl.java    From dynein with Apache License 2.0 5 votes vote down vote up
@Inject
AsyncSqsClientImpl(
    @NonNull final SqsAsyncClient client,
    @NonNull final AsyncConveyorMetrics metrics,
    @NonNull final ExecutorService executor) {

  this(
      client,
      metrics,
      executor,
      AsyncSqsClientConfiguration.DEFAULT_URL_CACHE_SIZE,
      AsyncSqsClientConfiguration.DEFAULT_RECEIVE_WAIT_SECONDS,
      AsyncSqsClientConfiguration.DEFAULT_BULKHEAD_MAX_WAIT_MS,
      AsyncSqsClientConfiguration.DEFAULT_CONSUMER_CONCURRENCY);
}
 
Example #23
Source File: SqsClientFactory.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
@Override
@Bean(preDestroy = "close")
@Singleton
@Requires(beans = SdkAsyncHttpClient.class)
public SqsAsyncClient asyncClient(SqsAsyncClientBuilder builder) {
    return super.asyncClient(builder);
}
 
Example #24
Source File: SqsClientFactory.java    From micronaut-aws with Apache License 2.0 4 votes vote down vote up
@Override
protected SqsAsyncClientBuilder createAsyncBuilder() {
    return SqsAsyncClient.builder();
}
 
Example #25
Source File: SqsClientHelper.java    From synapse with Apache License 2.0 4 votes vote down vote up
public SqsClientHelper(final SqsAsyncClient sqsAsyncClient) {
    this.sqsAsyncClient = sqsAsyncClient;
    waitForSqsToBeReady();
}
 
Example #26
Source File: SqsMessageSenderEndpointFactory.java    From synapse with Apache License 2.0 4 votes vote down vote up
public SqsMessageSenderEndpointFactory(final MessageInterceptorRegistry registry,
                                       final SqsAsyncClient sqsAsyncClient) {
    this.registry = registry;
    this.messageTranslator = new TextMessageTranslator();
    this.sqsAsyncClient = sqsAsyncClient;
}
 
Example #27
Source File: SqsClientProducer.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Produces
@ApplicationScoped
public SqsAsyncClient asyncClient() {
    asyncClient = asyncConfiguredBuilder.build();
    return asyncClient;
}
 
Example #28
Source File: SqsProcessor.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
protected DotName asyncClientName() {
    return DotName.createSimple(SqsAsyncClient.class.getName());
}
 
Example #29
Source File: SQSAsyncSender.java    From zipkin-aws with Apache License 2.0 4 votes vote down vote up
public static SQSAsyncSender create(String queueUrl) {
  return newBuilder()
      .queueUrl(queueUrl)
      .sqsClient(SqsAsyncClient.create())
      .build();
}