top.codings.websiphon.core.pipeline.FilePipeline Maven / Gradle / Ivy
package top.codings.websiphon.core.pipeline;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import top.codings.websiphon.bean.BasicWebRequest;
import java.io.*;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Slf4j
@NoArgsConstructor
public class FilePipeline extends ReadWritePipelineAdapter {
protected String filePath;
protected String charset;
protected ExecutorService executorService;
public FilePipeline(String filePath, String charset) {
this.filePath = filePath;
this.charset = charset;
}
@Override
public void init() {
executorService = Executors.newFixedThreadPool(1);
executorService.submit(() -> {
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath))))) {
String line;
List out = new LinkedList<>();
while ((line = br.readLine()) != null) {
out.clear();
conversion(line, out);
for (BasicWebRequest basicWebRequest : out) {
// queue.transfer(basicWebRequest);
write(basicWebRequest);
}
}
} catch (IOException e) {
throw new RuntimeException("读取文件异常");
} catch (InterruptedException e) {
return;
}
});
}
@Override
public void conversion(String param, List out) {
if (param.startsWith("#")) {
return;
}
BasicWebRequest request = new BasicWebRequest();
request.setUri(param);
out.add(request);
}
@Override
public void close() throws Exception {
super.close();
if (null != executorService) {
executorService.shutdownNow();
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy