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

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(".")){
file = new File("/root/abc");
}else {
String name = path.split("\\.")[0];
String name = path.substring(0, path.lastIndexOf('.'));
System.out.println(name);
filePath += (name + "/" + name + ".zip");
file = new File(filePath);
}

View File

@ -28,7 +28,7 @@ public class DownloadCheckService {
}
public boolean downloadCheck(){
if(queue.size() == 0)
if(queue.isEmpty())
return false;
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 lombok.extern.log4j.Log4j;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.concurrent.*;
@ -63,8 +66,9 @@ public class storageNode {
}
public void mainThread(){
try {
//检查
if(!downloadCheckService.downloadCheck())
if (!downloadCheckService.downloadCheck())
return;
//发送
DownloadStatusMessage downloadStatusMessage = new DownloadStatusMessage();
@ -73,13 +77,23 @@ public class storageNode {
ListIterator<GalleryTask> listIterator = queue.listIterator();
lock.lock();
while (listIterator.hasNext()){
while (listIterator.hasNext()) {
GalleryTask galleryTask = listIterator.next();
if(galleryTask.is_download_complete())
if (galleryTask.is_download_complete())
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();
}
}
}
class MyChannelInboundHandlerAdapter extends ChannelInboundHandlerAdapter{
@ -101,6 +115,7 @@ public class storageNode {
}else if(msg instanceof DownloadPostMessage dpm){
lock.lock();
queue.add(dpm.getGalleryTask());
System.out.println(queue);
lock.unlock();
ctx.writeAndFlush(new ResponseMessage(dpm.messageId, (byte) 0));
@ -132,6 +147,7 @@ public class storageNode {
FileUtil.del(storagePath + ugm.getGalleryTask().getName());
lock.lock();
queue.add(ugm.getGalleryTask());
System.out.println(queue);
lock.unlock();
ctx.writeAndFlush(new ResponseMessage(ugm.messageId, (byte) 0));
@ -147,7 +163,7 @@ public class storageNode {
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
public void channelUnregistered(ChannelHandlerContext ctx) {
if(ctx.channel().equals(server)) {
log.info("server 下线");
server = null;