Search in sources :

Example 66 with IJobChangeEvent

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

the class ObjectListControl method loadData.

public void loadData(boolean lazy) {
    if (this.loadingJob != null) {
        int dataLoadUpdatePeriod = 200;
        int dataLoadTimes = getDataLoadTimeout() / dataLoadUpdatePeriod;
        try {
            for (int i = 0; i < dataLoadTimes; i++) {
                Thread.sleep(dataLoadUpdatePeriod);
                if (this.loadingJob == null) {
                    break;
                }
            }
        } catch (InterruptedException e) {
        // interrupted
        }
        if (loadingJob != null) {
            UIUtils.showMessageBox(getShell(), "Load", "Service is busy", SWT.ICON_WARNING);
            return;
        }
        return;
    }
    getListPropertySource();
    clearLazyCache();
    this.lazyLoadCanceled = false;
    if (lazy) {
        // start loading service
        synchronized (this) {
            this.loadingJob = createLoadService();
            if (this.loadingJob != null) {
                this.loadingJob.addJobChangeListener(new JobChangeAdapter() {

                    @Override
                    public void done(IJobChangeEvent event) {
                        loadingJob = null;
                    }
                });
                this.loadingJob.schedule(LAZY_LOAD_DELAY);
            }
        }
    } else {
        // Load data synchronously
        final LoadingJob<Collection<OBJECT_TYPE>> loadService = createLoadService();
        if (loadService != null) {
            loadService.syncRun();
        }
    }
}
Also used : JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent)

Example 67 with IJobChangeEvent

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

the class ObjectListControl method addLazyObject.

private synchronized void addLazyObject(OBJECT_TYPE object, ObjectColumn column) {
    if (lazyObjects == null) {
        lazyObjects = new LinkedHashMap<>();
    }
    List<ObjectColumn> objectColumns = lazyObjects.get(object);
    if (objectColumns == null) {
        objectColumns = new ArrayList<>();
        lazyObjects.put(object, objectColumns);
    }
    if (!objectColumns.contains(column)) {
        objectColumns.add(column);
    }
    if (lazyLoadingJob == null) {
        lazyLoadingJob = new LazyLoaderJob();
        lazyLoadingJob.addJobChangeListener(new JobChangeAdapter() {

            @Override
            public void done(IJobChangeEvent event) {
                synchronized (ObjectListControl.this) {
                    if (lazyObjects == null || lazyObjects.isEmpty()) {
                        lazyLoadingJob = null;
                    } else {
                        lazyLoadingJob.schedule(LAZY_LOAD_DELAY);
                    }
                }
            }
        });
        lazyLoadingJob.schedule(LAZY_LOAD_DELAY);
    }
}
Also used : JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent)

Example 68 with IJobChangeEvent

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

