【MinaFile】【七】【2.0】客户端以及服务器

这一章写客户端和服务器以及读取配置类文件的内容。

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值