public abstract class ConcurrentAdapter
extends Object
implements Runnable, Callable<Object>
Do: separate UI update code and service execution code
Do not: put UI code in the service execution thread and vice versa
In the example below the particular implementation of the ConcurrentAdapter is executed when user clicks on Test Connection button:
public void testConnection() { ConcurrentFeature concurrentFeature = new ConcurrentFeature(true); concurrentFeature.setMessage(TestConnectionResource.TESTING_CONNECTION .getValue()); ConcurrentAdapter concurrentAdapter = new ConcurrentAdapter() { String status = ""; public void canceled() { } public void done() { getField().setText(status); if (TestConnectionResource.CONNECTION_IS_OK.getValue() .equalsIgnoreCase(status)) getField().setForeground(Color.BLACK); else getField().setForeground(Color.RED); } public void execute() throws Exception { TestService testService = ServiceFactory .getService(TestService.class); status = testService.testConnection(_connectionProvider, _connectionParamsProvider.getConnectionParams()); } public void onException(Throwable ex) { DefaultExceptionHandler.instance().handleException( Logger.SEVERE, this, TestConnectionResource.ERROR_TESTING_CONNECTION .getValue(), ex); } }; concurrentAdapter.executeTask(concurrentFeature); }
ConcurrentFeature
Modifier and Type | Class and Description |
---|---|
static class |
ConcurrentAdapter.HeadlessConcurrentAdapter |
Constructor and Description |
---|
ConcurrentAdapter() |
Modifier and Type | Method and Description |
---|---|
Object |
call() |
abstract void |
canceled()
Executed when service thread is interrupted before it finished.
|
abstract void |
done()
Executed when service thread is finished.
|
abstract void |
execute()
Executes service thread.
|
void |
executeTask()
Execute task and waits until service thread is finished or interrupted and UI is updated.
|
void |
executeTask(ConcurrentFeature concurrentFeature)
Execute task and waits until service thread is finished or interrupted and UI is updated.
|
ConcurrentFeature |
getConcurrentFeature()
Gets the concurrent feature.
|
abstract void |
onException(Throwable ex)
Executed if service thread throws an exception.
|
void |
progress(Object data)
Display progress.
|
void |
run() |
void |
start()
Start.
|
public final Object call() throws Exception
call
in interface Callable<Object>
Exception
public abstract void canceled()
public abstract void done()
public abstract void execute() throws Exception
Exception
- in case of any errorpublic void executeTask()
public void executeTask(ConcurrentFeature concurrentFeature)
concurrentFeature
- the ConcurrentFeaturepublic ConcurrentFeature getConcurrentFeature()
public abstract void onException(Throwable ex)
ex
- the expublic void progress(Object data)
public final void run()
run
in interface Runnable
public void start()
Copyright © 2010-2020 Toolsverse. All Rights Reserved.