the class GenerateMultiSQLDialog method executeSQL.

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

        Exception objectProcessingError;

        @Override
        protected IStatus run(final DBRProgressMonitor monitor) {
            final DataSourceJob curJob = this;
            UIUtils.asyncExec(() -> scriptListener.beginScriptProcessing(curJob, objects));
            monitor.beginTask(jobName, objects.size());
            try (DBCSession session = getExecutionContext().openSession(monitor, DBCExecutionPurpose.UTIL, jobName)) {
                if (isRunInSeparateTransaction()) {
                    commitChanges(session);
                }
                for (int i = 0; i < objects.size(); i++) {
                    if (monitor.isCanceled()) {
                        break;
                    }
                    final int objectNumber = i;
                    final T object = objects.get(i);
                    monitor.subTask("Process " + DBUtils.getObjectFullName(object, DBPEvaluationContext.UI));
                    objectProcessingError = null;
                    UIUtils.asyncExec(() -> scriptListener.beginObjectProcessing(object, objectNumber));
                    try {
                        final List<String> lines = objectsSQL.get(object);
                        for (String line : lines) {
                            try (final DBCStatement statement = DBUtils.makeStatement(session, line, false)) {
                                if (statement.executeStatement()) {
                                    try (DBCResultSet resultSet = statement.openResultSet()) {
                                        // Run in sync because we need result set
                                        UIUtils.syncExec(() -> {
                                            try {
                                                scriptListener.processObjectResults(object, statement, resultSet);
                                            } catch (DBCException e) {
                                                objectProcessingError = e;
                                            }
                                        });
                                    }
                                    if (objectProcessingError != null) {
                                        break;
                                    }
                                } else {
                                    UIUtils.syncExec(() -> {
                                        try {
                                            scriptListener.processObjectResults(object, statement, null);
                                        } catch (DBCException e) {
                                            objectProcessingError = e;
                                        }
                                    });
                                }
                            }
                        }
                    } catch (Exception e) {
                        objectProcessingError = e;
                    } finally {
                        UIUtils.asyncExec(() -> scriptListener.endObjectProcessing(object, objectProcessingError));
                    }
                    monitor.worked(1);
                }
                if (isRunInSeparateTransaction()) {
                    commitChanges(session);
                }
            } finally {
                monitor.done();
                UIUtils.asyncExec(scriptListener::endScriptProcessing);
            }
            return Status.OK_STATUS;
        }
    };
    job.setUser(false);
    job.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
            if (needsRefreshOnFinish()) {
                List<T> objectToRefresh = new ArrayList<>(selectedObjects);
                UIUtils.asyncExec(() -> {
                    try {
                        UIUtils.runInProgressDialog(monitor -> {
                            monitor.beginTask("Refresh objects", objectToRefresh.size());
                            for (T object : objectToRefresh) {
                                try {
                                    DBNDatabaseNode objectNode = DBNUtils.getNodeByObject(object);
                                    if (objectNode != null) {
                                        objectNode.refreshNode(monitor, DBNEvent.FORCE_REFRESH);
                                    }
                                } catch (Exception e) {
                                    log.error("Error refreshing object '" + object.getName() + "'", e);
                                }
                            }
                            monitor.done();
                        });
                    } catch (InvocationTargetException e) {
                        DBWorkbench.getPlatformUI().showError("Objects refresh", "Error refreshing navigator objects", e);
                    }
                });
            }
        }
    });
    job.schedule();
}
Also used : DBWorkbench(org.jkiss.dbeaver.runtime.DBWorkbench) java.util(java.util) IWorkbenchPartSite(org.eclipse.ui.IWorkbenchPartSite) IDialogConstants(org.eclipse.jface.dialogs.IDialogConstants) IDialogSettings(org.eclipse.jface.dialogs.IDialogSettings) Table(org.eclipse.swt.widgets.Table) NotNull(org.jkiss.code.NotNull) IStatus(org.eclipse.core.runtime.IStatus) DBNUtils(org.jkiss.dbeaver.model.navigator.DBNUtils) DataSourceJob(org.jkiss.dbeaver.runtime.jobs.DataSourceJob) DBeaverIcons(org.jkiss.dbeaver.ui.DBeaverIcons) Composite(org.eclipse.swt.widgets.Composite) UIUtils(org.jkiss.dbeaver.ui.UIUtils) Log(org.jkiss.dbeaver.Log) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) GridData(org.eclipse.swt.layout.GridData) DBNEvent(org.jkiss.dbeaver.model.navigator.DBNEvent) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) TableItem(org.eclipse.swt.widgets.TableItem) DBUtils(org.jkiss.dbeaver.model.DBUtils) org.jkiss.dbeaver.model.exec(org.jkiss.dbeaver.model.exec) Status(org.eclipse.core.runtime.Status) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBIcon(org.jkiss.dbeaver.model.DBIcon) SWT(org.eclipse.swt.SWT) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DBPEvaluationContext(org.jkiss.dbeaver.model.DBPEvaluationContext) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) SWT(org.eclipse.swt.SWT) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DataSourceJob(org.jkiss.dbeaver.runtime.jobs.DataSourceJob) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 69 with IJobChangeEvent

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

the class GenerateSQLDialog method executeSQL.

protected void executeSQL() {
    final String jobName = getShell().getText();
    final String[] scriptLines = generateSQLScript();
    DataSourceJob job = new DataSourceJob(jobName, executionContext) {

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            try (DBCSession session = getExecutionContext().openSession(monitor, DBCExecutionPurpose.UTIL, jobName)) {
                for (String line : scriptLines) {
                    DBCStatement statement = DBUtils.makeStatement(session, line, false);
                    try {
                        statement.executeStatement();
                    } finally {
                        statement.close();
                    }
                }
            } catch (DBCException e) {
                return GeneralUtils.makeExceptionStatus(e);
            }
            return Status.OK_STATUS;
        }
    };
    job.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
            if (event.getResult().isOK()) {
                if (onSuccess != null) {
                    onSuccess.run();
                }
            }
        }
    });
    job.schedule();
}
Also used : DataSourceJob(org.jkiss.dbeaver.runtime.jobs.DataSourceJob) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent)

