У меня есть ServerSocket, который не принимает новые запросы от клиента, пока идет процесс. Я уже довольно давно отлаживаю это и не знаю, где моя ошибка. Насколько мне известно, я выполнил всю инициализацию для создания новых потоков.
Ниже мой код:
public class CService
{
boolean decision = false;
public void otherConnect() throws Exception {
synchronized (_SYNC_){
ServerSocket server = new ServerSocket(14344, 1000);
IsoServerBean isoServerBean = new IsoServerBean();
log.info("Waiting for connections...");
byte[] lenbuf = new byte[4];
while(true){
Socket clientSocket = null;
clientSocket = server.accept();
int otherLen = isoServerBean.getOtherHeaderLength();
int size = 352;
byte[] buf = new byte[size];
clientSocket.getInputStream().read(buf);
HexDump.hexDump(buf,1,size);
byte[] targetBuf = new byte[size-2];
System.arraycopy(buf, 4, targetBuf, 0, size-4);
setMyConnectionClient(clientSocket);
new Thread(new CProcessorForClient(targetBuf, clientSocket, 4), "resp").start();
}
}
}
private class CProcessorForClient extends Thread{
private volatile boolean stopFlag;
private volatile boolean stopped;
private byte[] msg;
private Socket clientSocket;
private Socket socket;
private int millis;
private int ctr =0;
CProcessorForClient(byte[] buf, Socket s, int waitMillis) {
stopFlag = false;
stopped = false;
msg = buf;
socket = s;
millis = waitMillis;
}
public void killMe()
{
stopFlag = true;
synchronized (_SYNC_)
{
this.interrupt();
}
}
public boolean killed()
{
return stopped;
}
public void run() {
try {
Thread.sleep(millis);
log.debug("Parsing incoming: [" + new String(msg) + "]");
IsoMessage request = null;
//OMITTED THE PROCESS OF THE CREATION OF ISO TO BE THROWN TO ANOTHER SERVER
msgFactory.print(request);
try {
this.socket = getMyConnection();//Connection to another server
this.clientSocket = getMyConnectionClient();//Connection from client
request.write(socket.getOutputStream(), 0, 0);
int i = 0;
Integer sleepTime = 500;
Integer waitTime = 5000;
decision = true;
while(decision){
i = i + 1;
Thread.sleep(sleepTime);
if (i * sleepTime >= waitTime) {
this.clientSocket.close();
decision = false;
}
}
} catch (IOException ex) {
log.debug("IO Exception encountered: " + ex.getMessage());
} catch (Exception ex) {
java.util.logging.Logger.getLogger(CService.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
} catch (ParseException ex) {
log.error("Error in parsing incoming message:", ex);
log.debug("ex.getMessage: " + ex.getMessage());
} catch (InterruptedException ex) {
log.error("Interrupted!", ex);
} finally{
if (log != null) log.debug("Thread has finished processing txn.");
}
}
}
public static void main(String[] args) throws IOException
{
System.out.println("RANDOM PRINT OUTS");
}
}
Надеюсь, кто-то может мне с этим помочь.
ИЗМЕНИТЬ 1: Ничего страшного, если я не поставил инициализацию ServerSocket в основной метод?
РЕДАКТИРОВАТЬ 2: Обновлен мой метод otherConnect () и все тот же результат. Я перенес всю обработку запроса в поток, который нужно создать, и он все еще не может обработать новый запрос, пока идет процесс. См. Код ниже.
public void otherConnect() throws Exception {
synchronized (_SYNC_){
ServerSocket server = new ServerSocket(14344, 1000);
IsoServerBean isoServerBean = new IsoServerBean();
log.info("Waiting for connections...");
byte[] lenbuf = new byte[4];
while(true){
Socket clientSocket = null;
clientSocket = server.accept();
int size = 352;
byte[] targetBuf = new byte[size-2];
setMyConnectionClient(clientSocket);
new Thread(new CProcessorForClient(targetBuf, clientSocket, 4), "resp").start();
}
}
}
Ниже приведен код для резьбы
private class CProcessorForClient extends Thread{
private volatile boolean stopFlag;
private volatile boolean stopped;
private byte[] msg;
private Socket clientSocket;
private Socket socket;
private int millis;
private int ctr =0;
CProcessorForClient(byte[] buf, Socket s, int waitMillis) {
stopFlag = false;
stopped = false;
msg = buf;
socket = s;
millis = waitMillis;
}
public void killMe()
{
stopFlag = true;
synchronized (_SYNC_)
{
this.interrupt();
}
}
public boolean killed()
{
return stopped;
}
public void run() {
try {
Thread.sleep(millis);
int isoSize = 352;
byte[] buf = new byte[isoSize];
socket.getInputStream().read(buf);
HexDump.hexDump(buf,1,isoSize);
System.arraycopy(buf, 4, msg, 0, isoSize-4);
IsoMessage request = null;
//OMITTED THE PROCESS OF THE CREATION OF ISO TO BE THROWN TO ANOTHER SERVER
msgFactory.print(request);
try {
this.socket = getMyConnection();//Connection to another server
this.clientSocket = getMyConnectionClient();//Connection from client
request.write(socket.getOutputStream(), 0, 0);
int i = 0;
Integer sleepTime = 500;
Integer waitTime = 5000;
decision = true;
while(decision){
i = i + 1;
Thread.sleep(sleepTime);
if (i * sleepTime >= waitTime) {
this.clientSocket.close();
decision = false;
}
}
} catch (IOException ex) {
log.debug("IO Exception encountered: " + ex.getMessage());
} catch (Exception ex) {
java.util.logging.Logger.getLogger(CService.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
} catch (ParseException ex) {
log.error("Error in parsing incoming message:", ex);
log.debug("ex.getMessage: " + ex.getMessage());
} catch (InterruptedException ex) {
log.error("Interrupted!", ex);
} finally{
if (log != null) log.debug("Thread has finished processing txn.");
}
}
}