Can an object for a Struts Action be made thread safe?

J

jazzdman

Hi,
I don't know a whole heck of a lot about threads. Is it possible
to make an object in a Struts action thread-safe? Normally, you're not
supposed to do I/O to the file system in a webapp, but I am, so it
occurs to me that I should make the I/O thread safe. Can I make the
objects, or more specifically the object's methods, that do I/O thread
safe without causing an IllegalMonitorStateException?


Thanks,

Jason Mazzotta
 
R

Ryan Stewart

Hi,
I don't know a whole heck of a lot about threads. Is it possible
to make an object in a Struts action thread-safe? Normally, you're not
supposed to do I/O to the file system in a webapp, but I am, so it
occurs to me that I should make the I/O thread safe. Can I make the
objects, or more specifically the object's methods, that do I/O thread
safe without causing an IllegalMonitorStateException?
That's a pretty broad question. The short version is that you had sure *better*
find a way to make things thread safe if you're doing them in an Action, since
those are multithreaded environments.
 
J

jazzdman

Well, here's more detail. I've created a class that manipulates an XML
file. An instance of this class is stored in the application scope and
used by different Actions to add or remove info from the XML file based
on input from the user. So, it could be that multiple Actions try to
modify the XML file at the same time. If I synchronize the methods in
this delegate class, will I cause a problem by asking the Action to
wait()?
 
W

Wendy S

Well, here's more detail. I've created a class that manipulates an XML
file. An instance of this class is stored in the application scope and
used by different Actions to add or remove info from the XML file based
on input from the user.

How about... construct an object from the data in the XML file and place it
in application scope. Modify as necessary from Action code, then write it
back out to XML when the webapp is unloaded. (You can do this in a
ServletContextListener.)
 
J

jazzdman

Thanks for the reply, but the Document could end up getting really
large, which poses a problem in itself anyway. When is a webapp
unloaded from Tomcat other than when Tomcat itself is shutdown?
 
W

Wendy Smoak

Thanks for the reply, but the Document could end up getting really
large, which poses a problem in itself anyway. When is a webapp
unloaded from Tomcat other than when Tomcat itself is shutdown?

When Tomcat is shut down, or when you use the Manager app to stop or reload
the webapp. Obviously this wouldn't work if something outside the webapp
needs access to this data, and there is a risk of losing what's in memory if
Tomcat or the server goes down unexpectedly.

Good luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top