Example 70 with IJobChangeEvent

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

the class DataSourceHandler method connectToDataSource.

/**
 * Connects datasource
 * @param monitor progress monitor or null. If nul then new job will be started
 * @param dataSourceContainer    container to connect
 * @param onFinish               finish handler
 */
public static void connectToDataSource(@Nullable DBRProgressMonitor monitor, @NotNull DBPDataSourceContainer dataSourceContainer, @Nullable final DBRProgressListener onFinish) {
    if (dataSourceContainer instanceof DataSourceDescriptor && !dataSourceContainer.isConnected()) {
        final DataSourceDescriptor dataSourceDescriptor = (DataSourceDescriptor) dataSourceContainer;
        if (!ArrayUtils.isEmpty(Job.getJobManager().find(dataSourceDescriptor))) {
            // Already connecting/disconnecting - just return
            return;
        }
        final ConnectJob connectJob = new ConnectJob(dataSourceDescriptor);
        final JobChangeAdapter jobChangeAdapter = new JobChangeAdapter() {

            @Override
            public void done(IJobChangeEvent event) {
                IStatus result = connectJob.getConnectStatus();
                if (result.isOK()) {
                    if (!dataSourceDescriptor.isSavePassword()) {
                    // Rest password back to null
                    // TODO: to be correct we need to reset password info.
                    // but we need a password to open isolated contexts (e.g. for data export)
                    // Currently it is not possible to ask for password from isolation context opening
                    // procedure. We need to do something here...
                    // dataSourceDescriptor.getConnectionConfiguration().setUserName(oldName);
                    // dataSourceDescriptor.getConnectionConfiguration().setUserPassword(oldPassword);
                    }
                }
                if (onFinish != null) {
                    onFinish.onTaskFinished(result);
                } else if (!result.isOK()) {
                    UIUtils.asyncExec(() -> DBWorkbench.getPlatformUI().showError(connectJob.getName(), // NLS.bind(CoreMessages.runtime_jobs_connect_status_error, dataSourceContainer.getName()),
                    null, result));
                }
            }
        };
        if (monitor != null) {
            connectJob.runSync(monitor);
            jobChangeAdapter.done(new IJobChangeEvent() {

                @Override
                public long getDelay() {
                    return 0;
                }

                @Override
                public Job getJob() {
                    return connectJob;
                }

                @Override
                public IStatus getResult() {
                    return connectJob.getConnectStatus();
                }

                public IStatus getJobGroupResult() {
                    return null;
                }
            });
        } else {
            connectJob.addJobChangeListener(jobChangeAdapter);
            // Schedule in UI because connect may be initiated during application startup
            // and UI is still not initiated. In this case no progress dialog will appear
            // to be sure run in UI async
            UIUtils.asyncExec(new Runnable() {

                @Override
                public void run() {
                    connectJob.schedule();
                }
            });
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) ConnectJob(org.jkiss.dbeaver.runtime.jobs.ConnectJob) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) DisconnectJob(org.jkiss.dbeaver.runtime.jobs.DisconnectJob) ConnectJob(org.jkiss.dbeaver.runtime.jobs.ConnectJob) Job(org.eclipse.core.runtime.jobs.Job) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor)

Aggregations

IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)172 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)163 Job (org.eclipse.core.runtime.jobs.Job)46 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)45 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 File (java.io.File)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)12 CoreException (org.eclipse.core.runtime.CoreException)12 DataSourceJob (org.jkiss.dbeaver.runtime.jobs.DataSourceJob)12 Status (org.eclipse.core.runtime.Status)11 IJobChangeListener (org.eclipse.core.runtime.jobs.IJobChangeListener)11 IOException (java.io.IOException)10 DBException (org.jkiss.dbeaver.DBException)10 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)10 IProject (org.eclipse.core.resources.IProject)9 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)8 Composite (org.eclipse.swt.widgets.Composite)7