Skip to content

Commit 13c0211

Browse files
committed
并发处理
1 parent a05485d commit 13c0211

1 file changed

Lines changed: 80 additions & 87 deletions

File tree

docviewerapi/src/com/log4ic/DocViewer.java

Lines changed: 80 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
import org.artofsolving.jodconverter.office.OfficeConnectionProtocol;
1515

1616
import java.io.*;
17-
import java.util.LinkedList;
18-
import java.util.List;
19-
import java.util.Properties;
20-
import java.util.Random;
17+
import java.util.*;
18+
import java.util.concurrent.locks.Lock;
19+
import java.util.concurrent.locks.ReentrantLock;
2120

2221
/**
2322
* @author: 张立鑫
@@ -288,6 +287,7 @@ public synchronized static int getNextId() {
288287

289288
private static final ConvertQueue officeQueue = new ConvertQueue(OFFICE_POOL_MAX_THREAD, "office_queue");
290289
private static final ConvertQueue pdfQueue = new ConvertQueue(PDF_POOL_MAX_THREAD, "pdf_queue");
290+
private static Map<String, Lock> fileIds = new HashMap<String, Lock>();
291291
private static final byte[] lock = new byte[0];
292292

293293
/**
@@ -298,63 +298,58 @@ public synchronized static int getNextId() {
298298
* @throws Exception
299299
*/
300300
public static File getDoc(int id) throws Exception {
301-
synchronized (lock) {
302-
if (DocViewer.isConverting(id) || hasDoc(id)) {
303-
while (DocViewer.isConverting(id)) {
304-
LOGGER.debug(id + " Waiting other converting...");
305-
lock.wait();
301+
if (hasDoc(id)) {
302+
LOGGER.debug(id + " return doc");
303+
return new File(OUTPUT_PATH + id);
304+
}
305+
306+
String docId = id + "_doc";
307+
308+
if (!fileIds.containsKey(docId)) {
309+
synchronized (lock) {
310+
if (!fileIds.containsKey(docId)) {
311+
fileIds.put(docId, new ReentrantLock());
306312
}
313+
}
314+
}
315+
Lock lk = fileIds.get(docId);
307316

317+
lk.lock();
318+
try {
319+
if (hasDoc(id)) {
308320
LOGGER.debug(id + " return doc");
309321
return new File(OUTPUT_PATH + id);
310322
}
311-
}
323+
ConvertWorker worker = null;
324+
if (pdfQueue.isWaiting(id)) {
325+
worker = (ConvertWorker) pdfQueue.getWaitingWorker(id);
326+
if (worker != null) {
327+
pdfQueue.removeWaitingWorker(worker);
328+
}
312329

313-
ConvertWorker worker = null;
314-
if (pdfQueue.isWaiting(id)) {
315-
worker = (ConvertWorker) pdfQueue.getWaitingWorker(id);
316-
if (worker != null) {
317-
pdfQueue.removeWaitingWorker(worker);
330+
} else if (officeQueue.isWaiting(id)) {
331+
worker = (ConvertWorker) officeQueue.getWaitingWorker(id);
332+
if (worker != null) {
333+
officeQueue.removeWaitingWorker(worker);
334+
}
318335
}
319336

320-
} else if (officeQueue.isWaiting(id)) {
321-
worker = (ConvertWorker) officeQueue.getWaitingWorker(id);
337+
File in;
338+
322339
if (worker != null) {
323-
officeQueue.removeWaitingWorker(worker);
340+
in = worker.getInFile();
341+
} else {
342+
in = getDocFileFromSource(id);
324343
}
325-
}
326344

327-
File in;
328-
329-
if (worker != null) {
330-
in = worker.getInFile();
331-
} else {
332-
in = getDocFileFromSource(id);
333-
}
334-
335-
if (in == null) {
336-
return null;
337-
}
338-
339-
synchronized (lock) {
340-
if (DocViewer.isConverting(id) || hasDoc(id)) {
341-
while (DocViewer.isConverting(id)) {
342-
LOGGER.debug(id + " Waiting other converting...");
343-
lock.wait();
344-
}
345-
LOGGER.debug(id + " return doc");
346-
return new File(OUTPUT_PATH + id);
345+
if (in == null) {
346+
return null;
347347
}
348-
}
349-
File file;
350-
try {
351-
file = DocViewerConverter.toSwf(in, OUTPUT_PATH);
348+
return DocViewerConverter.toSwf(in, OUTPUT_PATH);
352349
} finally {
353-
synchronized (lock) {
354-
lock.notifyAll();
355-
}
350+
lk.unlock();
351+
fileIds.remove(docId);
356352
}
357-
return file;
358353
}
359354

360355
public synchronized static boolean isConverting(int id) {
@@ -373,61 +368,59 @@ public synchronized static boolean isConverting(int id) {
373368
}
374369

375370
public static File getPDFDoc(int id) throws Exception {
376-
synchronized (lock) {
377-
if (DocViewer.isConverting(id) || DocViewer.hasDocDir(id)) {
378-
while (DocViewer.isConverting(id)) {
379-
LOGGER.debug(id + " Waiting other converting...");
380-
lock.wait();
371+
if (DocViewer.hasDocDir(id)) {
372+
File file = new File(OUTPUT_PATH + id + File.separator + id + ".pdf");
373+
if (file.exists()) {
374+
LOGGER.debug(id + " return pdf");
375+
return file;
376+
}
377+
}
378+
379+
String pdfId = id + "_pdf";
380+
if (!fileIds.containsKey(pdfId)) {
381+
synchronized (lock) {
382+
if (!fileIds.containsKey(pdfId)) {
383+
fileIds.put(pdfId, new ReentrantLock());
381384
}
385+
}
386+
}
387+
Lock lk = fileIds.get(pdfId);
388+
389+
lk.lock();
390+
try {
391+
if (DocViewer.hasDocDir(id)) {
382392
File file = new File(OUTPUT_PATH + id + File.separator + id + ".pdf");
383393
if (file.exists()) {
384-
LOGGER.debug(id + " return doc");
394+
LOGGER.debug(id + " return pdf");
385395
return file;
386396
}
387397
}
388-
}
398+
ConvertWorker worker = null;
399+
if (pdfQueue.isWaiting(id)) {
400+
worker = (ConvertWorker) pdfQueue.getWaitingWorker(id);
401+
if (worker != null) {
402+
pdfQueue.removeWaitingWorker(worker);
403+
}
389404

390-
ConvertWorker worker = null;
391-
if (pdfQueue.isWaiting(id)) {
392-
worker = (ConvertWorker) pdfQueue.getWaitingWorker(id);
393-
if (worker != null) {
394-
pdfQueue.removeWaitingWorker(worker);
395405
}
396406

397-
}
407+
File in;
398408

399-
File in;
409+
if (worker != null) {
410+
in = worker.getInFile();
411+
} else {
412+
in = getDocFileFromSource(id);
413+
}
400414

401-
if (worker != null) {
402-
in = worker.getInFile();
403-
} else {
404-
in = getDocFileFromSource(id);
405-
}
406415

407-
synchronized (lock) {
408-
if (DocViewer.isConverting(id) || DocViewer.hasDocDir(id)) {
409-
while (DocViewer.isConverting(id)) {
410-
LOGGER.debug(id + " Waiting other converting...");
411-
lock.wait();
412-
}
413-
File file = new File(OUTPUT_PATH + id + File.separator + id + ".pdf");
414-
if (file.exists()) {
415-
LOGGER.debug(id + " return doc");
416-
return file;
417-
}
416+
if (in == null) {
417+
return null;
418418
}
419-
}
420-
421-
File file;
422-
try {
423-
file = DocViewerConverter.toPDF(in, OUTPUT_PATH);
419+
return DocViewerConverter.toPDF(in, OUTPUT_PATH);
424420
} finally {
425-
synchronized (lock) {
426-
lock.notifyAll();
427-
}
421+
lk.unlock();
422+
fileIds.remove(pdfId);
428423
}
429-
430-
return file;
431424
}
432425

433426
/**

0 commit comments

Comments
 (0)