Presentation is loading. Please wait.

Presentation is loading. Please wait.

EsprIT 7.x 1 AsyncTask Framework Programming asynchroneous activity - as local and remote tasks -

Similar presentations


Presentation on theme: "EsprIT 7.x 1 AsyncTask Framework Programming asynchroneous activity - as local and remote tasks -"— Presentation transcript:

1 EsprIT 7.x 1 AsyncTask Framework Programming asynchroneous activity - as local and remote tasks -

2 EsprIT 7.x 2 TnTsoft Products Application Suite DBObject Suite Server Suite DynamIT Customer Applications INCA, BDEIS, HiLocate, … DBObject Suite

3 EsprIT 7.x 3 Application Suite LoggingLaunchingAsync TaskTranslationXML/HTMLData ParsingGUI Comps/FieldsUtilities…

4 EsprIT 7.x 4 What‘s missing in Java? Java Concurrency Framework Executor (Threadpool) Wrap async code in Runnable or Callable Submit into a pool – and get a Future Future.cancel() Missing functionality Observing a running task (state changes) How to get progress messages of a task? Cancel a task (with task specific cleanup) Manage many running tasks Manage remote tasks – running on server Tasks with sub-tasks with sub-tasks with sub-tasks… Async interaction with GUI (SwingWorker is very raw) Run tasks in a serie (Workflow)

5 EsprIT 7.x 5 AsyncTask AsyncTask Instances Any async code is implemented in a AsyncTask Instance Override execute() method Submit AsyncTask and receive a TaskId TaskId (in contrast to Future) is serializable and thus also works remotely TaskManager (in ApplicationContext) Contains a default threadpool Contains a registry for running tasks based on TaskId Task remains accessible by TaskId Cancel by TaskId Manages GUI Activity-Lists

6 EsprIT 7.x 6 Submitting a Task Future Thread-Pool Runnable Java Concurrency Framework TaskId Thread-Pool Task EsprIT AsyncTask Framework TaskManager 1Task1 2Task2 3Task3 4Task4 cancel Manages tasks of all pools. Cancellable by TaskId

7 EsprIT 7.x 7 RunStateFlag AsyncTask has a RunStateFlag Reflects the task‘s current state INITIALIZED STARTED PROCEEDED (n times) FAILED (on error) CANCELLED (on cancellation) SUCCEEDED FINISHED Task may react by ist own on state changes, just override performOnStarted() performOnFailed() performOnCancelled() performOnSucceeded() performOnFinished()

8 EsprIT 7.x 8 TaskMonitor TaskMonitor makes an AsyncTask observable Monitor methods are called on state changes taskProceeded(…) taskFailed(…) taskCancelled(…) Monitor implements async reaction on state changes Can add one or more TaskMonitors to a task SwingTaskMonitor Implements GUI-synchroneous reaction on state changes Converts async calls to synchroneous TaskEvents Allows to register as Listener for TaskEvents TaskEvent notifies the GUI about state change (i.e. ProgressBar) TaskEvent contains RunStateFlag and additional state-specific info progress step=1, maxSteps=100, message=„extracting xxx.yy“

9 EsprIT 7.x 9 Monitoring a Task TaskMonitor taskStarted() taskProceeded() taskFailed() taskCancelled() taskFinished() SwingTaskMonitor taskStarted() taskProceeded() taskFailed() taskCancelled() taskFinished() TaskEvent Listeners TaskEvent Listener doneStarted(e) doneProceeded(e) doneFailed(e) doneCancelled(e) doneFinished(e) Asynchroneous notification GUI synchroneous notification

10 EsprIT 7.x 10 Monitoring Task Activity GUI Activity Monitor Provides overview about running tasks Allows for direct cancelling any or all tasks

11 EsprIT 7.x 11 Summary AsyncTask Tasks are managed based on TaskId Tasks are always accessible/cancellable by TaskId You always know which tasks are running Easy to render running tasks in lists/tables Easy to drive ProgressBars React on RunState changes Task may define its own reaction on cancellation/failure Its easy to react on async state changes (TaskMonitor) Its easy to react on sync state changes (SwingTaskMonitor) A single task can drive many monitors

12 EsprIT 7.x 12 Task Features Naming/Typing Task always have a name and type (icon) for rendering Reusablility Task instances can be reused to run multiple times (saves resources) Cancellation Task may change it‘s cancellable-state during run (critical phases) Stopping Simply throw StopException if you want to terminate a task early Priority Task may run with a defined thread priority (degrading other tasks) Threadpool assignment Any task may run in any threadpool Task knows its target-threadpool by its own

13 EsprIT 7.x 13 AsyncExecutable AsyncExecutable is the smallest executable unit May derive from any class, just implement AsyncExecutable May be executed async by an AsycTask instance May also execute sync (by calling execute()) May be Serializable AsycTask instance Derives from AbstractTask implements AsyncExecutable Is submittable into a threadpool Provides a new TaskId on each submit Is NOT Serializable AsyncExecutableAdapter Is default implementation of AsyncExecutable Sub-Task may derive from AsyncExecutableAdapter

