HttpServletRequestWrapper模擬實現分散式Session

壹頁書發表於2013-12-11
HttpSession的內容都放在一個單獨的Map中,模擬遠端分散式Session。

1.使用HttpServletRequestWrapper建立自定義Request
2.使用動態代理包裝自定義Request返回的HttpSession物件
3.建立過濾器,使用自定義Request替換原有的Request物件。
4.在Servlet中得到的HttpSession物件,寫入和讀取內容都假設透過遠端Session伺服器。

建立自定義的Request,返回動態代理的HttpSession

  1. import java.lang.reflect.InvocationHandler;
  2. import java.lang.reflect.Method;
  3. import java.lang.reflect.Proxy;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.concurrent.ConcurrentHashMap;

  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletRequestWrapper;
  9. import javax.servlet.http.HttpServletResponse;
  10. import javax.servlet.http.HttpServletResponseWrapper;
  11. import javax.servlet.http.HttpSession;

  12. public class RemoteSessionRequest extends HttpServletRequestWrapper {

  13.     public RemoteSessionRequest(HttpServletRequest request) {
  14.         super(request);
  15.     }

  16.     @Override
  17.     public HttpSession getSession() {
  18.         return RemoteSessionHandler.getInstance(super.getSession());
  19.     }
  20. }

  21. class RemoteSessionHandler implements InvocationHandler {
  22.     //模擬遠端Session伺服器,Key表示SessionId,Value表示該Session的內容
  23.     private static Map<String, Map<String, Object>> map = new ConcurrentHashMap<String, Map<String, Object>>();

  24.     private HttpSession session = null;

  25.     private RemoteSessionHandler(HttpSession httpSession) {
  26.         this.session = httpSession;
  27.     };

  28.     public static HttpSession getInstance(HttpSession httpSession) {
  29.         InvocationHandler handler = new RemoteSessionHandler(httpSession);
  30.         return (HttpSession) Proxy.newProxyInstance(httpSession.getClass().getClassLoader(), httpSession.getClass().getInterfaces(), handler);
  31.     }

  32.     @Override
  33.     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  34.         if ("setAttribute".equals(method.getName())) {
  35.             String id = session.getId();
  36.             Map<String, Object> m = map.get(id);
  37.             if (m == null) {
  38.                 m = new HashMap<String, Object>();
  39.                 map.put(id, m);
  40.             }
  41.             m.put((String) args[0], args[1]);
  42.             System.out.println("[存入]key:" + args[0] + ",value:" + args[1]);
  43.             return null;
  44.         } else if ("getAttribute".equals(method.getName())) {
  45.             String id = session.getId();
  46.             Map<String, Object> m = map.get(id);
  47.             if (m == null) {
  48.                 return null;
  49.             }
  50.             Object result = m.get(args[0]);
  51.             System.out.println("[取出]key:" + args[0] + ",value:" + result);
  52.             return result;
  53.         }
  54.         return method.invoke(session, args);
  55.     }

  56. }
使用過濾器替換原有的Request

  1. import java.io.IOException;
  2. import javax.servlet.Filter;
  3. import javax.servlet.FilterChain;
  4. import javax.servlet.FilterConfig;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.ServletRequest;
  7. import javax.servlet.ServletResponse;
  8. import javax.servlet.annotation.WebFilter;
  9. import javax.servlet.http.HttpServletRequest;

  10. @WebFilter("/*")
  11. public class SessionFilter implements Filter {
  12.     @Override
  13.     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  14.         chain.doFilter(new RemoteSessionRequest((HttpServletRequest) request), response);
  15.     }

  16.     @Override
  17.     public void destroy() {
  18.         // TODO Auto-generated method stub

  19.     }

  20.     @Override
  21.     public void init(FilterConfig arg0) throws ServletException {
  22.         // TODO Auto-generated method stub

  23.     }
  24. }

在Servlet中按照原有方式使用HttpSession。

  1. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  2.         HttpSession session = request.getSession();
  3.         session.setAttribute("name", "Hello");
  4.         session.getAttribute("name");
  5.         session.getAttribute("other");
  6.     }
結果可以看到,他已經模擬從遠端伺服器存取資料

[存入]key:name,value:Hello
[取出]key:name,value:Hello
[取出]key:other,value:null




來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-1063018/,如需轉載,請註明出處,否則將追究法律責任。

相關文章