Thanks. This helped me alot.

Regards,
Behrang S.


On Wed, 15 Dec 2004 00:18:46 +0100, Leon Rosenberg <[EMAIL PROTECTED]> wrote:
> Ok, just in case it was more confusing then helping.
> Here is the (imho) safe solution (haven't tested it, but it should work
> fine):
> The isLock method is needed to ensure, that all threads have the same "lock"
> value. Volatile should guarantee it, but it's not supported on all vms. You
> should call the unlock method after you know, that the processing of the
> file is finished.
> Another problem, you could have, is to ensure, that the page where the link
> is placed is still valid. You can ensure it, by giving your call to the
> action a timestamp parameter and check whether it's still the proper
> timestamp for the file.
> 
> Here the code (without timestamp):
> 
> public class youraction ...{
> 
>        private volatile boolean lock;
> 
>        private void lock() throws AlreadyLockedException{
>                if (lock)
>                        throw new AlreadyLockedException();
> 
>                lock = true;
>        }
> 
>        private void unlock(){
>                lock = false;
>        }
> 
>        private synchronized boolean isLocked(){
>                return lock;
>        }
> 
>        private synchronized void updateXML() throws AlreadyLockedException{
>                //double check needed, in case another thread was already
> //between first isLocked and updateXML call.
>                if (isLocked())
>                        throw new AlreadyLockedException();
> 
>                lock();
>                //.... generate the xml file.
>                unlock();
> 
>        }
> 
>        public ActionForward execute(
>                ActionMapping mapping,
>                ActionForm af,
>                HttpServletRequest req,
>                HttpServletResponse res)
>                throws Exception {
> 
>                //...
>                if (isLocked())
>                        return mapping.findForward("noupdate");
>                try{
>                        updateXML();
>                        return mapping.findForward("updatecomplete");
>                }catch(AlreadyLockedException e){
>                        return mapping.findForward("noupdate");
>                }
>        }
> 
> }
> 
> > >
> > >
> > > > -----Original Message-----
> > > > From: Behrang Saeedzadeh [mailto:[EMAIL PROTECTED]
> > > > Sent: Tuesday, December 14, 2004 4:34 PM
> > > > To: [EMAIL PROTECTED]
> > > > Subject: Preventing concurrent execution of an Action
> > > >
> > > > Hi
> > > >
> > > > I've an action that creates an XML file on the server. It's executed
> > > > by clicking on a link. I don't want multiple instances of it to be
> > > > executed concurrently.
> > > >
> > > > Does an approach like the following work?
> > > >
> > > > public class UpdateXmlAction ...
> > > > {
> > > >        private static final boolean locked;
> > > >
> > > >        public void execute(...) {
> > > >
> > > >                if (UpdateXmlAction.locked) {
> > > >                        return;
> > > >                }
> > > >
> > > >                Synchronized(UpdateXmlAction.class) {
> > > >                        locked = true;
> > > >                        updateXml();
> > > >                        locked = false;
> > > >                }
> > > >
> > > >        }
> > > > }
> > > >
> > > > Thanks in advance.
> > > >
> > > >
> > > > --
> > > >
> > > > Behrang Saeedzadeh
> > > > http://www.jroller.com/page/behrangsa
> > > >
> > > > ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > > For additional commands, e-mail: [EMAIL PROTECTED]
> > > >
> > > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 


-- 

Behrang Saeedzadeh
http://www.jroller.com/page/behrangsa

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to