14 EsprIT 7.x 14 Sub-Tasks Each AsyncTask can run an AsyncExecutable Sub-Task executeSubTask(AsyncExecutable) Sub-Task uses same RunStateFlag and drives same Monitors Main task blocks until Sub-Task has finished Main task breaks when Sub-Task fails or was cancelled Main task may run many Sub-Tasks one after the other TaskCollection Is a task by its own that runs a set of Sub-Tasks in a serie addTask(AsyncExecutable) Knows how many tasks to run => can drive a double ProgressBar Stops when any of the Sub-Tasks fails or is cancelled

15 EsprIT 7.x 15 Child-Tasks A task can run another task as Child-Task executeChildTask(AsyncTask) A Child-Task has its own execution Thread => own TaskId Child-Task may run in any threadpool Child-Task uses same RunStateFlag and drives same Monitors Main task blocks until Child-Task has finished Main task breaks when Child-Task fails or was cancelled Main task may run many Child-Tasks one after the other Any Child-Task may run another Sub/Child-Task by its own Any Sub-Task may run another Sub/Child-Task by its own

16 EsprIT 7.x 16 Task Trees You may build task-trees at any nesting level

17 EsprIT 7.x 17 Summary SubTasks AsyncExecutable You can make any class executing asynchroneously Examples: DataParser, ZipArchiver, XmlWriter, etc… TaskCollections Allows for logical grouping of async code Child/Sub-Tasks Allows for nesting tasks at any nesting level Very flexible structuring of async code Well controlled execution in any constellation Built-in error handling Robust cancellation at any time

18 EsprIT 7.x 18 Workflows WorkflowTask Special task that switches between sync and async execution @Sync enterTask() may access GUI @Async executeAsync()access to GUI is forbidden @Sync exitTask()may access GUI Workflow Is a main task that runs many WorkflowTasks in a serie addTask(WorkflowTask) Has an attached WorkflowMonitorDialog Displays each WorkflowTask as a CheckLabel Each succeeded WorkflowTask is flagged OK Shows the progress of the WorkflowTasks Contains an own logging console Allows for cancelling

19 EsprIT 7.x 19 WorkflowMonitorDialog Currently running WorkflowTask Logging console Cancel-Button Another DeciderTask Tasks already succeeded Interactive user choice ProgressBar Total duration

20 EsprIT 7.x 20 DeciderTask A DeciderTask is a WorkflowTask with GUI-interaction Must be answered by user Decision is handled by underlying Workflow New WorkflowTasks can be inserted dynamically Particular WorkflowTasks can be skipped A finalization WorkflowTask can be set

21 EsprIT 7.x 21 Workflow Manager The WorkflowManager Tracks all running Workflows Allows for cancellation of one or all Allows for open/close WorflowMontitorDialog during execution Prevents Workflows from running twice at the same time

22 EsprIT 7.x 22 Sumary Workflow Workflows An ideal way to concatenate asynchroneous tasks Async tasks may interact with GUI (decisions, progress, logging) Directly cancellable Allows for adding any type of task Decider tasks (File-input/ File-output chooser, etc..) File transfer, Stream download OS-Processes (external non Java processes) Foreign Java-Processes (in other VM) DataParser, XmlWriter, etc…. Simplifies enormeously the programming of async acitvity Ensures robust execution and safe GUI synchronization Provides flow-control and visible progress to the user

23 EsprIT 7.x 23 RemoteTask on Server RemoteTask Runs on the server under the ownership and control of the client RemoteTask is associated to a local ClientEDT (Event Driven Task) ClientEDT receives RemoteTaskEvents from the RemoteTask Client performs sync actions on receiving Events Cancellation actually cancels remote task on server

24 EsprIT 7.x 24 RemoteTask on CoServer Runs on the CoServer under the ownership and control of the client Actually involves four tasks as shown below Cancellation is forwarded to the CoServer

25 EsprIT 7.x 25 Summary RemoteTask RemoteTask Examples Any async execution on Server/CoServer FileTransferTask, StreamDownloadTask RemoteOsProcessTask, RemoteJavaProcessTask Treat RemoteTask like a local Task Can be added to TaskCollection Can be added to a Workflow Can run as Sub/Child-Task of another task Can be cancelled the same way as a local task Extremely easy to program remote activity Framework handles both local and remote tasks the same way Robust execution of mixed (local/remote) Workflows

26 EsprIT 7.x 26 Conclusion The AsyncTask Framework Enormeously simplifies the programming of async code Frees the programmer from complicated synchronization issues Ensures robust execution, cancellation, error handling Handles local and remote tasks the same way Convenient grouping of async tasks (TaskCollections, Workflows) Easy to manage dependent tasks (Sub-Task, Child-Task) Make any class AsyncExecutable in a consistent way Easy to test, debug and to localize problems Saves a lot of time and work (and development cost)


Download ppt "EsprIT 7.x 1 AsyncTask Framework Programming asynchroneous activity - as local and remote tasks -"

Similar presentations


Ads by Google