Portlet lifecycle and concurrency

Ralph van Etten, modified 10 Years ago. New Member Posts: 6 Join Date: 9/2/15 Recent Posts
Hi,

I have a few questions about the portlet lifecycle and concurrency.

1. How many instances are there of a portlet? It seems there is only a single instance in existence. Is this correct?

2. Can the doView() method (for instance) be called concurrently by multiple threads/for multiple requests?

Is there any documentation about this? I tried googling for this but I could not find anything useful about concurrency.

Thanks.
thumbnail
David H Nebinger, modified 10 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
Well, it's kinda like asking how many instances of a servlet are in a web app because there, too, there will seem to be only one instance.

Like a servlet, your portlet code must be multithreadable. That's why the action and render phases pass you the request and response objects, so when you have X number of users rendering your portlet, there will be X threads all invoking your portlet code with X requests and X responses.

doView() is an artifact of Liferay MVC framework built upon the standard portlet lifecycle so yes, it too will be called concurrently.

Sometimes developers get stuck on this, they'll use private member variables for local value stores between action and render calls and it will actually test out fine on their local system where they are the only ones invoking the portlet lifecycle. But when the code gets pushed to test and hit by many others, this code problem will reveal itself, sometimes in odd ways (depends, of course, on how it was coded).
Ralph van Etten, modified 10 Years ago. New Member Posts: 6 Join Date: 9/2/15 Recent Posts
I was reviewing some code and you confirmed my suspicions. I could not find a definite answer using Google.

Anyway, thanks!
thumbnail
Olaf Kock, modified 10 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts
I'd say that the number of portlet object instances is probably undefined. It's definitely insignificant and there's no reason to have more than one. Any member variable in a portlet (or a servlet for that matter) is most likely the manifestation of a bug that you haven't yet found.
thumbnail
Devang Patel, modified 10 Years ago. Regular Member Posts: 247 Join Date: 1/19/15 Recent Posts
Hi Ralph van Etten,

You can refer this links :
Blog on Liferay Portlet
Portlet Lifecycle

And by default there is only one instance for portlet but you can do multiple instance for the portlet.
Regards,
Devang Patel