Link to home
Start Free TrialLog in
Avatar of letsbedecent
letsbedecent

asked on

Struts Action doubt.

Hello,

If an Action class has only one instance created, by the request processor, then how can we use instance variables like the following in Action class.

public class InTouchUserDetailAction extends ActionSupport {
      // The following is an instance variable.
      protected transient final Log log = LogFactory.getLog(getClass());

        public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
          throws ApplicationException{

// blah blah

}

When can we use an instance variable in a Action class ??
and when can we not ??

In this case why is it safe to use this instance variable ?

Thanks.
Avatar of limaideal
limaideal

You need to create the instance as static. And that is how Log object is normally used:
static final protected Log log = LogFactory.getLog(InTouchUserDetailAction .class);
>> When can we use an instance variable in a Action class ??
As there is going to be only one Action instance there is not going to be much difference between storing it as static or as an instance variable.
What important is that the instance pointed to either by a static or a member variable will be thread safe.

In this case why is it safe to use this instance variable ?
Log4j Logger is thread safe (hence can be accessed by multiple threads concurrently) as all access to the same category are synchronized.
Avatar of letsbedecent

ASKER

How is Log4j logger threadsafe ? How can we know that ??
http://logging.apache.org/log4j/docs/faq.html#1.7

It says log4j components are designed for heavily multithreaded environments.

How can we design such components

1) By not using instance variables ??


thank u
Dear etsbedecent,

Can you please clarify your question first?

Do you want help on constructing multithreaded environemnt or do you have trouble using Log4J?

Thanks!
Obviously question was intended to understand how log4j is threadsafe ...

because action class is not.

There is no trouble in using log4j.
ASKER CERTIFIED SOLUTION
Avatar of aozarov
aozarov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial