这一章写客户端和服务器以及读取配置类文件的内容。
1.服务器端:Server
/**
* 服务器类。开启服务。
* @author king_fu
*
*/
public class Server {
private static final int SERVER_PORT = 8080;
// 是否是自定义的消息。实现接口MessageDecoder。
private static final int USE_CUSTOM_CODEC = 2;
public static void main(String[] args) throws Throwable {
NioSocketAcceptor acceptor = new NioSocketAcceptor();
/*acceptor.getFilterChain().addLast("logger", new LoggingFilter()); //日志过滤链
*/
acceptor.getFilterChain()
.addLast(
"codec",
new ProtocolCodecFilter(new ByteProtocalCodecFactory(true)));
acceptor.setHandler(new FileObjectServerHandler()); // 客户端传过来的Message服务器处理。
acceptor.bind(new InetSocketAddress(SERVER_PORT)); // 绑定监听的端口。
System.out.println("监听端口 " + SERVER_PORT);
}
}
2. 客户端:Client
/**
* 客户端
*
* @author king_fu
*
*/
public class Client {
private static final String HOSTNAME = "localhost";
private static final int PORT = 8080;
private static final long CONNECT_TIMEOUT = 30 * 1000L; // 30秒
private static final int USE_CUSTOM_CODEC = 2; // 是否是自定义的编码
public static void main(String[] args) throws Throwable {
NioSocketConnector connector = new NioSocketConnector();
connector.setConnectTimeoutMillis(CONNECT_TIMEOUT);
/*
* connector.getFilterChain().addLast("logger", new LoggingFilter());
* connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter(
* new ObjectSerializationCodecFactory()));
*/
connector.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new ByteProtocalCodecFactory(false)));
connector.setHandler(new FileObjectClientHandler());
IoSession session;
for (;;) {
try {
ConnectFuture future = connector.connect(new InetSocketAddress(
HOSTNAME, PORT));
future.awaitUninterruptibly();
session = future.getSession();
break;
} catch (RuntimeIoException e) {
e.printStackTrace();
Thread.sleep(5000);
}
}
session.getCloseFuture().awaitUninterruptibly();
connector.dispose();
}
}
3.读取配置文件:ReadProperties
public class ReadProperties {
// xml文件名。
private static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
public static void main(String[] args) {
// 读取bean。
PropertiesModel d = (PropertiesModel) context.getBean("propertiesModel");
}
public static PropertiesModel getModel(){
PropertiesModel d =(PropertiesModel) context.getBean("propertiesModel");
return d;
}
/**
* 这个方法暂时未用到。不过这里实现的是通过类加载器进行方法的调用。
* @param fieldName
* @return
*/
public static String getPropertyByFieldName(String fieldName){
if(fieldName != null && !fieldName.equals("")){
char[] chars= fieldName.toCharArray();
chars[0] = Character.toUpperCase(chars[0]); // 把第一个转为大写
fieldName = String.valueOf(chars);
}else{
throw new MyRuntimeException("请输入有效的字段名");
}
Class classObject = PropertiesModel.class;
Object invokeTester;
Object result = "";
try {
invokeTester = classObject.getConstructor().newInstance();
Method getMethod = classObject.getMethod("get" + fieldName);
result = getMethod.invoke(invokeTester);
System.out.println((String) result);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return (String) result;
}
}
最新代码已经更新在github中,欢迎fork。
项目名:MinaFile