Java-backed Web Scripts Multithread

isloat
Champ in-the-making
Champ in-the-making
Hello,
I have a Java-backed Web Script (like this example: http://wiki.alfresco.com/wiki/Java-backed_Web_Scripts_Samples#SimpleWebScript.java)

Are java webscripts already prepared for multithreading?
Or I have to put something in the <bean> configuration? Or I have to program the java class for multithreading?

In other words, is created a new object of the WebScript java class for each request or is a Singleton?

Thanks a lot!
5 REPLIES 5

openpj
Elite Collaborator
Elite Collaborator
By default Webscripts are declared as singletons.

If you need for a single request a new instance of the WebScript you have to declare the attribute singleton="false" in the Spring bean definition. But I never tried to use a prototype for a WebScript, I don't know if there are some issues for this.

Hope this helps.

isloat
Champ in-the-making
Champ in-the-making
But I think that even declaring this attribute, if the object that creates webscripts is a Singleton, and only creates the webscript once at the begining (not one for each request), and then uses the same object, this attribute will be useless.

Am I right?

That's a fast reply! Thanks so much.

mrogers
Star Contributor
Star Contributor
See http://static.springsource.org/spring/docs/1.2.x/reference/beans.html

If you declare a spring bean as not a singleton then for each invocation a new instance is created and destroyed.


I doubt that this is ever required for a web script.    If you are considering using it,  I suggest you think again.  Or post your use-case so we can debate. Smiley Happy

isloat
Champ in-the-making
Champ in-the-making
My use case is:
1- Obtain a document (using the object ServiceRegistry.getContentService.getContentReader)
2- Using the document's data, create another document in the same folder as the original one
3- Save new data in the document created (using ServiceRegistry.getContentService.getContentWriter)

I'm interested in multithreading because I don't know if these Alfresco Interfaces (ServiceRegistry, ContentService, ContentReader and ContentWriter) are ready for it.

mrogers
Star Contributor
Star Contributor
You have no class level "state" in the above use-case, so thread safety is not an issue.    The interfaces you are calling are all "thread safe" there will not be a problem.

However as an asside I'd avoid using the ServiceRegistry, there's no need for it if you inject your dependencies.