生产者消费者模型如情景不起作用

时间:2016-02-26 09:41:09

标签: java producer-consumer timertask

这就是场景。我必须以定时间隔轮询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 (制作人Tasker)

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 (Consumer Scheduler) 此类为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的对象,并将该文件添加到Queue中。 但是 FileProcessorTasker会创建另一个FileProcessor对象,其队列大小为零。这两个不同的对象就是问题所在。当类为singleton时,如何创建两个对象。我在单身实现中遗漏了什么吗?

1 个答案:

答案 0 :(得分:1)

首先,请勿使用TimerTimerTask。使用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以获取更多信息。