You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@jclouds.apache.org by GARDAIS Ionel <io...@tech-advantage.com> on 2017/02/15 14:23:01 UTC

Life-span of a Context

Hi,

We are using JClouds on a server-side application.
That is, it is supposed to be long-lived, compared to a end-user application that could be closed after several minutes/hours of use.

     public byte[] getFile(String basePath, String containerName, String blobPath) {
         log.debug("load File in basePath: {}, containerName: {}, blobPath:{}", basePath, containerName, basePath);

         Properties properties = new java.util.Properties();
properties.setProperty(FilesystemConstants.PROPERTY_BASEDIR, basePath);

         BlobStoreContext context = ContextBuilder.newBuilder("filesystem").overrides(properties)
             .buildView(BlobStoreContext.class);

         BlobStore blobStore = context.getBlobStore();

         byte[] bFile = new byte[0];
         Blob blob = blobStore.getBlob(containerName, blobPath);

         if (null != blob) {
             log.debug("Blob headers : {}", blob.getAllHeaders());
             log.debug("Blob metadata : {}", blob.getMetadata());

             try {
                 bFile = IOUtils.toByteArray(blob.getPayload().openStream());
             } catch (IOException e) {
                 log.warn("Error reading file to input stream.", e);
             }
         } else {
             log.warn("Blob at {}/{} does not exist.", containerName, blobPath);
         }

         context.close();

         return bFile;
     }


The base directory is global for the whole application.
I wonder if the context and the blobstore should then be stored in a singleton a retrieved when needed, instead of being instantiated every 
time ?
(thus saving few milliseconds of works).

How long a context should remain open ?
How often should it be closed ?

Also, when using putBlob(), is the payload flushed directly or when the context is closed ?

Thanks,
Ionel
-- 
TECH'advantage SA - 1 rue Isabey 92500 RUEIL MALMAISON
Capital  EUR 219 300,00 - RCS Nanterre B 408 832 301 - TVA FR 09 408 832 301 00027


Re: Life-span of a Context

Posted by GARDAIS Ionel <io...@tech-advantage.com>.
HI Ignasi,

Thanks for your reply.

Ionel


On 02/15/2017 03:30 PM, Ignasi Barrera wrote:
> You should try to keep the context open as long as possible, and reuse it when needed.
>
> A context itself does not hold any connection to external services. It just provides the container for the IoC framework used in jclouds 
> (Guice), and the different ExecutorService instances jclouds uses to perform async oeprations. creating the context creates all those 
> resourecs, and closing it releases them all, so you'd better open the context (it is meant to be thread-safe), and leave it open as long 
> as it makes sense, and try to reuse it where possible.
>
> On 15 February 2017 at 15:23, GARDAIS Ionel <ionel.gardais@tech-advantage.com <ma...@tech-advantage.com>> wrote:
>
>     Hi,
>
>     We are using JClouds on a server-side application.
>     That is, it is supposed to be long-lived, compared to a end-user application that could be closed after several minutes/hours of use.
>
>         public byte[] getFile(String basePath, String containerName, String blobPath) {
>             log.debug("load File in basePath: {}, containerName: {}, blobPath:{}", basePath, containerName, basePath);
>
>             Properties properties = new java.util.Properties();
>     properties.setProperty(FilesystemConstants.PROPERTY_BASEDIR, basePath);
>
>             BlobStoreContext context = ContextBuilder.newBuilder("filesystem").overrides(properties)
>                 .buildView(BlobStoreContext.class);
>
>             BlobStore blobStore = context.getBlobStore();
>
>             byte[] bFile = new byte[0];
>             Blob blob = blobStore.getBlob(containerName, blobPath);
>
>             if (null != blob) {
>                 log.debug("Blob headers : {}", blob.getAllHeaders());
>                 log.debug("Blob metadata : {}", blob.getMetadata());
>
>                 try {
>                     bFile = IOUtils.toByteArray(blob.getPayload().openStream());
>                 } catch (IOException e) {
>                     log.warn("Error reading file to input stream.", e);
>                 }
>             } else {
>                 log.warn("Blob at {}/{} does not exist.", containerName, blobPath);
>             }
>
>             context.close();
>
>             return bFile;
>         }
>
>
>     The base directory is global for the whole application.
>     I wonder if the context and the blobstore should then be stored in a singleton a retrieved when needed, instead of being instantiated
>     every time ?
>     (thus saving few milliseconds of works).
>
>     How long a context should remain open ?
>     How often should it be closed ?
>
>     Also, when using putBlob(), is the payload flushed directly or when the context is closed ?
>
>     Thanks,
>     Ionel
>     -- 
>     TECH'advantage SA - 1 rue Isabey 92500 RUEIL MALMAISON
>     Capital  EUR 219 300,00 - RCS Nanterre B 408 832 301 - TVA FR 09 408 832 301 00027
>
>

