Hadoop中的远程过程调用

IPC接口

/**
 * Superclass of all protocols that use Hadoop RPC.
 * Subclasses of this interface are also supposed to have
 * a static final long versionID field.
 */
public interface VersionedProtocol 
{
  /**
   * Return protocol version corresponding to protocol interface.
   * @param 协议接口的类名//protocol The classname of the protocol interface
   * @param 客户端的版本号//clientVersion The version of the protocol that the client speaks
   * @return 协议接口的版本
   */
  public long getProtocolVersion(String protocol, 
                                 long clientVersion) throws IOException;
}
public interface IPCQueryStatus extends VersionedProtocol 
{
	IPCFileStatus getFileStatus(String filename);
}

IPC接口的实现类

public class IPCQueryStatusImpl implements IPCQueryStatus 
{	
	public IPCQueryStatusImpl() 
	{
	
	}

	@Override
	public IPCFileStatus getFileStatus(String filename) 
	{
		IPCFileStatus status=new IPCFileStatus(filename);
		System.out.println("Method getFileStatus Called, return: "+status);
		return status;
	}
	/**
	 * 用于服务器与客户端,进行IPC接口版本检查,再服务器返回给客户端时调用,如果服务器端的IPC版本与客户端不一致
	 * 那么就会抛出版本不一致的异常
	 */
	@Override
	public long getProtocolVersion(String protocol, long clientVersion) throws IOException 
	{
		System.out.println("protocol: "+protocol);
		System.out.println("clientVersion: "+clientVersion);
		return IPCQueryServer.IPC_VER;
	}
}

服务端程序

public class IPCQueryServer 
{
	public static final int IPC_PORT = 32121;
	public static final long IPC_VER = 5473L;
	
	public static void main(String[] args) {
		try {
			ConsoleAppender append=new ConsoleAppender(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN));
			append.setThreshold(Level.DEBUG);
			BasicConfigurator.configure();
			Configuration conf = new Configuration();
			
	        IPCQueryStatusImpl queryService=new IPCQueryStatusImpl();
	        
	        System.out.println(conf);
	        
	        /**
	        * 在对象queryService上获取一个IPC服务器
	        **/
	        Server server = RPC.getServer(queryService,  //接口实现对象
	        		                      "0.0.0.0", //监听地址
	        		                      IPC_PORT,  //端口
	        		                      1, true,
	        		                      conf ); //配置类的实例
	       
	       //启动服务,
			server.start();
			
			System.out.println("Server ready, press any key to stop");
			System.in.read();
			
			//停止服务
			server.stop();
			System.out.println("Server stopped");
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值