mirror of
https://github.com/Estom/notes.git
synced 2026-02-07 04:23:55 +08:00
Java内容重新整理删除过期的东西
This commit is contained in:
20
Java/JavaDemo/codedemo/java/rmi/Gender.java
Normal file
20
Java/JavaDemo/codedemo/java/rmi/Gender.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package cn.aofeng.demo.java.rmi;
|
||||
|
||||
/**
|
||||
* 性别。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class Gender {
|
||||
|
||||
/**
|
||||
* 男。
|
||||
*/
|
||||
public final static char MALE = 'M';
|
||||
|
||||
/**
|
||||
* 女。
|
||||
*/
|
||||
public final static char FEMALE = 'F';
|
||||
|
||||
}
|
||||
60
Java/JavaDemo/codedemo/java/rmi/RmiClient.java
Normal file
60
Java/JavaDemo/codedemo/java/rmi/RmiClient.java
Normal file
@@ -0,0 +1,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);
|
||||
}
|
||||
|
||||
}
|
||||
53
Java/JavaDemo/codedemo/java/rmi/RmiServer.java
Normal file
53
Java/JavaDemo/codedemo/java/rmi/RmiServer.java
Normal file
@@ -0,0 +1,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);
|
||||
}
|
||||
|
||||
}
|
||||
170
Java/JavaDemo/codedemo/java/rmi/User.java
Normal file
170
Java/JavaDemo/codedemo/java/rmi/User.java
Normal file
@@ -0,0 +1,170 @@
|
||||
package cn.aofeng.demo.java.rmi;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 用户信息。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class User implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 7616705579045104892L;
|
||||
|
||||
/**
|
||||
* 身份证号。
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 姓名。
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 性别:M-男;F-女。
|
||||
*/
|
||||
private char gender;
|
||||
|
||||
/**
|
||||
* 出生日期。以毫秒存储。
|
||||
*/
|
||||
private long birthday;
|
||||
|
||||
/**
|
||||
* 国家。
|
||||
*/
|
||||
private String country;
|
||||
|
||||
/**
|
||||
* 省/州。
|
||||
*/
|
||||
private String province;
|
||||
|
||||
/**
|
||||
* 市/区。
|
||||
*/
|
||||
private String city;
|
||||
|
||||
/**
|
||||
* 街道详细地址。
|
||||
*/
|
||||
private String address;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public char getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(char gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
public long getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public void setBirthday(long birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getProvince() {
|
||||
return province;
|
||||
}
|
||||
|
||||
public void setProvince(String province) {
|
||||
this.province = province;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((country == null) ? 0 : country.hashCode());
|
||||
result = prime * result + ((id == null) ? 0 : id.hashCode());
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
User other = (User) obj;
|
||||
if (country == null) {
|
||||
if (other.country != null)
|
||||
return false;
|
||||
} else if (!country.equals(other.country))
|
||||
return false;
|
||||
if (id == null) {
|
||||
if (other.id != null)
|
||||
return false;
|
||||
} else if (!id.equals(other.id))
|
||||
return false;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder buffer = new StringBuilder(256)
|
||||
.append("User [id=").append(id)
|
||||
.append(", name=").append(name)
|
||||
.append(", gender=").append(gender)
|
||||
.append(", birthday=").append(birthday)
|
||||
.append(", country=").append(country)
|
||||
.append(", province=").append(province)
|
||||
.append(", city=").append(city)
|
||||
.append(", address=").append(address)
|
||||
.append("]");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
}
|
||||
19
Java/JavaDemo/codedemo/java/rmi/UserService.java
Normal file
19
Java/JavaDemo/codedemo/java/rmi/UserService.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package cn.aofeng.demo.java.rmi;
|
||||
|
||||
import java.rmi.Remote;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
/**
|
||||
* 用户信息服务。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public interface UserService extends Remote {
|
||||
|
||||
public User findByName(String name) throws RemoteException;
|
||||
|
||||
public User findById(String id) throws RemoteException;
|
||||
|
||||
public boolean add(User user) throws RemoteException;
|
||||
|
||||
}
|
||||
70
Java/JavaDemo/codedemo/java/rmi/UserServiceImpl.java
Normal file
70
Java/JavaDemo/codedemo/java/rmi/UserServiceImpl.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package cn.aofeng.demo.java.rmi;
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
/**
|
||||
* 用户信息服务。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class UserServiceImpl extends UnicastRemoteObject implements UserService {
|
||||
|
||||
public UserServiceImpl() throws RemoteException {
|
||||
super();
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = -9134952963637302483L;
|
||||
|
||||
@Override
|
||||
public User findByName(String name) throws RemoteException {
|
||||
if (StringUtils.isBlank(name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ("小明".equals(name)) {
|
||||
return createUser("10000", "小明", Gender.MALE);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User findById(String id) throws RemoteException {
|
||||
if (StringUtils.isBlank(id)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ("10000".equals(id)) {
|
||||
return createUser("10000", "小丽", Gender.FEMALE);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(User user) throws RemoteException {
|
||||
if (null == user || StringUtils.isBlank(user.getId()) || StringUtils.isBlank(user.getName())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private User createUser(String id, String name, char gender) {
|
||||
User user = new User();
|
||||
user.setId(id);
|
||||
user.setName(name);
|
||||
user.setGender(gender);
|
||||
user.setBirthday(System.currentTimeMillis());
|
||||
user.setCountry("中国");
|
||||
user.setProvince("广东");
|
||||
user.setCity("广州");
|
||||
user.setAddress("xxx区xxx街道xxx号");
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user