【问题标题】:AWS cognito `connection pool shutdown`AWS cognito `连接池关闭`
【发布时间】:2022-01-08 01:01:40
【问题描述】:

在我的 spring boot 项目中,我首先在 cognito 中创建一个用户池,然后将一个用户添加到用户池中。将用户添加到用户池时,我需要创建一个userPoolClient

我正在创建具有所有必需参数的用户客户端

CreateUserPoolClientResponse response = cognitoClient.createUserPoolClient(clientRequest);

但它的行为很奇怪。当我运行代码时,它会抛出connection pool shut down,但是当我在带有断点的调试模式下运行我的代码时,它能够成功创建user pool client。我不确定连接池的创建是否是异步过程,这就是为什么调试器在创建它之前需要一些时间?

有人遇到过这个问题吗?将不胜感激任何帮助。谢谢

【问题讨论】:

    标签: amazon-web-services amazon-cognito aws-userpools


    【解决方案1】:

    尝试为此用例运行 AWS SDK Java V2。这是 V2 的代码。

    package com.example.cognito;
    
    //snippet-start:[cognito.java2.user_pool.create_user_pool_client.import]
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.cognitoidentityprovider.CognitoIdentityProviderClient;
    import software.amazon.awssdk.services.cognitoidentityprovider.model.CognitoIdentityProviderException;
    import software.amazon.awssdk.services.cognitoidentityprovider.model.CreateUserPoolClientRequest;
    import software.amazon.awssdk.services.cognitoidentityprovider.model.CreateUserPoolClientResponse;
    //snippet-end:[cognito.java2.user_pool.create_user_pool_client.import]
    
    /**
     * To run this Java V2 code example, ensure that you have setup your development environment, including your credentials.
     *
     * For information, see this documentation topic:
     *
     * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
     */
    public class CreateUserPoolClient {
    
        public static void main(String[] args) {
            final String USAGE = "\n" +
                    "Usage:\n" +
                    "    <clientName> <userPoolId> \n\n" +
                    "Where:\n" +
                    "    clientName - the name for the user pool client to create.\n\n" +
                    "    userPoolId - the ID for the user pool.\n\n" ;
    
            if (args.length != 2) {
                System.out.println(USAGE);
                System.exit(1);
            }
    
            String clientName = args[0];
            String userPoolId = args[1];
    
            CognitoIdentityProviderClient cognitoClient = CognitoIdentityProviderClient.builder()
                    .region(Region.US_EAST_1)
                    .build();
    
            createPoolClient (cognitoClient, clientName, userPoolId) ;
            cognitoClient.close();
        }
    
        //snippet-start:[cognito.java2.user_pool.create_user_pool_client.main]
        public static void createPoolClient ( CognitoIdentityProviderClient cognitoClient,
                                              String clientName,
                                              String userPoolId ) {
    
            try {
    
                CreateUserPoolClientResponse response = cognitoClient.createUserPoolClient(
                        CreateUserPoolClientRequest.builder()
                                .clientName(clientName)
                                .userPoolId(userPoolId)
                                .build()
                );
    
                System.out.println("User pool " + response.userPoolClient().clientName() + " created. ID: " + response.userPoolClient().clientId());
    
            } catch (CognitoIdentityProviderException e){
                System.err.println(e.awsErrorDetails().errorMessage());
                System.exit(1);
            }
        }
        //snippet-end:[cognito.java2.user_pool.create_user_pool_client.main]
    }
    

    我刚刚在单元测试中运行了它,它运行良好。

    您可以在此处找到此服务的这个和其他代码示例:

    https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/cognito

    【讨论】:

    • 谢谢。让我试试。
    • 亚马逊建议使用 V2 而不是 V1。所有 V2 代码都在单元测试等中经过多次测试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多