62,628
社区成员
发帖
与我相关
我的任务
分享
public void handleRequest(){
//work well
mainThreadPool.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("In socket waiting");
Socket socketTask = null;
try {
socketTask = serverSocket.accept();
dispatcher.dispatch(socketTask);
} catch (SocketTimeoutException e){
System.out.println("Accept timeout");
} catch (IOException e) {
e.printStackTrace();
}
}
}, 3, 10, TimeUnit.MILLISECONDS);
//surprise
mainThreadPool.scheduleAtFixedRate(dispatcher.tmqManager.new PersistenceTask(), 2, 10, TimeUnit.MILLISECONDS);
}
class PersistenceTask implements Runnable{
@Override
public void run(){
synchronized (this) {
System.out.println("In persistence task");
Message messageObject = findMaxLengthQueue().pop();
if (messageObject != null) {
long epoch = System.currentTimeMillis() / 1000;
ObjectOutputStream outputStream = null;
try {
outputStream = new ObjectOutputStream(new FileOutputStream("msg" + epoch));
outputStream.writeObject(messageObject);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
logger.warning("failure to persist message object");
}
}
}
}
}