简体   繁体   English

第2部分:Web Start应用程序:并发问题

[英]Part-2: Web Start Application: Concurrency Issue

With your suggestions given on this thread , 有了您对此线程的建议,

I tried using FileLock, however, when I write something in the file, somehow excel file gets corrupted and there is nothing in the file (it gets empty, no contents in there) 我尝试使用FileLock,但是,当我在文件中写入内容时,excel文件以某种方式损坏,文件中没有任何内容(该文件为空,其中没有内容)

I have the following method: 我有以下方法:

void writeIntoTheFile(XSSFWorkbook defectWorkBook, File fileToWrite) {

            FileLock lock = null;
            FileChannel channel = null;
            FileOutputStream out = null;
            try {
                //fileToWrite contains an excel .xlsx file
                channel = new RandomAccessFile(fileToWrite, "rw").getChannel();
                lock = channel.tryLock();
                if (lock != null) {
                    out = new FileOutputStream(fileToWrite.getPath());
                    defectWorkBook.write(out);

                } else {
                    JOptionPane.showMessageDialog(null, "Another instance is already writing, Try after a few seconds.", "Write Error...", JOptionPane.INFORMATION_MESSAGE);
                }
                    out.close();

            } catch (Exception e) {
                e.getMessage();
            } 


           finally{   
            if (lock != null && lock.isValid()) {
                    lock.release();
                }
            channel.close();

            } 

        }

Seems the problem is coming from below code: 似乎问题来自以下代码:

channel = new RandomAccessFile(fileToWrite, "rw").getChannel();
 lock = channel.tryLock();

Can anyone please help me on this issue? 谁能帮我解决这个问题?

rahul 拉胡尔

I suspect you are getting an exception in your try{} block, but your catch clock doesn't prin it. 我怀疑您在try {}块中遇到了异常,但是您的捕获时钟没有清除它。 e.getMessage() will get the message but not print it. e.getMessage()将获取消息,但不打印。

I suggest something along the lines of e.printStackTrace() (for a production system you would want to do something more useful with the exception). 我建议采取类似e.printStackTrace()的方式 (对于生产系统,您可能希望做一些更有用的例外处理)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM