读书人

ice 远程调用施用application类

发布时间: 2012-10-11 10:16:10 作者: rapoo

ice 远程调用,使用application类

[["java:package:com.demo"]]module zhz{    struct Person{    string name;    string password;    };    dictionary<long,Person>PersonList;    interface PersonInter{    void sendUser(Person p);    Person returnPerson();    };};

? ice文件添加一个Person对象,一个字典表,表中为person对象,另外是一个方法调用。

client.java

package test;import org.apache.log4j.Logger;import com.demo.zhz.Person;import com.demo.zhz.PersonInterPrx;import com.demo.zhz.PersonInterPrxHelper;public class Client extends Ice.Application {/** * Logger for this class */private static final Logger logger = Logger.getLogger(Client.class);    class ShutdownHook extends Thread {        public void run() {if (logger.isDebugEnabled()) {logger.debug("run() - start"); //$NON-NLS-1$}            try {                communicator().destroy();            } catch (Ice.LocalException ex) {logger.error("run()", ex); //$NON-NLS-1$                ex.printStackTrace();            }if (logger.isDebugEnabled()) {logger.debug("run() - end"); //$NON-NLS-1$}        }    }    private String str = "My name is alex J, monster fairy is my baidu ID, the ICE Demo is running now! 八月初十";        public int run(String[] args) {if (logger.isDebugEnabled()) {logger.debug("run(String[]) - start"); //$NON-NLS-1$}        if (args.length > 0) {            System.err.println(appName() + ": too many arguments");if (logger.isDebugEnabled()) {logger.debug("run(String[]) - end"); //$NON-NLS-1$}            return 1;        }//        setInterruptHook(new ShutdownHook());        PersonInterPrx prx = PersonInterPrxHelper.checkedCast(communicator().propertyToProxy("Span.Proxy").ice_twoway().ice_timeout(-1).ice_secure(false));// 这里会读取配置文件中的内容,找到对应的suibianjiao服务相关端口和协议,当然,配置文件中用span也可以,我们在服务端代码中写了对应代码。        if (prx == null) {            System.err.println("invalid proxy");if (logger.isDebugEnabled()) {logger.debug("run(String[]) - end"); //$NON-NLS-1$}            return 1;        }        Person holder = new Person();        holder.name = "zhz";        holder.password = "zhenghuazhi";        prx.sendUser(holder);        if (logger.isDebugEnabled()) {logger.debug("run(String[]) - end"); //$NON-NLS-1$}        return 0;    }    public static void main(String[] args) {if (logger.isDebugEnabled()) {logger.debug("main(String[]) - start"); }        Client app = new Client();        int status = app.main("Client", args, "client.properties");// 这里加载配置文件        System.exit(status);if (logger.isDebugEnabled()) {logger.debug("main(String[]) - end"); }    }}server.java
package test;import org.apache.log4j.Logger;import Ice.Application;public class Server extends Ice.Application {/** * Logger for this class */private static final Logger logger = Logger.getLogger(Server.class);// class ShutdownHook extends Thread {// public void run() {// try {// communicator().destroy();// } catch(Ice.LocalException ex) {// ex.printStackTrace();// }// }// }public int run(String[] args) {if (logger.isDebugEnabled()) {logger.debug("run(String[]) - start"); //$NON-NLS-1$}if (args.length > 0) {System.err.println(appName() + ": too many arguments");if (logger.isDebugEnabled()) {logger.debug("run(String[]) - end"); //$NON-NLS-1$}return 1;}if (interrupted()) {System.out.println(appName() + ": terminating");}Ice.ObjectAdapter adapter = Application.communicator().createObjectAdapter("zhz"); // 这里的Span要和Server.config里的配置对应adapter.add(new PersonInterI(), Application.communicator().stringToIdentity("test"));// 我们可以定义多个ID,这样也是没问题的,客户端找哪个Identity都可以。不过一般不这么用。adapter.activate(); // 激活Servantcommunicator().getLogger().print("Span Server is start ...");// 这里调用插件communicator().waitForShutdown();if (logger.isDebugEnabled()) {logger.debug("run(String[]) - end"); //$NON-NLS-1$}return 0;}public static void main(String[] args) {if (logger.isDebugEnabled()) {logger.debug("main(String[]) - start"); //$NON-NLS-1$}Server app = new Server();int status = app.main("Server", args, "server.properties"); // 这里的配置文件没有写对应路径,所以运行程序的时候,要将配置文件打到ClassPath中System.exit(status);if (logger.isDebugEnabled()) {logger.debug("main(String[]) - end"); //$NON-NLS-1$}}}
?

?PersonInterI.java

package test;import org.apache.log4j.Logger;import Ice.Current;import com.demo.zhz.Person;import com.demo.zhz._PersonInterDisp;public class PersonInterI extends _PersonInterDisp {/** * Logger for this class */private static final Logger logger = Logger.getLogger(PersonInterI.class);@Overridepublic void sendUser(Person p, Current __current) {if (logger.isDebugEnabled()) {logger.debug("sendUser(Person, Current) - start"); //$NON-NLS-1$} System.out.println("Person = "+p.name+":"+p.password);if (logger.isDebugEnabled()) {logger.debug("sendUser(Person, Current) - end"); //$NON-NLS-1$}}@Overridepublic Person returnPerson(Current __current) {// TODO Auto-generated method stubreturn null;}}

?个人认为跨语言有必要使用,如果都是java应用的话,不觉得没有必要,毕竟java的远程调用方式比较多。

以上例子需要安装slice2java的eclipse插件。。

读书人网 >行业软件

热点推荐