http://wesee.iteye.com/blog/663876
我们查看HessianServiceExporter原代码发现:
1 2 3 |
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException |
在该方法中可以获得客户端request,那我们就会有很多种方法得到此request;Aop方式或重写方法,下面针对重写方法做了一个简单的显示。
Hessian Service 线程上下文,用以线程安全地保存客户端 request:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class HessianContext { private ServletRequest _request; private static final ThreadLocal<HessianContext> _localContext = new ThreadLocal<HessianContext>() { @Override public HessianContext initialValue() { return new HessianContext(); } }; private HessianContext() { } public static void setRequest(ServletRequest request) { _localContext.get()._request = request; } public static ServletRequest getRequest() { return _localContext.get()._request; } public static void clear() { _localContext.get()._request = null; } } |
我们自定义类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class OurServiceExporter extends HessianExporter implements HttpRequestHandler { private static final Logger logger = LoggerFactory.getLogger(OurServiceExporter.class); @Override public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (!"POST".equals(request.getMethod())) { throw new HttpRequestMethodNotSupportedException( request.getMethod(), new String[]{"POST"}, "HessianServiceExporter only supports POST requests"); } response.setContentType(CONTENT_TYPE_HESSIAN); try { HessianContext.setRequest(request); //保存Request到Hessian线程上下文 invoke(request.getInputStream(), response.getOutputStream()); } catch (Throwable ex) { logger.error("Hessian skeleton invocation failed"); throw new NestedServletException("Hessian skeleton invocation failed", ex); } finally { HessianContext.clear(); } } } |
在remoting-servlet.xml中配置服务接口
1 2 3 4 5 |
<bean name="/userService" class="com.mycompany.test.OurServiceExporter"> <property name="service" ref="user"/> <property name="serviceInterface" value="com.company.IUser"/> </bean> </beans> |
Service开发中就可以直接使用 HessianContext.getRequest(); 来获得客户端的request了,所有的客户端信息随你所用了,而且是线程安全的。