简体   繁体   中英

how to code in right way with Azure table storage sdk for java servlet

i have a Servlet(Jersey2 + Jax-rs) API app running on Azure App Service.

This API is retrieving data from Azure Table Storage and send back to client side.

So here is my question, Which is better choice between "static method" and "instance" for implementing Azure Storage SDK.

for example what my codes look like is,

public class AzureTableStorage {

    private static final String storageConnectionString = "DefaultEndpointsProtocol=http;" + "AccountName=;"
            + "AccountKey=";

    public static CloudTable getTable() {
        try {

            CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

            CloudTableClient tableClient = storageAccount.createCloudTableClient();

            CloudTable cloudTable = tableClient.getTableReference("");

            return cloudTable;

        } catch (Exception e) {

            e.printStackTrace();

            return null;
        }
    }

    public static Entity getEntity(String rowKey) {
        // TODO Auto-generated method stub

        try {


            TableOperation operation = TableOperation.retrieve("", "", xxx.class);


            Entity entity = AzureTableStorage.getTable().execute(operation).getResultAsType();
            // Output the entity.

            return entity;

        } catch (Exception e) {
            // Output the stack trace.
            e.printStackTrace();
            return null;
        }

    }

}

and using like

AzureTableStorage.getEntity(rowKey);

Is this bad idea?

Please can anyone give me some answer?

BTW I already have looked,

Java Static vs Instance

Java: when to use static methods

But still cant find out.

Per my experience, I think the design pattern for using Azure Table Storage is very similar with ORM for RDBMS, but there seems to be no more need for considering performance optimization because the Azure Java SDK wrapped the related REST APIs.

So in my mind, using singleton pattern to declaring static object for CloudClient in some scenarios, such as batch task or schedule task. And creating a container as pool to control the number of CloudClient objects and getting a Client instance from the pool to do the operations by user sessions which include Create, Read, Update & Delete.

There are some offical samples on GitHub as best practices that I think you can refer to, please see https://github.com/Azure/azure-storage-java/tree/master/microsoft-azure-storage-samples/src/com/microsoft/azure/storage/table and https://github.com/Azure-Samples/storage-table-java-getting-started .

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