简体   繁体   中英

tomcat session manager using dynamodb

I am trying to use tomcat session manger with dynamoDB.

I was taking reference from this aws guide http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/java-dg-tomcat-session-manager.html .

they have mentioned in the guide that we have to use

    <Context>
        <WatchedResource>WEB-INF/web.xml</WatchedResource>
        <Manager className="com.amazonaws.services.dynamodb.sessionmanager.DynamoDBSessionManager"
         awsAccessKey="myAccessKey"
         awsSecretKey="mySecretKey"
         createIfNotExist="true" />
    </Context>

so if i don't give any target region this by default will pick us-east-1(as its hardcoded in the .jar file mentioned in aws documention). and if i specify any region it will pick that region. but i have my application in multiple regions and want separate dynamo instances for each region. As for multiple regions session id's are not unique so i can't use the same dynamo instance for all the regions.

How it can be done, any help will be appreciated.

Its better you create multiple DyanoDB clients, one for each region. You should use some config/prop file, which contains the different aws access-key, secret and config and based on which you create the client at the application startups. And then use it according to your business logic.

For example :- I am using the ProfileCredentialsProvider and below config file in my application, Which uses multiple dynamoDB clients.

# AWS DynamoDB setting
mumDynamoDB:
  # profile name of aws IAM role which has access to dynamoDB
  profileName: mumdynamoDBdev
  region: ap-south-1 
OrgonDynamoDB:
  # profile name of aws IAM role which has access to dynamoDB
  profileName: OrgdynamoDBdev
  region: us-west-2

And below piece of code create clients based on region and profile.

public static DynamoDB getDocClient(String profileName, String region) {
        // http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/creating-clients.html new way of
        // creating all the aws clients.
        // thread safe impl
        AmazonDynamoDB amazonDynamoDBClient = AmazonDynamoDBClientBuilder.standard()
                .withRegion(region)
                .withCredentials(new ProfileCredentialsProvider(profileName)).build();
        docClient = new DynamoDB(amazonDynamoDBClient);

        return docClient;
    } 

Then in application, based on your requirement you can use appropriate client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM