通过反射执行方法时线程挂起

时间:2013-08-02 15:02:38

标签: java concurrency

我有一个基于反射运行新线程的执行程序服务。我有一个方法,当运行时不退出线程并将挂起程序执行。我不确定为什么会这样,有人能指出我所缺少的东西吗?

另外,如果那里的任何并发专家都注意到我可能遇到的任何问题,请告诉我,我在并发方面相当绿色....

注意:

  • 将重构connectToFTP方法以在将来返回ChannelSftp。
  • downloadFromFTP在结束时返回false,因为方法未完成。我觉得这是导致线程挂起的方法。我只是不知道为什么。
  • 该方法的目标是列出SFTP目录中的每个文件。
来自NetworkingShopCa的

    @Override
    public Object connectToFTP(String username, String password, String host, String port, FtpTypes ftpTypes) {
        switch(ftpTypes){
        case FTP:
            LOGGER.error("Plain FTP is not implemented yet (if ever)");
            break;
        case FTPS:
            FTPSClient client = new FTPSClient();
            client.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
            try {
                client.connect(host);           
                client.enterLocalPassiveMode();
                client.login(username, password);           
            } catch (IOException e) {               
                LOGGER.error(e.toString());
            }
            return client;
        case SFTP:
            JSch jsch = new JSch();
            Session session = null;

            try {
                session = jsch.getSession(username, host);          
                session.setConfig("StrictHostKeyChecking", "no");
                session.setPassword(password);
                session.connect();  
                Channel channel = session.openChannel("sftp");
                channel.connect();
                ChannelSftp sftpChannel = (ChannelSftp) channel;
                return sftpChannel;

            } catch (JSchException e) {
                // TODO Auto-generated catch block
                LOGGER.error(e.toString());
            }
            break;          
        default:
            LOGGER.error("Invalid FtpType");
            break;          
        }

        return false;
    }
     @Override
    public boolean downloadFromFTP(String directory, String filename, boolean downloadAll,Object activeConnection) {
            if(activeConnection instanceof ChannelSftp){
                ChannelSftp sftpChannel = (ChannelSftp) activeConnection;
                try {
                    sftpChannel.cd(directory);
                    //List our files within the directory
                    Vector vv = sftpChannel.ls(directory);
                    if (vv != null) {
                        for (int ii = 0; ii < vv.size(); ii++) {
                            Object obj = vv.elementAt(ii);
                            if (obj instanceof ChannelSftp.LsEntry) {
                                LOGGER.debug("[" + ((LsEntry) obj).getFilename() + "]");
                            }
                        }
                    }
                } catch (SftpException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
            return false;
        }

从主要

runtimes.put(config.getInt("ESE_PRIORITY"),"RUN_ESE");
ExecutorService threadPool = Executors.newFixedThreadPool(totalRunnables);

LOGGER.info("Executing runtimes in order of priority.");
for(final int priority : runtimes.keySet()){
    if(!threadPool.isShutdown() && !threadPool.isTerminated()){
        //run the method denoted by the property
        final java.lang.reflect.Method method = m.getClass().getMethod(runtimes.get(priority));
        Future<?> f = threadPool.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    method.invoke(m);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        });
        f.get();
    }
}

public void RUN_ESE(){
    LOGGER.info("Running ESE");
    Networking networking = new NetworkingShopCa();
    networking.downloadFromFTP("/toclient/order/processed", "", true, networking.connectToFTP("user", "password", "host", "", FtpTypes.SFTP));

}

- 编辑 -

在调试器下载完全执行FromFTP后,返回该方法并在挂起时关闭该runnable:

enter image description here

ThreadPoolExecutor.java的第1153行显示:

afterExecute(task, thrown);

有什么想法吗?我的构建环境值得:

  • Ubuntu 13.04 64位
  • 附带来源的OpenJDK 7(默认为ubuntu)。
  • Eclipse kepler

0 个答案:

没有答案