user3102235
user3102235

Reputation: 11

When I run pg_restore via process in java application, application freeze

When I run pg_restore via process in java application, application freeze and info message don't show, but process successfully finished. Why it so? I use PostgreSQL 9.2.

Method's code:

private void restoreDB() {

    JFileChooser fileopen = new JFileChooser();   
    FileNameExtensionFilter filter = new FileNameExtensionFilter("DB FILES", "sql");
    fileopen.addChoosableFileFilter(filter);
    fileopen.setCurrentDirectory(new java.io.File("C:/backups/"));
    fileopen.setDialogTitle("Выберите папку для резервной копии");
    fileopen.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fileopen.setAcceptAllFileFilterUsed(false);   
    int ret = fileopen.showDialog(null, "Restore DB");                
    if (ret == JFileChooser.APPROVE_OPTION) {
        File file = fileopen.getSelectedFile();
        try {
            String path = file.getAbsolutePath();
            System.out.println(path);
            String user = "postgres";
            String dbName = "Auto";
            String executeCmd = "pg_restore -i -U " + user + " -d " + dbName+" -v "+ path;
            Process runtimeProcess;
            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            if (processComplete == 0) {
                javax.swing.JOptionPane.showMessageDialog(null, "Successfull!");
                log.info("Successfull!");
            } else {
                javax.swing.JOptionPane.showMessageDialog(null, "Unsuccessfully");
                log.info("Unsuccessfully");
            }
    }catch (Exception ex) {
        ex.printStackTrace();
    }
}

}

Upvotes: 0

Views: 262

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168845

Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling Thread.sleep(n) implement a Swing Timer for repeating tasks or a SwingWorker for long running tasks. See Concurrency in Swing for more details.

Upvotes: 3

Related Questions