You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by James Mitchell <jm...@apache.org> on 2005/03/01 02:07:31 UTC

Re: caching of message resources when using bean:message tag

Yes, I have also written a database implementation or three ;)

http://sourceforge.net/project/showfiles.php?group_id=49385&package_id=76369

In addition to this one, I have also written several impls for 
commons-resources (which Struts will be moving to very soon).  There were 
license issues so I had to host them elsewhere (same link above) but I 
haven't released these as of yet.  That's on my todo list ;)


The workaround for Struts 1.1 and 1.2.x is to clear the hashmap of formatted 
messages.  Look at org.apache.struts.util.MessageResources#getMessage(Locale 
locale, String key, Object args[]).

In that method you'll see where if the formatted message (param replacement 
already done) is found, then that is returned.  That certainly is a nice way 
to speed up things, but the impl fails to make that configurable, so you are 
just stuck with it :(

My impl simply does formats.clear() on each call to getMessage(), so, in 
effect, unless the very next call to your getMessage() is asking for the 
last one you just gave it, the cache is (basically) disabled.

You also might consider implementing your own cache or taking advantage of 
caching available via O/R Mapping tools like Hibernate.



--
James Mitchell
Software Engineer / Open Source Evangelist
EdgeTech, Inc.
678.910.8017
AIM: jmitchtx

----- Original Message ----- 
From: "Günther Wieser" <gw...@creative-it.com>
To: <us...@struts.apache.org>
Sent: Monday, February 28, 2005 5:57 PM
Subject: caching of message resources when using bean:message tag



hi,

we are using struts version 1.2.4 and we implemented our own
MessageResources and MessageResourceFactory. we needed to have a database
based system for managing message resources for different locales so that we
can change valued on the fly without the need to restart the app.
these implementation work quite well, but we found one issue when using
<bean:message key="whatever"/>:
it seems that the message with the key "whatever" gets cached somewhere. the
first call to this bean with key="whatever" results in a getMessage() call
to our implementation of MessageResources. the message gets delivered and
inserted into the JSP.
but all further calls to the message tag do NOT query
MessageResources.getMessage(), so we assume that the message value is cached
somewhere.

what we see from the source code of the message tag is that
TagUtils.message() is called. so far we haven't found anything that caches
in TagUtils. and there's no caching in the message tag..

anyone an idea where the caching takes place?
am i missing something fundamentally when i assume that it's possible (with
my own implementation of MessageResources) to change message resources in
struts dynamically during runtime?

kr,
guenther



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


Re: caching of message resources when using bean:message tag

Posted by Lucas Opara <lu...@gmail.com>.
In fact, when you look at the code of the bean:message tag, it calls 
TagUtils.message(). In the message() method of TagUtils, args is never null, 
because of this piece of code in MessageResources : Object args[] = new Object
[] { arg0, arg1, arg2, arg3, arg4 };

Thus resources.getMessage(userLocale, key, args); is the method that is always 
called even if the arguments are null because the array is never null.

   public String message(
            PageContext pageContext,
            String bundle,
            String locale,
            String key,
            Object args[])
            throws JspException {

        MessageResources resources =
                retrieveMessageResources(pageContext, bundle, false);

        Locale userLocale = getUserLocale(pageContext, locale);
        String message = null;
        if (args == null) {
        //This is never called!!!!    
        message = resources.getMessage(userLocale, key);
        } else {
            message = resources.getMessage(userLocale, key, args);
        }
        if ((message == null) && log.isDebugEnabled()) {
            // log missing key to ease debugging
            log.debug(resources.getMessage("message.resources", key, bundle, 
locale));
        }
        return message;
    }



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


Re: caching of message resources when using bean:message tag

Posted by James Mitchell <ja...@mac.com>.
Yes, they are cached by default.  You can always provide your own  
implementation or extension.  I did.

