【RPC框架Hessian三】Hessian 异常处理

RPC异常处理概述

 

RPC异常处理指是,当客户端调用远端的服务,如果服务执行过程中发生异常,这个异常能否序列到客户端?

 

如果服务在执行过程中可能发生异常,那么在服务接口的声明中,就该声明该接口可能抛出的异常。

 

在Hessian中,服务器端发生异常,可以将异常信息从服务器端序列化到客户端,因为Exception本身是实现了Serializable的,因此,我们在自定义异常时,不需要显式的让自定义的

异常实现Serializable接口

 

 

举例

 

继续以【RPC框架Hessian二】中的代码为例,修改服务的save方法,刻意的让服务器端的代码抛出异常(抛出的是运行时异常,受检异常与此类似)

 

package com.tom.hessian.server;

import com.tom.hessian.common.ComplexModel;
import com.tom.hessian.common.IComplexModelService;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by tom on 14-9-2.
 */
public class ComplexModelService implements IComplexModelService {
    private Map<Integer,ComplexModel> models = new HashMap<Integer, ComplexModel>();
    @Override
    public void save(ComplexModel model) {
        if (model.getId() == null){
            throw new IllegalArgumentException("id could not be null");
        }

        if (model.getId() == 1) { //客户端的model的id为1,因此会发生unchecked exception
            throw new IllegalArgumentException("id could not be 1");
        }

        models.put(model.getId(), model);
    }

    @Override
    public ComplexModel read(Integer modelId) {
        return models.get(modelId);
    }
}

 当客户端的model的id为1时,调用save方法,抛出

 

        if (model.getId() == 1) { //客户端的model的id为1,因此会发生unchecked exception
            throw new IllegalArgumentException("id could not be 1");
        }

 

此时服务器端没有抛出异常,而客户端抛出异常:

 

Exception in thread "main" java.lang.IllegalArgumentException: id could not be 1
	at com.tom.hessian.server.ComplexModelService.save(ComplexModelService.java:21)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at com.caucho.hessian.server.HessianSkeleton.invoke(HessianSkeleton.java:302)
	at com.caucho.hessian.server.HessianSkeleton.invoke(HessianSkeleton.java:198)
	at com.caucho.hessian.server.HessianServlet.invoke(HessianServlet.java:399)
	at com.caucho.hessian.server.HessianServlet.service(HessianServlet.java:379)
	at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
	at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
	at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
	at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
	at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726)
	at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
	at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:206)
	at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
	at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)
	at org.mortbay.jetty.Server.handle(Server.java:324)
	at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
	at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:842)
	at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:648)
	at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
	at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
	at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
	at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:450)

 

Hessian服务器端异常处理源代码分析

 

在Hessian的HessianSkeleton类的invoke方法中,通过反射调用服务代码:

 

    try {
      result = method.invoke(service, values); //调用的服务端类、参数和方法
    } catch (Exception e) { //捕获异常
      Throwable e1 = e;
      if (e1 instanceof InvocationTargetException)
        e1 = ((InvocationTargetException) e).getTargetException();

      log.log(Level.FINE, this + " " + e1.toString(), e1);

      out.writeFault("ServiceException", 
                     escapeMessage(e1.getMessage()), 
                     e1);//将异常信息序列到客户端
      out.close();
      return;
    }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(hessian)