Package demo

Source Code of demo.HessianInvoke

package demo;

import java.net.MalformedURLException;

import com.caucho.hessian.client.HessianProxyFactory;

public class HessianInvoke {
  private String remoteServer;

  private long readTimeout = -1;

  private long connectTimeout = -1;

  private HessianProxyFactory factory;

  /**
   * 构造方法
   *
   * @param remoteServer
   *            Hessian 服务地址 如:Http://localhost:8080/biz/hessian/
   */
  public HessianInvoke(String remoteServer) {
    if (remoteServer != null && !remoteServer.endsWith("/")) {
      this.remoteServer = remoteServer + "/";
    } else {
      this.remoteServer = remoteServer;
    }
    factory = new HessianProxyFactory();
    factory.setConnectTimeout(connectTimeout);
    factory.setReadTimeout(readTimeout);
  }

  public void setReadTimeout(long readTimeout) {
    this.readTimeout = readTimeout;
    factory.setReadTimeout(readTimeout);
  }

  public void setConnectTimeout(long connectTimeout) {
    this.connectTimeout = connectTimeout;
    factory.setConnectTimeout(connectTimeout);
  }

  public void setOverloadEnabled(boolean isOverloadEnabled) {
    factory.setOverloadEnabled(isOverloadEnabled);
  }

  /**
   *
   *
   * 方法说明: 获取接口的远程实现对象
   *
   * @param interfaceClazz
   *            远程调用接口类,服务名称为接口类名去掉第一个字母并小写化
   * @return 接口类的远程实现
   */
  public Object getImpl(Class interfaceClazz) {
    return getImpl(interfaceClazz, null);
  }

  /**
   *
   *
   * 方法说明: 获取接口的远程实现对象
   *
   * @param interfaceClazz
   *            远程调用接口类,
   * @param serviceName
   *            远程服务名称,如果为空,则服务名为接口类名去掉第一个字母并小写化
   * @return
   */
  public Object getImpl(Class interfaceClazz, String serviceName) {

    String url = null;

    if (serviceName != null) {
      url = getUrl(serviceName);
    } else {
      url = getUrl(interfaceClazz.getName().substring(1).toLowerCase());
    }
    try {
      return factory.create(interfaceClazz, url);
    } catch (MalformedURLException e) {
      throw new RuntimeException(
          "invoke remote service throws exception,", e);
    }
  }

  private String getUrl(String serviceName) {
    String url = remoteServer + serviceName;
    return url;
  }
}
TOP

Related Classes of demo.HessianInvoke

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.