Skip to content

Instantly share code, notes, and snippets.

@xcaspar
Created July 20, 2015 09:11
Show Gist options
  • Save xcaspar/05eb74077721e25637da to your computer and use it in GitHub Desktop.
Save xcaspar/05eb74077721e25637da to your computer and use it in GitHub Desktop.

Hession超时控制

设置hession调用超时的配置如下(默认为20秒,可根据业务场景自己调整):

<bean id="gateConfigFacade" class="org.springframework.remoting.caucho.HessianProxyFactoryBean" lazy-init="true" >
    <property name="serviceUrl" value="${mvn.config.url}/gateConfigFacade" />
    <property name="serviceInterface" value="com.changyou.npt.config.facade.gate.GateConfigFacade" />
    <property name="proxyFactory">
        <bean class="com.caucho.hessian.client.HessianProxyFactory" p:hessian2Request="true" p:readTimeout="20000" p:connectTimeout="20000" p:hessian2Reply="true" />
    </property>
</bean>

使用时需要注意主动去捕获RemoteConnectFailureException异常(当创建连接超时、服务不可用、调用超时抛出该异常),其它异常会原样抛出,示例如下:

public void testFindIpWhitelist() {
    List<IpWhitelist> list;
    try {
        list = gateConfigFacade.findIpWhitelist(); //业务调用
        for(IpWhitelist ip : list) {
            System.out.println(ip.getIp());
        }
    } catch (RemoteConnectFailureException e) {
        //捕获到创建连接超时、服务不可用、调用超时异常,根据业务判断是否需要重试
        System.out.println("===============================" + e.toString());
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment