Announcement Announcement Module
Collapse
No announcement yet.
thread-safety in MessageSource bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • thread-safety in MessageSource bean

    Hi guys,

    I am trying to create a subclass of MessageSource, say DbBundleMessageSource that access the
    database for resource bundle. (e.g. like ReloadableResourceBundleMessageSource.java except it uses DB rather
    than static properties file).

    Now I plan to wire it up as singleton bean to controllers and the problem
    is it has an instance variable 'module', so not thread safe.

    By searching the forum, the answer I arrived is to use ThreadLocal
    as below:


    public class DbBundleMessageSource extends AbstractMessageSource {

    private String module;

    private static ThreadLocal moduleContext = new ThreadLocal();

    public String setModule(String module) {
    return moduleContext.set(module);
    }

    public String getModule() {
    return (String)moduleContext.get();
    }

    ....

    protected MessageFormat resolveCode(String code, Locale locale) {

    if ( getModule() == null ) {
    throw new IllegalStateException("Argument module can not be null.");
    }

    if ( applicationResourceManager == null ) {
    throw new IllegalStateException("Instance variable 'applicationResourceManager' is null.");
    }

    // access DB ...
    MessageFormat mf = resolveCode(code, locale, getModule());

    return mf;
    }

    }
    Then in spring config,

    <bean id="messageSource" class="org.i18nfuse.util.i18n.DbBundleMessageSourc e">
    </bean>
    and any controller that wants to use messageSource bean as above
    can wire it up as

    Code:
     <bean id="myController" class="org.i18nfuse.webapp.action.MyController">
            <property name="messageSource" ref="messageSource"/>
            ........
    </bean>
    Now the idea is hundreds of controllers could be using messageSource
    bean as above.

    So can I safely assume this will fix thread-safety issue on messageSource bean??

    Thanks,

    Sam

  • #2
    correction to my crappy code, the setModule(..) method
    should be

    public void setModule(String module) {
    moduleContext.set(module);
    }

    Thanks

    Comment


    • #3
      ummm no response. Ok, I am going to assume it's safe this way.
      Right?


      BTW, has anyone tried to write a subclass of AbstractMessageSource
      that access database for key/value pair instead of static properties file?


      Regards

      Sam

      Comment


      • #4
        How is going this?

        Hi there I'm about to code a database backed messasource. Any advice will be appreciated

        Comment

        Working...
        X