分享

openoffice+pdf2swf实现在线转换office文档,实现在线预览

 WindySky 2016-09-20

利用队列+多线程实现文档在线转换:

主意:为什么要用 队列+线程 ,因为openoffice的转换任务,如果文档过大,多次调用转换就会卡掉,再次利用队列,在转换的过程中其他转换任务处于队列的等待状态,只有转换成功后再调用下个转换任务

第一步:创建一个线程池,(上篇博客有写)

  1. package com.cloud.job;  
  2.   
  3. import java.util.concurrent.ExecutorService;  
  4. import java.util.concurrent.Executors;  
  5. /** 
  6.  *  
  7. * @ClassName: ThreadPool  
  8. * @author caixl  
  9. * @date 2016-6-3 下午4:50:00  
  10.  */  
  11. public class ThreadPool {  
  12.     private static ExecutorService threadPool = null;  
  13.     public static ExecutorService getThreadPool(){  
  14.         if(threadPool==null){  
  15.             threadPool = Executors.newCachedThreadPool();  
  16.         }  
  17.         return  threadPool;  
  18.     }  
  19.   
  20. }  

第二步:创建一个无界队列(上篇博客有写)

  1. package com.cloud.job;  
  2.   
  3. import java.util.concurrent.LinkedBlockingQueue;  
  4. /** 
  5.  *  
  6. * @ClassName: TaskQueue  
  7. * @author caixl  
  8. * @date 2016-6-3 下午4:49:50  
  9.  */  
  10. public class TaskQueue {  
  11.     private static  LinkedBlockingQueue queues = null;  
  12.       
  13.     public static LinkedBlockingQueue getTaskQueue(){  
  14.         if(queues==null){  
  15.             queues =  new LinkedBlockingQueue();  
  16.             System.out.println("初始化 队列");  
  17.         }  
  18.         return queues;  
  19.     }  
  20.       
  21.     public static void add(Object obj){  
  22.         if(queues==null)  
  23.             queues =  getTaskQueue();  
  24.         if(!queues.contains(obj))  
  25.             queues.offer(obj);  
  26.         System.out.println("-------------------------------");  
  27.         System.out.println("入队:"+obj);  
  28.     }  
  29. }  
第三步:创建一个入队的线程,专门负责往队列中加入元素

  1. package com.cloud.job;  
  2. /** 
  3.  *  
  4. * @ClassName: Produce  
  5. * @author caixl  
  6. * @date 2016-6-3 下午4:49:41  
  7.  */  
  8. public class Produce implements Runnable {  
  9.     private static volatile boolean isRunning=true;  
  10.     private static volatile Object obj;  
  11.     public Produce(Object obj){  
  12.         this.obj = obj;  
  13.     }  
  14.     public void run() {  
  15.         TaskQueue.add(obj);  
  16.     }  
  17.   
  18. }  

第四步:创建一个消费的线程,次线程主要负责阻塞作用,也就是转换任务转换任务是否已完成,如果完成调用下一次转换

  1. package com.cloud.job;  
  2.   
  3. import org.apache.log4j.Logger;  
  4.   
  5. import com.cloud.platform.DocConstants;  
  6. /** 
  7.  *  
  8. * @ClassName: Consumer  
  9. * @author caixl  
  10. * @date 2016-6-3 下午4:48:46  
  11.  */  
  12. public class Consumer implements Runnable {  
  13.     private static Consumer consumer;  
  14.     private static Logger logger = Logger.getLogger(Consumer.class);  
  15.     public static volatile boolean isRunning=true;  
  16.     public void run() {  
  17.         while(isRunning)    
  18.         {   
  19.             isRunning = false;  
  20.             DocCovertThread docCovertThread = new DocCovertThread();  
  21.             docCovertThread.run();  
  22.         }  
  23.           
  24.     }  
  25.     public static Consumer getInstance(){  
  26.         if(consumer==null){  
  27.             consumer = new Consumer();  
  28.             System.out.println("初始化消费线程");  
  29.             logger.info("初始化消费线程");  
  30.         }  
  31.         return consumer;  
  32.     }  
  33.   
  34. }  
