关于phprpc测试实例
一. 服务接口定义
package com.yanek.study.phprpc;
public interface Hello {
String say(String name);
}
二 . 服务接口实现
package com.yanek.study.phprpc;
/**
* 服务接口实现
*
*
*/
public class MyHello implements Hello {
public String say(String name) {
return "Hello "+name;
}
}
3. 发布服务:
编写rpc.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="com.yanek.study.phprpc.*" %>
<%@ page import="org.phprpc.*" %>
<%
Hello hello = new MyHello();
PHPRPC_Server phprpc_server = new PHPRPC_Server();
phprpc_server.add(hello);
phprpc_server.start(request, response);
%>
4 . 客户端测试
package com.yanek.study.phprpc;
import org.phprpc.PHPRPC_Callback;
import org.phprpc.PHPRPC_Client;
public class TestClient {
/**
* @param args
*/
public static void main(String[] args) {
PHPRPC_Client client = new PHPRPC_Client("http://study.myweb.com:8080/MyStudy/phprpc/rpc.jsp");
Hello m = (Hello) client.useService(Hello.class);
System.out.println(m.say("javaboy"));
//也可以以 callback形式调用.
client.invoke("say", new Object[] {"other"}, new PHPRPC_Callback() {
public void handler(String result, Object[] args) {
System.out.println("handler:");
System.out.println(result);
System.out.println("args:"+args[0]);
System.out.println();
}
});
}
}
关于phprpc官方:
注意:
工程需要引入 phprpc.jar.