You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Mike Curwen <gb...@gb-im.com> on 2004/02/17 17:45:37 UTC

VelocityEngine and thread safety

Hello,

I have a question regarding the thread-safety of the
VelocityEngine.mergeTemplate() method.
 
The javadocs for VE don't specifically mention thread safety.  But then
there's that property: parser.pool.size  which makes me think there is a
pool of parsers available to parse multiple requests. Plus this all
seems to 'just work' in a servlet environment, which is multi-threaded.
 
I'm writing a java class to use VelocityEngine.  Right now it's
single-thread, but I'm starting an attempt to multithread the class.
Basically, imagine a system that sends out notification emails.  If the
list of people to notify is large, I want to spawn multiple threads to
process the list, query a database, merge templates for the body of the
email I'm sending, send the email, update the database, etc. Actually, a
(perhaps) more detailed description exists here:
http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=2
7&t=001064
 
The VelocityEngine is the 'template merger' object in the above
scenario.  
 
It's not explicit in the javadocs which calls are 'safe'.  Can more than
one thread call .mergeTemplate()  and not experience any difficulty?  
 

 
-------------------------------------------
Mike Curwen                    204-885-7733
Intermediate Programmer       www.gb-im.com
-------------------------------------------
  ____   ____            ___   __  __ 
 / ___| | __ )          |_ _| |  \/  |
| |  _  |  _ \   _____   | |  | |\/| |
| |_| | | |_) | |_____|  | |  | |  | |
 \____| |____/          |___| |_|  |_|


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: VelocityEngine and thread safety

Posted by Geir Magnusson Jr <ge...@4quarters.com>.
On Feb 17, 2004, at 12:49 PM, Mike Curwen wrote:

> awesomes!
>
> I was pretty sure that was the answer, but wanted confirmation before I
> started coding which of my objects could be shared between threads.
>
> The VelocityContext object is constructed anew for each call to the
> mergeTemplate(), so I'm good there.  Perhaps I should figure out how to
> re-use one though...  ;)

Generally speaking, they can be reused, just not concurrently.  And, 
since you are rendering the same template, reusing will result in 
performance improvement over a new VelocityContext() above and beyond 
the allocation savings.

Further, you can do things like

VelocityContext common = new VelocityContext();

// fill common with stuff you want shared

VelocityContext outer = new VelocityContext(common);

// use outer in rendering

and this will save you the cost of loading the outer everytime with 
things that can be shared.  Of course, make sure that anything in 
common is threadsafe.

geir

>
>
>> -----Original Message-----
>> From: Geir Magnusson Jr [mailto:geir@4quarters.com]
>> Sent: Tuesday, February 17, 2004 11:12 AM
>> To: Velocity Users List
>> Subject: Re: VelocityEngine and thread safety
>>
>>
>>
>> On Feb 17, 2004, at 11:45 AM, Mike Curwen wrote:
>>
>>> Hello,
>>>
>>> I have a question regarding the thread-safety of the
>>> VelocityEngine.mergeTemplate() method.
>>>
>>> The javadocs for VE don't specifically mention thread safety.  But
>>> then there's that property: parser.pool.size  which makes
>> me think there is
>>> a
>>> pool of parsers available to parse multiple requests. Plus this all
>>> seems to 'just work' in a servlet environment, which is
>> multi-threaded.
>>
>> Yep.  It's thread safe.
>>
>>>
>>> I'm writing a java class to use VelocityEngine.  Right now it's
>>> single-thread, but I'm starting an attempt to multithread
>> the class.
>>> Basically, imagine a system that sends out notification emails.  If
>>> the list of people to notify is large, I want to spawn multiple
>>> threads to process the list, query a database, merge
>> templates for the
>>> body of the email I'm sending, send the email, update the
>> database, etc. Actually,
>>> a
>>> (perhaps) more detailed description exists here:
>>> http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?
>>> ubb=get_topic&f=2
>>> 7&t=001064
>>>
>>> The VelocityEngine is the 'template merger' object in the above
>>> scenario.
>>>
>>> It's not explicit in the javadocs which calls are 'safe'.  Can more
>>> than
>>> one thread call .mergeTemplate()  and not experience any difficulty?
>>
>> Yes.  You'll be fine.  The VelocityContext is *not* threadsafe, but
>> most usage patterns don't share a global context, and yours
>> certainly
>> can't unless you are sending the message to the same person over and
>> over...
>>
>> geir
>>
>> -- 
>> Geir Magnusson Jr                                   203-247-1713(m)
>> geir@4quarters.com
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
-- 
Geir Magnusson Jr                                   203-247-1713(m)
geir@4quarters.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


RE: VelocityEngine and thread safety

Posted by Mike Curwen <gb...@gb-im.com>.
awesomes!

I was pretty sure that was the answer, but wanted confirmation before I
started coding which of my objects could be shared between threads.
 
The VelocityContext object is constructed anew for each call to the
mergeTemplate(), so I'm good there.  Perhaps I should figure out how to
re-use one though...  ;)


> -----Original Message-----
> From: Geir Magnusson Jr [mailto:geir@4quarters.com] 
> Sent: Tuesday, February 17, 2004 11:12 AM
> To: Velocity Users List
> Subject: Re: VelocityEngine and thread safety
> 
> 
> 
> On Feb 17, 2004, at 11:45 AM, Mike Curwen wrote:
> 
> > Hello,
> >
> > I have a question regarding the thread-safety of the
> > VelocityEngine.mergeTemplate() method.
> >
> > The javadocs for VE don't specifically mention thread safety.  But 
> > then there's that property: parser.pool.size  which makes 
> me think there is
> > a
> > pool of parsers available to parse multiple requests. Plus this all
> > seems to 'just work' in a servlet environment, which is 
> multi-threaded.
> 
> Yep.  It's thread safe.
> 
> >
> > I'm writing a java class to use VelocityEngine.  Right now it's 
> > single-thread, but I'm starting an attempt to multithread 
> the class. 
> > Basically, imagine a system that sends out notification emails.  If 
> > the list of people to notify is large, I want to spawn multiple 
> > threads to process the list, query a database, merge 
> templates for the 
> > body of the email I'm sending, send the email, update the 
> database, etc. Actually,
> > a
> > (perhaps) more detailed description exists here:
> > http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi? 
> > ubb=get_topic&f=2
> > 7&t=001064
> >
> > The VelocityEngine is the 'template merger' object in the above 
> > scenario.
> >
> > It's not explicit in the javadocs which calls are 'safe'.  Can more
> > than
> > one thread call .mergeTemplate()  and not experience any difficulty?
> 
> Yes.  You'll be fine.  The VelocityContext is *not* threadsafe, but  
> most usage patterns don't share a global context, and yours 
> certainly  
> can't unless you are sending the message to the same person over and  
> over...
> 
> geir
> 
> -- 
> Geir Magnusson Jr                                   203-247-1713(m)
> geir@4quarters.com
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: VelocityEngine and thread safety

Posted by Geir Magnusson Jr <ge...@4quarters.com>.
On Feb 17, 2004, at 11:45 AM, Mike Curwen wrote:

> Hello,
>
> I have a question regarding the thread-safety of the
> VelocityEngine.mergeTemplate() method.
>
> The javadocs for VE don't specifically mention thread safety.  But then
> there's that property: parser.pool.size  which makes me think there is  
> a
> pool of parsers available to parse multiple requests. Plus this all
> seems to 'just work' in a servlet environment, which is multi-threaded.

Yep.  It's thread safe.

>
> I'm writing a java class to use VelocityEngine.  Right now it's
> single-thread, but I'm starting an attempt to multithread the class.
> Basically, imagine a system that sends out notification emails.  If the
> list of people to notify is large, I want to spawn multiple threads to
> process the list, query a database, merge templates for the body of the
> email I'm sending, send the email, update the database, etc. Actually,  
> a
> (perhaps) more detailed description exists here:
> http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi? 
> ubb=get_topic&f=2
> 7&t=001064
>
> The VelocityEngine is the 'template merger' object in the above
> scenario.
>
> It's not explicit in the javadocs which calls are 'safe'.  Can more  
> than
> one thread call .mergeTemplate()  and not experience any difficulty?

Yes.  You'll be fine.  The VelocityContext is *not* threadsafe, but  
most usage patterns don't share a global context, and yours certainly  
can't unless you are sending the message to the same person over and  
over...

geir

-- 
Geir Magnusson Jr                                   203-247-1713(m)
geir@4quarters.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org