Search in sources :

Example 21 with IJobChangeEvent

use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by dbeaver.

the class KeepAliveJob method checkDataSourceAlive.

private void checkDataSourceAlive(final DBPDataSourceContainer dataSourceDescriptor) {
    if (!dataSourceDescriptor.isConnected()) {
        return;
    }
    final int keepAliveInterval = dataSourceDescriptor.getConnectionConfiguration().getKeepAliveInterval();
    if (keepAliveInterval <= 0) {
        return;
    }
    final String dsId = dataSourceDescriptor.getId();
    synchronized (this) {
        if (pingCache.contains(dsId)) {
            // Anyway - just skip it
            return;
        }
    }
    final DBPDataSource dataSource = dataSourceDescriptor.getDataSource();
    if (dataSource == null) {
        return;
    }
    Long lastCheckTime;
    synchronized (this) {
        lastCheckTime = checkCache.get(dsId);
    }
    if (lastCheckTime == null) {
        final Date connectTime = dataSourceDescriptor.getConnectTime();
        if (connectTime != null) {
            lastCheckTime = connectTime.getTime();
        }
    }
    if (lastCheckTime == null) {
        log.debug("Can't determine last check time for " + dsId);
        return;
    }
    long curTime = System.currentTimeMillis();
    if ((curTime - lastCheckTime) / 1000 > keepAliveInterval) {
        final PingJob pingJob = new PingJob(dataSource);
        pingJob.addJobChangeListener(new JobChangeAdapter() {

            @Override
            public void done(IJobChangeEvent event) {
                synchronized (KeepAliveJob.this) {
                    checkCache.put(dsId, System.currentTimeMillis());
                    pingCache.remove(dsId);
                }
            }
        });
        synchronized (this) {
            pingCache.add(dsId);
        }
        pingJob.schedule();
    }
}
Also used : JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent)

Example 22 with IJobChangeEvent

use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by dbeaver.

the class ExasolBaseTableToolDialog method executeSQL.

@Override
protected void executeSQL() {
    final String jobName = getShell().getText();
    final SQLScriptProgressListener<ExasolTableBase> scriptListener = getScriptListener();
    final List<ExasolTableBase> objects = getCheckedObjects();
    final Map<ExasolTableBase, List<String>> objectsSQL = new LinkedHashMap<>();
    for (ExasolTableBase object : objects) {
        final List<String> lines = new ArrayList<>();
        generateObjectCommand(lines, object);
        objectsSQL.put(object, lines);
    }
    final DataSourceJob job = new DataSourceJob(jobName, getExecutionContext()) {

        public Exception objectProcessingError;

        @SuppressWarnings("rawtypes")
        @Override
        protected IStatus run(final DBRProgressMonitor monitor) {
            final DataSourceJob curJob = this;
            DBeaverUI.asyncExec(new Runnable() {

                @Override
                public void run() {
                    scriptListener.beginScriptProcessing(curJob, objects);
                }
            });
            monitor.beginTask(jobName, objects.size());
            try (DBCSession session = getExecutionContext().openSession(monitor, DBCExecutionPurpose.UTIL, jobName)) {
                for (int i = 0; i < objects.size(); i++) {
                    if (monitor.isCanceled()) {
                        break;
                    }
                    final int objectNumber = i;
                    final ExasolTableBase object = objects.get(i);
                    monitor.subTask("Process " + DBUtils.getObjectFullName(object, DBPEvaluationContext.UI));
                    objectProcessingError = null;
                    DBeaverUI.asyncExec(new Runnable() {

                        @Override
                        public void run() {
                            scriptListener.beginObjectProcessing(object, objectNumber);
                        }
                    });
                    try {
                        final List<String> lines = objectsSQL.get(object);
                        for (String line : lines) {
                            try (final Statement statement = ((JDBCSession) session).getOriginal().createStatement()) {
                                int affectedRows = statement.executeUpdate(line);
                                Integer[] resultSetData = new Integer[] { affectedRows };
                                final LocalResultSet resultSet = new LocalResultSet<>(session, new JDBCStatementImpl<>((JDBCSession) session, statement, true));
                                resultSet.addColumn("ROWS_AFFECTED", DBPDataKind.NUMERIC);
                                resultSet.addRow((Object[]) resultSetData);
                                // Run in sync because we need result set
                                DBeaverUI.syncExec(new Runnable() {

                                    @Override
                                    public void run() {
                                        try {
                                            scriptListener.processObjectResults(object, null, resultSet);
                                        } catch (DBCException e) {
                                            objectProcessingError = e;
                                        }
                                    }
                                });
                                if (objectProcessingError != null) {
                                    break;
                                }
                            }
                        }
                    } catch (Exception e) {
                        objectProcessingError = e;
                    } finally {
                        DBeaverUI.asyncExec(new Runnable() {

                            @Override
                            public void run() {
                                scriptListener.endObjectProcessing(object, objectProcessingError);
                            }
                        });
                    }
                    monitor.worked(1);
                }
            } finally {
                monitor.done();
                DBeaverUI.asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        scriptListener.endScriptProcessing();
                    }
                });
            }
            return Status.OK_STATUS;
        }
    };
    job.setUser(false);
    job.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
        }
    });
    job.schedule();
}
Also used : LocalResultSet(org.jkiss.dbeaver.model.impl.local.LocalResultSet) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) Statement(java.sql.Statement) ExasolTableBase(org.jkiss.dbeaver.ext.exasol.model.ExasolTableBase) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) DataSourceJob(org.jkiss.dbeaver.runtime.jobs.DataSourceJob) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 23 with IJobChangeEvent

use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by dbeaver.

the class DataSourceManagementToolbar method changeDataSourceSelection.

private void changeDataSourceSelection(final DBPDataSourceContainer selectedDataSource) {
    if (connectionCombo == null || connectionCombo.isDisposed()) {
        return;
    }
    final IDataSourceContainerProviderEx dataSourceUpdater = getActiveDataSourceUpdater();
    if (dataSourceUpdater == null) {
        return;
    }
    final AbstractJob updateJob = new AbstractJob("Change active database") {

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            dataSourceUpdater.setDataSourceContainer(selectedDataSource);
            return Status.OK_STATUS;
        }
    };
    updateJob.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
            DBeaverUI.asyncExec(new Runnable() {

                @Override
                public void run() {
                    updateControls(false);
                }
            });
        }
    });
    updateJob.schedule();
}
Also used : AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent)

Example 24 with IJobChangeEvent

use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by dbeaver.

the class DataSourceManagementToolbar method fillDatabaseCombo.

private void fillDatabaseCombo() {
    if (databaseCombo != null && !databaseCombo.isDisposed()) {
        final DBPDataSourceContainer dsContainer = getDataSourceContainer();
        databaseCombo.setEnabled(dsContainer != null);
        if (dsContainer != null && dsContainer.isConnected()) {
            final DBPDataSource dataSource = dsContainer.getDataSource();
            if (dataSource != null) {
                synchronized (dbListReads) {
                    for (DatabaseListReader reader : dbListReads) {
                        if (reader.getExecutionContext().getDataSource() == dataSource) {
                            return;
                        }
                    }
                    DatabaseListReader databaseReader = new DatabaseListReader(dataSource.getDefaultContext(true));
                    databaseReader.addJobChangeListener(new JobChangeAdapter() {

                        @Override
                        public void done(final IJobChangeEvent event) {
                            DBeaverUI.syncExec(new Runnable() {

                                @Override
                                public void run() {
                                    fillDatabaseList((DatabaseListReader) event.getJob());
                                }
                            });
                        }
                    });
                    dbListReads.add(databaseReader);
                    databaseReader.schedule();
                }
            }
            curDataSourceContainer = new SoftReference<>(dsContainer);
        } else {
            curDataSourceContainer = null;
            databaseCombo.removeAll();
        }
    }
}
Also used : JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent)

Example 25 with IJobChangeEvent

use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by dbeaver.

the class OpenObjectConsoleHandler method openConsole.

protected void openConsole(IWorkbenchWindow workbenchWindow, DBRRunnableWithResult<String> generator, DBPDataSourceContainer ds, String title, boolean doRun) {
    DBeaverUI.runInUI(workbenchWindow, generator);
    String sql = generator.getResult();
    SQLEditor editor = OpenHandler.openSQLConsole(workbenchWindow, ds, title, sql);
    if (editor != null) {
        AbstractJob execJob = new AbstractJob("Execute SQL in console") {

            @Override
            protected IStatus run(DBRProgressMonitor monitor) {
                // So let's give it a chance and wait for 10 seconds
                for (int i = 0; i < 100; i++) {
                    if (editor.getExecutionContext() != null) {
                        break;
                    }
                    RuntimeUtils.pause(100);
                }
                return Status.OK_STATUS;
            }
        };
        execJob.addJobChangeListener(new JobChangeAdapter() {

            @Override
            public void done(IJobChangeEvent event) {
                DBeaverUI.syncExec(new Runnable() {

                    @Override
                    public void run() {
                        if (doRun) {
                            editor.processSQL(false, false);
                        }
                    }
                });
            }
        });
        execJob.schedule();
    }
}
Also used : AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) SQLEditor(org.jkiss.dbeaver.ui.editors.sql.SQLEditor) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent)

Aggregations

IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)166 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)157 Job (org.eclipse.core.runtime.jobs.Job)44 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)43 IStatus (org.eclipse.core.runtime.IStatus)36 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)30 ArrayList (java.util.ArrayList)13 IFile (org.eclipse.core.resources.IFile)13 CoreException (org.eclipse.core.runtime.CoreException)12 DataSourceJob (org.jkiss.dbeaver.runtime.jobs.DataSourceJob)12 Status (org.eclipse.core.runtime.Status)11 IOException (java.io.IOException)10 DBException (org.jkiss.dbeaver.DBException)10 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)10 File (java.io.File)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 IJobChangeListener (org.eclipse.core.runtime.jobs.IJobChangeListener)8 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)8 Composite (org.eclipse.swt.widgets.Composite)7 IProject (org.eclipse.core.resources.IProject)6