📄 netserverimpl.java
字号:
package com.briup.inf.impl;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collection;
import java.util.Date;
import java.util.Properties;
import com.briup.exception.BackupException;
import com.briup.exception.DBStoreException;
import com.briup.exception.NetServerException;
import com.briup.inf.Backup;
import com.briup.inf.Config;
import com.briup.inf.DBStore;
import com.briup.inf.Log;
import com.briup.inf.NetServer;
import com.briup.pojo.BIDR;
/**
* class NetServerImpl
*
* @author Jimmy Zhou
* @Date 2008-2-4 下午11:35:56
*/
public class NetServerImpl implements NetServer {
private int port;
// 最大允许连接数
private int backlog;
private Config config;
public NetServerImpl(Properties pro) {
port = Integer.parseInt(pro.getProperty("port"));
backlog = Integer.parseInt(pro.getProperty("backlog"));
}
public void setConfig(Config config) throws NetServerException {
this.config = config;
}
public void start() throws NetServerException {
ServerSocket ss = null;
Socket s = null;
Log log = null;
try {
log = config.getLog();
ss = new ServerSocket(port, backlog);
// 日志记录监听起动
log.writeInfo("Gather server is listening on port: " + port);
while (true) {
s = ss.accept();
// 通过构造器调用线程类
new NetServerThread(s, config);
log.writeInfo("Gather server is listening on thread");
}
} catch (Exception e) {
e.printStackTrace();
throw new NetServerException(e);
}
}
/*
* 1、获取config的实例 2、获取NetServer,log的实例 3、调用netServer的setConfig方法 4、调用start方法
*/
public static void main(String[] agrs) {
Properties pro = new Properties();
pro.setProperty("config_file_path", "src/com/briup/config.xml");
Config config = ConfigImpl.newInstance(pro);
NetServer ns = null;
try {
ns = config.getNetServer();
System.out.println(ns);
ns.setConfig(config);
ns.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class NetServerThread extends Thread {
private Socket s;
private Config config;
public NetServerThread(Socket s, Config config) {
this.config = config;
this.s = s;
start();
}
@SuppressWarnings("unchecked")
@Override
public void run() {
ObjectInputStream ois = null;
InputStream is = null;
Collection<BIDR> c = null;
Collection<BIDR> backdatas = null;
DBStore ds = null;
Backup bu = null;
Log log = null;
try {
ds = config.getDBStore();
bu = config.getBackup();
log = config.getLog();
is = s.getInputStream();
ois = new ObjectInputStream(is);
c = (Collection<BIDR>) ois.readObject();
log.writeInfo(new Date(System.currentTimeMillis())
+ "Gather server receive :" + c.size() + "records!!");
// 获取备份的数据
backdatas = bu.load();
bu.clear();
if (backdatas != null) {
log.writeInfo(new Date(System.currentTimeMillis())
+ "there are " + backdatas.size()
+ " recorde to load!!");
// 连同备份数据一起加入到集合c中
c.addAll(backdatas);
}
} catch (Exception e) {
log.writeError(e.getMessage());
e.printStackTrace();
} finally {
try {
if (ois != null) {
ois.close();
}
if (s != null) {
s.close();
}
} catch (Exception e) {
log.writeError(e.getMessage());
e.printStackTrace();
}
}
try {
ds.insert(c);
log.writeInfo(new Date(System.currentTimeMillis()) + "there are "
+ c.size() + "insert into the db");
} catch (DBStoreException e) {
try {
bu.store(c);
log.writeInfo(new Date(System.currentTimeMillis())
+ "there are " + c.size() + " recorde to store!");
} catch (BackupException e1) {
log.writeError(e1.getMessage());
e1.printStackTrace();
}
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -