From ef6d0d8ce8f08727d11892781eb5cee6f887f5f1 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Thu, 10 Jun 2021 14:53:43 +0200 Subject: [PATCH 1/2] Amazon Services - Use synthetic beans to push builders to producers This simplifies things a lot and will also avoid race conditions. --- .../AbstractAmazonServiceProcessor.java | 93 +++++++++---------- .../AmazonClientBuilderBuildItem.java | 30 ------ ...mazonClientBuilderConfiguredBuildItem.java | 30 ------ .../deployment/DynamodbProcessor.java | 50 ++++------ .../runtime/DynamodbClientProducer.java | 34 +++---- .../dynamodb/runtime/DynamodbRecorder.java | 20 ---- .../amazon/iam/deployment/IamProcessor.java | 50 ++++------ .../amazon/iam/runtime/IamClientProducer.java | 35 ++++--- .../amazon/iam/runtime/IamRecorder.java | 20 ---- .../amazon/kms/deployment/KmsProcessor.java | 50 ++++------ .../amazon/kms/runtime/KmsClientProducer.java | 35 ++++--- .../amazon/kms/runtime/KmsRecorder.java | 20 ---- .../amazon/s3/deployment/S3Processor.java | 49 ++++------ .../S3SyncClientFullConfigTest.java | 6 +- .../amazon/s3/runtime/S3ClientProducer.java | 34 +++---- .../quarkus/amazon/s3/runtime/S3Recorder.java | 20 ---- .../amazon/ses/deployment/SesProcessor.java | 50 ++++------ .../amazon/ses/runtime/SesClientProducer.java | 37 ++++---- .../amazon/ses/runtime/SesRecorder.java | 20 ---- .../amazon/sns/deployment/SnsProcessor.java | 48 ++++------ .../amazon/sns/runtime/SnsClientProducer.java | 34 +++---- .../amazon/sns/runtime/SnsRecorder.java | 20 ---- .../amazon/sqs/deployment/SqsProcessor.java | 48 ++++------ .../amazon/sqs/runtime/SqsClientProducer.java | 34 +++---- .../amazon/sqs/runtime/SqsRecorder.java | 20 ---- .../amazon/ssm/deployment/SsmProcessor.java | 50 ++++------ .../amazon/ssm/runtime/SsmClientProducer.java | 35 ++++--- .../amazon/ssm/runtime/SsmRecorder.java | 20 ---- 28 files changed, 333 insertions(+), 659 deletions(-) delete mode 100644 extensions/amazon-services/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/AmazonClientBuilderBuildItem.java delete mode 100644 extensions/amazon-services/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/AmazonClientBuilderConfiguredBuildItem.java diff --git a/extensions/amazon-services/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/AbstractAmazonServiceProcessor.java b/extensions/amazon-services/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/AbstractAmazonServiceProcessor.java index 7928a7617e82e..41a27a520f476 100644 --- a/extensions/amazon-services/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/AbstractAmazonServiceProcessor.java +++ b/extensions/amazon-services/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/AbstractAmazonServiceProcessor.java @@ -4,6 +4,8 @@ import java.util.Optional; import java.util.function.Function; +import javax.enterprise.context.ApplicationScoped; + import org.jboss.jandex.DotName; import org.jboss.jandex.Type; @@ -18,6 +20,7 @@ import io.quarkus.amazon.common.runtime.SyncHttpClientBuildTimeConfig; import io.quarkus.amazon.common.runtime.SyncHttpClientConfig; import io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem; +import io.quarkus.arc.deployment.SyntheticBeanBuildItem; import io.quarkus.arc.processor.BuildExtension; import io.quarkus.arc.processor.InjectionPointInfo; import io.quarkus.deployment.Feature; @@ -26,7 +29,6 @@ import io.quarkus.deployment.builditem.FeatureBuildItem; import io.quarkus.runtime.RuntimeValue; import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder; -import software.amazon.awssdk.core.SdkClient; import software.amazon.awssdk.http.SdkHttpClient; import software.amazon.awssdk.http.async.SdkAsyncHttpClient; @@ -148,68 +150,57 @@ protected void createNettyAsyncTransportBuilder(List amaz }); } - protected void createClientBuilders(List syncClientBuilders, - List asyncClientBuilders, - BuildProducer builderProducer, - Function, RuntimeValue> syncFunc, - Function, RuntimeValue> asyncFunc) { + protected void createClientBuilders( + AmazonClientRecorder recorder, + RuntimeValue awsConfigRuntime, + RuntimeValue sdkConfigRuntime, + SdkBuildTimeConfig sdkBuildConfig, + List amazonClientSyncTransports, + List amazonClientAsyncTransports, + Class syncClientBuilderClass, + Function, RuntimeValue> syncClientBuilderFunction, + Class asyncClientBuilderClass, + Function, RuntimeValue> asyncClientBuilderFunction, + BuildProducer syntheticBeans) { String configName = configName(); - Optional> syncClientBuilder = syncClientBuilders.stream() + Optional> syncSdkHttpClientBuilder = amazonClientSyncTransports.stream() .filter(c -> configName.equals(c.getAwsClientName())) .map(c -> c.getClientBuilder()) .findFirst(); - Optional> asyncClientBuilder = asyncClientBuilders.stream() + Optional> asyncSdkAsyncHttpClientBuilder = amazonClientAsyncTransports.stream() .filter(c -> configName.equals(c.getAwsClientName())) .map(c -> c.getClientBuilder()) .findFirst(); - if (!syncClientBuilder.isPresent() && !asyncClientBuilder.isPresent()) { + if (!syncSdkHttpClientBuilder.isPresent() && !asyncSdkAsyncHttpClientBuilder.isPresent()) { return; } - builderProducer.produce(new AmazonClientBuilderBuildItem(configName, - syncClientBuilder.isPresent() ? syncFunc.apply(syncClientBuilder.get()) : null, - asyncClientBuilder.isPresent() ? asyncFunc.apply(asyncClientBuilder.get()) : null)); - } - - protected void buildClients(List configuredClients, - Function, RuntimeValue> syncClient, - Function, RuntimeValue> asyncClient) { - - for (AmazonClientBuilderConfiguredBuildItem client : configuredClients) { - if (configName().equals(client.getAwsClientName())) { - if (client.getSyncBuilder() != null) { - syncClient.apply(client.getSyncBuilder()); - } - if (client.getAsyncBuilder() != null) { - asyncClient.apply(client.getAsyncBuilder()); - } - } + RuntimeValue syncClientBuilder = syncSdkHttpClientBuilder.isPresent() + ? syncClientBuilderFunction.apply(syncSdkHttpClientBuilder.get()) + : null; + RuntimeValue asyncClientBuilder = asyncSdkAsyncHttpClientBuilder.isPresent() + ? asyncClientBuilderFunction.apply(asyncSdkAsyncHttpClientBuilder.get()) + : null; + + if (syncClientBuilder != null) { + syncClientBuilder = recorder.configure(syncClientBuilder, awsConfigRuntime, sdkConfigRuntime, + sdkBuildConfig, configName()); + syntheticBeans.produce(SyntheticBeanBuildItem.configure(syncClientBuilderClass) + .setRuntimeInit() + .scope(ApplicationScoped.class) + .runtimeValue(syncClientBuilder) + .done()); + } + if (asyncClientBuilder != null) { + asyncClientBuilder = recorder.configure(asyncClientBuilder, awsConfigRuntime, sdkConfigRuntime, + sdkBuildConfig, configName()); + syntheticBeans.produce(SyntheticBeanBuildItem.configure(asyncClientBuilderClass) + .setRuntimeInit() + .scope(ApplicationScoped.class) + .runtimeValue(asyncClientBuilder) + .done()); } - } - - protected void initClientBuilders(List clients, AmazonClientRecorder recorder, - RuntimeValue awsConfigRuntime, - RuntimeValue sdkConfigRuntime, SdkBuildTimeConfig sdkBuildConfig, - BuildProducer producer) { - Optional matchingClientBuilderBuildItem = clients.stream() - .filter(c -> c.getAwsClientName().equals(configName())) - .findAny(); - - matchingClientBuilderBuildItem.ifPresent(client -> { - RuntimeValue syncBuilder = null; - RuntimeValue asyncBuilder = null; - - if (client.getSyncBuilder() != null) { - syncBuilder = recorder.configure(client.getSyncBuilder(), awsConfigRuntime, sdkConfigRuntime, - sdkBuildConfig, configName()); - } - if (client.getAsyncBuilder() != null) { - asyncBuilder = recorder.configure(client.getAsyncBuilder(), awsConfigRuntime, sdkConfigRuntime, - sdkBuildConfig, configName()); - } - producer.produce(new AmazonClientBuilderConfiguredBuildItem(configName(), syncBuilder, asyncBuilder)); - }); } } diff --git a/extensions/amazon-services/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/AmazonClientBuilderBuildItem.java b/extensions/amazon-services/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/AmazonClientBuilderBuildItem.java deleted file mode 100644 index 1056a02a2b4ba..0000000000000 --- a/extensions/amazon-services/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/AmazonClientBuilderBuildItem.java +++ /dev/null @@ -1,30 +0,0 @@ -package io.quarkus.amazon.common.deployment; - -import io.quarkus.builder.item.MultiBuildItem; -import io.quarkus.runtime.RuntimeValue; -import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder; - -public final class AmazonClientBuilderBuildItem extends MultiBuildItem { - private final String awsClientName; - private final RuntimeValue syncBuilder; - private final RuntimeValue asyncBuilder; - - public AmazonClientBuilderBuildItem(String awsClientName, RuntimeValue syncBuilder, - RuntimeValue asyncBuilder) { - this.awsClientName = awsClientName; - this.syncBuilder = syncBuilder; - this.asyncBuilder = asyncBuilder; - } - - public String getAwsClientName() { - return awsClientName; - } - - public RuntimeValue getSyncBuilder() { - return syncBuilder; - } - - public RuntimeValue getAsyncBuilder() { - return asyncBuilder; - } -} diff --git a/extensions/amazon-services/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/AmazonClientBuilderConfiguredBuildItem.java b/extensions/amazon-services/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/AmazonClientBuilderConfiguredBuildItem.java deleted file mode 100644 index f4ab2e901876e..0000000000000 --- a/extensions/amazon-services/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/AmazonClientBuilderConfiguredBuildItem.java +++ /dev/null @@ -1,30 +0,0 @@ -package io.quarkus.amazon.common.deployment; - -import io.quarkus.builder.item.MultiBuildItem; -import io.quarkus.runtime.RuntimeValue; -import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder; - -public final class AmazonClientBuilderConfiguredBuildItem extends MultiBuildItem { - private final String awsClientName; - private final RuntimeValue syncBuilder; - private final RuntimeValue asyncBuilder; - - public AmazonClientBuilderConfiguredBuildItem(String awsClientName, RuntimeValue syncBuilder, - RuntimeValue asyncBuilder) { - this.awsClientName = awsClientName; - this.syncBuilder = syncBuilder; - this.asyncBuilder = asyncBuilder; - } - - public String getAwsClientName() { - return awsClientName; - } - - public RuntimeValue getSyncBuilder() { - return syncBuilder; - } - - public RuntimeValue getAsyncBuilder() { - return asyncBuilder; - } -} diff --git a/extensions/amazon-services/dynamodb/deployment/src/main/java/io/quarkus/amazon/dynamodb/deployment/DynamodbProcessor.java b/extensions/amazon-services/dynamodb/deployment/src/main/java/io/quarkus/amazon/dynamodb/deployment/DynamodbProcessor.java index 77f5a558ac678..ec1e79c0c398b 100644 --- a/extensions/amazon-services/dynamodb/deployment/src/main/java/io/quarkus/amazon/dynamodb/deployment/DynamodbProcessor.java +++ b/extensions/amazon-services/dynamodb/deployment/src/main/java/io/quarkus/amazon/dynamodb/deployment/DynamodbProcessor.java @@ -7,8 +7,6 @@ import io.quarkus.amazon.common.deployment.AbstractAmazonServiceProcessor; import io.quarkus.amazon.common.deployment.AmazonClientAsyncTransportBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientBuildItem; -import io.quarkus.amazon.common.deployment.AmazonClientBuilderBuildItem; -import io.quarkus.amazon.common.deployment.AmazonClientBuilderConfiguredBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientInterceptorsPathBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientSyncTransportBuildItem; import io.quarkus.amazon.common.deployment.AmazonHttpClients; @@ -21,8 +19,8 @@ import io.quarkus.amazon.dynamodb.runtime.DynamodbConfig; import io.quarkus.amazon.dynamodb.runtime.DynamodbRecorder; import io.quarkus.arc.deployment.AdditionalBeanBuildItem; -import io.quarkus.arc.deployment.BeanContainerBuildItem; import io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem; +import io.quarkus.arc.deployment.SyntheticBeanBuildItem; import io.quarkus.deployment.Feature; import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; @@ -30,10 +28,11 @@ import io.quarkus.deployment.annotations.Record; import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem; import io.quarkus.deployment.builditem.FeatureBuildItem; -import io.quarkus.deployment.builditem.ShutdownContextBuildItem; import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem; import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient; +import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClientBuilder; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; +import software.amazon.awssdk.services.dynamodb.DynamoDbClientBuilder; public class DynamodbProcessor extends AbstractAmazonServiceProcessor { @@ -128,34 +127,23 @@ void setupNettyAsyncTransport(List amazonClients, Dynamod @BuildStep @Record(ExecutionTime.RUNTIME_INIT) - void createClientBuilders(List syncTransports, - List asyncTransports, DynamodbRecorder recorder, - DynamodbConfig runtimeConfig, BuildProducer builderProducer) { - - createClientBuilders(syncTransports, asyncTransports, builderProducer, - (syncTransport) -> recorder.createSyncBuilder(runtimeConfig, syncTransport), - (asyncTransport) -> recorder.createAsyncBuilder(runtimeConfig, asyncTransport)); - } - - @BuildStep - @Record(ExecutionTime.RUNTIME_INIT) - void configureClient(List clients, DynamodbRecorder recorder, + void createClientBuilders(DynamodbRecorder recorder, AmazonClientRecorder commonRecorder, DynamodbConfig runtimeConfig, - BuildProducer producer) { - - initClientBuilders(clients, commonRecorder, recorder.getAwsConfig(runtimeConfig), recorder.getSdkConfig(runtimeConfig), - buildTimeConfig.sdk, producer); - } - - @BuildStep - @Record(ExecutionTime.RUNTIME_INIT) - void buildClients(List configuredClients, DynamodbRecorder recorder, - BeanContainerBuildItem beanContainer, - ShutdownContextBuildItem shutdown) { - - buildClients(configuredClients, - (syncBuilder) -> recorder.buildClient(syncBuilder, beanContainer.getValue(), shutdown), - (asyncBuilder) -> recorder.buildAsyncClient(asyncBuilder, beanContainer.getValue(), shutdown)); + List syncTransports, + List asyncTransports, + BuildProducer syntheticBeans) { + + createClientBuilders(commonRecorder, + recorder.getAwsConfig(runtimeConfig), + recorder.getSdkConfig(runtimeConfig), + buildTimeConfig.sdk, + syncTransports, + asyncTransports, + DynamoDbClientBuilder.class, + (syncTransport) -> recorder.createSyncBuilder(runtimeConfig, syncTransport), + DynamoDbAsyncClientBuilder.class, + (asyncTransport) -> recorder.createAsyncBuilder(runtimeConfig, asyncTransport), + syntheticBeans); } } diff --git a/extensions/amazon-services/dynamodb/runtime/src/main/java/io/quarkus/amazon/dynamodb/runtime/DynamodbClientProducer.java b/extensions/amazon-services/dynamodb/runtime/src/main/java/io/quarkus/amazon/dynamodb/runtime/DynamodbClientProducer.java index 6b48f4988e2ef..9cad17869fd39 100644 --- a/extensions/amazon-services/dynamodb/runtime/src/main/java/io/quarkus/amazon/dynamodb/runtime/DynamodbClientProducer.java +++ b/extensions/amazon-services/dynamodb/runtime/src/main/java/io/quarkus/amazon/dynamodb/runtime/DynamodbClientProducer.java @@ -2,6 +2,7 @@ import javax.annotation.PreDestroy; import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Instance; import javax.enterprise.inject.Produces; import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient; @@ -12,41 +13,40 @@ @ApplicationScoped public class DynamodbClientProducer { - private volatile DynamoDbClientBuilder syncConfiguredBuilder; - private volatile DynamoDbAsyncClientBuilder asyncConfiguredBuilder; + private final DynamoDbClient syncClient; + private final DynamoDbAsyncClient asyncClient; - private DynamoDbClient client; - private DynamoDbAsyncClient asyncClient; + DynamodbClientProducer(Instance syncClientBuilderInstance, + Instance asyncClientBuilderInstance) { + this.syncClient = syncClientBuilderInstance.isResolvable() ? syncClientBuilderInstance.get().build() : null; + this.asyncClient = asyncClientBuilderInstance.isResolvable() ? asyncClientBuilderInstance.get().build() : null; + } @Produces @ApplicationScoped public DynamoDbClient client() { - client = syncConfiguredBuilder.build(); - return client; + if (syncClient == null) { + throw new IllegalStateException("The DynamoDbClient is required but has not been detected/configured."); + } + return syncClient; } @Produces @ApplicationScoped public DynamoDbAsyncClient asyncClient() { - asyncClient = asyncConfiguredBuilder.build(); + if (asyncClient == null) { + throw new IllegalStateException("The DynamoDbAsyncClient is required but has not been detected/configured."); + } return asyncClient; } @PreDestroy public void destroy() { - if (client != null) { - client.close(); + if (syncClient != null) { + syncClient.close(); } if (asyncClient != null) { asyncClient.close(); } } - - public void setSyncConfiguredBuilder(DynamoDbClientBuilder syncConfiguredBuilder) { - this.syncConfiguredBuilder = syncConfiguredBuilder; - } - - public void setAsyncConfiguredBuilder(DynamoDbAsyncClientBuilder asyncConfiguredBuilder) { - this.asyncConfiguredBuilder = asyncConfiguredBuilder; - } } diff --git a/extensions/amazon-services/dynamodb/runtime/src/main/java/io/quarkus/amazon/dynamodb/runtime/DynamodbRecorder.java b/extensions/amazon-services/dynamodb/runtime/src/main/java/io/quarkus/amazon/dynamodb/runtime/DynamodbRecorder.java index c13c9f6a34d00..b2fff818e3867 100644 --- a/extensions/amazon-services/dynamodb/runtime/src/main/java/io/quarkus/amazon/dynamodb/runtime/DynamodbRecorder.java +++ b/extensions/amazon-services/dynamodb/runtime/src/main/java/io/quarkus/amazon/dynamodb/runtime/DynamodbRecorder.java @@ -4,9 +4,7 @@ import io.quarkus.amazon.common.runtime.NettyHttpClientConfig; import io.quarkus.amazon.common.runtime.SdkConfig; import io.quarkus.amazon.common.runtime.SyncHttpClientConfig; -import io.quarkus.arc.runtime.BeanContainer; import io.quarkus.runtime.RuntimeValue; -import io.quarkus.runtime.ShutdownContext; import io.quarkus.runtime.annotations.Recorder; import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder; import software.amazon.awssdk.http.SdkHttpClient; @@ -56,22 +54,4 @@ public RuntimeValue createAsyncBuilder(DynamodbConfig config, } return new RuntimeValue<>(builder); } - - public RuntimeValue buildClient(RuntimeValue builder, - BeanContainer beanContainer, - ShutdownContext shutdown) { - DynamodbClientProducer producer = beanContainer.instance(DynamodbClientProducer.class); - producer.setSyncConfiguredBuilder((DynamoDbClientBuilder) builder.getValue()); - shutdown.addShutdownTask(producer::destroy); - return new RuntimeValue<>(producer.client()); - } - - public RuntimeValue buildAsyncClient(RuntimeValue builder, - BeanContainer beanContainer, - ShutdownContext shutdown) { - DynamodbClientProducer producer = beanContainer.instance(DynamodbClientProducer.class); - producer.setAsyncConfiguredBuilder((DynamoDbAsyncClientBuilder) builder.getValue()); - shutdown.addShutdownTask(producer::destroy); - return new RuntimeValue<>(producer.asyncClient()); - } } diff --git a/extensions/amazon-services/iam/deployment/src/main/java/io/quarkus/amazon/iam/deployment/IamProcessor.java b/extensions/amazon-services/iam/deployment/src/main/java/io/quarkus/amazon/iam/deployment/IamProcessor.java index 54016a4c4cabe..0224788e9c65f 100644 --- a/extensions/amazon-services/iam/deployment/src/main/java/io/quarkus/amazon/iam/deployment/IamProcessor.java +++ b/extensions/amazon-services/iam/deployment/src/main/java/io/quarkus/amazon/iam/deployment/IamProcessor.java @@ -7,8 +7,6 @@ import io.quarkus.amazon.common.deployment.AbstractAmazonServiceProcessor; import io.quarkus.amazon.common.deployment.AmazonClientAsyncTransportBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientBuildItem; -import io.quarkus.amazon.common.deployment.AmazonClientBuilderBuildItem; -import io.quarkus.amazon.common.deployment.AmazonClientBuilderConfiguredBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientInterceptorsPathBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientSyncTransportBuildItem; import io.quarkus.amazon.common.deployment.AmazonHttpClients; @@ -21,8 +19,8 @@ import io.quarkus.amazon.iam.runtime.IamConfig; import io.quarkus.amazon.iam.runtime.IamRecorder; import io.quarkus.arc.deployment.AdditionalBeanBuildItem; -import io.quarkus.arc.deployment.BeanContainerBuildItem; import io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem; +import io.quarkus.arc.deployment.SyntheticBeanBuildItem; import io.quarkus.deployment.Feature; import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; @@ -30,9 +28,10 @@ import io.quarkus.deployment.annotations.Record; import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem; import io.quarkus.deployment.builditem.FeatureBuildItem; -import io.quarkus.deployment.builditem.ShutdownContextBuildItem; import software.amazon.awssdk.services.iam.IamAsyncClient; +import software.amazon.awssdk.services.iam.IamAsyncClientBuilder; import software.amazon.awssdk.services.iam.IamClient; +import software.amazon.awssdk.services.iam.IamClientBuilder; public class IamProcessor extends AbstractAmazonServiceProcessor { @@ -119,34 +118,23 @@ void setupNettyAsyncTransport(List amazonClients, IamReco @BuildStep @Record(ExecutionTime.RUNTIME_INIT) - void createClientBuilders(List syncTransports, - List asyncTransports, IamRecorder recorder, - IamConfig runtimeConfig, BuildProducer builderProducer) { - - createClientBuilders(syncTransports, asyncTransports, builderProducer, - (syncTransport) -> recorder.createSyncBuilder(runtimeConfig, syncTransport), - (asyncTransport) -> recorder.createAsyncBuilder(runtimeConfig, asyncTransport)); - } - - @BuildStep - @Record(ExecutionTime.RUNTIME_INIT) - void configureClient(List clients, IamRecorder recorder, + void createClientBuilders(IamRecorder recorder, AmazonClientRecorder commonRecorder, IamConfig runtimeConfig, - BuildProducer producer) { - - initClientBuilders(clients, commonRecorder, recorder.getAwsConfig(runtimeConfig), recorder.getSdkConfig(runtimeConfig), - buildTimeConfig.sdk, producer); - } - - @BuildStep - @Record(ExecutionTime.RUNTIME_INIT) - void buildClients(List configuredClients, IamRecorder recorder, - BeanContainerBuildItem beanContainer, - ShutdownContextBuildItem shutdown) { - - buildClients(configuredClients, - (syncBuilder) -> recorder.buildClient(syncBuilder, beanContainer.getValue(), shutdown), - (asyncBuilder) -> recorder.buildAsyncClient(asyncBuilder, beanContainer.getValue(), shutdown)); + List syncTransports, + List asyncTransports, + BuildProducer syntheticBeans) { + + createClientBuilders(commonRecorder, + recorder.getAwsConfig(runtimeConfig), + recorder.getSdkConfig(runtimeConfig), + buildTimeConfig.sdk, + syncTransports, + asyncTransports, + IamClientBuilder.class, + (syncTransport) -> recorder.createSyncBuilder(runtimeConfig, syncTransport), + IamAsyncClientBuilder.class, + (asyncTransport) -> recorder.createAsyncBuilder(runtimeConfig, asyncTransport), + syntheticBeans); } } diff --git a/extensions/amazon-services/iam/runtime/src/main/java/io/quarkus/amazon/iam/runtime/IamClientProducer.java b/extensions/amazon-services/iam/runtime/src/main/java/io/quarkus/amazon/iam/runtime/IamClientProducer.java index 6bc9926bdc8f0..8025642e1eb06 100644 --- a/extensions/amazon-services/iam/runtime/src/main/java/io/quarkus/amazon/iam/runtime/IamClientProducer.java +++ b/extensions/amazon-services/iam/runtime/src/main/java/io/quarkus/amazon/iam/runtime/IamClientProducer.java @@ -2,6 +2,7 @@ import javax.annotation.PreDestroy; import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Instance; import javax.enterprise.inject.Produces; import software.amazon.awssdk.services.iam.IamAsyncClient; @@ -11,42 +12,40 @@ @ApplicationScoped public class IamClientProducer { + private final IamClient syncClient; + private final IamAsyncClient asyncClient; - private volatile IamClientBuilder syncConfiguredBuilder; - private volatile IamAsyncClientBuilder asyncConfiguredBuilder; - - private IamClient client; - private IamAsyncClient asyncClient; + IamClientProducer(Instance syncClientBuilderInstance, + Instance asyncClientBuilderInstance) { + this.syncClient = syncClientBuilderInstance.isResolvable() ? syncClientBuilderInstance.get().build() : null; + this.asyncClient = asyncClientBuilderInstance.isResolvable() ? asyncClientBuilderInstance.get().build() : null; + } @Produces @ApplicationScoped public IamClient client() { - client = syncConfiguredBuilder.build(); - return client; + if (syncClient == null) { + throw new IllegalStateException("The IamClient is required but has not been detected/configured."); + } + return syncClient; } @Produces @ApplicationScoped public IamAsyncClient asyncClient() { - asyncClient = asyncConfiguredBuilder.build(); + if (asyncClient == null) { + throw new IllegalStateException("The IamAsyncClient is required but has not been detected/configured."); + } return asyncClient; } @PreDestroy public void destroy() { - if (client != null) { - client.close(); + if (syncClient != null) { + syncClient.close(); } if (asyncClient != null) { asyncClient.close(); } } - - public void setSyncConfiguredBuilder(IamClientBuilder syncConfiguredBuilder) { - this.syncConfiguredBuilder = syncConfiguredBuilder; - } - - public void setAsyncConfiguredBuilder(IamAsyncClientBuilder asyncConfiguredBuilder) { - this.asyncConfiguredBuilder = asyncConfiguredBuilder; - } } diff --git a/extensions/amazon-services/iam/runtime/src/main/java/io/quarkus/amazon/iam/runtime/IamRecorder.java b/extensions/amazon-services/iam/runtime/src/main/java/io/quarkus/amazon/iam/runtime/IamRecorder.java index eaabce592f437..03588b7b90024 100644 --- a/extensions/amazon-services/iam/runtime/src/main/java/io/quarkus/amazon/iam/runtime/IamRecorder.java +++ b/extensions/amazon-services/iam/runtime/src/main/java/io/quarkus/amazon/iam/runtime/IamRecorder.java @@ -4,9 +4,7 @@ import io.quarkus.amazon.common.runtime.NettyHttpClientConfig; import io.quarkus.amazon.common.runtime.SdkConfig; import io.quarkus.amazon.common.runtime.SyncHttpClientConfig; -import io.quarkus.arc.runtime.BeanContainer; import io.quarkus.runtime.RuntimeValue; -import io.quarkus.runtime.ShutdownContext; import io.quarkus.runtime.annotations.Recorder; import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder; import software.amazon.awssdk.http.SdkHttpClient; @@ -52,22 +50,4 @@ public RuntimeValue createAsyncBuilder(IamConfig config, } return new RuntimeValue<>(builder); } - - public RuntimeValue buildClient(RuntimeValue builder, - BeanContainer beanContainer, - ShutdownContext shutdown) { - IamClientProducer producer = beanContainer.instance(IamClientProducer.class); - producer.setSyncConfiguredBuilder((IamClientBuilder) builder.getValue()); - shutdown.addShutdownTask(producer::destroy); - return new RuntimeValue<>(producer.client()); - } - - public RuntimeValue buildAsyncClient(RuntimeValue builder, - BeanContainer beanContainer, - ShutdownContext shutdown) { - IamClientProducer producer = beanContainer.instance(IamClientProducer.class); - producer.setAsyncConfiguredBuilder((IamAsyncClientBuilder) builder.getValue()); - shutdown.addShutdownTask(producer::destroy); - return new RuntimeValue<>(producer.asyncClient()); - } } diff --git a/extensions/amazon-services/kms/deployment/src/main/java/io/quarkus/amazon/kms/deployment/KmsProcessor.java b/extensions/amazon-services/kms/deployment/src/main/java/io/quarkus/amazon/kms/deployment/KmsProcessor.java index 64d256bd147be..3a7880f8f5162 100644 --- a/extensions/amazon-services/kms/deployment/src/main/java/io/quarkus/amazon/kms/deployment/KmsProcessor.java +++ b/extensions/amazon-services/kms/deployment/src/main/java/io/quarkus/amazon/kms/deployment/KmsProcessor.java @@ -7,8 +7,6 @@ import io.quarkus.amazon.common.deployment.AbstractAmazonServiceProcessor; import io.quarkus.amazon.common.deployment.AmazonClientAsyncTransportBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientBuildItem; -import io.quarkus.amazon.common.deployment.AmazonClientBuilderBuildItem; -import io.quarkus.amazon.common.deployment.AmazonClientBuilderConfiguredBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientInterceptorsPathBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientSyncTransportBuildItem; import io.quarkus.amazon.common.deployment.AmazonHttpClients; @@ -21,8 +19,8 @@ import io.quarkus.amazon.kms.runtime.KmsConfig; import io.quarkus.amazon.kms.runtime.KmsRecorder; import io.quarkus.arc.deployment.AdditionalBeanBuildItem; -import io.quarkus.arc.deployment.BeanContainerBuildItem; import io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem; +import io.quarkus.arc.deployment.SyntheticBeanBuildItem; import io.quarkus.deployment.Feature; import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; @@ -30,9 +28,10 @@ import io.quarkus.deployment.annotations.Record; import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem; import io.quarkus.deployment.builditem.FeatureBuildItem; -import io.quarkus.deployment.builditem.ShutdownContextBuildItem; import software.amazon.awssdk.services.kms.KmsAsyncClient; +import software.amazon.awssdk.services.kms.KmsAsyncClientBuilder; import software.amazon.awssdk.services.kms.KmsClient; +import software.amazon.awssdk.services.kms.KmsClientBuilder; public class KmsProcessor extends AbstractAmazonServiceProcessor { @@ -119,34 +118,23 @@ void setupNettyAsyncTransport(List amazonClients, KmsReco @BuildStep @Record(ExecutionTime.RUNTIME_INIT) - void createClientBuilders(List syncTransports, - List asyncTransports, KmsRecorder recorder, - KmsConfig runtimeConfig, BuildProducer builderProducer) { - - createClientBuilders(syncTransports, asyncTransports, builderProducer, - (syncTransport) -> recorder.createSyncBuilder(runtimeConfig, syncTransport), - (asyncTransport) -> recorder.createAsyncBuilder(runtimeConfig, asyncTransport)); - } - - @BuildStep - @Record(ExecutionTime.RUNTIME_INIT) - void configureClient(List clients, KmsRecorder recorder, + void createClientBuilders(KmsRecorder recorder, AmazonClientRecorder commonRecorder, KmsConfig runtimeConfig, - BuildProducer producer) { - - initClientBuilders(clients, commonRecorder, recorder.getAwsConfig(runtimeConfig), recorder.getSdkConfig(runtimeConfig), - buildTimeConfig.sdk, producer); - } - - @BuildStep - @Record(ExecutionTime.RUNTIME_INIT) - void buildClients(List configuredClients, KmsRecorder recorder, - BeanContainerBuildItem beanContainer, - ShutdownContextBuildItem shutdown) { - - buildClients(configuredClients, - (syncBuilder) -> recorder.buildClient(syncBuilder, beanContainer.getValue(), shutdown), - (asyncBuilder) -> recorder.buildAsyncClient(asyncBuilder, beanContainer.getValue(), shutdown)); + List syncTransports, + List asyncTransports, + BuildProducer syntheticBeans) { + + createClientBuilders(commonRecorder, + recorder.getAwsConfig(runtimeConfig), + recorder.getSdkConfig(runtimeConfig), + buildTimeConfig.sdk, + syncTransports, + asyncTransports, + KmsClientBuilder.class, + (syncTransport) -> recorder.createSyncBuilder(runtimeConfig, syncTransport), + KmsAsyncClientBuilder.class, + (asyncTransport) -> recorder.createAsyncBuilder(runtimeConfig, asyncTransport), + syntheticBeans); } } diff --git a/extensions/amazon-services/kms/runtime/src/main/java/io/quarkus/amazon/kms/runtime/KmsClientProducer.java b/extensions/amazon-services/kms/runtime/src/main/java/io/quarkus/amazon/kms/runtime/KmsClientProducer.java index 33cbc6cf1686e..cf3c3150ae432 100644 --- a/extensions/amazon-services/kms/runtime/src/main/java/io/quarkus/amazon/kms/runtime/KmsClientProducer.java +++ b/extensions/amazon-services/kms/runtime/src/main/java/io/quarkus/amazon/kms/runtime/KmsClientProducer.java @@ -2,6 +2,7 @@ import javax.annotation.PreDestroy; import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Instance; import javax.enterprise.inject.Produces; import software.amazon.awssdk.services.kms.KmsAsyncClient; @@ -11,42 +12,40 @@ @ApplicationScoped public class KmsClientProducer { + private final KmsClient syncClient; + private final KmsAsyncClient asyncClient; - private volatile KmsClientBuilder syncConfiguredBuilder; - private volatile KmsAsyncClientBuilder asyncConfiguredBuilder; - - private KmsClient client; - private KmsAsyncClient asyncClient; + KmsClientProducer(Instance syncClientBuilderInstance, + Instance asyncClientBuilderInstance) { + this.syncClient = syncClientBuilderInstance.isResolvable() ? syncClientBuilderInstance.get().build() : null; + this.asyncClient = asyncClientBuilderInstance.isResolvable() ? asyncClientBuilderInstance.get().build() : null; + } @Produces @ApplicationScoped public KmsClient client() { - client = syncConfiguredBuilder.build(); - return client; + if (syncClient == null) { + throw new IllegalStateException("The KmsClient is required but has not been detected/configured."); + } + return syncClient; } @Produces @ApplicationScoped public KmsAsyncClient asyncClient() { - asyncClient = asyncConfiguredBuilder.build(); + if (asyncClient == null) { + throw new IllegalStateException("The KmsAsyncClient is required but has not been detected/configured."); + } return asyncClient; } @PreDestroy public void destroy() { - if (client != null) { - client.close(); + if (syncClient != null) { + syncClient.close(); } if (asyncClient != null) { asyncClient.close(); } } - - public void setSyncConfiguredBuilder(KmsClientBuilder syncConfiguredBuilder) { - this.syncConfiguredBuilder = syncConfiguredBuilder; - } - - public void setAsyncConfiguredBuilder(KmsAsyncClientBuilder asyncConfiguredBuilder) { - this.asyncConfiguredBuilder = asyncConfiguredBuilder; - } } diff --git a/extensions/amazon-services/kms/runtime/src/main/java/io/quarkus/amazon/kms/runtime/KmsRecorder.java b/extensions/amazon-services/kms/runtime/src/main/java/io/quarkus/amazon/kms/runtime/KmsRecorder.java index 8cfb835ccfac7..86e8b3b855406 100644 --- a/extensions/amazon-services/kms/runtime/src/main/java/io/quarkus/amazon/kms/runtime/KmsRecorder.java +++ b/extensions/amazon-services/kms/runtime/src/main/java/io/quarkus/amazon/kms/runtime/KmsRecorder.java @@ -4,9 +4,7 @@ import io.quarkus.amazon.common.runtime.NettyHttpClientConfig; import io.quarkus.amazon.common.runtime.SdkConfig; import io.quarkus.amazon.common.runtime.SyncHttpClientConfig; -import io.quarkus.arc.runtime.BeanContainer; import io.quarkus.runtime.RuntimeValue; -import io.quarkus.runtime.ShutdownContext; import io.quarkus.runtime.annotations.Recorder; import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder; import software.amazon.awssdk.http.SdkHttpClient; @@ -52,22 +50,4 @@ public RuntimeValue createAsyncBuilder(KmsConfig config, } return new RuntimeValue<>(builder); } - - public RuntimeValue buildClient(RuntimeValue builder, - BeanContainer beanContainer, - ShutdownContext shutdown) { - KmsClientProducer producer = beanContainer.instance(KmsClientProducer.class); - producer.setSyncConfiguredBuilder((KmsClientBuilder) builder.getValue()); - shutdown.addShutdownTask(producer::destroy); - return new RuntimeValue<>(producer.client()); - } - - public RuntimeValue buildAsyncClient(RuntimeValue builder, - BeanContainer beanContainer, - ShutdownContext shutdown) { - KmsClientProducer producer = beanContainer.instance(KmsClientProducer.class); - producer.setAsyncConfiguredBuilder((KmsAsyncClientBuilder) builder.getValue()); - shutdown.addShutdownTask(producer::destroy); - return new RuntimeValue<>(producer.asyncClient()); - } } diff --git a/extensions/amazon-services/s3/deployment/src/main/java/io/quarkus/amazon/s3/deployment/S3Processor.java b/extensions/amazon-services/s3/deployment/src/main/java/io/quarkus/amazon/s3/deployment/S3Processor.java index b8b128ef98555..6f8b19979601b 100644 --- a/extensions/amazon-services/s3/deployment/src/main/java/io/quarkus/amazon/s3/deployment/S3Processor.java +++ b/extensions/amazon-services/s3/deployment/src/main/java/io/quarkus/amazon/s3/deployment/S3Processor.java @@ -7,8 +7,6 @@ import io.quarkus.amazon.common.deployment.AbstractAmazonServiceProcessor; import io.quarkus.amazon.common.deployment.AmazonClientAsyncTransportBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientBuildItem; -import io.quarkus.amazon.common.deployment.AmazonClientBuilderBuildItem; -import io.quarkus.amazon.common.deployment.AmazonClientBuilderConfiguredBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientInterceptorsPathBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientSyncTransportBuildItem; import io.quarkus.amazon.common.deployment.AmazonHttpClients; @@ -21,8 +19,8 @@ import io.quarkus.amazon.s3.runtime.S3Config; import io.quarkus.amazon.s3.runtime.S3Recorder; import io.quarkus.arc.deployment.AdditionalBeanBuildItem; -import io.quarkus.arc.deployment.BeanContainerBuildItem; import io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem; +import io.quarkus.arc.deployment.SyntheticBeanBuildItem; import io.quarkus.deployment.Feature; import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; @@ -30,9 +28,10 @@ import io.quarkus.deployment.annotations.Record; import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem; import io.quarkus.deployment.builditem.FeatureBuildItem; -import io.quarkus.deployment.builditem.ShutdownContextBuildItem; import software.amazon.awssdk.services.s3.S3AsyncClient; +import software.amazon.awssdk.services.s3.S3AsyncClientBuilder; import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.S3ClientBuilder; public class S3Processor extends AbstractAmazonServiceProcessor { @@ -120,35 +119,23 @@ void setupNettyAsyncTransport(List amazonClients, S3Recor @BuildStep @Record(ExecutionTime.RUNTIME_INIT) - void createClientBuilders(List syncTransports, - List asyncTransports, - S3Recorder recorder, - S3Config runtimeConfig, BuildProducer builderProducer) { - - createClientBuilders(syncTransports, asyncTransports, builderProducer, - (syncTransport) -> recorder.createSyncBuilder(runtimeConfig, syncTransport), - (asyncTransport) -> recorder.createAsyncBuilder(runtimeConfig, asyncTransport)); - } - - @BuildStep - @Record(ExecutionTime.RUNTIME_INIT) - void configureClient(List clients, S3Recorder recorder, + void createClientBuilders(S3Recorder recorder, AmazonClientRecorder commonRecorder, S3Config runtimeConfig, - BuildProducer producer) { - - initClientBuilders(clients, commonRecorder, recorder.getAwsConfig(runtimeConfig), recorder.getSdkConfig(runtimeConfig), - buildTimeConfig.sdk, producer); - } - - @BuildStep - @Record(ExecutionTime.RUNTIME_INIT) - void buildClients(List configuredClients, S3Recorder recorder, - BeanContainerBuildItem beanContainer, - ShutdownContextBuildItem shutdown) { + List syncTransports, + List asyncTransports, + BuildProducer syntheticBeans) { - buildClients(configuredClients, - (syncBuilder) -> recorder.buildClient(syncBuilder, beanContainer.getValue(), shutdown), - (asyncBuilder) -> recorder.buildAsyncClient(asyncBuilder, beanContainer.getValue(), shutdown)); + createClientBuilders(commonRecorder, + recorder.getAwsConfig(runtimeConfig), + recorder.getSdkConfig(runtimeConfig), + buildTimeConfig.sdk, + syncTransports, + asyncTransports, + S3ClientBuilder.class, + (syncTransport) -> recorder.createSyncBuilder(runtimeConfig, syncTransport), + S3AsyncClientBuilder.class, + (asyncTransport) -> recorder.createAsyncBuilder(runtimeConfig, asyncTransport), + syntheticBeans); } } diff --git a/extensions/amazon-services/s3/deployment/src/test/java/io/quarkus/amazon/s3/deployment/S3SyncClientFullConfigTest.java b/extensions/amazon-services/s3/deployment/src/test/java/io/quarkus/amazon/s3/deployment/S3SyncClientFullConfigTest.java index 698fc352a2949..2c4a86d04f027 100644 --- a/extensions/amazon-services/s3/deployment/src/test/java/io/quarkus/amazon/s3/deployment/S3SyncClientFullConfigTest.java +++ b/extensions/amazon-services/s3/deployment/src/test/java/io/quarkus/amazon/s3/deployment/S3SyncClientFullConfigTest.java @@ -1,5 +1,8 @@ package io.quarkus.amazon.s3.deployment; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import javax.enterprise.inject.Instance; import javax.inject.Inject; import org.jboss.shrinkwrap.api.ShrinkWrap; @@ -13,7 +16,7 @@ public class S3SyncClientFullConfigTest { @Inject - S3Client client; + Instance client; @RegisterExtension static final QuarkusUnitTest config = new QuarkusUnitTest() @@ -22,6 +25,7 @@ public class S3SyncClientFullConfigTest { @Test public void test() { + assertNotNull(client.get()); // should finish with success } } diff --git a/extensions/amazon-services/s3/runtime/src/main/java/io/quarkus/amazon/s3/runtime/S3ClientProducer.java b/extensions/amazon-services/s3/runtime/src/main/java/io/quarkus/amazon/s3/runtime/S3ClientProducer.java index ae40b20366e12..a5005bc1d35d3 100644 --- a/extensions/amazon-services/s3/runtime/src/main/java/io/quarkus/amazon/s3/runtime/S3ClientProducer.java +++ b/extensions/amazon-services/s3/runtime/src/main/java/io/quarkus/amazon/s3/runtime/S3ClientProducer.java @@ -2,6 +2,7 @@ import javax.annotation.PreDestroy; import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Instance; import javax.enterprise.inject.Produces; import software.amazon.awssdk.services.s3.S3AsyncClient; @@ -11,41 +12,40 @@ @ApplicationScoped public class S3ClientProducer { - private volatile S3ClientBuilder syncConfiguredBuilder; - private volatile S3AsyncClientBuilder asyncConfiguredBuilder; + private final S3Client syncClient; + private final S3AsyncClient asyncClient; - private S3Client client; - private S3AsyncClient asyncClient; + S3ClientProducer(Instance syncClientBuilderInstance, + Instance asyncClientBuilderInstance) { + this.syncClient = syncClientBuilderInstance.isResolvable() ? syncClientBuilderInstance.get().build() : null; + this.asyncClient = asyncClientBuilderInstance.isResolvable() ? asyncClientBuilderInstance.get().build() : null; + } @Produces @ApplicationScoped public S3Client client() { - client = syncConfiguredBuilder.build(); - return client; + if (syncClient == null) { + throw new IllegalStateException("The S3Client is required but has not been detected/configured."); + } + return syncClient; } @Produces @ApplicationScoped public S3AsyncClient asyncClient() { - asyncClient = asyncConfiguredBuilder.build(); + if (asyncClient == null) { + throw new IllegalStateException("The S3AsyncClient is required but has not been detected/configured."); + } return asyncClient; } @PreDestroy public void destroy() { - if (client != null) { - client.close(); + if (syncClient != null) { + syncClient.close(); } if (asyncClient != null) { asyncClient.close(); } } - - public void setSyncConfiguredBuilder(S3ClientBuilder syncConfiguredBuilder) { - this.syncConfiguredBuilder = syncConfiguredBuilder; - } - - public void setAsyncConfiguredBuilder(S3AsyncClientBuilder asyncConfiguredBuilder) { - this.asyncConfiguredBuilder = asyncConfiguredBuilder; - } } diff --git a/extensions/amazon-services/s3/runtime/src/main/java/io/quarkus/amazon/s3/runtime/S3Recorder.java b/extensions/amazon-services/s3/runtime/src/main/java/io/quarkus/amazon/s3/runtime/S3Recorder.java index 7022bbece5ef8..7e85b8082ddf2 100644 --- a/extensions/amazon-services/s3/runtime/src/main/java/io/quarkus/amazon/s3/runtime/S3Recorder.java +++ b/extensions/amazon-services/s3/runtime/src/main/java/io/quarkus/amazon/s3/runtime/S3Recorder.java @@ -4,9 +4,7 @@ import io.quarkus.amazon.common.runtime.NettyHttpClientConfig; import io.quarkus.amazon.common.runtime.SdkConfig; import io.quarkus.amazon.common.runtime.SyncHttpClientConfig; -import io.quarkus.arc.runtime.BeanContainer; import io.quarkus.runtime.RuntimeValue; -import io.quarkus.runtime.ShutdownContext; import io.quarkus.runtime.annotations.Recorder; import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder; import software.amazon.awssdk.http.SdkHttpClient.Builder; @@ -58,24 +56,6 @@ public RuntimeValue createAsyncBuilder(S3Config config, return new RuntimeValue<>(builder); } - public RuntimeValue buildClient(RuntimeValue builder, - BeanContainer beanContainer, - ShutdownContext shutdown) { - S3ClientProducer producer = beanContainer.instance(S3ClientProducer.class); - producer.setSyncConfiguredBuilder((S3ClientBuilder) builder.getValue()); - shutdown.addShutdownTask(producer::destroy); - return new RuntimeValue<>(producer.client()); - } - - public RuntimeValue buildAsyncClient(RuntimeValue builder, - BeanContainer beanContainer, - ShutdownContext shutdown) { - S3ClientProducer producer = beanContainer.instance(S3ClientProducer.class); - producer.setAsyncConfiguredBuilder((S3AsyncClientBuilder) builder.getValue()); - shutdown.addShutdownTask(producer::destroy); - return new RuntimeValue<>(producer.asyncClient()); - } - private void configureS3Client(S3BaseClientBuilder builder, S3Config config) { S3Configuration.Builder s3ConfigBuilder = S3Configuration.builder() .accelerateModeEnabled(config.accelerateMode) diff --git a/extensions/amazon-services/ses/deployment/src/main/java/io/quarkus/amazon/ses/deployment/SesProcessor.java b/extensions/amazon-services/ses/deployment/src/main/java/io/quarkus/amazon/ses/deployment/SesProcessor.java index 198924ee1ae45..141ce82264051 100644 --- a/extensions/amazon-services/ses/deployment/src/main/java/io/quarkus/amazon/ses/deployment/SesProcessor.java +++ b/extensions/amazon-services/ses/deployment/src/main/java/io/quarkus/amazon/ses/deployment/SesProcessor.java @@ -7,8 +7,6 @@ import io.quarkus.amazon.common.deployment.AbstractAmazonServiceProcessor; import io.quarkus.amazon.common.deployment.AmazonClientAsyncTransportBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientBuildItem; -import io.quarkus.amazon.common.deployment.AmazonClientBuilderBuildItem; -import io.quarkus.amazon.common.deployment.AmazonClientBuilderConfiguredBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientInterceptorsPathBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientSyncTransportBuildItem; import io.quarkus.amazon.common.deployment.AmazonHttpClients; @@ -21,8 +19,8 @@ import io.quarkus.amazon.ses.runtime.SesConfig; import io.quarkus.amazon.ses.runtime.SesRecorder; import io.quarkus.arc.deployment.AdditionalBeanBuildItem; -import io.quarkus.arc.deployment.BeanContainerBuildItem; import io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem; +import io.quarkus.arc.deployment.SyntheticBeanBuildItem; import io.quarkus.deployment.Feature; import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; @@ -30,9 +28,10 @@ import io.quarkus.deployment.annotations.Record; import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem; import io.quarkus.deployment.builditem.FeatureBuildItem; -import io.quarkus.deployment.builditem.ShutdownContextBuildItem; import software.amazon.awssdk.services.ses.SesAsyncClient; +import software.amazon.awssdk.services.ses.SesAsyncClientBuilder; import software.amazon.awssdk.services.ses.SesClient; +import software.amazon.awssdk.services.ses.SesClientBuilder; public class SesProcessor extends AbstractAmazonServiceProcessor { @@ -119,34 +118,23 @@ void setupNettyAsyncTransport(List amazonClients, SesReco @BuildStep @Record(ExecutionTime.RUNTIME_INIT) - void createClientBuilders(List syncTransports, - List asyncTransports, SesRecorder recorder, - SesConfig runtimeConfig, BuildProducer builderProducer) { - - createClientBuilders(syncTransports, asyncTransports, builderProducer, - (syncTransport) -> recorder.createSyncBuilder(runtimeConfig, syncTransport), - (asyncTransport) -> recorder.createAsyncBuilder(runtimeConfig, asyncTransport)); - } - - @BuildStep - @Record(ExecutionTime.RUNTIME_INIT) - void configureClient(List clients, SesRecorder recorder, + void createClientBuilders(SesRecorder recorder, AmazonClientRecorder commonRecorder, SesConfig runtimeConfig, - BuildProducer producer) { - - initClientBuilders(clients, commonRecorder, recorder.getAwsConfig(runtimeConfig), recorder.getSdkConfig(runtimeConfig), - buildTimeConfig.sdk, producer); - } - - @BuildStep - @Record(ExecutionTime.RUNTIME_INIT) - void buildClients(List configuredClients, SesRecorder recorder, - BeanContainerBuildItem beanContainer, - ShutdownContextBuildItem shutdown) { - - buildClients(configuredClients, - (syncBuilder) -> recorder.buildClient(syncBuilder, beanContainer.getValue(), shutdown), - (asyncBuilder) -> recorder.buildAsyncClient(asyncBuilder, beanContainer.getValue(), shutdown)); + List syncTransports, + List asyncTransports, + BuildProducer syntheticBeans) { + + createClientBuilders(commonRecorder, + recorder.getAwsConfig(runtimeConfig), + recorder.getSdkConfig(runtimeConfig), + buildTimeConfig.sdk, + syncTransports, + asyncTransports, + SesClientBuilder.class, + (syncTransport) -> recorder.createSyncBuilder(runtimeConfig, syncTransport), + SesAsyncClientBuilder.class, + (asyncTransport) -> recorder.createAsyncBuilder(runtimeConfig, asyncTransport), + syntheticBeans); } } diff --git a/extensions/amazon-services/ses/runtime/src/main/java/io/quarkus/amazon/ses/runtime/SesClientProducer.java b/extensions/amazon-services/ses/runtime/src/main/java/io/quarkus/amazon/ses/runtime/SesClientProducer.java index fbc95a83a91c3..c222e61127750 100644 --- a/extensions/amazon-services/ses/runtime/src/main/java/io/quarkus/amazon/ses/runtime/SesClientProducer.java +++ b/extensions/amazon-services/ses/runtime/src/main/java/io/quarkus/amazon/ses/runtime/SesClientProducer.java @@ -2,6 +2,7 @@ import javax.annotation.PreDestroy; import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Instance; import javax.enterprise.inject.Produces; import software.amazon.awssdk.services.ses.SesAsyncClient; @@ -11,42 +12,40 @@ @ApplicationScoped public class SesClientProducer { + private final SesClient syncClient; + private final SesAsyncClient asyncClient; - private volatile SesClientBuilder syncConfiguredBuilder; - private volatile SesAsyncClientBuilder asyncConfiguredBuilder; - - private SesClient client; - private SesAsyncClient asyncClient; + SesClientProducer(Instance syncClientBuilderInstance, + Instance asyncClientBuilderInstance) { + this.syncClient = syncClientBuilderInstance.isResolvable() ? syncClientBuilderInstance.get().build() : null; + this.asyncClient = asyncClientBuilderInstance.isResolvable() ? asyncClientBuilderInstance.get().build() : null; + } @Produces @ApplicationScoped public SesClient client() { - client = syncConfiguredBuilder.build(); - return client; + if (syncClient == null) { + throw new IllegalStateException("The SesClient is required but has not been detected/configured."); + } + return syncClient; } @Produces @ApplicationScoped public SesAsyncClient asyncClient() { - asyncClient = asyncConfiguredBuilder.build(); + if (asyncClient == null) { + throw new IllegalStateException("The SesAsyncClient is required but has not been detected/configured."); + } return asyncClient; } @PreDestroy public void destroy() { - if (client != null) { - client.close(); + if (syncClient != null) { + syncClient.close(); } if (asyncClient != null) { asyncClient.close(); } } - - public void setSyncConfiguredBuilder(SesClientBuilder syncConfiguredBuilder) { - this.syncConfiguredBuilder = syncConfiguredBuilder; - } - - public void setAsyncConfiguredBuilder(SesAsyncClientBuilder asyncConfiguredBuilder) { - this.asyncConfiguredBuilder = asyncConfiguredBuilder; - } -} +} \ No newline at end of file diff --git a/extensions/amazon-services/ses/runtime/src/main/java/io/quarkus/amazon/ses/runtime/SesRecorder.java b/extensions/amazon-services/ses/runtime/src/main/java/io/quarkus/amazon/ses/runtime/SesRecorder.java index 51dae0c8e85ab..fa0a2ac8f8734 100644 --- a/extensions/amazon-services/ses/runtime/src/main/java/io/quarkus/amazon/ses/runtime/SesRecorder.java +++ b/extensions/amazon-services/ses/runtime/src/main/java/io/quarkus/amazon/ses/runtime/SesRecorder.java @@ -4,9 +4,7 @@ import io.quarkus.amazon.common.runtime.NettyHttpClientConfig; import io.quarkus.amazon.common.runtime.SdkConfig; import io.quarkus.amazon.common.runtime.SyncHttpClientConfig; -import io.quarkus.arc.runtime.BeanContainer; import io.quarkus.runtime.RuntimeValue; -import io.quarkus.runtime.ShutdownContext; import io.quarkus.runtime.annotations.Recorder; import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder; import software.amazon.awssdk.http.SdkHttpClient; @@ -52,22 +50,4 @@ public RuntimeValue createAsyncBuilder(SesConfig config, } return new RuntimeValue<>(builder); } - - public RuntimeValue buildClient(RuntimeValue builder, - BeanContainer beanContainer, - ShutdownContext shutdown) { - SesClientProducer producer = beanContainer.instance(SesClientProducer.class); - producer.setSyncConfiguredBuilder((SesClientBuilder) builder.getValue()); - shutdown.addShutdownTask(producer::destroy); - return new RuntimeValue<>(producer.client()); - } - - public RuntimeValue buildAsyncClient(RuntimeValue builder, - BeanContainer beanContainer, - ShutdownContext shutdown) { - SesClientProducer producer = beanContainer.instance(SesClientProducer.class); - producer.setAsyncConfiguredBuilder((SesAsyncClientBuilder) builder.getValue()); - shutdown.addShutdownTask(producer::destroy); - return new RuntimeValue<>(producer.asyncClient()); - } } diff --git a/extensions/amazon-services/sns/deployment/src/main/java/io/quarkus/amazon/sns/deployment/SnsProcessor.java b/extensions/amazon-services/sns/deployment/src/main/java/io/quarkus/amazon/sns/deployment/SnsProcessor.java index b4240b44c288d..8f29eadf6188f 100644 --- a/extensions/amazon-services/sns/deployment/src/main/java/io/quarkus/amazon/sns/deployment/SnsProcessor.java +++ b/extensions/amazon-services/sns/deployment/src/main/java/io/quarkus/amazon/sns/deployment/SnsProcessor.java @@ -7,8 +7,6 @@ import io.quarkus.amazon.common.deployment.AbstractAmazonServiceProcessor; import io.quarkus.amazon.common.deployment.AmazonClientAsyncTransportBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientBuildItem; -import io.quarkus.amazon.common.deployment.AmazonClientBuilderBuildItem; -import io.quarkus.amazon.common.deployment.AmazonClientBuilderConfiguredBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientInterceptorsPathBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientSyncTransportBuildItem; import io.quarkus.amazon.common.deployment.AmazonHttpClients; @@ -21,8 +19,8 @@ import io.quarkus.amazon.sns.runtime.SnsConfig; import io.quarkus.amazon.sns.runtime.SnsRecorder; import io.quarkus.arc.deployment.AdditionalBeanBuildItem; -import io.quarkus.arc.deployment.BeanContainerBuildItem; import io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem; +import io.quarkus.arc.deployment.SyntheticBeanBuildItem; import io.quarkus.deployment.Feature; import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; @@ -30,9 +28,10 @@ import io.quarkus.deployment.annotations.Record; import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem; import io.quarkus.deployment.builditem.FeatureBuildItem; -import io.quarkus.deployment.builditem.ShutdownContextBuildItem; import software.amazon.awssdk.services.sns.SnsAsyncClient; +import software.amazon.awssdk.services.sns.SnsAsyncClientBuilder; import software.amazon.awssdk.services.sns.SnsClient; +import software.amazon.awssdk.services.sns.SnsClientBuilder; public class SnsProcessor extends AbstractAmazonServiceProcessor { @@ -120,34 +119,23 @@ void setupNettyAsyncTransport(List amazonClients, SnsReco @BuildStep @Record(ExecutionTime.RUNTIME_INIT) - void createClientBuilders(List syncTransports, - List asyncTransports, SnsRecorder recorder, - SnsConfig runtimeConfig, BuildProducer builderProducer) { - - createClientBuilders(syncTransports, asyncTransports, builderProducer, - (syncTransport) -> recorder.createSyncBuilder(runtimeConfig, syncTransport), - (asyncTransport) -> recorder.createAsyncBuilder(runtimeConfig, asyncTransport)); - } - - @BuildStep - @Record(ExecutionTime.RUNTIME_INIT) - void configureClient(List clients, SnsRecorder recorder, + void createClientBuilders(SnsRecorder recorder, AmazonClientRecorder commonRecorder, SnsConfig runtimeConfig, - BuildProducer producer) { + List syncTransports, + List asyncTransports, + BuildProducer syntheticBeans) { - initClientBuilders(clients, commonRecorder, recorder.getAwsConfig(runtimeConfig), recorder.getSdkConfig(runtimeConfig), - buildTimeConfig.sdk, producer); - } - - @BuildStep - @Record(ExecutionTime.RUNTIME_INIT) - void buildClients(List configuredClients, SnsRecorder recorder, - BeanContainerBuildItem beanContainer, - ShutdownContextBuildItem shutdown) { - - buildClients(configuredClients, - (syncBuilder) -> recorder.buildClient(syncBuilder, beanContainer.getValue(), shutdown), - (asyncBuilder) -> recorder.buildAsyncClient(asyncBuilder, beanContainer.getValue(), shutdown)); + createClientBuilders(commonRecorder, + recorder.getAwsConfig(runtimeConfig), + recorder.getSdkConfig(runtimeConfig), + buildTimeConfig.sdk, + syncTransports, + asyncTransports, + SnsClientBuilder.class, + (syncTransport) -> recorder.createSyncBuilder(runtimeConfig, syncTransport), + SnsAsyncClientBuilder.class, + (asyncTransport) -> recorder.createAsyncBuilder(runtimeConfig, asyncTransport), + syntheticBeans); } } diff --git a/extensions/amazon-services/sns/runtime/src/main/java/io/quarkus/amazon/sns/runtime/SnsClientProducer.java b/extensions/amazon-services/sns/runtime/src/main/java/io/quarkus/amazon/sns/runtime/SnsClientProducer.java index 874b9c407603b..2bc937309700e 100644 --- a/extensions/amazon-services/sns/runtime/src/main/java/io/quarkus/amazon/sns/runtime/SnsClientProducer.java +++ b/extensions/amazon-services/sns/runtime/src/main/java/io/quarkus/amazon/sns/runtime/SnsClientProducer.java @@ -2,6 +2,7 @@ import javax.annotation.PreDestroy; import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Instance; import javax.enterprise.inject.Produces; import software.amazon.awssdk.services.sns.SnsAsyncClient; @@ -11,41 +12,40 @@ @ApplicationScoped public class SnsClientProducer { - private volatile SnsClientBuilder syncConfiguredBuilder; - private volatile SnsAsyncClientBuilder asyncConfiguredBuilder; + private final SnsClient syncClient; + private final SnsAsyncClient asyncClient; - private SnsClient client; - private SnsAsyncClient asyncClient; + SnsClientProducer(Instance syncClientBuilderInstance, + Instance asyncClientBuilderInstance) { + this.syncClient = syncClientBuilderInstance.isResolvable() ? syncClientBuilderInstance.get().build() : null; + this.asyncClient = asyncClientBuilderInstance.isResolvable() ? asyncClientBuilderInstance.get().build() : null; + } @Produces @ApplicationScoped public SnsClient client() { - client = syncConfiguredBuilder.build(); - return client; + if (syncClient == null) { + throw new IllegalStateException("The SnsClient is required but has not been detected/configured."); + } + return syncClient; } @Produces @ApplicationScoped public SnsAsyncClient asyncClient() { - asyncClient = asyncConfiguredBuilder.build(); + if (asyncClient == null) { + throw new IllegalStateException("The SnsAsyncClient is required but has not been detected/configured."); + } return asyncClient; } @PreDestroy public void destroy() { - if (client != null) { - client.close(); + if (syncClient != null) { + syncClient.close(); } if (asyncClient != null) { asyncClient.close(); } } - - public void setSyncConfiguredBuilder(SnsClientBuilder syncConfiguredBuilder) { - this.syncConfiguredBuilder = syncConfiguredBuilder; - } - - public void setAsyncConfiguredBuilder(SnsAsyncClientBuilder asyncConfiguredBuilder) { - this.asyncConfiguredBuilder = asyncConfiguredBuilder; - } } diff --git a/extensions/amazon-services/sns/runtime/src/main/java/io/quarkus/amazon/sns/runtime/SnsRecorder.java b/extensions/amazon-services/sns/runtime/src/main/java/io/quarkus/amazon/sns/runtime/SnsRecorder.java index cd1003eac31eb..58f8a12287e32 100644 --- a/extensions/amazon-services/sns/runtime/src/main/java/io/quarkus/amazon/sns/runtime/SnsRecorder.java +++ b/extensions/amazon-services/sns/runtime/src/main/java/io/quarkus/amazon/sns/runtime/SnsRecorder.java @@ -4,9 +4,7 @@ import io.quarkus.amazon.common.runtime.NettyHttpClientConfig; import io.quarkus.amazon.common.runtime.SdkConfig; import io.quarkus.amazon.common.runtime.SyncHttpClientConfig; -import io.quarkus.arc.runtime.BeanContainer; import io.quarkus.runtime.RuntimeValue; -import io.quarkus.runtime.ShutdownContext; import io.quarkus.runtime.annotations.Recorder; import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder; import software.amazon.awssdk.http.SdkHttpClient.Builder; @@ -53,22 +51,4 @@ public RuntimeValue createAsyncBuilder(SnsConfig config, } return new RuntimeValue<>(builder); } - - public RuntimeValue buildClient(RuntimeValue builder, - BeanContainer beanContainer, - ShutdownContext shutdown) { - SnsClientProducer producer = beanContainer.instance(SnsClientProducer.class); - producer.setSyncConfiguredBuilder((SnsClientBuilder) builder.getValue()); - shutdown.addShutdownTask(producer::destroy); - return new RuntimeValue<>(producer.client()); - } - - public RuntimeValue buildAsyncClient(RuntimeValue builder, - BeanContainer beanContainer, - ShutdownContext shutdown) { - SnsClientProducer producer = beanContainer.instance(SnsClientProducer.class); - producer.setAsyncConfiguredBuilder((SnsAsyncClientBuilder) builder.getValue()); - shutdown.addShutdownTask(producer::destroy); - return new RuntimeValue<>(producer.asyncClient()); - } } diff --git a/extensions/amazon-services/sqs/deployment/src/main/java/io/quarkus/amazon/sqs/deployment/SqsProcessor.java b/extensions/amazon-services/sqs/deployment/src/main/java/io/quarkus/amazon/sqs/deployment/SqsProcessor.java index b718a9239f458..d2ae0a43cd2cb 100644 --- a/extensions/amazon-services/sqs/deployment/src/main/java/io/quarkus/amazon/sqs/deployment/SqsProcessor.java +++ b/extensions/amazon-services/sqs/deployment/src/main/java/io/quarkus/amazon/sqs/deployment/SqsProcessor.java @@ -7,8 +7,6 @@ import io.quarkus.amazon.common.deployment.AbstractAmazonServiceProcessor; import io.quarkus.amazon.common.deployment.AmazonClientAsyncTransportBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientBuildItem; -import io.quarkus.amazon.common.deployment.AmazonClientBuilderBuildItem; -import io.quarkus.amazon.common.deployment.AmazonClientBuilderConfiguredBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientInterceptorsPathBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientSyncTransportBuildItem; import io.quarkus.amazon.common.deployment.AmazonHttpClients; @@ -21,8 +19,8 @@ import io.quarkus.amazon.sqs.runtime.SqsConfig; import io.quarkus.amazon.sqs.runtime.SqsRecorder; import io.quarkus.arc.deployment.AdditionalBeanBuildItem; -import io.quarkus.arc.deployment.BeanContainerBuildItem; import io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem; +import io.quarkus.arc.deployment.SyntheticBeanBuildItem; import io.quarkus.deployment.Feature; import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; @@ -30,9 +28,10 @@ import io.quarkus.deployment.annotations.Record; import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem; import io.quarkus.deployment.builditem.FeatureBuildItem; -import io.quarkus.deployment.builditem.ShutdownContextBuildItem; import software.amazon.awssdk.services.sqs.SqsAsyncClient; +import software.amazon.awssdk.services.sqs.SqsAsyncClientBuilder; import software.amazon.awssdk.services.sqs.SqsClient; +import software.amazon.awssdk.services.sqs.SqsClientBuilder; public class SqsProcessor extends AbstractAmazonServiceProcessor { @@ -120,34 +119,23 @@ void setupNettyAsyncTransport(List amazonClients, SqsReco @BuildStep @Record(ExecutionTime.RUNTIME_INIT) - void createClientBuilders(List syncTransports, - List asyncTransports, SqsRecorder recorder, - SqsConfig runtimeConfig, BuildProducer builderProducer) { - - createClientBuilders(syncTransports, asyncTransports, builderProducer, - (syncTransport) -> recorder.createSyncBuilder(runtimeConfig, syncTransport), - (asyncTransport) -> recorder.createAsyncBuilder(runtimeConfig, asyncTransport)); - } - - @BuildStep - @Record(ExecutionTime.RUNTIME_INIT) - void configureClient(List clients, SqsRecorder recorder, + void createClientBuilders(SqsRecorder recorder, AmazonClientRecorder commonRecorder, SqsConfig runtimeConfig, - BuildProducer producer) { + List syncTransports, + List asyncTransports, + BuildProducer syntheticBeans) { - initClientBuilders(clients, commonRecorder, recorder.getAwsConfig(runtimeConfig), recorder.getSdkConfig(runtimeConfig), - buildTimeConfig.sdk, producer); - } - - @BuildStep - @Record(ExecutionTime.RUNTIME_INIT) - void buildClients(List configuredClients, SqsRecorder recorder, - BeanContainerBuildItem beanContainer, - ShutdownContextBuildItem shutdown) { - - buildClients(configuredClients, - (syncBuilder) -> recorder.buildClient(syncBuilder, beanContainer.getValue(), shutdown), - (asyncBuilder) -> recorder.buildAsyncClient(asyncBuilder, beanContainer.getValue(), shutdown)); + createClientBuilders(commonRecorder, + recorder.getAwsConfig(runtimeConfig), + recorder.getSdkConfig(runtimeConfig), + buildTimeConfig.sdk, + syncTransports, + asyncTransports, + SqsClientBuilder.class, + (syncTransport) -> recorder.createSyncBuilder(runtimeConfig, syncTransport), + SqsAsyncClientBuilder.class, + (asyncTransport) -> recorder.createAsyncBuilder(runtimeConfig, asyncTransport), + syntheticBeans); } } diff --git a/extensions/amazon-services/sqs/runtime/src/main/java/io/quarkus/amazon/sqs/runtime/SqsClientProducer.java b/extensions/amazon-services/sqs/runtime/src/main/java/io/quarkus/amazon/sqs/runtime/SqsClientProducer.java index 787c1b86cfa2e..f015d3acc3f4a 100644 --- a/extensions/amazon-services/sqs/runtime/src/main/java/io/quarkus/amazon/sqs/runtime/SqsClientProducer.java +++ b/extensions/amazon-services/sqs/runtime/src/main/java/io/quarkus/amazon/sqs/runtime/SqsClientProducer.java @@ -2,6 +2,7 @@ import javax.annotation.PreDestroy; import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Instance; import javax.enterprise.inject.Produces; import software.amazon.awssdk.services.sqs.SqsAsyncClient; @@ -11,41 +12,40 @@ @ApplicationScoped public class SqsClientProducer { - private volatile SqsClientBuilder syncConfiguredBuilder; - private volatile SqsAsyncClientBuilder asyncConfiguredBuilder; + private final SqsClient syncClient; + private final SqsAsyncClient asyncClient; - private SqsClient client; - private SqsAsyncClient asyncClient; + SqsClientProducer(Instance syncClientBuilderInstance, + Instance asyncClientBuilderInstance) { + this.syncClient = syncClientBuilderInstance.isResolvable() ? syncClientBuilderInstance.get().build() : null; + this.asyncClient = asyncClientBuilderInstance.isResolvable() ? asyncClientBuilderInstance.get().build() : null; + } @Produces @ApplicationScoped public SqsClient client() { - client = syncConfiguredBuilder.build(); - return client; + if (syncClient == null) { + throw new IllegalStateException("The SqsClient is required but has not been detected/configured."); + } + return syncClient; } @Produces @ApplicationScoped public SqsAsyncClient asyncClient() { - asyncClient = asyncConfiguredBuilder.build(); + if (asyncClient == null) { + throw new IllegalStateException("The SqsAsyncClient is required but has not been detected/configured."); + } return asyncClient; } @PreDestroy public void destroy() { - if (client != null) { - client.close(); + if (syncClient != null) { + syncClient.close(); } if (asyncClient != null) { asyncClient.close(); } } - - public void setSyncConfiguredBuilder(SqsClientBuilder syncConfiguredBuilder) { - this.syncConfiguredBuilder = syncConfiguredBuilder; - } - - public void setAsyncConfiguredBuilder(SqsAsyncClientBuilder asyncConfiguredBuilder) { - this.asyncConfiguredBuilder = asyncConfiguredBuilder; - } } diff --git a/extensions/amazon-services/sqs/runtime/src/main/java/io/quarkus/amazon/sqs/runtime/SqsRecorder.java b/extensions/amazon-services/sqs/runtime/src/main/java/io/quarkus/amazon/sqs/runtime/SqsRecorder.java index 454c25b9880d7..931ffd351a479 100644 --- a/extensions/amazon-services/sqs/runtime/src/main/java/io/quarkus/amazon/sqs/runtime/SqsRecorder.java +++ b/extensions/amazon-services/sqs/runtime/src/main/java/io/quarkus/amazon/sqs/runtime/SqsRecorder.java @@ -4,9 +4,7 @@ import io.quarkus.amazon.common.runtime.NettyHttpClientConfig; import io.quarkus.amazon.common.runtime.SdkConfig; import io.quarkus.amazon.common.runtime.SyncHttpClientConfig; -import io.quarkus.arc.runtime.BeanContainer; import io.quarkus.runtime.RuntimeValue; -import io.quarkus.runtime.ShutdownContext; import io.quarkus.runtime.annotations.Recorder; import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder; import software.amazon.awssdk.http.SdkHttpClient.Builder; @@ -53,22 +51,4 @@ public RuntimeValue createAsyncBuilder(SqsConfig config, } return new RuntimeValue<>(builder); } - - public RuntimeValue buildClient(RuntimeValue builder, - BeanContainer beanContainer, - ShutdownContext shutdown) { - SqsClientProducer producer = beanContainer.instance(SqsClientProducer.class); - producer.setSyncConfiguredBuilder((SqsClientBuilder) builder.getValue()); - shutdown.addShutdownTask(producer::destroy); - return new RuntimeValue<>(producer.client()); - } - - public RuntimeValue buildAsyncClient(RuntimeValue 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()); - } } diff --git a/extensions/amazon-services/ssm/deployment/src/main/java/io/quarkus/amazon/ssm/deployment/SsmProcessor.java b/extensions/amazon-services/ssm/deployment/src/main/java/io/quarkus/amazon/ssm/deployment/SsmProcessor.java index 3e7ac5d62e3a6..f4d94b16ac189 100644 --- a/extensions/amazon-services/ssm/deployment/src/main/java/io/quarkus/amazon/ssm/deployment/SsmProcessor.java +++ b/extensions/amazon-services/ssm/deployment/src/main/java/io/quarkus/amazon/ssm/deployment/SsmProcessor.java @@ -7,8 +7,6 @@ import io.quarkus.amazon.common.deployment.AbstractAmazonServiceProcessor; import io.quarkus.amazon.common.deployment.AmazonClientAsyncTransportBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientBuildItem; -import io.quarkus.amazon.common.deployment.AmazonClientBuilderBuildItem; -import io.quarkus.amazon.common.deployment.AmazonClientBuilderConfiguredBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientInterceptorsPathBuildItem; import io.quarkus.amazon.common.deployment.AmazonClientSyncTransportBuildItem; import io.quarkus.amazon.common.deployment.AmazonHttpClients; @@ -21,8 +19,8 @@ import io.quarkus.amazon.ssm.runtime.SsmConfig; import io.quarkus.amazon.ssm.runtime.SsmRecorder; import io.quarkus.arc.deployment.AdditionalBeanBuildItem; -import io.quarkus.arc.deployment.BeanContainerBuildItem; import io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem; +import io.quarkus.arc.deployment.SyntheticBeanBuildItem; import io.quarkus.deployment.Feature; import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; @@ -30,9 +28,10 @@ import io.quarkus.deployment.annotations.Record; import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem; import io.quarkus.deployment.builditem.FeatureBuildItem; -import io.quarkus.deployment.builditem.ShutdownContextBuildItem; import software.amazon.awssdk.services.ssm.SsmAsyncClient; +import software.amazon.awssdk.services.ssm.SsmAsyncClientBuilder; import software.amazon.awssdk.services.ssm.SsmClient; +import software.amazon.awssdk.services.ssm.SsmClientBuilder; public class SsmProcessor extends AbstractAmazonServiceProcessor { @@ -119,34 +118,23 @@ void setupNettyAsyncTransport(List amazonClients, SsmReco @BuildStep @Record(ExecutionTime.RUNTIME_INIT) - void createClientBuilders(List syncTransports, - List asyncTransports, SsmRecorder recorder, - SsmConfig runtimeConfig, BuildProducer builderProducer) { - - createClientBuilders(syncTransports, asyncTransports, builderProducer, - (syncTransport) -> recorder.createSyncBuilder(runtimeConfig, syncTransport), - (asyncTransport) -> recorder.createAsyncBuilder(runtimeConfig, asyncTransport)); - } - - @BuildStep - @Record(ExecutionTime.RUNTIME_INIT) - void configureClient(List clients, SsmRecorder recorder, + void createClientBuilders(SsmRecorder recorder, AmazonClientRecorder commonRecorder, SsmConfig runtimeConfig, - BuildProducer producer) { - - initClientBuilders(clients, commonRecorder, recorder.getAwsConfig(runtimeConfig), recorder.getSdkConfig(runtimeConfig), - buildTimeConfig.sdk, producer); - } - - @BuildStep - @Record(ExecutionTime.RUNTIME_INIT) - void buildClients(List configuredClients, SsmRecorder recorder, - BeanContainerBuildItem beanContainer, - ShutdownContextBuildItem shutdown) { - - buildClients(configuredClients, - (syncBuilder) -> recorder.buildClient(syncBuilder, beanContainer.getValue(), shutdown), - (asyncBuilder) -> recorder.buildAsyncClient(asyncBuilder, beanContainer.getValue(), shutdown)); + List syncTransports, + List asyncTransports, + BuildProducer syntheticBeans) { + + createClientBuilders(commonRecorder, + recorder.getAwsConfig(runtimeConfig), + recorder.getSdkConfig(runtimeConfig), + buildTimeConfig.sdk, + syncTransports, + asyncTransports, + SsmClientBuilder.class, + (syncTransport) -> recorder.createSyncBuilder(runtimeConfig, syncTransport), + SsmAsyncClientBuilder.class, + (asyncTransport) -> recorder.createAsyncBuilder(runtimeConfig, asyncTransport), + syntheticBeans); } } diff --git a/extensions/amazon-services/ssm/runtime/src/main/java/io/quarkus/amazon/ssm/runtime/SsmClientProducer.java b/extensions/amazon-services/ssm/runtime/src/main/java/io/quarkus/amazon/ssm/runtime/SsmClientProducer.java index 88c168f13d8bd..d33d7e4aefe05 100644 --- a/extensions/amazon-services/ssm/runtime/src/main/java/io/quarkus/amazon/ssm/runtime/SsmClientProducer.java +++ b/extensions/amazon-services/ssm/runtime/src/main/java/io/quarkus/amazon/ssm/runtime/SsmClientProducer.java @@ -2,6 +2,7 @@ import javax.annotation.PreDestroy; import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Instance; import javax.enterprise.inject.Produces; import software.amazon.awssdk.services.ssm.SsmAsyncClient; @@ -11,42 +12,40 @@ @ApplicationScoped public class SsmClientProducer { + private final SsmClient syncClient; + private final SsmAsyncClient asyncClient; - private volatile SsmClientBuilder syncConfiguredBuilder; - private volatile SsmAsyncClientBuilder asyncConfiguredBuilder; - - private SsmClient client; - private SsmAsyncClient asyncClient; + SsmClientProducer(Instance syncClientBuilderInstance, + Instance asyncClientBuilderInstance) { + this.syncClient = syncClientBuilderInstance.isResolvable() ? syncClientBuilderInstance.get().build() : null; + this.asyncClient = asyncClientBuilderInstance.isResolvable() ? asyncClientBuilderInstance.get().build() : null; + } @Produces @ApplicationScoped public SsmClient client() { - client = syncConfiguredBuilder.build(); - return client; + if (syncClient == null) { + throw new IllegalStateException("The SsmClient is required but has not been detected/configured."); + } + return syncClient; } @Produces @ApplicationScoped public SsmAsyncClient asyncClient() { - asyncClient = asyncConfiguredBuilder.build(); + if (asyncClient == null) { + throw new IllegalStateException("The SsmAsyncClient is required but has not been detected/configured."); + } return asyncClient; } @PreDestroy public void destroy() { - if (client != null) { - client.close(); + if (syncClient != null) { + syncClient.close(); } if (asyncClient != null) { asyncClient.close(); } } - - public void setSyncConfiguredBuilder(SsmClientBuilder syncConfiguredBuilder) { - this.syncConfiguredBuilder = syncConfiguredBuilder; - } - - public void setAsyncConfiguredBuilder(SsmAsyncClientBuilder asyncConfiguredBuilder) { - this.asyncConfiguredBuilder = asyncConfiguredBuilder; - } } diff --git a/extensions/amazon-services/ssm/runtime/src/main/java/io/quarkus/amazon/ssm/runtime/SsmRecorder.java b/extensions/amazon-services/ssm/runtime/src/main/java/io/quarkus/amazon/ssm/runtime/SsmRecorder.java index b74a2aa26f90c..3add0adb99cbd 100644 --- a/extensions/amazon-services/ssm/runtime/src/main/java/io/quarkus/amazon/ssm/runtime/SsmRecorder.java +++ b/extensions/amazon-services/ssm/runtime/src/main/java/io/quarkus/amazon/ssm/runtime/SsmRecorder.java @@ -4,9 +4,7 @@ import io.quarkus.amazon.common.runtime.NettyHttpClientConfig; import io.quarkus.amazon.common.runtime.SdkConfig; import io.quarkus.amazon.common.runtime.SyncHttpClientConfig; -import io.quarkus.arc.runtime.BeanContainer; import io.quarkus.runtime.RuntimeValue; -import io.quarkus.runtime.ShutdownContext; import io.quarkus.runtime.annotations.Recorder; import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder; import software.amazon.awssdk.http.SdkHttpClient; @@ -52,22 +50,4 @@ public RuntimeValue createAsyncBuilder(SsmConfig config, } return new RuntimeValue<>(builder); } - - public RuntimeValue buildClient(RuntimeValue builder, - BeanContainer beanContainer, - ShutdownContext shutdown) { - SsmClientProducer producer = beanContainer.instance(SsmClientProducer.class); - producer.setSyncConfiguredBuilder((SsmClientBuilder) builder.getValue()); - shutdown.addShutdownTask(producer::destroy); - return new RuntimeValue<>(producer.client()); - } - - public RuntimeValue buildAsyncClient(RuntimeValue builder, - BeanContainer beanContainer, - ShutdownContext shutdown) { - SsmClientProducer producer = beanContainer.instance(SsmClientProducer.class); - producer.setAsyncConfiguredBuilder((SsmAsyncClientBuilder) builder.getValue()); - shutdown.addShutdownTask(producer::destroy); - return new RuntimeValue<>(producer.asyncClient()); - } } From 34d8f4bca44ac035eaa1e64a3d07d5b3c790a171 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Thu, 10 Jun 2021 14:56:10 +0200 Subject: [PATCH 2/2] Amazon Services - Unwrap Instance when inspecting injection points Fixes #15213 --- .../AbstractAmazonServiceProcessor.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/extensions/amazon-services/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/AbstractAmazonServiceProcessor.java b/extensions/amazon-services/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/AbstractAmazonServiceProcessor.java index 41a27a520f476..7f2886b6ee4ff 100644 --- a/extensions/amazon-services/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/AbstractAmazonServiceProcessor.java +++ b/extensions/amazon-services/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/AbstractAmazonServiceProcessor.java @@ -7,6 +7,7 @@ import javax.enterprise.context.ApplicationScoped; import org.jboss.jandex.DotName; +import org.jboss.jandex.ParameterizedType; import org.jboss.jandex.Type; import io.quarkus.amazon.common.runtime.AmazonClientApacheTransportRecorder; @@ -22,6 +23,7 @@ import io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem; import io.quarkus.arc.deployment.SyntheticBeanBuildItem; import io.quarkus.arc.processor.BuildExtension; +import io.quarkus.arc.processor.DotNames; import io.quarkus.arc.processor.InjectionPointInfo; import io.quarkus.deployment.Feature; import io.quarkus.deployment.annotations.BuildProducer; @@ -61,12 +63,12 @@ protected void setupExtension(BeanRegistrationPhaseBuildItem beanRegistrationPha //Discover all clients injections in order to determine if async or sync client is required for (InjectionPointInfo injectionPoint : beanRegistrationPhase.getContext().get(BuildExtension.Key.INJECTION_POINTS)) { - Type requiredType = injectionPoint.getRequiredType(); + Type injectedType = getInjectedType(injectionPoint); - if (syncClientName().equals(requiredType.name())) { + if (syncClientName().equals(injectedType.name())) { syncClassName = Optional.of(syncClientName()); } - if (asyncClientName().equals(requiredType.name())) { + if (asyncClientName().equals(injectedType.name())) { asyncClassName = Optional.of(asyncClientName()); } } @@ -203,4 +205,15 @@ protected void createClientBuilders( .done()); } } + + private Type getInjectedType(InjectionPointInfo injectionPoint) { + Type requiredType = injectionPoint.getRequiredType(); + Type injectedType = requiredType; + + if (DotNames.INSTANCE.equals(requiredType.name()) && requiredType instanceof ParameterizedType) { + injectedType = requiredType.asParameterizedType().arguments().get(0); + } + + return injectedType; + } }