使用hessian實現web服務介面(一)
阿新 • • 發佈:2018-12-29
Hessian介紹
Hessian是一個輕量級的remoting on http工具,採用的是Binary RPC協議,所以它很適合於傳送二進位制資料,同時又具有防火牆穿透能力。Hessian一般是通過Web應用來提供服務,因此非常類似於平時我們用的 WebService。只是它不使用SOAP協議,但相比webservice而言更輕量級、簡單、快捷。
Hessian官網:http://hessian.caucho.com/
Hessian 可通過Servlet提供遠端服務,需要將匹配某個模式的請求對映到Hessian服務。也可Spring框架整合,通過它的 DispatcherServlet可以完成該功能,DispatcherServlet可將匹配模式的請求轉發到Hessian服務。Hessian的server端提供一個servlet基類, 用來處理髮送的請求,而Hessian的這個遠端過程呼叫,完全使用動態代理來實現的,,建議採用面向介面程式設計,Hessian服務通過介面暴露。
Hessian處理過程示意圖:客戶端——>序列化寫到輸出流——>遠端方法(伺服器端)——>序列化寫到輸出流 ——>客戶端讀取輸入流——>輸出結果
具體Hessian的開發:
建立HessianService介面
public interface HessianService {
public String sayHello(String name);
}
建立實現類HessianServiceImpl
public class HessianServiceImpl implements HessianService{
@Override
public String sayHello(String name) {
return "hello,"+name;
}
}
pom.xml中新增hessian依賴
<!-- hessian --> <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.7</version> </dependency>
配置web.xml
<servlet> <!-- 配置 HessianServlet,Servlet的名字隨便配置,例如這裡配置成ServiceServlet--> <servlet-name>hessianServiceTest</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <load-on-startup>1</load-on-startup> <init-param> <!-- 配置服務的介面,引數名稱service-class或其他固定寫法(下面貼上原始碼),如果不是這種寫法,會拋ServletException異常提示server must extend HessianServlet --> <param-name>service-class</param-name> <param-value>com.test.hessian.HessianServiceImpl</param-value> </init-param> </servlet> <servlet-mapping> <!-- 對映 HessianServlet的訪問URL地址--> <servlet-name>hessianServiceTest</servlet-name> <url-pattern>/hs/service</url-pattern> </servlet-mapping>
Hessian部分原始碼
public void init(ServletConfig config)
/* */ throws ServletException
/* */ {
/* 200 */ super.init(config);
/* */ try
/* */ {
/* 203 */ if (this._homeImpl == null)
/* */ {
/* 205 */ if (getInitParameter("home-class") != null) {
/* 206 */ String className = getInitParameter("home-class");
/* */
/* 208 */ Class homeClass = loadClass(className);
/* */
/* 210 */ this._homeImpl = homeClass.newInstance();
/* */
/* 212 */ init(this._homeImpl);
/* */ }
/* 214 */ else if (getInitParameter("service-class") != null) {
/* 215 */ String className = getInitParameter("service-class");
/* */
/* 217 */ Class homeClass = loadClass(className);
/* */
/* 219 */ this._homeImpl = homeClass.newInstance();
/* */
/* 221 */ init(this._homeImpl);
/* */ }
/* */ else {
/* 224 */ if (getClass().equals(HessianServlet.class)) {
/* 225 */ throw new ServletException("server must extend HessianServlet");
/* */ }
/* 227 */ this._homeImpl = this;
/* */ }
/* */ }
/* 230 */ if (this._homeAPI == null)
/* */ {
/* 232 */ if (getInitParameter("home-api") != null) {
/* 233 */ String className = getInitParameter("home-api");
/* */
/* 235 */ this._homeAPI = loadClass(className);
/* */ }
/* 237 */ else if (getInitParameter("api-class") != null) {
/* 238 */ String className = getInitParameter("api-class");
/* */
/* 240 */ this._homeAPI = loadClass(className);
/* */ }
/* 242 */ else if (this._homeImpl != null) {
/* 243 */ this._homeAPI = findRemoteAPI(this._homeImpl.getClass());
/* */
/* 245 */ if (this._homeAPI == null)
/* 246 */ this._homeAPI = this._homeImpl.getClass();
/* */ }
/* */ }
/* 249 */ if (this._objectImpl == null)
/* */ {
/* 251 */ if (getInitParameter("object-class") != null) {
/* 252 */ String className = getInitParameter("object-class");
/* */
/* 254 */ Class objectClass = loadClass(className);
/* */
/* 256 */ this._objectImpl = objectClass.newInstance();
/* */
/* 258 */ init(this._objectImpl);
/* */ }
/* */ }
/* 261 */ if (this._objectAPI == null)
/* */ {
/* 263 */ if (getInitParameter("object-api") != null) {
/* 264 */ String className = getInitParameter("object-api");
/* */
/* 266 */ this._objectAPI = loadClass(className);
/* */ }
/* 268 */ else if (this._objectImpl != null) {
/* 269 */ this._objectAPI = this._objectImpl.getClass();
/* */ } }
/* 271 */ this._homeSkeleton = new HessianSkeleton(this._homeImpl, this._homeAPI);
/* 272 */ if (this._objectAPI != null) {
/* 273 */ this._homeSkeleton.setObjectClass(this._objectAPI);
/* */ }
/* 275 */ if (this._objectImpl != null) {
/* 276 */ this._objectSkeleton = new HessianSkeleton(this._objectImpl, this._objectAPI);
/* 277 */ this._objectSkeleton.setHomeClass(this._homeAPI);
/* */ }
/* */ else {
/* 280 */ this._objectSkeleton = this._homeSkeleton;
/* */ }
/* 282 */ if ("true".equals(getInitParameter("debug"))) {
/* 283 */ this._isDebug = true;
/* */ }
/* 285 */ if ("false".equals(getInitParameter("send-collection-type")))
/* 286 */ setSendCollectionType(false);
/* */ } catch (ServletException e) {
/* 288 */ throw e;
/* */ } catch (Exception e) {
/* 290 */ throw new ServletException(e);
/* */ }
/* */ }