-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathRmiClient.java
More file actions
60 lines (49 loc) · 1.96 KB
/
RmiClient.java
File metadata and controls
60 lines (49 loc) · 1.96 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
54
55
56
57
58
59
60
package cn.aofeng.demo.java.rmi;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class RmiClient {
private static Logger _logger = LoggerFactory.getLogger(RmiClient.class);
private static String assembleUrl(String hostUrl, String bindName) {
if (StringUtils.isBlank(hostUrl) || StringUtils.isBlank(bindName)) {
return null;
}
String host = hostUrl.endsWith("/") ? hostUrl.substring(0, hostUrl.length()-1) : hostUrl;
String name = bindName.startsWith("/") ? bindName.substring(1) : bindName;
return host + "/" + name;
}
/**
* @param args
* <ul>
* <li>[0]:待连接的RMI主机。如:rmi://192.168.56.102:9999</li>
* <li>[1]:服务名称。如:UserService</li>
* </ul>
*/
public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException {
String host = args[0];
String serviceName = args[1];
String url = assembleUrl(host, serviceName);
UserService userService = (UserService) Naming.lookup(url);
String userId = "10000";
User user = userService.findById(userId);
_logger.info("身份证号为{}的用户信息{}", userId, user);
userId = "10001";
user = userService.findById(userId);
_logger.info("身份证号为{}的用户信息{}", userId, user);
String userName = "小明";
user = userService.findByName(userName);
_logger.info("姓名为{}的用户信息{}", userName, user);
userName = "张三";
user = userService.findByName(userName);
_logger.info("姓名为{}的用户信息{}", userName, user);
}
}