1.创建协议接口(UserService)
/****/
packagecom.junge.demo.rmi.protocol.service;importjava.io.Serializable;importjava.rmi.Remote;importjava.rmi.RemoteException;importcom.junge.demo.rmi.protocol.model.User;/***@authorAdministrator
**/
public interface UserService extendsRemote, Serializable {
Integer addUser(User user)throwsRemoteException;void delUser(Integer userid) throwsRemoteException;void modifyUser(User user) throwsRemoteException;
User getUserByUserId(Integer userid)throwsRemoteException;
}
User.java
/****/
packagecom.junge.demo.rmi.protocol.model;importjava.io.Serializable;/***@authorAdministrator
**/
public class User implementsSerializable {/****/
private static final long serialVersionUID = -8967262939434460765L;privateInteger userid;privateString username;publicInteger getUserid() {returnuserid;
}public voidsetUserid(Integer userid) {this.userid =userid;
}publicString getUsername() {returnusername;
}public voidsetUsername(String username) {this.username =username;
}
@OverridepublicString toString() {return "User [userid=" + userid + ", username=" + username + "]";
}
}
2、创建服务实现类UserServiceProvider.java
packagecom.junge.demo.rmi.server.service.provider;importjava.rmi.RemoteException;importjava.rmi.server.UnicastRemoteObject;importcom.junge.demo.rmi.protocol.model.User;importcom.junge.demo.rmi.protocol.service.UserService;public class UserServiceProvider extends UnicastRemoteObject implementsUserService {/****/
private static final long serialVersionUID = 1L;public UserServiceProvider() throwsRemoteException {super();
}public Integer addUser(User user) throwsRemoteException {
System.out.println("addUser:" +user);returnuser.getUserid();
}public void delUser(Integer userid) throwsRemoteException {
System.out.println("delUser,userid=" +userid);
}public void modifyUser(User user) throwsRemoteException {
System.out.println("modifyUser:" +user);
}public User getUserByUserId(Integer userid) throwsRemoteException {
System.out.println("getUserByUserId,userid={}" +userid);
User user= newUser();
user.setUserid(userid);
user.setUsername("寮犱笁");returnuser;
}
}
3.启动注册表,注册服务(ServiceStartServlet.java)
/****/
packagecom.junge.demo.rmi.server.service.start;importjava.rmi.RemoteException;importjava.rmi.registry.LocateRegistry;importjava.rmi.registry.Registry;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importcom.alibaba.fastjson.JSONObject;importcom.junge.demo.rmi.server.service.provider.UserServiceProvider;/***@authorAdministrator
**/
public class ServiceStartServlet extendsHttpServlet {private static final long serialVersionUID = 1L;privateRegistry registry;private static final String BIND_NAME = "com.junge.demo.rmi.protocol.service.UserService";private Registry getRegistry() throwsRemoteException {return LocateRegistry.createRegistry(1099);
}
@Overridepublic void init() throwsServletException {try{
registry=getRegistry();
System.out.println(JSONObject.toJSON(registry.list()));
registry.rebind(BIND_NAME,newUserServiceProvider());
}catch(RemoteException e) {
e.printStackTrace();
}
}
@Overridepublic voiddestroy() {if (null !=registry) {try{
registry.unbind(BIND_NAME);
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
4.创建客户端调用rmi(RmiClient.java)
/****/
packagecom.junge.demo.rmi;importjava.rmi.Naming;importjava.util.concurrent.ArrayBlockingQueue;importjava.util.concurrent.ThreadPoolExecutor;importjava.util.concurrent.TimeUnit;importcom.junge.demo.rmi.protocol.model.User;importcom.junge.demo.rmi.protocol.service.UserService;/***@authorAdministrator
**/
public classRmiClient {/***@paramargs*/
public static voidmain(String[] args) {
addUserBatch();
}public static voidaddUserBatch() {
Long btime=System.currentTimeMillis();
ThreadPoolExecutor executor= new ThreadPoolExecutor(10, 1000, 200L, TimeUnit.SECONDS, new ArrayBlockingQueue(50000));try{
UserService userService= (UserService)Naming.lookup("com.junge.demo.rmi.protocol.service.UserService");for (int i = 0; i < 50000; i++) {
executor.submit(newOperUser(userService));
}
}catch(Exception e) {
e.printStackTrace();
}finally{
executor.shutdown();
}
Long etime=System.currentTimeMillis();
System.out.println(etime-btime);
}
}class OperUser implementsRunnable {privateUserService userService;publicOperUser(UserService userService) {this.userService =userService;
}
@Overridepublic voidrun() {try{//增加、详情、修改、删除
User user = newUser();
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运行服务端,找不到协议接口