假设我要为文件建立索引。该文件存储在filequeue
表中。表格结构如下:
UniqueID FilePath Status
1 C:\Folder1\abc.pdf Active
2 C:\Folder1\def.pdf Active
3 C:\Folder1\efg.pdf Error
有四种不同的状态:有效,处理中,成功和错误
有效:将文件插入表中以待建立索引过程
正在处理:开始建立索引过程时,表状态将更新为正在处理。
成功:在完成索引过程之后,应该将表状态更新为正在处理。
错误:如果偶然,由于某种原因处理失败。
由于某种原因,假设abc.pdf
不存在。当我扫描表时,它将检索状态为Active的所有文件路径,并开始对其进行迭代并执行索引功能。在此过程中,如果没有问题,它将状态更新为“正在处理”,然后更新为“完成”。
但是,它将引发错误FileNotFoundException
在abc.pdf
上,这很好,因为该文件不存在,但仍将状态更新为Complete
。它应该更新为错误状态。
我当时正在考虑使用if else语句,它看起来像这样:
public void doScan_DB() throws Exception {
boolean fileprocessstatus=false;
try {
Statement statement = con.connect().createStatement();
ResultSet rs = statement.executeQuery("select * from filequeue where Status='Active'");
while (rs.next()) {
// get the filepath of the PDF document
String path1 = rs.getString(2);
// while running the process, update status : Processing
updateProcess_DB();
// call the index function
Indexing conn = new Indexing();
conn.doScan(path1);
fileProcessStatus =true;
// After completing the process, update status: Complete
if(fileProcessStatus=true){
updateComplete_DB();
}else{
//call function to update status to error if index fails
}
}
}catch(SQLException|IOException e){
e.printStackTrace();
}
我的DoScan()方法:
public void doScan(String path) throws Exception{
/* File folder = new File("D:\\PDF1");
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile()) {
// HashSet<String> uniqueWords = new HashSet<>();
String path = "D:\\PDF1\\" + file.getName();*/
ArrayList<String> list = new ArrayList<String>();
try (PDDocument document = PDDocument.load(new File(path))) {
if (!document.isEncrypted()) {
PDFTextStripper tStripper = new PDFTextStripper();
String pdfFileInText = tStripper.getText(document);
String lines[] = pdfFileInText.split("\\r?\\n");
for (String line : lines) {
String[] words = line.split(" ");
// words.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
for (String word : words) {
// check if one or more special characters at end of string then remove OR
// check special characters in beginning of the string then remove
// uniqueWords.add(word.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
list.add(word.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
// uniqueWords.add(word.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
}
}
}
} catch (IOException e) {
System.err.println("Exception while trying to read pdf document - " + e);
}
String[] words1 =list.toArray(new String[list.size()]);
// String[] words2 =uniqueWords.toArray(new String[uniqueWords.size()]);
// MysqlAccessIndex connection = new MysqlAccessIndex();
index(words1,path);
System.out.println("Completed");
}
}
UpdateError_DB():
public void updateError_DB(){
try{
Statement statement = con.connect().createStatement();
statement.execute("update filequeue SET STATUS ='Error' where STATUS ='Processing' ");
}catch(Exception e){
e.printStackTrace();
}
}
UpdateComplete_DB():
public void updateComplete_DB() {
try {
Statement statement = con.connect().createStatement();
statement.execute("update filequeue SET STATUS ='Complete' where STATUS ='Processing' ");
} catch (Exception e) {
e.printStackTrace();
}
}
但是,它并不能真正解决正确更新状态的问题。 有什么方法可以实现我想要的?
答案 0 :(得分:0)
这是根据我对您的情况的了解而得出的解决方案。
doScan_DB()方法:
public void doScan_DB() throws Exception {
boolean fileprocessstatus = false;
try {
Statement statement = con.connect().createStatement();
ResultSet rs = statement.executeQuery("select * from filequeue where Status='Active'");
while (rs.next()) {
//Get the uniqueID of active filepath
String uniqueID = rs.getString(1);
// get the filepath of the PDF document
String path1 = rs.getString(2);
// while running the process, update status : Processing
updateProcess_DB(uniqueID);
// call the index function
Indexing conn = new Indexing();
if (conn.doScan(path1)) {
updateComplete_DB(uniqueID);
} else {
updateError_DB(uniqueID);
}
}
} catch (SQLException | IOException e) {
e.printStackTrace();
}
}
doScan()方法:
public boolean doScan(String path) {
/*
* File folder = new File("D:\\PDF1"); File[] listOfFiles = folder.listFiles();
*
* for (File file : listOfFiles) { if (file.isFile()) { // HashSet<String>
* uniqueWords = new HashSet<>();
*
* String path = "D:\\PDF1\\" + file.getName();
*/
ArrayList<String> list = new ArrayList<String>();
boolean isSuccess = true;
try {
File f = new File(path);
if (!f.exists()) {
isSuccess = false;
} else {
PDDocument document = PDDocument.load(f);
if (!document.isEncrypted()) {
PDFTextStripper tStripper = new PDFTextStripper();
String pdfFileInText = tStripper.getText(document);
String lines[] = pdfFileInText.split("\\r?\\n");
for (String line : lines) {
String[] words = line.split(" ");
// words.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
for (String word : words) {
// check if one or more special characters at end of string then
// remove OR
// check special characters in beginning of the string then
// remove
// uniqueWords.add(word.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
list.add(word.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
// uniqueWords.add(word.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
}
}
}
}
String[] words1 = list.toArray(new String[list.size()]);
// String[] words2 =uniqueWords.toArray(new String[uniqueWords.size()]);
// MysqlAccessIndex connection = new MysqlAccessIndex();
index(words1, path);
} catch (Exception e) {
System.err.println("Exception while trying to read pdf document - " + e);
isSuccess = false;
}
return isSuccess;
}
现在所有的更新方法。这里我将使用JDBC PreparedStatement 代替JDBC Statement 。我假设您的UniqueId是 Int 类型。
注意:根据您的环境进行必要的更改
public void updateComplete_DB(String uniqueID) {
try {
String sql="UPDATE filequeue SET STATUS ='Complete' WHERE STATUS ='Processing' AND UniqueID=?";
PreparedStatement statement = con.connect().prepareStatement(sql);
statement.setInt(1,Integer.parseInt(uniqueID));
int rows = statement.executeUpdate();
System.out.prrintln("No. of rows updated:"+rows)
} catch (Exception e) {
e.printStackTrace();
}
}
public void updateProcess_DB(String uniqueID) {
try {
String sql="UPDATE filequeue SET STATUS ='Processing' WHERE UniqueID=?";
PreparedStatement statement = con.connect().prepareStatement(sql);
statement.setInt(1,Integer.parseInt(uniqueID));
int rows = statement.executeUpdate();
System.out.prrintln("No. of rows updated:"+rows)
} catch (Exception e) {
e.printStackTrace();
}
}
public void updateError_DB(String uniqueID) {
try {
String sql="UPDATE filequeue SET STATUS ='Error' WHERE STATUS ='Processing' AND UniqueID=?";
PreparedStatement statement = con.connect().prepareStatement(sql);
statement.setInt(1,Integer.parseInt(uniqueID));
int rows = statement.executeUpdate();
System.out.prrintln("No. of rows updated:"+rows)
} catch (Exception e) {
e.printStackTrace();
}
}