Thrift(2)RMI Strategy Sample

本文详细介绍了Java RMI技术的使用方法,包括接口实现、服务注册与客户端调用过程,以及如何通过自定义实现跨防火墙通信。此外,还讨论了RMI与其他现代远程调用技术(如Hessian、PHP-RPC、SOAP等)的对比,并提供了Spring框架中对RMI的支持配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值