-- 
Ionel GARDAIS
Tech'Advantage CIO - IT Team manager


-- 
TECH'advantage SA - 1 rue Isabey 92500 RUEIL MALMAISON
Capital  EUR 219 300,00 - RCS Nanterre B 408 832 301 - TVA FR 09 408 832 301 00027

Re: Life-span of a Context

Posted by Ignasi Barrera <na...@apache.org>.
You should try to keep the context open as long as possible, and reuse it
when needed.

A context itself does not hold any connection to external services. It just
provides the container for the IoC framework used in jclouds (Guice), and
the different ExecutorService instances jclouds uses to perform async
oeprations. creating the context creates all those resourecs, and closing
it releases them all, so you'd better open the context (it is meant to be
thread-safe), and leave it open as long as it makes sense, and try to reuse
it where possible.

On 15 February 2017 at 15:23, GARDAIS Ionel <
ionel.gardais@tech-advantage.com> wrote:

> Hi,
>
> We are using JClouds on a server-side application.
> That is, it is supposed to be long-lived, compared to a end-user
> application that could be closed after several minutes/hours of use.
>
>     public byte[] getFile(String basePath, String containerName, String
> blobPath) {
>         log.debug("load File in basePath: {}, containerName: {},
> blobPath:{}", basePath, containerName, basePath);
>
>         Properties properties = new java.util.Properties();
> properties.setProperty(FilesystemConstants.PROPERTY_BASEDIR, basePath);
>
>         BlobStoreContext context = ContextBuilder.newBuilder("fil
> esystem").overrides(properties)
>             .buildView(BlobStoreContext.class);
>
>         BlobStore blobStore = context.getBlobStore();
>
>         byte[] bFile = new byte[0];
>         Blob blob = blobStore.getBlob(containerName, blobPath);
>
>         if (null != blob) {
>             log.debug("Blob headers : {}", blob.getAllHeaders());
>             log.debug("Blob metadata : {}", blob.getMetadata());
>
>             try {
>                 bFile = IOUtils.toByteArray(blob.getPa
> yload().openStream());
>             } catch (IOException e) {
>                 log.warn("Error reading file to input stream.", e);
>             }
>         } else {
>             log.warn("Blob at {}/{} does not exist.", containerName,
> blobPath);
>         }
>
>         context.close();
>
>         return bFile;
>     }
>
>
> The base directory is global for the whole application.
> I wonder if the context and the blobstore should then be stored in a
> singleton a retrieved when needed, instead of being instantiated every time
> ?
> (thus saving few milliseconds of works).
>
> How long a context should remain open ?
> How often should it be closed ?
>
> Also, when using putBlob(), is the payload flushed directly or when the
> context is closed ?
>
> Thanks,
> Ionel
> --
> TECH'advantage SA - 1 rue Isabey 92500 RUEIL MALMAISON
> Capital  EUR 219 300,00 - RCS Nanterre B 408 832 301 - TVA FR 09 408 832
> 301 00027
>
>