[jboss-dev-forums] [JBoss ESB Development] - Re: Performance Improvement Proposal: Cache result of HttpSe

dward do-not-reply at jboss.com
Mon Dec 7 10:55:16 EST 2009


Again, something to weigh.  If I do this:

private static final String getLocalName(HttpServletRequest request) {
  |     String localAddr = request.getLocalAddr();
  |     String localName = localAddr_to_localName.get(localAddr);
  |     if (localName == null) {
  |         localName = request.getLocalName();
  |         localAddr_to_localName.put(localAddr, localName);
  |     }
  |     return localName;
  | }
  | private static final Map<String,String> localAddr_to_localName = Collections.synchronizedMap(new HashMap<String,String>());

The it is thread safe, but opens the door for multiple calls to request.getLocalName().  But if I do this:

private static final String getLocalName(HttpServletRequest request) {
  |     String localAddr = request.getLocalAddr();
  |     synchronized (localAddr_to_localName) {
  |         String localName = localAddr_to_localName.get(localAddr);
  |         if (localName == null) {
  |             localName = request.getLocalName();
  |             localAddr_to_localName.put(localAddr, localName);
  |         }
  |         return localName;
  |     }
  | }
  | private static final Map<String,String> localAddr_to_localName = new HashMap<String,String>();

Then request.getLocalName() will only ever get called once, since both the get and put calls to the map are in the same atomic block.

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4269383#4269383

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4269383



More information about the jboss-dev-forums mailing list