mystarrocks
mystarrocks

Reputation: 4088

Struts2- extending action classes and thread safety

I understand the instance variables on an action class are thread-safe since action classes are instantiated per request. But I have this need for extending action classes and I'm concerned about the thread-safety.

Say, for example I have some common attributes and a few methods handling those attributes among several action classes. I prefer to put them in a single action class and make that extend the ActionSupport. And all action classes will then extend the base action class that I just created. My question is, are the instance variables on the base action class thread-safe? Does S2 manage the base action class at all?

Also what makes an action class an action class to be managed by the S2 and instantiated per request? Getting declared in the struts.xml? Extending ActionSupport class?

Upvotes: 2

Views: 2868

Answers (2)

Dave Newton
Dave Newton

Reputation: 160251

I think you're a bit confused about how Java works. If you have a class A and a class B extends A, when you instantiate B there is a B. It's not like there's a single instance of A backing all instances of B. There's no "management of base classes".

Classes declared as actions either via XML, annotations, or convention are instantiated by the Struts action instantiation mechanism. Extending ActionSupport has (almost) nothing to do with it, the only time it might have something to do with it is because ActionSupport implements the Action interface.

Upvotes: 3

Andrea Ligios
Andrea Ligios

Reputation: 50261

Also what makes an action class an action class to be managed by the S2 and instantiated per request? Getting declared in the struts.xml? Extending ActionSupport class?

Getting declared in the struts.xml: yes, is that that turns a Java class into an Action.

And every Action class is thread-safe because it's ThreadLocal, no matter what it extends nor implements. Every request of every user will have its own copy of each Action class.

P.S: The other classes (not declared in struts.xml) are not "(action classes) not managed by S2 and instantiated per request", they're simply NOT Actions.

Upvotes: 2

Related Questions