-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathRmiServer.java
More file actions
53 lines (43 loc) · 1.42 KB
/
RmiServer.java
File metadata and controls
53 lines (43 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package cn.aofeng.demo.java.rmi;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* RMI服务端。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class RmiServer {
private static Logger _logger = LoggerFactory.getLogger(RmiServer.class);
public static Registry createRegistry(int port) {
Registry registry = null;
try {
registry = LocateRegistry.createRegistry(port);
} catch (RemoteException e) {
_logger.info( String.format("注册端口%s失败", port), e);
}
return registry;
}
/**
* @param args [0]:绑定端口
* @throws RemoteException
*/
public static void main(String[] args) throws RemoteException {
int port = Integer.parseInt(args[0]);
UserService userService = new UserServiceImpl();
Registry registry = createRegistry(port);
if (null == registry) {
System.exit(0);
}
String bindName = "UserService";
try {
registry.bind(bindName, userService);
} catch (AlreadyBoundException e) {
_logger.info("服务{}已经绑定过", bindName);
}
_logger.info("RMI Server started, listen port:{}", port);
}
}