第五步:创建一个转换文档的线程

  1. package com.cloud.job;  
  2.   
  3. import java.io.File;  
  4. import java.util.Map;  
  5.   
  6. import org.apache.log4j.Logger;  
  7. import org.artofsolving.jodconverter.OfficeDocumentConverter;  
  8. import org.artofsolving.jodconverter.document.DefaultDocumentFormatRegistry;  
  9. import org.artofsolving.jodconverter.document.DocumentFormat;  
  10. import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;  
  11. import org.artofsolving.jodconverter.office.OfficeManager;  
  12. import org.quartz.JobExecutionContext;  
  13.   
  14. import com.cloud.doc.convert.pdf2swf;  
  15. import com.cloud.platform.DocConstants;  
  16. import com.database.bean.MainTableAttachFile;  
  17. /** 
  18.  *  
  19. * @ClassName: DocCovertThread  
  20. * @author caixl  
  21. * @date 2016-6-3 下午4:49:15  
  22.  */  
  23. public class DocCovertThread implements Runnable{  
  24.     private static Logger logger = Logger.getLogger(DocCovertThread.class);  
  25.     /** 
  26.      * openoffice的安装路径 
  27.      */  
  28.     public static final String OPENOFFICE_HOME = DocConstants.ROOTPATH + "include/OpenOffice";  
  29.     @Override  
  30.     public void run() {  
  31.           
  32.         try {  
  33.               JobExecutionContext context = (JobExecutionContext)TaskQueue.getTaskQueue().take();  
  34.               System.out.println("出队"+context);  
  35.               Map data = context.getJobDetail().getJobDataMap();  
  36.               MainTableAttachFile attach = (MainTableAttachFile)data.get("attach");  
  37.               if (attach == null) {   
  38.                 return;  
  39.               }  
  40.               if (DocConstants.isOffice(com.common.StringHelper.getFileExt(attach.getAttachFileName())))  
  41.               {  
  42.                   System.out.println("开始转换文件"+OPENOFFICE_HOME+"  "  +attach.getPath());  
  43.                 DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();  
  44.                 config.setOfficeHome(OPENOFFICE_HOME);  
  45.                 OfficeManager officeManager = config.buildOfficeManager();  
  46.                 try  
  47.                 {  
  48.                   officeManager.start();  
  49.                   OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);  
  50.                   DocumentFormat outputFormat = new DefaultDocumentFormatRegistry().getFormatByExtension("pdf");  
  51.                     
  52.                   String srcPath = attach.getPath();  
  53.                   String destPath = com.common.StringHelper.getFileName(srcPath) + ".pdf";  
  54.                     
  55.                   converter.convert(new File(srcPath), new File(destPath), outputFormat);  
  56.                   String docPath = com.common.StringHelper.getFileName(attach.getPath());  
  57.                   pdf2swf swfConverter = pdf2swf.getInstance();  
  58.                   swfConverter.convert(docPath, "");  
  59.                 }  
  60.                 catch (Exception e)  
  61.                 {  
  62.                  e.printStackTrace();  
  63.                   logger.error("***** 异常信息 ***** 方法:switchDocToSwf at switch office to pdf", e); return;  
  64.                 }  
  65.                 finally  
  66.                 {  
  67.                   officeManager.stop();  
  68.                   Consumer.isRunning=true;  
  69.                 }  
  70.                 System.out.println("转换结束");  
  71.               }  
  72.         } catch (InterruptedException e) {  
  73.             e.printStackTrace();  
  74.             Consumer.isRunning=true;  
  75.             logger.error("***** 异常信息 ***** 方法:DocConvertJob execute", e);  
  76.         }  
  77.     }  
  78.   
  79. }  

第六步:在需要转换文档时调用

  1.        ExecutorService threadPool = ThreadPool.getThreadPool();  
  2. Produce consumer2 = new Produce(context);  
  3. threadPool.execute(consumer2);  
  4. Consumer consumer=Consumer.getInstance();  
  5. threadPool.execute(consumer);  

源码详见:https://github.com/izhbg/typz


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多