--
James Mitchell
Software Engineer / Open Source Evangelist
Consulting / Mentoring / Freelance
EdgeTech, Inc.
http://www.edgetechservices.net/
678.910.8017
AIM:   jmitchtx
Yahoo: jmitchtx
MSN:   jmitchell@apache.org
Skype: callto://jmitchtx





On Oct 28, 2005, at 8:28 AM, Lucas Opara wrote:

> I have got same problem as Günther. Are you sure the messages are  
> cached in
> formats HashMap? Because when you look at the source code of  
> MessageResources,
> only the method getMessage with args uses the cache. How are the  
> messages cached
> when you use getMessage(Locale,String)?
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


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


Re: caching of message resources when using bean:message tag

Posted by Lucas Opara <lu...@gmail.com>.
I have got same problem as Günther. Are you sure the messages are cached in
formats HashMap? Because when you look at the source code of MessageResources,
only the method getMessage with args uses the cache. How are the messages cached
when you use getMessage(Locale,String)?


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


AW: caching of message resources when using bean:message tag

Posted by Günther Wieser <gw...@creative-it.com>.
thanks very much for this solution.
i'm using ojb plus a hashmap for heavy caching, there is NO database acccess
for reading values after initialization, as all operations on
key-value-pairs go through a service that fills the cache with new or
changed values.

kr,
guenther

-----Ursprüngliche Nachricht-----
Von: James Mitchell [mailto:jmitchell@apache.org] 
Gesendet: Dienstag, 01. März 2005 02:08
An: Struts Users Mailing List
Betreff: Re: caching of message resources when using bean:message tag

Yes, I have also written a database implementation or three ;)

http://sourceforge.net/project/showfiles.php?group_id=49385&package_id=76369

In addition to this one, I have also written several impls for
commons-resources (which Struts will be moving to very soon).  There were
license issues so I had to host them elsewhere (same link above) but I
haven't released these as of yet.  That's on my todo list ;)


The workaround for Struts 1.1 and 1.2.x is to clear the hashmap of formatted
messages.  Look at org.apache.struts.util.MessageResources#getMessage(Locale
locale, String key, Object args[]).

In that method you'll see where if the formatted message (param replacement
already done) is found, then that is returned.  That certainly is a nice way
to speed up things, but the impl fails to make that configurable, so you are
just stuck with it :(

My impl simply does formats.clear() on each call to getMessage(), so, in
effect, unless the very next call to your getMessage() is asking for the
last one you just gave it, the cache is (basically) disabled.

You also might consider implementing your own cache or taking advantage of
caching available via O/R Mapping tools like Hibernate.



--
James Mitchell
Software Engineer / Open Source Evangelist
EdgeTech, Inc.
678.910.8017
AIM: jmitchtx

----- Original Message ----- 
From: "Günther Wieser" <gw...@creative-it.com>
To: <us...@struts.apache.org>
Sent: Monday, February 28, 2005 5:57 PM
Subject: caching of message resources when using bean:message tag



hi,

we are using struts version 1.2.4 and we implemented our own
MessageResources and MessageResourceFactory. we needed to have a database
based system for managing message resources for different locales so that we
can change valued on the fly without the need to restart the app.
these implementation work quite well, but we found one issue when using
<bean:message key="whatever"/>:
it seems that the message with the key "whatever" gets cached somewhere. the
first call to this bean with key="whatever" results in a getMessage() call
to our implementation of MessageResources. the message gets delivered and
inserted into the JSP.
but all further calls to the message tag do NOT query
MessageResources.getMessage(), so we assume that the message value is cached
somewhere.

what we see from the source code of the message tag is that
TagUtils.message() is called. so far we haven't found anything that caches
in TagUtils. and there's no caching in the message tag..

anyone an idea where the caching takes place?
am i missing something fundamentally when i assume that it's possible (with
my own implementation of MessageResources) to change message resources in
struts dynamically during runtime?

kr,
guenther



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




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