这是方案。我必须在一定的时间间隔内轮询ftp服务器并获取csv文件。然后,必须解析这些CSV文件并将其作为输入发送到某些业务逻辑。我是这样做的。
FTPClientPolling(生产者)
public class FTPClientPolling {
private static FTPClientPolling instance = null;
private FTPClientPolling() {
}
public synchronized static FTPClientPolling getInstance() {
if (instance == null) {
logger.info("Object created for Client Polling");
instance = new FTPClientPolling();
initializeFTPClient();
}
return instance;
}
public static void initializeFTPClient() {
// initialize the values from properties file
}
public void startPolling() {
FTPClient ftpClient = null;
try {
//connecting to ftp server
//iterating the files in it
FTPFile[] filesList = ftpClient.listFiles();
for (FTPFile tmpFile : filesList) {
//..
File tempFile = File.createTempFile(tmpFile.getName(), null);
FileOutputStream fileOut = new FileOutputStream(tempFile);
ftpClient.retrieveFile(tmpFile.getName(), fileOut);
//adding the file to the Queue of the file processor
FileProcessor.getInstance().getFilesToBeProcessedQueue().add(tempFile);
}
if (ftpClient.isConnected())
ftpClient.disconnect();
} catch (Exception e) {
//logging
} finally {
//closing ftpclient
}
}
}
FTPClientPollingTasker(生产者任务程序)
public class FTPClientTasker extends TimerTask {
private static Long timeInterval = 10000l;
@Override
public void run() {
FTPClientPolling.getInstance().startPolling();
}
public static void start() {
TimerTask timerTask = new FTPClientTasker();
Timer timer = new Timer();
timer.scheduleAtFixedRate(timerTask, timeInterval, timeInterval);
}
public static void main(String[] args) {
start();
}
}
FileProcessor(消费者)
public final class FileProcessor {
private static FileProcessor instance = null;
private Queue<File> filesToBeProcessedQueue = new ArrayBlockingQueue<File>(10);
private FileProcessor() {
}
public synchronized static FileProcessor getInstance() {
if (instance == null) {
instance = new FileProcessor();
}
return instance;
}
public void run() {
while (!filesToBeProcessedQueue.isEmpty()) {
processSyncFiles(filesToBeProcessedQueue.poll());
}
}
private void processSyncFiles(File inputFile) {
try {
HashMap<String, Boolean> outputConsolidation = new HashMap<String, Boolean>();
FileReader fileReader = new FileReader(inputFile);
List<InputBean> csvContentsList = CSVParser.readContentsFromCSV(fileReader, new InputBean());
for (InputBean inputBean : csvContentsList) {
boolean output = false;
// some business logic
outputConsolidation.put(inputBean.toString(), output);
}
} catch (Exception e) {
//logging
}
}
public synchronized Queue<File> getFilesToBeProcessedQueue() {
return filesToBeProcessedQueue;
}
}
FileProcessor Tasker(消费者计划程序)
此类为FileProcessor创建Tasker并按计划的时间间隔运行它。
public final class FileProcessorTasker extends TimerTask {
private static Long timeInterval = 5000l;
@Override
public void run() {
FileProcessor.getInstance().run();
}
public static void start() {
TimerTask timerTask = new FileProcessorTasker();
Timer timer = new Timer();
timer.schedule(timerTask, timeInterval, timeInterval);
}
public static void main(String[] args) {
FileProcessorTasker.start();
}
}
这两个程序都可以独立运行。但是当通过
filesToBeProcessedQueue
链接在一起时,它似乎无法正常工作。问题是
FTPClientPolling
创建FileProcessor
对象并将文件添加到队列。但是FileProcessorTasker
创建另一个FileProcessor
对象,该对象的队列大小为零。这两个不同的对象就是问题所在。当类为singleton
时,如何创建两个对象。我在单例实现中缺少什么吗? 最佳答案
首先,不要使用Timer
和TimerTask
。使用ExecutorService
进行多线程处理。
并在Singleton类中使用Eager Initialization。或仔细检查null的锁定,以使您真正成为Singleton。
FTPClientPolling.java
public class FTPClientPolling {
private static FTPClientPolling instance = new FTPClientPolling();
private FTPClientPolling() {
logger.info("Object created for Client Polling");
initializeFTPClient();
}
public static FTPClientPolling getInstance() {
return instance;
}
public static void initializeFTPClient() {
// initialize the values from properties file
}
public void startPolling() {
FTPClient ftpClient = null;
try {
//connecting to ftp server
//iterating the files in it
FTPFile[] filesList = ftpClient.listFiles();
for (FTPFile tmpFile : filesList) {
//..
File tempFile = File.createTempFile(tmpFile.getName(), null);
FileOutputStream fileOut = new FileOutputStream(tempFile);
ftpClient.retrieveFile(tmpFile.getName(), fileOut);
//adding the file to the Queue of the file processor
FileProcessor.getInstance().getFilesToBeProcessedQueue().add(tempFile);
}
if (ftpClient.isConnected())
ftpClient.disconnect();
} catch (Exception e) {
//logging
} finally {
//closing ftpclient
}
}
}
FileProcessor.java
public final class FileProcessor {
private static FileProcessor instance = new FileProcessor();
private Queue<File> filesToBeProcessedQueue = new ArrayBlockingQueue<File>(10);
private FileProcessor() {
}
public static FileProcessor getInstance() {
return instance;
}
public void run() {
while (!filesToBeProcessedQueue.isEmpty()) {
processSyncFiles(filesToBeProcessedQueue.poll());
}
}
private void processSyncFiles(File inputFile) {
try {
HashMap<String, Boolean> outputConsolidation = new HashMap<String, Boolean>();
FileReader fileReader = new FileReader(inputFile);
List<InputBean> csvContentsList = CSVParser.readContentsFromCSV(fileReader, new InputBean());
for (InputBean inputBean : csvContentsList) {
boolean output = false;
// some business logic
outputConsolidation.put(inputBean.toString(), output);
}
} catch (Exception e) {
//logging
}
}
public synchronized Queue<File> getFilesToBeProcessedQueue() {
return filesToBeProcessedQueue;
}
}
阅读此post以获得更多信息。