• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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.
0
letsbedecent
Asked:
letsbedecent
  • 3
  • 3
  • 2
1 Solution
 
limaidealCommented:
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);
0
 
aozarovCommented:
>> 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.
0
 
letsbedecentAuthor Commented:
How is Log4j logger threadsafe ? How can we know that ??
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
limaidealCommented:
0
 
letsbedecentAuthor Commented:
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
0
 
limaidealCommented:
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!
0
 
letsbedecentAuthor Commented:
Obviously question was intended to understand how log4j is threadsafe ...

because action class is not.

There is no trouble in using log4j.
0
 
aozarovCommented:
Any class can be thread safe if its methods that modifiy/read memeber variables are synchronized (or have similar access control) or using methods that do not modify
member variables (only local ones). Log4J code is thread safe by by applying both techniques.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now