Thrift(2)RMI Strategy Sample
RMI is short for remote method invocation. It is easy to use it in java world.
But it is complex to go through the firewall. And the language is limited to java.
From other's blogs, we can use RMI easily like this
Interface class first:
package com.sillycat.easytalker.plugins.rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface IHello extends Remote {
public String helloWorld() throws RemoteException;
public String sayHelloToSomeBody(String someBodyName)
throws RemoteException;
}
Implementation class like this:
package com.sillycat.easytalker.plugins.rmi;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class HelloImpl extends UnicastRemoteObject implements IHello {
private static final long serialVersionUID = 6318519261707859826L;
public HelloImpl() throws RemoteException {
}
public String helloWorld() throws RemoteException {
return "Hello World!";
}
public String sayHelloToSomeBody(String someBodyName)
throws RemoteException {
return "Hello, nice to meet you, " + someBodyName + "!";
}
}
Server side to register the Interface and implementation:
package com.sillycat.easytalker.plugins.rmi;
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class RMIServer {
public static void main(String[] args) {
try {
IHello rhello = new HelloImpl();
LocateRegistry.createRegistry(9813);
Naming.bind("//localhost:9813/rhello", rhello);
System.out.println(">>>>>INFO:Remote IHello Binding Success!");
} catch (RemoteException e) {
e.printStackTrace();
} catch (AlreadyBoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
Client side to call the server side:
package com.sillycat.easytalker.plugins.rmi;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class RMIClient {
public static void main(String[] args) {
try {
IHello rhello = (IHello) Naming.lookup("//localhost:9813/rhello");
System.out.println(rhello.helloWorld());
System.out.println(rhello.sayHelloToSomeBody("sillycat"));
} catch (NotBoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
For go across the firewall, base on the blog: http://wangse.iteye.com/blog/191797
we can use SMRMISocket.java
1.import java.rmi.server.*;
2.import java.io.*;
3.import java.net.*;
4.public class SMRMISocket extends RMISocketFactory {
5. public Socket createSocket(String host, int port)
6. throws IOException{
7. return new Socket(host,port);
8. }
9. public ServerSocket createServerSocket(int port)
10. throws IOException {
11. if (port == 0)
12. port = 2098;//this port will be random if we did not set it.
13. return new ServerSocket(port);
14. }
15.}
And spring will have easy configuration for this:
<bean id="rmiSearchService" class="org.springframework.remoting.rmi.RmiServiceExporter">
2.<property name="serviceName" value="search"/><!-- service name -->
3.<property name="service" ref="searchService"/>
4.<property name="serviceInterface" value="velcro.searchengine.ISearcher"/>
5.<property name="registryPort" value="2098"/><!-- port -->
6.<property name="servicePort" value="2098"/>><!-- port-->
7.</bean>
But I do not think RMI is a good choice for us, there is easy ways for hessian, phprpc, soap(xfire/cxf/axis2) and so on.
So I just read document about RMI after google it. Just get to know about this knowledge point.
references:
http://gemantic.iteye.com/blog/1199214
http://www.cnblogs.com/birdshover/archive/2010/03/16/1687301.html
http://www.javabloger.com/article/thrift-java-code-example.html
http://www.javabloger.com/article/apache-thrift-architecture.html
http://yangfanchao.iteye.com/blog/1271737
http://thrift.apache.org/docs/idl/
http://thrift.apache.org/docs/types/
http://wangse.iteye.com/blog/191797
Thrift(2)RMI Strategy Sample
猜你喜欢
转载自sillycat.iteye.com/blog/1584446
今日推荐
周排行