mailMessage property -- multiple threads

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

mailMessage property -- multiple threads

I'm sorry if this is the wrong forum to post this message to.  I couldn't find a more appropriate place.  And let me also say that I love AppFuse.

I was looking at the AppFuse technique for sending emails when certain things happen, such as a user signing up or asking for the password hint.  It occurred to me that I don't understand how the mail message bean is properly guarded against multiple threads modifying it at the same time.  I do realize that the bean is configured with singleton="false", but I think that only means that each container object (namely the web controllers that have it as a property) will get its own instance of a SimpleMailMessage object.  Multiple threads hitting a single controller will share the same SimpleMailMessage object, as far as I can tell.

Imagine that two users sign up at more or less the same instant.  Two threads will be handling the resulting requests and will go into the onSubmit method of the SignupController.  They will both call the sendUserMessage method.  This method modifies the "to" property of the message, to be the email of the user that is currently signing up.  So isn't there a chance for one thread to set the address to "", and then the second thread comes along and sets it to ""?  Then each thread sets the text property with particular data about the user signing up.  Both threads finally send the email, but the state of the mailMessage object might be a garbled mash-up of what each thread wanted it to be.

I'm not very good at thinking in multi-threading terms, and I tried searching around and did not see any other mention of this as an issue in AppFuse, so there's a good chance that I'm just being stupid.  But considering that I've started my application with AppFuse, and I will be sending emails in response to various user-triggered actions, I need to get a better handle on it.  A simple solution is just to create a new SimpleMailMessage each time an email is sent, which is probably what I'm going to do.  The only thing the mailMessage bean really seems to be used for is having a default "from" property.

Thanks for any enlightenment you can provide.