Rythm can be used as a singleton or instantiated as a normal object.
Currently I instantiate it once in a webapp in the Servlet Init() method. (This is almost the same as using a singleton since I'm only creating it once)
Now I need to add i18n support to my site and need to set the language dynamically before rendering.
I could do this:
engine.setLocale(userLocale);
engine.render(template,arguments);
I assume that if I use a singleton(or single instance created in the servlet) then I might get a race condition between the two statements.
- Is this correct or is there no race condition?
- Should I create a Rythm instance for each request?
- Is there another way to change the language for each rendering without changing the engine state?
First in the latest version of rythm, you should call
engine.prepare(Locale), notengine.setLocale(Locale);Now to answer your questions:
It will NOT cause race condition because inside the code it uses
ThreadLocalto store the render settingsDefinitely NO
Language and locale are NOT engine level states, they are per request states. However default language and locale could be configured at engine level