Java RMI 使用例子
2018-06-18 03:30:11来源:未知 阅读 ()
1.创建协议接口(UserService)
/** * */ package com.junge.demo.rmi.protocol.service; import java.io.Serializable; import java.rmi.Remote; import java.rmi.RemoteException; import com.junge.demo.rmi.protocol.model.User; /** * @author Administrator * */ public interface UserService extends Remote, Serializable { Integer addUser(User user) throws RemoteException; void delUser(Integer userid) throws RemoteException; void modifyUser(User user) throws RemoteException; User getUserByUserId(Integer userid) throws RemoteException; }
User.java
/** * */ package com.junge.demo.rmi.protocol.model; import java.io.Serializable; /** * @author Administrator * */ public class User implements Serializable { /** * */ private static final long serialVersionUID = -8967262939434460765L; private Integer userid; private String username; public Integer getUserid() { return userid; } public void setUserid(Integer userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Override public String toString() { return "User [userid=" + userid + ", username=" + username + "]"; } }
2、创建服务实现类UserServiceProvider.java
package com.junge.demo.rmi.server.service.provider; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import com.junge.demo.rmi.protocol.model.User; import com.junge.demo.rmi.protocol.service.UserService; public class UserServiceProvider extends UnicastRemoteObject implements UserService { /** * */ private static final long serialVersionUID = 1L; public UserServiceProvider() throws RemoteException { super(); } public Integer addUser(User user) throws RemoteException { System.out.println("addUser:" + user); return user.getUserid(); } public void delUser(Integer userid) throws RemoteException { System.out.println("delUser,userid=" + userid); } public void modifyUser(User user) throws RemoteException { System.out.println("modifyUser:" + user); } public User getUserByUserId(Integer userid) throws RemoteException { System.out.println("getUserByUserId,userid={}" + userid); User user = new User(); user.setUserid(userid); user.setUsername("寮犱笁"); return user; } }
3.启动注册表,注册服务(ServiceStartServlet.java)
/** * */ package com.junge.demo.rmi.server.service.start; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import com.alibaba.fastjson.JSONObject; import com.junge.demo.rmi.server.service.provider.UserServiceProvider; /** * @author Administrator * */ public class ServiceStartServlet extends HttpServlet { private static final long serialVersionUID = 1L; private Registry registry; private static final String BIND_NAME = "com.junge.demo.rmi.protocol.service.UserService"; private Registry getRegistry() throws RemoteException { return LocateRegistry.createRegistry(1099); } @Override public void init() throws ServletException { try { registry = getRegistry(); System.out.println(JSONObject.toJSON(registry.list())); registry.rebind(BIND_NAME, new UserServiceProvider()); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void destroy() { if (null != registry) { try { registry.unbind(BIND_NAME); } catch (Exception e) { e.printStackTrace(); } } } }
4.创建客户端调用rmi(RmiClient.java)
/** * */ package com.junge.demo.rmi; import java.rmi.Naming; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import com.junge.demo.rmi.protocol.model.User; import com.junge.demo.rmi.protocol.service.UserService; /** * @author Administrator * */ public class RmiClient { /** * @param args */ public static void main(String[] args) { addUserBatch(); } public static void addUserBatch() { Long btime = System.currentTimeMillis(); ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 1000, 200L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(50000)); try { UserService userService = (UserService)Naming.lookup("com.junge.demo.rmi.protocol.service.UserService"); for (int i = 0; i < 50000; i++) { executor.submit(new OperUser(userService)); } } catch (Exception e) { e.printStackTrace(); } finally { executor.shutdown(); } Long etime = System.currentTimeMillis(); System.out.println(etime - btime); } } class OperUser implements Runnable { private UserService userService; public OperUser(UserService userService) { this.userService = userService; } @Override public void run() { try { // 增加、详情、修改、删除 User user = new User(); user.setUserid(1); user.setUsername("名字"); userService.addUser(user); User user2 = userService.getUserByUserId(1); userService.modifyUser(user2); userService.delUser(user2.getUserid()); } catch (Exception e) { e.printStackTrace(); } } }
5.注意实现
a.协议接口需要继承Remote接口,对外提供的接口需要抛出RemoteException异常
b.协议接口中使用的对象需要实现序列化接口Serializable
c.服务端实现类需要继承UnicastRemoteObject类
6.问题
1.使用maven工程创建的服务端,直接使用java application运行服务端,找不到协议接口
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash