Use phphessian to call Java spring Hessian exposure Service
Favorites
<1> JAVA:
1. Write the spring service interface and implementation class, which is currently called
Tadgetmanager, tadgetmanagerimpl. These are all self-implemented exposures.
Tadgetmanager:
2. Configure spring MVC and spring Hessian components for ease of use. Expose Java services:
1.
Spring profiles tadgets-service.xml
:
// Here is the bean name of the implementation class of the tadgetmanager interface service.
<Bean id = "tadgetmanager"
Class = "org. springframework. remoting. Caucho. tadgetmanagerimpl">
</Bean>
// Put our bean in spring Hessian service Bean
<Bean id = "tadgetmanagerservice"
Class = "org. springframework. remoting. Caucho. hessianserviceexporter">
<Property name = "service">
<Ref bean = "tadgetmanager"/>
</Property>
<Property name = "serviceinterface">
<Value> com. luodongfu. Test. Service. tadgetmanager </value>
</Property>
</Bean>
// Request blocker for external services, similar to Java sevlet class
<Bean id = "publicurlmapping"
Class = "org. springframework. Web. servlet. handler. simpleurlhandlermapping">
<Property name = "mappings">
<Value>
/Ldftest = tadgetmanagerservice
</Value>
</Property>
</Bean>
2.
Web. xml
: Remember in our J2EE Project
Web. xml configuration
The request URL interceptor is the publicurlmapping we configured above.
<Servlet-mapping>
<Servlet-Name> spring </servlet-Name>
<URL-pattern>/ldftest </url-pattern>
</Servlet-mapping>
<2> PHP:
1.
[B] Use Zend studio for eclipse 6.1.
Create PHP
Project:
[B] 3.
Create Hessian
PHP do
Class:
[B] 1.
Create Hessian
PHP do
Class:
<
1>
Dbroute: used by dbroute. Class. php database sharding Technology
Class dbroute
2 .{
3. Protected $ userid;
4. Protected $ Itemid;
5. Protected $ Xid;
6. Protected $ routingstratery;
7.
8. Public Function getitemid ()
9 .{
10. Return $ this-> Itemid;
11 .}
12. Public Function getroutingstratery ()
13 .{
14. Return $ this-> routingstratery;
15 .}
.................................
Basedo: basedo. Class. php, corresponding to Java class of COM. Taobao. Common. Dao. Persistence. basedo:
Class basedo
{
Protected $ dbroute;
Function _ construct ()
{
$ This-> dbroute = new dbroute ();
}
Public Function getdbroute ()
{
Return $ this-> dbroute;
}
// Set attribute values in dbroute through array Traversal
Public Function setdbroute ($ dbroute)
{
Foreach ($ dbroute as $ key => $ value)
{
$ This-> dbroute->__ set ($ key, $ value );
}
}
}
Basetadgetsdo: basetadgetsdo. Class. php, corresponding to Java class of COM. luodongfu. Test. domain. basetadgetsdo:
Class basetadgetsdo extends basedo
{
Protected $ ID;
[B] 19.
Omitted get, set attribute function here
Protected $ catalogid;
Protected $ tadgetcode;
Protected $ isvid;
Protected $ title;
Protected $ description;
Protected $ detailref;
...........................
Public Function getappkey ()
{
Return $ this-> appkey;
}
Public Function getcatalogid ()
{
Return $ this-> catalogid;
}
Public Function getdescription ()
{
Return $ this-> description;
}
Omitted get, set attribute function here
Tadgetsdo class, corresponding to Java class of COM. luodongfu. Test. tadgetsdo
Class tadgetsdo extends basetadgetsdo
{
Protected $ isV;
Public Function _ construct ()
{
Parent: :__ construct ();
}
Public Function getisv ()
{
Return $ this-> isV;
}
Public Function setisv ($ isV)
{
$ This-> $ isV = $ isV;
}
Public Function _ set ($ propname, $ propvalue)
{
If ("dbroute" ==$ propname & count ($ propvalue)> 0)
{
$ This-> setdbroute ($ propvalue );
} Else
{
$ This-> $ propname = $ propvalue;
}
}
Public Function _ Get ($ propname)
{
Return $ this-> $ propname;
}
Public Function _ tostring ()
{
}
}
4.
Create a phphessian
Client-to-top
Service encapsulation,
Finally, we will focus on the class:
Ldfphphessianclient, ldfphphessianclient. Class. php:
Require_once 'lib/hessianclient. php ';
Class ldfphphessianclient
{
Private $ serviceurl; // Hessian service URL
Private $ proxy; // hessianclient instance, reflection mechanism proxy class
Private $ result; // Hessian result
// Constructor. Everyone on Earth knows this.
Public Function _ construct ($ serviceurl)
{
// Check the Hessian service URL
$ This-> initurl ($ serviceurl );
// Initialize the hessianclient instance. Pay attention to the & Address Transfer call here. If this is not used, the instance will be suspended.
$ This-> proxy = & New hessianclient ($ this-> serviceurl );
}
// Try it out and call the gettadgetbyid service in top tadgetmanager to check whether
Public Function gettadgetbyid ($ tadgetid)
{
// Check the parameters. If initialization fails, an exception is thrown.
If (! Isset ($ this-> proxy ))
{
Throw new nullpointexception ("The hessianclient object is not initial.", constants: exception_null_point );
}
// Check the parameter. If it is null, an exception occurs.
If (empty ($ tadgetid )&&! Is_numeric ($ tadgetid ))
{
Throw new nullpointexception ("the parameter tadgetid is
Null or is not a number. ", constants: prediction_null_point );
}
// Clear the result first. If it is a local variable, it will not be used.
Unset ($ this-> result );
// Start to call the service gettadgetbyid. An exception may occur. Note:
Try {
$ This-> result = $ this-> proxy-> gettadgetbyid ($ tadgetid );
} Catch (topexception $ E)
{
// Directly returns an exception and prints a simple log
Echo "topexception". $ e-> printstack ();
Return NULL;
}
// If there is a result, start the Conversion Result
If (isset ($ this-> result ))
{
// Conversion Result
Return $ this-> convertobjectvalues (New tadgetsdo (), $ this-> result );
}
Return NULL;
}
// Use the reflection mechanism to call the setxxxxx ($ OBJ) function one by one from the array returned by the server.
Private function convertobjectvalues ($ returnobject, $ valueobject)
{
// Reflection class for objects in the returned array
$ Object = new reflectionclass ($ returnobject );
// Traverse the index in the array and find the setxxx ($ XX) function corresponding to tadgetsdo.
Foreach ($ valueobject as $ key => $ value)
{
// Obtain the setxxx function name.
$ Methodname = "set". ucfirst ($ key );
// Reflection from the tadgetsdo instance to check whether the function methodname exists.
$ Method = $ object-> getmethod ($ methodname );
// Return directly without this function
If (! $ Method)
{
Continue;
}
// Judge whether a function is public or non-Abstract. It is called like a Java reflection mechanism.
If ($ method-> ispublic ()&&! $ Method-> isabstract ())
{
$ Method-> invoke ($ returnobject, $ value );
}
}
// In fact, there is another way to call the method without the reflection mechanism.
/* Foreach ($ valueobject as $ key => $ value)
{
$ Returnobject->__ set ($ key, $ value );
}
However, you must first Add the following method to the tadgetsdo class:
Public Function _ set ($ propname, $ propvalue)
{
$ This-> $ propname = $ propvalue;
}
*/
Return $ returnobject;
}
Public Function getserviceurl ()
{
Return $ this-> serviceurl;
}
Public Function setserviceurl ($ serviceurl)
{
$ This-> serviceurl = $ serviceurl;
}
// Determine whether the tophessian service URL is null
Private function initurl ($ serviceurl)
{
If (! If isset ($ serviceurl) is null, an exception is indicated.
{
Throw new nullpointexception ("the Hessian service URL is null.", constants: prediction_null_point );
}
$ This-> setserviceurl ($ serviceurl );
}
Public Function _ autoload ()
{
}
}
1.
Let's get started with our results.
:
2.
<? PHP
// Introduce our own classes, similar to the usage of import in Java or using in. net
Include_once 'ldfphessianclient. Class. php ';
3.
$ Testurl = 'HTTP: // 127.0.0.1/top/tadgettest ';
// Create our ldfphessianclient instance
4.
$ Ldfphphessianclient = new ldfphphessianclient ($ testurl );
// Test our tadget
5.
$ Result = $ ldfphessianclient-> gettadgetbyid (22 );
// Print the internal results of tadget.
6. print_r (
$ Result );
8.
$ Ldfphphessianclient = NULL;
?>
Running result: