jclouds-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ignasi Barrera <n...@apache.org>
Subject Re: Fw: Is SwiftApi is thread safe?
Date Thu, 24 Dec 2015 09:14:04 GMT
Contexts and apis are thread-safe. I don't really know why that javadoc
says such an ambiguous thing, but apis are (and should be!) thread safe. We
should fix that doc string.

Regarding whether APIs are singleton or not, the api objects (all apis have
just one impl which is the proxy that generates the http requests) are
generated here [1]. That provider class is declared to be a singleton, but
that applies only to the provider itself; not to the result produced by its
"get()" method. The provider and the api class are bound to the Guice
context here [2,3], and as you can see those bindings are not using the
".in(Scopes.SINGLETON)", so every time the provider is called (an injection
is requested or an instance is directly requested to the Guice injector) a
new api proxy will be created.

This said, there are two main points where apis are requested to Guice:
when calling the "ContextBuilder.buildApi()" and when calling the
"context.getApi()" method on an existing context.

In the first case users are not building an entire context, just the api,
so that's all they get. There will only be that instance unless they
manually request one to the Guice injector or build a new one.

In the second case, the api is a member variable of the context, and it is
injected in the context constructor, so the same instance of the api will
be returned on every call to ots "getApi()" method.

Just to summarize:

* Contexts and apis are thread-safe (or should! Otherwise it is an issue).
* Apis are NOT bound as singletons to the Guice context, however:
* The main accessors available to get an Api return the same instance.



El 22/12/2015 10:47, "Andrew Phillips" <andrewp@apache.org> escribió:

> Hi all
> Prompted by the question below on user@j.a.o: what is the thread-safety
> status of an API returned via ContextBuilder...buildApi?
> Context themselves are supposed to be thread-safe [1], but an API
> context's getApi method is documented as:
> "Threadsafe implementations will return a singleton." [2]
> This could be taken to imply that *the API itself* might not be threadsafe
> (in which case getApi may end up returning a different instance for
> diffrent calls).
> Given that, what can be concluded for the result of buildApi:
> 1. Thread-safe
> 2. Not thread-safe: pass the context around and all getApi on that when
> needed
> 3. Depends on the API implementation
> 4. Other
> ..?
> Regards
> ap
> [1] https://jclouds.apache.org/start/concepts/#contexts
> [2]
> http://jclouds-javadocs.elasticbeanstalk.com/org/jclouds/rest/ApiContext.html#getApi()
> On 2015-12-22 06:23, Kiran Singh wrote:
>> Hi
>> My name is Kiran Singh and i am using jCloud openstacks-swift library
>> to connect with Swift server. I want to write a multi-threaded
>> application.
>> For making connection to swift server i am using following code:
>> SwiftApi swiftApi = ContextBuilder.newBuilder(provider)
>>                 .endpoint(endUrl)
>>                 .credentials(identity, credential)
>>                 .buildApi(SwiftApi.class);
>> My question is SwiftApi is thread safe?
>> Can i use Singleton object of swiftApi for each thread or do i need to
>> create swiftApi object for each thread?
>> Thanks in advance
>> Kiran Singh

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message