Hessian connection timeout settings

Source: Internet
Author: User

In the previous article about Hessian parsing and application (Integrating spring), after learning the usage and principles of Hessian, I always felt that there was a flaw left in my mind ...... that is, the timeout settings. This remote call will inevitably cause timeout.

Today, I think of Google... and then I saw this guy'sArticleHttp://www.blogjava.net/dongbule/archive/2010/12/16/340894.html (solve Hessian Remote Call connection timeout problem), and comments, summarized as follows:

Hessianproxyfactory source code for connection Processing

Protected urlconnection openconnection (URL) throws ioexception {urlconnection conn = URL. openconnection (); Conn. setdooutput (true); If (_ readtimeout> 0) {try {Conn. setreadtimeout (INT) _ readtimeout);} catch (throwable e) {}} Conn. setrequestproperty ("Content-Type", "x-application/Hessian"); If (_ basicauth! = NULL) Conn. setrequestproperty ("Authorization", _ basicauth); else if (_ user! = NULL & _ password! = NULL) {_ basicauth = "Basic" + base64 (_ User + ":" + _ password); Conn. setrequestproperty ("Authorization", _ basicauth);} return conn ;}

Therefore, we inherit from this logic and rewrite the openconnection method. When creating an HTTP connection, we set the connection timeout to solve the network congestion.ProgramContinue

CodeFrom xuzhengsong

Protected urlconnection openconnection (URL) throws ioexception {urlconnection conn = super. openconnection (URL); If (this. connecttimeout> 0) {Conn. setconnecttimeout (this. connecttimeout);} retrun conn ;}

 

Check the source code of hessianproxyfactorybean of spring and find (For original code analysis, see Hessian parsing and application (Integrating spring)In the encapsulation of Hessian, it directly creates a hessianproxyfactory instance, and then uses this instance to create a remote service. Therefore, the solution is similar to the preceding one, inherit hessianproxyfactorybean, add the corresponding connection timeout and read timeout variables, rewrite the afterpropertiesset method, and complete the transformation of hessianproxyfactory in the first step, in this way, the program execution will not be blocked due to network reasons when connecting to the remote webserver server.

 Public   Class Myhessianproxyfactorybean Extends  Hessianproxyfactorybean {  Private Myhessianproxyfactory proxyfactory =New  Myhessianproxyfactory ();  Private   Int Readtimeout = 10000 ;  Private   Int Connecttimeout = 10000 ;  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 ;}  Public   Void  Afterpropertiesset () {proxyfactory. setreadtimeout (readtimeout); proxyfactory. setconnecttimeout (connecttimeout); setproxyfactory (proxyfactory );  Super  . Afterpropertiesset ();}} 

 

--------------------------------------------------------------------- Split line -----------------------------------------------------------------------------------

Google's above content, and then constantly compare the Hessian original code analysis, never found the openconnection method in hessianproxyfactory, and then thought of the version problem, I used Hessian-4.0.7, in hessianproxyfactory of version 3, we saw the openconnection method.

Then I read the related code in step 4 and saw the following code in hessianproxyfactory (as for how to get to the createhessianconnectionfactory () method, let's look at Hessian parsing and application (Integrating spring)

 
Protected hessianconnectionfactory createhessianconnectionfactory () {string classname = system. getproperty (hessianconnectionfactory. Class. getname (); hessianconnectionfactory factory = NULL; try {If (classname! = NULL) {classloader loader = thread. currentthread (). getcontextclassloader (); Class <?> CL = Class. forname (classname, false, loader); factory = (hessianconnectionfactory) Cl. newinstance (); Return factory ;}} catch (exception e) {Throw new runtimeexception (E);} return New hessianurlconnectionfactory ();}

Enter the hessianurlconnectionfactory class and see the open method in it.

/*** Opens a new or recycled connection to the HTTP server. */Public hessianconnection open (URL) throws ioexception {If (log. isloggable (level. finer) log. finer (This + "open (" + URL + ")"); urlconnection conn = URL. openconnection (); // httpurlconnection httpconn = (httpurlconnection) Conn; // httpconn. setrequestmethod ("Post"); // Conn. setdoinput (true); long connecttimeout = _ proxyfactory. getconnecttimeout (); If (connecttimeout> = 0) Conn. setconnecttimeout (INT) connecttimeout); Conn. setdooutput (true); long readtimeout = _ proxyfactory. getreadtimeout (); If (readtimeout> 0) {try {Conn. setreadtimeout (INT) readtimeout);} catch (throwable e ){}}

It can be seen that hessian4 has improved the timeout time, so you only need to set the timeout time through spring.

In hessianproxyfactory, spring's hessianclientinterceptor is its subclass and contains a hessianproxyfactory instance. Therefore, you can provide a setconnectiontimeout method in hessianclientinterceptor:

 
Public void setconnecttimeout (long timeout) {This. proxyfactory. setconnecttimeout (timeout );}

Configure the time in spring.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.