Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Liferay并发文件条目上载_Java_File_Concurrency_Upload_Liferay - Fatal编程技术网

Java Liferay并发文件条目上载

Java Liferay并发文件条目上载,java,file,concurrency,upload,liferay,Java,File,Concurrency,Upload,Liferay,问题陈述: 在liferay中,我必须将zip文件导入到liferay cms中的某个文件夹中,到目前为止,我已经实现了zip文件的串行解压缩,创建了它的文件夹,然后创建了它的文件。这里的问题是整个过程需要很多时间。所以我不得不使用并行方法来创建文件夹和文件 我的解决方案: 我使用java.util.concurrent.ExecutorService创建了Executors.newFixedThreadPoolNTHREDS,其中NTHREDS是要并行运行的线程数,比如说5 我从zip中读取所

问题陈述:

在liferay中,我必须将zip文件导入到liferay cms中的某个文件夹中,到目前为止,我已经实现了zip文件的串行解压缩,创建了它的文件夹,然后创建了它的文件。这里的问题是整个过程需要很多时间。所以我不得不使用并行方法来创建文件夹和文件

我的解决方案:

我使用java.util.concurrent.ExecutorService创建了Executors.newFixedThreadPoolNTHREDS,其中NTHREDS是要并行运行的线程数,比如说5

我从zip中读取所有文件夹路径并放置,zip列表 根据文件夹路径将文件作为HashMap中的键进行entires 遍历映射中的所有键并连续创建文件夹 现在遍历map中的zip条目文件列表,并将其传递给线程工作者,每个工作者一个文件,然后将这些工作者发送到 要执行的Executor服务 到目前为止,我没有发现整个过程中的时间有任何明显的减少,我是否朝着正确的方向前进?liferay是否支持并发文件添加?我做错了什么

我将非常感谢在这方面给予的任何帮助

下面是我的代码

imports 
...
...
public class TestImportZip {

    private static final int NTHREDS = 5;
    ExecutorService executor = null;
    ...
        ...
        ....
    Map<String,Folder> folders = new HashMap<String,Folder>();
    File zipsFile = null;

    public TestImportZip(............,File zipFile, .){

        .
                .
        this.zipsFile = zipFile;
        this.executor = Executors.newFixedThreadPool(NTHREDS);
    }

       // From here the process starts
    public void importZip() {

        Map<String,List<ZipEntry>> foldersMap = new HashMap<String, List<ZipEntry>>();

        try (ZipFile zipFile = new ZipFile(zipsFile)) {
            zipFile.stream().forEach(entry -> {

                   String entryName = entry.getName();
                   if(entryName.contains("/")) {

                       String key = entryName.substring(0, entryName.lastIndexOf("/"));

                       List<ZipEntry> zipEntries = foldersMap.get(key);

                       if(zipEntries == null){
                           zipEntries = new ArrayList<>();
                       }

                       zipEntries.add(entry);

                       foldersMap.put(key,zipEntries);

                   }
               });

            createFolders(foldersMap.keySet());

            createFiles(foldersMap);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void createFolders(Set<String> folderPathSets) {

        // create folder and put the folder in map
       .
       .
       .

    folders.put(folderPath,folder); 
    }

    private void createFiles(Map<String, List<ZipEntry>> foldersMap) {
          .
              .
              . 
         //Traverse all the files from all the list in map and send them to worker
       createFileWorker(folderPath,zipEntry);

    }

    private void createFileWorker(String folderPath,ZipEntry zipEntry) {

        CreateEntriesWorker cfw = new CreateEntriesWorker(folderPath, zipEntry);
        executor.execute(cfw);
    }

    class CreateEntriesWorker implements  Runnable{

        Folder folder = null;
        ZipEntry entryToCreate = null;


        public CreateEntriesWorker(String folderPath, ZipEntry zipEntry){

            this.entryToCreate = zipEntry;
            // get folder from already created folder map
            this.folder = folders.get(folderPath);

        }

        public void run() {

            if(this.folder != null) {
                long startTime = System.currentTimeMillis();

                try (ZipFile zipFile = new ZipFile(zipsFile)) {

                        InputStream inputStream = zipFile.getInputStream(entryToCreate);

                        try{

                            String name = entryToCreate.getName();
                            // created file entry here 
                        }catch(Exception e){

                        }finally{

                            if(inputStream != null)
                                inputStream.close();
                        }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

}

您的简化代码不包含我识别的任何Liferay引用。您提供的描述暗示您正在尝试优化某些代码,但没有从中获得更好的性能。这通常是一个迹象,表明您试图优化问题的错误方面,或者它已经相当优化

您需要确定操作的实际瓶颈,以便知道优化是否可行。有句俗话说,过早优化是万恶之源。这是什么意思

我将在这里完全编造数字——不要引用我的话:它们是为了说明而自由发明的。比方说,将Zip文件的内容添加到Liferay存储库的操作分配给以下百分比的操作资源:

4%压缩文件解码/解压缩 压缩操作和临时文件的6%文件I/O 10%的数据库操作用于存储文件 60%用于仅从zip文件中存储的word、pdf、excel和其他文件中提取文本,以便在全文索引中为文档编制索引 用于组合索引的全文索引库的20%开销。 假设您正在优化zip文件的解码/解压缩-您能期望数字的总体改进是什么

虽然我的数字是虚构的:如果你的优化没有任何结果,我建议你将其逆转,衡量你需要优化的地方,然后去那个地方,或者接受它,如果那个地方遥不可及,升级你的硬件


运行CPU、I/O、内存和其他潜在瓶颈的数字。确定实际瓶颈1,修复它,再次测量。你会看到瓶颈2得到了提升。重复冲洗,直到您满意为止

@OlafKock,理解并从其他站点发布删除。。