(文章本人原創,若轉載請注明出處)
下面看一下一些具體的實現,先看一下Sender接口的commitData方法的MySql實現,即SenderMySqlImp類:
public void commitData(String path, String file) {
......
Connection connect = null;
try {
//根據配置由Helper來確定是否用連接池,這只調用getConnection()即可
connect = Helper.getConnection();
connect.setAutoCommit(false);
FileInputStream fis = new FileInputStream(path + file);
//insert語句,有三個參數分別為id,image,filename字段。
ps = connect.prepareStatement(sql.toString());
ps.setString(1, UUID.randomUUID().toString());
//將圖片文件流化,用jdbc直接寫到數據庫
ps.setBinaryStream(2, fis, fis.available());
ps.setString(3, file);
ps.executeUpdate();
connect.commit();
count++;
Logger.writeLog("已處理文件數:"+count+",時間:"+new java.util.Date());
} catch (Exception e) {
........
}
很簡單吧,其實就是用Stream來做,另外在網上可以找到有關Oracle上傳blob的實現,一般都是先insert一條記錄,blob字段用empty_blob()函數插入一個空數據,然后再取出這個blob字段,最后按字節寫入blob,具體參考SenderOracleImp類吧。個人感覺還是在mysql的這個效率高些并且看起來簡單了很多。
然后來看看使用線程池的ProcessMulti類:
public class ProcessMulti implements Process{
private String path;
private Vector<String> files = new Vector<String>();
private Properties prop;
ProcessMulti() {
prop = ConfigMgr.getProperties(); //取config.properties中配置信息
this.path = prop.getProperty("path");
this.files = Helper.getFiles(prop.getProperty("filetype"), this.path);
}
public void doProcess() {
//正如前面兩篇所說,這里是線程池構建器,傳入相關參數
BlobSenderThreadPool tpe = new BlobSenderThreadPool(Integer
.valueOf(prop.getProperty("corePoolSize")), Integer
.valueOf(prop.getProperty("maxPoolSize")), Integer.valueOf(prop
.getProperty("keepAliveTime")), TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(Integer.valueOf(prop
.getProperty("maxPoolSize"))),
new BlobSenderThreadPool.DiscardPolicy(), files.size());
Logger.writeLogForce("開始處理
." + new java.util.Date());
for (int i = 0; i < this.files.size(); i++) {
//向線程池提交要處理的任務,線程池根據其配置進行處理
//Helper.getSender()會根據配置取得支持mysql或是oracel的寫入方法
tpe.execute(new Runner(path, files.get(i), Helper.getSender()));
Logger.writeLog("已提交第" + (int)(i+1) + "個文件" + ",時間為:"
+ new java.util.Date());
}
//可以在這里寫一個打印輸出,實際上程序很快就執行完上面的for,運行到這里,但是處理并沒有完成,
//主程序好像職業經理人,他的工作就是分配任務給下屬,自已就完成工作了,但下屬們還要接著忙活呵呵...
System.out.println("任務已分配...");
}
//線程池類
class BlobSenderThreadPool extends ThreadPoolExecutor {
volatile int planTask;//計劃任務,即計劃要寫的文件數
public BlobSenderThreadPool(int corePoolSize, int maximumPoolSize,
long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue,
RejectedExecutionHandler handler, int planTask) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
this.planTask = planTask;
}
@Override
//當某個線程處理完時會調用此方法
protected void afterExecute(Runnable r, Throwable t) {
Logger.writeLog("當前已完成任務數:" + (int)(this.getCompletedTaskCount()+1)
+ "計劃任務數:" + planTask);
//若已處理完任務和計劃的任務數相同說明所有線程已完成處理,終止線程池處理
if ((this.getCompletedTaskCount()+1)== planTask)
this.terminated();
super.afterExecute(r, t);
}
@Override
protected void terminated() {
Logger.writeLogForce("所有任務已完成 ,處理結束時間===>>" + new java.util.Date());
System.exit(0);
}
}
//要使用線程進行處理,類要實現Runable接口
class Runner implements Runnable {
String file;
String path;
Sender sender;
Runner(String path, String file, Sender sender) {
this.file = file;
this.path = path;
this.sender = sender;
}
//Runer的實例會被傳入線程池,線程被運行時會調用run方法
public void run() {
sender.commitData(path, file);
}
}
posted on 2009-03-21 10:40
依然Fantasy 閱讀(622)
評論(0) 編輯 收藏 所屬分類:
原創