一个最典型的多线程的应用,主线程EventReceiver起了一个针对某个端口的Server Socket,然后轮训接受客户端socket的连接请求并接收和处理数据,由于接收和处理数据是相对复杂和耗时的一个事情,所以最好的办法就是主线程在socket连接建立完成之后就启动一个专门负责接收和处理的数据的线程,这样就可以做到在连接数量比较大的情况下,以多线程的方式并行快速处理数据。下面是主线程EventReceiver和处理数据的线程ClientEventReceiver。
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import org.apache.log4j.Logger;
public class EventReceiver extends Thread {
private int port = 3809;
private final static Logger logger = Logger.getLogger(EventReceiver.class);
public EventReceiver() {
}
public EventReceiver(int port) {
this.port = port;
}
public void run() {
logger.info("Main event receiver is starting at port: " + port);
try {
ServerSocket socket = new ServerSocket(port);
while(true) {
try {
Socket clientSocket = socket.accept();
logger.info("receive a client from " + clientSocket.getRemoteSocketAddress());
new ClientEventReceiver(clientSocket).start();
} catch (IOException e) {
logger.error("", e);
}
}
} catch (IOException e) {
logger.error("" ,e);
}
}
}
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import org.apache.log4j.Logger;
import com.zznode.inms.alarm.process.Setting;
import com.zznode.inms.unification.collector.util.IpScope;
public class ClientEventReceiver extends Thread {
private Socket socket;
private String remoteIp = null;
private static final Logger logger = Logger
.getLogger(ClientEventReceiver.class);
public ClientEventReceiver(Socket socket) {
super();
this.socket = socket;
this.remoteIp = IpScope.getIpStr(socket.getInetAddress().getAddress());
}
public void run() {
logger.info("Session to " + remoteIp + " is starting...");
try {
InputStream in = socket.getInputStream();
BufferedInputStream bis = new BufferedInputStream(in);
byte[] aAlarmByte = new byte[Setting.ALARM_READ_ONCE];
byte aByte = (byte) bis.read();
int i = 0;
StringBuffer alarmSb = new StringBuffer();
while (aByte != -1) {
if (aByte != 0) {
aAlarmByte[i++] = aByte;
if (i >= Setting.ALARM_READ_ONCE) {
// logger.error("Error: the received alarm's size is bigger than the lengh defined: " + Setting.ALARM_MAX_LENGH);
alarmSb.append(new String(aAlarmByte, 0, i, "UTF-8"));
i = 0;
}
} else {
alarmSb.append(new String(aAlarmByte, 0, i, "UTF-8"));
i = 0;
String aAlarmStr = alarmSb.toString();
alarmSb = new StringBuffer();
AlarmDecoder.getInstance().addAlarm(aAlarmStr, remoteIp);
}
aByte = (byte) bis.read();
}
} catch (UnsupportedEncodingException e) {
logger.error("", e);
} catch (IOException e) {
logger.error("", e);
} finally {
logger.info("Session to " + remoteIp + "stoped. ");
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import org.apache.log4j.Logger;
public class EventReceiver extends Thread {
private int port = 3809;
private final static Logger logger = Logger.getLogger(EventReceiver.class);
public EventReceiver() {
}
public EventReceiver(int port) {
this.port = port;
}
public void run() {
logger.info("Main event receiver is starting at port: " + port);
try {
ServerSocket socket = new ServerSocket(port);
while(true) {
try {
Socket clientSocket = socket.accept();
logger.info("receive a client from " + clientSocket.getRemoteSocketAddress());
new ClientEventReceiver(clientSocket).start();
} catch (IOException e) {
logger.error("", e);
}
}
} catch (IOException e) {
logger.error("" ,e);
}
}
}
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import org.apache.log4j.Logger;
import com.zznode.inms.alarm.process.Setting;
import com.zznode.inms.unification.collector.util.IpScope;
public class ClientEventReceiver extends Thread {
private Socket socket;
private String remoteIp = null;
private static final Logger logger = Logger
.getLogger(ClientEventReceiver.class);
public ClientEventReceiver(Socket socket) {
super();
this.socket = socket;
this.remoteIp = IpScope.getIpStr(socket.getInetAddress().getAddress());
}
public void run() {
logger.info("Session to " + remoteIp + " is starting...");
try {
InputStream in = socket.getInputStream();
BufferedInputStream bis = new BufferedInputStream(in);
byte[] aAlarmByte = new byte[Setting.ALARM_READ_ONCE];
byte aByte = (byte) bis.read();
int i = 0;
StringBuffer alarmSb = new StringBuffer();
while (aByte != -1) {
if (aByte != 0) {
aAlarmByte[i++] = aByte;
if (i >= Setting.ALARM_READ_ONCE) {
// logger.error("Error: the received alarm's size is bigger than the lengh defined: " + Setting.ALARM_MAX_LENGH);
alarmSb.append(new String(aAlarmByte, 0, i, "UTF-8"));
i = 0;
}
} else {
alarmSb.append(new String(aAlarmByte, 0, i, "UTF-8"));
i = 0;
String aAlarmStr = alarmSb.toString();
alarmSb = new StringBuffer();
AlarmDecoder.getInstance().addAlarm(aAlarmStr, remoteIp);
}
aByte = (byte) bis.read();
}
} catch (UnsupportedEncodingException e) {
logger.error("", e);
} catch (IOException e) {
logger.error("", e);
} finally {
logger.info("Session to " + remoteIp + "stoped. ");
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}