RMI大杂烩

栏目: Java · 发布时间: 7年前

package io.github.baijifeilong.rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface ISayHelloService extends Remote {

    String hello(String name) throws RemoteException;
}

服务端

package io.github.baijifeilong.rmi;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;

public class RmiServer {
    public static void main(String[] args) throws RemoteException {
        ISayHelloService sayHelloService = new SayHelloService();
        UnicastRemoteObject.exportObject(sayHelloService, 0);
        LocateRegistry.createRegistry(1099).rebind("SayHello", sayHelloService);
    }


    static class SayHelloService implements ISayHelloService {
        @Override
        public String hello(String name) {
            return "hello, " + name;
        }
    }
}

客户端

package io.github.baijifeilong.rmi;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class RmiClient {
    public static void main(String[] args) throws RemoteException, NotBoundException {
        // RMI默认端口1099,不需要显式声明
        Registry registry = LocateRegistry.getRegistry();
        ISayHelloService sayHelloService = (ISayHelloService) registry.lookup("SayHello");
        System.out.println(sayHelloService.hello("word"));
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Shallows

The Shallows

Nicholas Carr / W. W. Norton & Company / 2010-6-15 / USD 26.95

"Is Google making us stupid?" When Nicholas Carr posed that question, in a celebrated Atlantic Monthly cover story, he tapped into a well of anxiety about how the Internet is changing us. He also crys......一起来看看 《The Shallows》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具