HessianHessian连接超时处理

Posted 蜗牛不怕慢

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HessianHessian连接超时处理相关的知识,希望对你有一定的参考价值。

        Hessian作为一个远程连接工具,必然存在连接超时等问题,如果不对连接超时等参数进行相关的设置,当网络出现问题后就会造成整个hessian处理的阻塞,进而阻塞整个线程后续的处理。

目前,Hessian客户端远程调用webService主要采用了以下工具类:

(1)hessian的HessianProxyFactory(com.caucho.hessian.client.HessianProxyFactory)
(2)spring的HessianProxyFactoryBean(org.springframework.remoting.caucho.HessianProxyFactoryBean).

一、HessianProxyFactory的连接超时处理

查看源码,存在_readTimeout和_connectTimeout属性,直接设置即可

public class HessianProxyFactory implements ServiceProxyFactory, ObjectFactory 
  protected static Logger log
    = Logger.getLogger(HessianProxyFactory.class.getName());

  private final ClassLoader _loader;
  
  private SerializerFactory _serializerFactory;

  private HessianConnectionFactory _connFactory;
  
  private HessianRemoteResolver _resolver;

  private String _user;
  private String _password;
  private String _basicAuth;

  private boolean _isOverloadEnabled = false;

  private boolean _isHessian2Reply = true;
  private boolean _isHessian2Request = false;

  private boolean _isChunkedPost = true;
  private boolean _isDebug = false;

  private long _readTimeout = -1;
  private long _connectTimeout = -1;
  ...........
  
client方法调用如下:

  public class Test 
	public static void main(String[] args) 
		String url = "http://localhost:8080/remote/getMessageService";
		HessianProxyFactory factory = new HessianProxyFactory();
		IGetMessage basic;
		try 
			basic = (IGetMessage) factory.create(IGetMessage.class, url);
			factory.setConnectTimeout(1000);
			factory.setReadTimeout(5000);
			System.out.println("Message From Server: " + basic.getMsg("hello,my first hessian"));
		 catch (MalformedURLException e) 
			e.printStackTrace();
		
	
二、HessianProxyFactoryBean的连接超时处理

HessianProxyFactoryBean不存在连接超时等参数的设置,需要我们加入相应的连接超时和读取超时的变量,重写afterPropertiesSet方法等。

新建类MyHessianProxyFactoryBean

public class MyHessianProxyFactoryBean extends HessianProxyFactoryBean 
	//读取超时
	private int readTimeOut = 500;
	// 连接超时
	private int connectTimeOut = 1000;

	public int getReadTimeOut() 
		return readTimeOut;
	

	public void setReadTimeOut(int readTimeOut) 
		this.readTimeOut = readTimeOut;
	

	public int getConnectTimeOut() 
		return connectTimeOut;
	

	public void setConnectTimeOut(int connectTimeOut) 
		this.connectTimeOut = connectTimeOut;
	

	@Override
	public void prepare() throws RemoteLookupFailureException 
		HessianProxyFactory proxyFactory = new HessianProxyFactory();
		if (this.readTimeOut > 0) 
			proxyFactory.setReadTimeout(this.readTimeOut);
		
		if (this.connectTimeOut > 0) 
			proxyFactory.setConnectTimeout(this.connectTimeOut);
		
		this.setProxyFactory(proxyFactory);

		super.prepare();
	
修改bean的配置,增加属性

  <bean id="getMessageService"
		class="com.sf.my.MyHessian.MyHessianProxyFactoryBean">
		<property name="serviceUrl" value="http://localhost:8080/remote/getMessageService" />
		<property name="serviceInterface" value="com.sf.my.interfaces.IGetMessage" />
		<property name="connectTimeOut" value="100"></property>
		<property name="readTimeOut" value="50000"></property>
  </bean>

以上是关于HessianHessian连接超时处理的主要内容,如果未能解决你的问题,请参考以下文章

MySQL连接超时自动断开连接应该怎样处理

mysql连接超时怎么处理

mysql连接超时的问题处理

接口超时需要怎么处理

使用连接池处理活动记录超时的正确方法是啥?

如何正确处理连接空闲超时错误