Project

General

Profile

« Previous | Next » 

Revision 7807

Added by Jing Tao almost 11 years ago

Add code to implment set and get the last processed date.

View differences:

IndexEventFileLog.java
26 26
import java.io.FileOutputStream;
27 27
import java.io.IOException;
28 28
import java.text.DateFormat;
29
import java.text.ParseException;
29 30
import java.text.SimpleDateFormat;
30 31
import java.util.Date;
31 32
import java.util.List;
......
45 46
public class IndexEventFileLog implements IndexEventLog {
46 47
    private static final String FIELDSEPERATOR = " ";
47 48
    private static final String LOGFILENAME = "solr-index.log";
49
    private static final String LASTPROCESSEDDATEFILENAME = "solr-last-proccessed-date";
48 50
    private File logFile = null;
51
    private File lastProcessedDateFile = null;
49 52
    private long index=1;
53
    private SimpleDateFormat format = new SimpleDateFormat();
50 54
    
51 55
    /**
52 56
     * Constructor. Initialize the log file. The log file locates solr.homeDir. 
......
66 70
        if(!logFile.exists()) {
67 71
            logFile.createNewFile();
68 72
        }
73
        lastProcessedDateFile = new File(pathDir, LASTPROCESSEDDATEFILENAME);
74
        if(!lastProcessedDateFile.exists()) {
75
            lastProcessedDateFile.createNewFile();
76
        }
69 77
    }
70 78
    /**
71 79
     * Write an IndexEvent into a file
......
145 153
     * @throws IndexEventLogException
146 154
     */
147 155
    public Date getLastProcessDate() throws IndexEventLogException {
148
        return null;
156
        Date date = null;
157
        try {
158
            String dateStr = FileUtils.readFileToString(lastProcessedDateFile, "UTF-8");
159
            if(dateStr != null && !dateStr.trim().equals("")) {
160
                date = format.parse(dateStr);
161
            }
162
        } catch (IOException e) {
163
            throw new IndexEventLogException("IndexEventFileLog.getLastProcessedDate - couldn't read the last processed date :", e);
164
        } catch (ParseException e) {
165
            // TODO Auto-generated catch block
166
            throw new IndexEventLogException("IndexEventFileLog.getLastProcessedDate - couldn't read the last processed date since the content of file is not date :", e);
167
        }
168
        return date;
149 169
    }
150 170
    
151 171
    
......
155 175
     * @throws IndexEventLogException
156 176
     */
157 177
    public void setLastProcessDate(Date date) throws IndexEventLogException {
158
        
178
        String dateStr = format.format(date);
179
        try {
180
            FileUtils.writeStringToFile(lastProcessedDateFile, dateStr, "UTF-8");
181
        } catch (IOException e) {
182
            // TODO Auto-generated catch block
183
           throw new IndexEventLogException("IndexEventFileLog.setLastProcessedDate - couldn't set the last processed date :", e);
184
        }
159 185
    }
160 186
}

Also available in: Unified diff