ITeye博客

Java RMI用法小例子

sunney2012 · 2012-02-15 19:34 · J2SE · 编程语言 · Markdown

部署


public class StartService {
	private static final String IP = "127.0.0.1";
	private static final int PORT = 9999;
	private static final String REMOTE_NAME = "userDao";
	private static final String REMOTE_URL = "rmi://" + IP + ":" + PORT + "/" + REMOTE_NAME;

	public static void main(String[] args) {
		try {
			UserDao userDao = new UserDaoImpl(); // 实例化对象
			LocateRegistry.createRegistry(PORT); // 注册端口
			Naming.bind(REMOTE_URL, userDao); // 绑定远程服务对象
			System.out.println("远程" + REMOTE_NAME + "启动成功....");
		} catch (RemoteException e) {
			System.err.println("远程对象出错");
			e.printStackTrace();
		} catch (MalformedURLException e) {
			System.err.println("URL出错了");
			e.printStackTrace();
		} catch (AlreadyBoundException e) {
			System.err.println("绑定的对象已经存在了");
			e.printStackTrace();
		}
	}
}

/**
 * 远程接口 必须继承与Remote对象
 */
interface UserDao extends Remote {
	/**
	 * 简单的测试方法
	 * 
	 * @param name
	 */
	public void sayName(String name) throws RemoteException;
}

/**
 * 
 * 接口的实现类 必须继承UnicastRemoteObject(单一远程对象) 实现UserDao自己的接口
 */
class UserDaoImpl extends UnicastRemoteObject implements UserDao {

	public UserDaoImpl() throws RemoteException {
	}

	@Override
	public void sayName(String name) {
		if (name != null && !name.equals("")) {
			System.out.println("我的名字是:" + name);
		} else {
			System.err.println("名字不为空....");
		}
	}

}

测试调用


public class TestRemote {
	public static void main(String[] args) {
		try {
			// 在rmi服务中查询userdao的对象
			UserDao userDao = (UserDao) Naming.lookup("rmi://127.0.0.1:9999/userDao");
			// 调用远程服务的方法
			userDao.sayName("spring sky");
		} catch (MalformedURLException e) {
			System.err.println("URL出错");
			e.printStackTrace();
		} catch (RemoteException e) {
			System.err.println("远程对象出错");
			e.printStackTrace();
		} catch (NotBoundException e) {
			System.err.println("没有找到绑定的对象");
			e.printStackTrace();
		}
	}
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。