Search in sources :

Example 36 with JobChangeAdapter

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

the class QueryLogViewer method preferenceChange.

@Override
public synchronized void preferenceChange(PreferenceChangeEvent event) {
    if (event.getProperty().startsWith(QMConstants.PROP_PREFIX)) {
        // So here we just schedule single refresh job
        if (configRefreshJob == null) {
            configRefreshJob = new ConfigRefreshJob();
            configRefreshJob.schedule(250);
            configRefreshJob.addJobChangeListener(new JobChangeAdapter() {

                @Override
                public void done(IJobChangeEvent event) {
                    configRefreshJob = null;
                }
            });
        }
    }
}
Also used : JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent)

Example 37 with JobChangeAdapter

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

the class DataSourceHandler method disconnectDataSource.

public static void disconnectDataSource(DBPDataSourceContainer dataSourceContainer, @Nullable final Runnable onFinish) {
    // Save users
    for (DBPDataSourceUser user : dataSourceContainer.getUsers()) {
        if (user instanceof ISaveablePart) {
            if (!SaveChangesHandler.validateAndSave(VoidProgressMonitor.INSTANCE, (ISaveablePart) user)) {
                return;
            }
        }
    }
    if (!checkAndCloseActiveTransaction(dataSourceContainer)) {
        return;
    }
    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 DisconnectJob disconnectJob = new DisconnectJob(dataSourceDescriptor);
        disconnectJob.addJobChangeListener(new JobChangeAdapter() {

            @Override
            public void done(IJobChangeEvent event) {
                IStatus result = disconnectJob.getConnectStatus();
                if (onFinish != null) {
                    onFinish.run();
                } else if (!result.isOK()) {
                    UIUtils.showErrorDialog(null, disconnectJob.getName(), null, result);
                }
            }
        });
        // Run in UI thread to update actions (some Eclipse magic)
        DBeaverUI.asyncExec(new Runnable() {

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

Example 38 with JobChangeAdapter

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

the class DataSourceInvalidateHandler method execute.

public static void execute(final Shell shell, final DBCExecutionContext context) {
    if (context != null) {
        //final DataSourceDescriptor dataSourceDescriptor = (DataSourceDescriptor) context;
        if (!ArrayUtils.isEmpty(Job.getJobManager().find(context.getDataSource().getContainer()))) {
            // Already connecting/disconnecting - just return
            return;
        }
        final InvalidateJob invalidateJob = new InvalidateJob(context);
        invalidateJob.addJobChangeListener(new JobChangeAdapter() {

            @Override
            public void done(IJobChangeEvent event) {
                StringBuilder message = new StringBuilder();
                Throwable error = null;
                int totalNum = 0, connectedNum = 0, aliveNum = 0;
                for (InvalidateJob.ContextInvalidateResult result : invalidateJob.getInvalidateResults()) {
                    totalNum++;
                    if (result.error != null) {
                        error = result.error;
                    }
                    switch(result.result) {
                        case CONNECTED:
                        case RECONNECTED:
                            connectedNum++;
                            break;
                        case ALIVE:
                            aliveNum++;
                            break;
                        default:
                            break;
                    }
                }
                if (connectedNum > 0) {
                    message.insert(0, "Connections reopened: " + connectedNum + " (of " + totalNum + ")");
                } else if (message.length() == 0) {
                    message.insert(0, "All connections (" + totalNum + ") are alive!");
                }
                if (error != null) {
                    //                        UIUtils.showErrorDialog(
                    //                            shell,
                    //                            "Invalidate data source [" + context.getDataSource().getContainer().getName() + "]",
                    //                            "Error while connecting to the datasource",// + "\nTime spent: " + RuntimeUtils.formatExecutionTime(invalidateJob.getTimeSpent()),
                    //                            error);
                    DBeaverUI.syncExec(new Runnable() {

                        @Override
                        public void run() {
                        }
                    });
                    final DBPDataSourceContainer container = context.getDataSource().getContainer();
                    final Throwable dialogError = error;
                    final Integer result = new UITask<Integer>() {

                        @Override
                        protected Integer runTask() {
                            ConnectionLostDialog clDialog = new ConnectionLostDialog(shell, container, dialogError, "Disconnect");
                            return clDialog.open();
                        }
                    }.execute();
                    if (result == IDialogConstants.STOP_ID) {
                        // Disconnect - to notify UI and reflect model changes
                        new DisconnectJob(container).schedule();
                    } else if (result == IDialogConstants.RETRY_ID) {
                        execute(shell, context);
                    }
                } else {
                    log.info(message);
                }
            }
        });
        invalidateJob.schedule();
    }
}
Also used : UITask(org.jkiss.dbeaver.ui.UITask) ConnectionLostDialog(org.jkiss.dbeaver.ui.dialogs.ConnectionLostDialog) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) DisconnectJob(org.jkiss.dbeaver.runtime.jobs.DisconnectJob) InvalidateJob(org.jkiss.dbeaver.runtime.jobs.InvalidateJob) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 39 with JobChangeAdapter

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

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, null, 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 40 with JobChangeAdapter

use of org.eclipse.core.runtime.jobs.JobChangeAdapter 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, null, getExecutionContext()) {

        public Exception objectProcessingError;

        @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 T 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 DBCStatement statement = DBUtils.makeStatement(session, line, false)) {
                                if (statement.executeStatement()) {
                                    try (DBCResultSet resultSet = statement.openResultSet()) {
                                        // Run in sync because we need result set
                                        DBeaverUI.syncExec(new Runnable() {

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

                                        @Override
                                        public void run() {
                                            try {
                                                scriptListener.processObjectResults(object, statement, null);
                                            } catch (DBCException e) {
                                                objectProcessingError = e;
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    } 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 : JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) DataSourceJob(org.jkiss.dbeaver.runtime.jobs.DataSourceJob) SWT(org.eclipse.swt.SWT) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Aggregations

IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)52 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)52 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)20 Job (org.eclipse.core.runtime.jobs.Job)20 IStatus (org.eclipse.core.runtime.IStatus)12 IFile (org.eclipse.core.resources.IFile)7 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)6 File (java.io.File)5 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)5 LoadMetadataRepositoryJob (org.eclipse.equinox.p2.ui.LoadMetadataRepositoryJob)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 QAXmlHandler (net.heartsome.cat.ts.core.qa.QAXmlHandler)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 QATUDataBean (net.heartsome.cat.ts.core.qa.QATUDataBean)3 Status (org.eclipse.core.runtime.Status)3 DataSourceJob (org.jkiss.dbeaver.runtime.jobs.DataSourceJob)3 AnalyseBundleResolutionJob (bndtools.tasks.AnalyseBundleResolutionJob)2 FileOutputStream (java.io.FileOutputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2