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();
}
}
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();
}
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();
}
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();
}
}
}
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();
}
}
Aggregations