Before using RPC are WebService, Hessian, Dubbo, and so on, now the company used ice, have not heard of how to spend some time to learn the ice, and write a Demo,java the basis of data type transmission are not too big problem, Object transmission is troublesome, oneself in a lot of data to realize, online rarely find a transmission object type complete instance. Crap don't say much, now paste code.
Slice file, Hello.ice:
Module com{
Module test{
//predefined
module bean{
class Response;
Module bean{
class Request;
Module operator{
//Performer
Interface hello{
//Get Response object
idempotent com::test::bean::response SayHello (Com::test::bean::request Request);};};
Requestdef.ice file:
Module com{
module test{
module bean{
class request{
//Fields
string Traceid;
String message;
Methods
string Gettraceid ();
void Settraceid (string traceid);
String GetMessage ();
void Setmessage (String message);};};};};
Responsedef.ice file:
Module com{
module test{
module bean{
class response{
//Fields
string code;
String message;
Methods
string GetCode ();
void Setcode (string code);
String GetMessage ();
void Setmessage (String message);};};};};
Helloi.java
Package Com.test.impl;
Import java.io.Serializable;
Import ice.current;
Import Com.test.bean.Request;
Import Com.test.bean.Response;
Import Com.test.operator._hellodisp;
Import Com.test.response.ResponseI;
public class Helloi extends _hellodisp implements Serializable {
private static final long serialversionuid = 1L;
@Override public
Response SayHello (Request request, current __current) {
Response response=new responsei (); C11/>response.setcode ("x001");
Response.setmessage ("test response");
return response
}
}
Helloserver.java:
Package com.test.server;
Import java.io.Serializable;
Import Ice.objectadapter;
Import Com.test.bean.Request;
Import Com.test.impl.HelloI;
Import Com.test.request.ObjectFactory4Request;
public class HelloServer implements Serializable {private static final long serialversionuid = -691557224337330222l;
public static void Main (string[] args) {//execution status int status = 0;
Ice.communicator IC = null;
try {//initialization environment IC = Ice.Util.initialize (); Add a custom factory class to request the assembly of the information, if the request parameter is not a reference type, you do not need ic.addobjectfactory (new Objectfactory4request (), Request.ice_staticid ()
);
Initializes adapter String name = "HelloServer";
String endpoints = "default-h 127.0.0.1-p 9999";
Objectadapter objadapter = ic.createobjectadapterwithendpoints (name, endpoints);
Create servo Ice.object servant = new Helloi ();
Add servo to Adapter Objadapter.add (servant, Ice.Util.stringToIdentity ("helloid"));
Activate Objadapter.activate (); SYSTEM.OUT.PRINTLN ("Helloid service started, waiting to receive information ..."));
Wait for closure of Ic.waitforshutdown ();
catch (Exception e) {e.printstacktrace ();
status = 1;
finally {if (IC!= null) {Ic.destroy ();
} system.exit (status); }
}
}
Helloclient.java
/** * * * * * * Package com.test.client;
Import java.io.Serializable;
Import Com.test.bean.Request;
Import Com.test.bean.Response;
Import Com.test.operator.HelloPrx;
Import Com.test.operator.HelloPrxHelper;
Import Com.test.request.RequestI;
Import Com.test.response.ObjectFactory4Response;
Import Ice.objectprx;
public class Helloclient implements serializable{private static final long serialversionuid = 1L;
Private Ice.communicator IC = null;
public static void Main (string[] args) {helloclient client = new Helloclient ();
Request request=new Requesti ();
Request.settraceid ("002");
Request.setmessage ("test");
Response Response = Client.sayhello (request);
StringBuffer info = new StringBuffer ();
if (response = = null) {info.append ("no Return Information");
else {info.append ("code=" + Response.code);
Info.append (", message=" + response.getmessage ());
} System.out.println (Info.tostring ());
Exit JVM system.exit (0);
}/**/Public Response SayHello (Request request) {Response Response = null;
try {//Get proxy helloprx helloprx=null;
if (This.ic = = null) {//initialization environment IC = Ice.Util.initialize ();
//Create Proxy base class object String str = "helloid:default-h 127.0.0.1-p 9999";
Objectprx objprx = This.ic.stringToProxy (str);
Helloprx = Helloprxhelper.checkedcast (OBJPRX);
Add custom class Ice.objectfactory factory = new Objectfactory4response (); Add the factory class, used to return information assembly, if the return parameter is not a reference type, you do not need This.ic.addObjectFactory (factory, Com.test.response.ResponseI.ice_stat
ICID ());
Response = Helloprx.sayhello (request);
catch (Exception e) {e.printstacktrace ();
} return response;
}
}
Objectfactory4request.java, this is important.
/**
* * * * * *
package com.test.request;
Import ice.objectfactory;
public class Objectfactory4request implements Objectfactory {
@Override public
ice.object Create (String type) {
if (Type.equals (Com.test.request.RequestI.ice_staticId ())) {return
new Requesti ();
}
return null;
}
@Override public
Void Destroy () {
//TODO auto-generated method stub
}
}
Requesti.java
/**
* * * * * *
package com.test.request;
Import java.io.Serializable;
Import ice.current;
Import Com.test.bean.Request;
public class Requesti extends Request implements serializable{
/**
* */
private static final long Serialversionuid = -4990773688048548492l;
@Override public
String Gettraceid (current __current) {return
This.traceid;
}
@Override public
void Settraceid (String traceid, current __current) {
this.traceid=traceid;
}
@Override public
String getmessage (current __current) {return
this.message;
}
@Override public
void Setmessage (String message, current __current) {
this.message=message
}
}
Objectfactory4response.java, this is important.
/**
* * * * * *
package com.test.response;
Import ice.objectfactory;
public class Objectfactory4response implements Objectfactory {
@Override public
ice.object Create (String type) {
if (type.equals (Com.test.response.ResponseI.ice_staticId ())) {return
new Responsei ();
}
return null;
}
@Override public
Void Destroy () {
//TODO auto-generated method stub
}
}
Responsei.java
/**
* * * * * *
package com.test.response;
Import java.io.Serializable;
Import ice.current;
Import Com.test.bean.Response;
public class Responsei extends Response implements serializable{
/**
* */
private static final long Serialversionuid = 1L;
@Override public
String GetCode (current __current) {return
this.code;
}
@Override public
void Setcode (String code, current __current) {
this.code=code;
}
@Override public
String getmessage (current __current) {return
this.message;
}
@Override public
void Setmessage (String message, current __current) {
this.message=message
}
}
Other slice generated Java will not be posted, first start the HelloServer service, and then start the client helloclient, you can test.
SOURCE download