Hi Alex. Thanks for your feedback.
On 28/11/2016 20:52, Alex Fedotov wrote:
Hello All, ContextAware interface in Velocity is not really workable in a multi-threaded environment. The handler is a singleton and setting the Context on the singleton from multiple threads is not going to work well, short of using some form of thread local.
You are right about that. The ContextAware javadoc says: Important Note: Only local event handlers attached to the context (as opposed to global event handlers initialized in the velocity.properties file) should implement ContextAware. Since global event handlers are singletons individual requests will not be able to count on the correct context being loaded before a request. I agree that the site documentation could be clearer about it.
Also the event handlers (such as insertion handler) are creating new instance of executor for each inserted value which is inefficient.
I'm not sure that using a reference insertion event handler in the first place can be made efficient, and not sure either that the allocation/deallocation accounts for a lot of performance loss since modern JVMs do a really good job about allocation of small Java objects.
The reference insertion event handler was initially meant to be a debugging and checking tool rather than a production tool. This being said, I known there can be some legitimate use cases to do it in production, but can I ask what is your specific use case, out of simple curiosity?
Are you guys fixing any of this for 2.0?
It is not planned to fix the issue for 2.0, but you can easily implement a custom implementation that will use a thread local member.
The event management may be reviewed in a future version, but if you think this particular point should specifically be addressed, I strongly advise you to open an issue about it.
Claude