You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@jclouds.apache.org by Kiran Singh <ki...@gmail.com> on 2015/12/22 06:23:01 UTC

Is SwiftApi is thread safe?

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

Re: Fw: Is SwiftApi is thread safe?

Posted by Andrew Phillips <an...@apache.org>.
> HTH!

Yes, it does - many thanks for the summary. In fact, I think this would 
make an excellent blog post ;-) For now, I've added an article to the 
developer wiki as a start:

https://cwiki.apache.org/confluence/display/JCLOUDS/Context%2C+API+and+Provider+Thread+Safety

Please edit/delete/amend as necessary.

I see you've already replied to the original question...thanks.

Happy holidays, everyone!

ap

Re: Fw: Is SwiftApi is thread safe?

Posted by Ignasi Barrera <na...@apache.org>.
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.

HTH!

I.

[1]
https://github.com/jclouds/jclouds/blob/master/core/src/main/java/org/jclouds/rest/config/AnnotatedHttpApiProvider.java
[2]
https://github.com/jclouds/jclouds/blob/master/core/src/main/java/org/jclouds/rest/config/HttpApiModule.java
[3]
https://github.com/jclouds/jclouds/blob/master/core/src/main/java/org/jclouds/rest/config/BinderUtils.java
El 22/12/2015 10:47, "Andrew Phillips" <an...@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
>>
>

Fw: Is SwiftApi is thread safe?

Posted by Andrew Phillips <an...@apache.org>.
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

Re: Is SwiftApi is thread safe?

Posted by Ignasi Barrera <na...@apache.org>.
Hi!

It should be thread-safe, so you should be able to reuse it. Just take into
account that you'll have to close it when you're done.

I.
El 22/12/2015 6:23, "Kiran Singh" <ki...@gmail.com> escribió:

> 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
>