优化本子名字截取;下载线程出错时保存日志并退出

This commit is contained in:
chuzhongzai 2023-08-05 21:44:33 +08:00
parent cc6a19cf40
commit 5d84ebd4ee
3 changed files with 35 additions and 18 deletions

View File

@ -56,7 +56,8 @@ public class MultiThreadedHTTPServer {
if(!path.contains(".")){ if(!path.contains(".")){
file = new File("/root/abc"); file = new File("/root/abc");
}else { }else {
String name = path.split("\\.")[0]; String name = path.substring(0, path.lastIndexOf('.'));
System.out.println(name);
filePath += (name + "/" + name + ".zip"); filePath += (name + "/" + name + ".zip");
file = new File(filePath); file = new File(filePath);
} }

View File

@ -28,7 +28,7 @@ public class DownloadCheckService {
} }
public boolean downloadCheck(){ public boolean downloadCheck(){
if(queue.size() == 0) if(queue.isEmpty())
return false; return false;
log.info("下载检查:" + Arrays.toString(queue.toArray())); log.info("下载检查:" + Arrays.toString(queue.toArray()));

View File

@ -15,6 +15,9 @@ import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LoggingHandler; import io.netty.handler.logging.LoggingHandler;
import lombok.extern.log4j.Log4j; import lombok.extern.log4j.Log4j;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.ListIterator; import java.util.ListIterator;
import java.util.concurrent.*; import java.util.concurrent.*;
@ -63,23 +66,34 @@ public class storageNode {
} }
public void mainThread(){ public void mainThread(){
//检查 try {
if(!downloadCheckService.downloadCheck()) //检查
return; if (!downloadCheckService.downloadCheck())
//发送 return;
DownloadStatusMessage downloadStatusMessage = new DownloadStatusMessage(); //发送
downloadStatusMessage.setGalleryTasks(queue.toArray(GalleryTask[]::new)); DownloadStatusMessage downloadStatusMessage = new DownloadStatusMessage();
server.writeAndFlush(downloadStatusMessage); downloadStatusMessage.setGalleryTasks(queue.toArray(GalleryTask[]::new));
server.writeAndFlush(downloadStatusMessage);
ListIterator<GalleryTask> listIterator = queue.listIterator(); ListIterator<GalleryTask> listIterator = queue.listIterator();
lock.lock(); lock.lock();
while (listIterator.hasNext()){ while (listIterator.hasNext()) {
GalleryTask galleryTask = listIterator.next(); GalleryTask galleryTask = listIterator.next();
if(galleryTask.is_download_complete()) if (galleryTask.is_download_complete())
listIterator.remove(); listIterator.remove();
}
lock.unlock();
log.info("任务状态发送完成");
}catch (Exception e){
e.printStackTrace();
try (OutputStream outputStream = new FileOutputStream("/root/gallery/storageNode/err.txt")){
outputStream.write(e.getMessage().getBytes());
channelFuture.channel().close().sync();
System.exit(-1);
}catch (Exception ex){
ex.printStackTrace();
}
} }
lock.unlock();
log.info("任务状态发送完成");
} }
class MyChannelInboundHandlerAdapter extends ChannelInboundHandlerAdapter{ class MyChannelInboundHandlerAdapter extends ChannelInboundHandlerAdapter{
@ -101,6 +115,7 @@ public class storageNode {
}else if(msg instanceof DownloadPostMessage dpm){ }else if(msg instanceof DownloadPostMessage dpm){
lock.lock(); lock.lock();
queue.add(dpm.getGalleryTask()); queue.add(dpm.getGalleryTask());
System.out.println(queue);
lock.unlock(); lock.unlock();
ctx.writeAndFlush(new ResponseMessage(dpm.messageId, (byte) 0)); ctx.writeAndFlush(new ResponseMessage(dpm.messageId, (byte) 0));
@ -132,6 +147,7 @@ public class storageNode {
FileUtil.del(storagePath + ugm.getGalleryTask().getName()); FileUtil.del(storagePath + ugm.getGalleryTask().getName());
lock.lock(); lock.lock();
queue.add(ugm.getGalleryTask()); queue.add(ugm.getGalleryTask());
System.out.println(queue);
lock.unlock(); lock.unlock();
ctx.writeAndFlush(new ResponseMessage(ugm.messageId, (byte) 0)); ctx.writeAndFlush(new ResponseMessage(ugm.messageId, (byte) 0));
@ -147,7 +163,7 @@ public class storageNode {
} }
@Override @Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { public void channelUnregistered(ChannelHandlerContext ctx) {
if(ctx.channel().equals(server)) { if(ctx.channel().equals(server)) {
log.info("server 下线"); log.info("server 下线");
server = null; server = null;