Server-Side Worker Threads
Run background tasks with automatic UI updates. Keep your application responsive.
Background Processing Made Easy
Long-running tasks shouldn't freeze your UI. OorianWorkerThread handles background processing with live updates.
Non-Blocking
Long operations run in background threads. The UI remains responsive.
Live Updates
Push progress updates to the browser as work progresses.
Completion Notification
Automatically notify when the task completes, with results.
Cancellation
Cancel running tasks when needed. Clean shutdown.
Error Handling
Exceptions are captured and reported back to the UI.
Queue Management
Run multiple tasks. Track status of all running operations.
How It Works
Use the static factory method to create worker threads with automatic UI update capabilities.
public class ReportPage extends HtmlPage
{
private Div statusDiv;
private ProgressBar progressBar;
@Override
protected void createBody(Body body)
{
statusDiv = new Div();
statusDiv.setText("Ready to generate report");
progressBar = new ProgressBar();
progressBar.setValue(0);
Button generateBtn = new Button("Generate Report");
generateBtn.registerListener(this, MouseClickedEvent.class);
body.addElement(statusDiv);
body.addElement(progressBar);
body.addElement(generateBtn);
}
@Override
public void onEvent(MouseClickedEvent event)
{
// Create worker thread using static factory
OorianWorkerThread.create(() -> {
statusDiv.setText("Generating report...");
sendUpdate();
for (int i = 0; i <= 100; i += 10)
{
// Simulate work
Thread.sleep(500);
// Update progress
progressBar.setValue(i);
statusDiv.setText("Processing: " + i + "%");
sendUpdate(); // Push changes to browser
}
statusDiv.setText("Report complete!");
sendUpdate();
});
}
}Common Use Cases
File Processing
Process uploaded files in the background with progress updates.
OorianWorkerThread.create(() -> {
List<File> files = getUploadedFiles();
int total = files.size();
int processed = 0;
for (File file : files)
{
processFile(file);
processed++;
statusLabel.setText("Processed " + processed + " of " + total);
progressBar.setValue((processed * 100) / total);
sendUpdate();
}
statusLabel.setText("All files processed!");
sendUpdate();
});Data Import
Import large datasets with row-by-row progress.
OorianWorkerThread.create(() -> {
List<Record> records = parseImportFile(importFile);
int total = records.size();
for (int i = 0; i < total; i++)
{
Record record = records.get(i);
importRecord(record);
if (i % 100 == 0) // Update every 100 records
{
statusLabel.setText("Imported " + i + " of " + total);
sendUpdate();
}
}
statusLabel.setText("Import complete: " + total + " records");
resultTable.setData(records);
sendUpdate();
});Bulk Email
Send emails to multiple recipients with live status.
OorianWorkerThread.create(() -> {
List<String> recipients = getSelectedRecipients();
int sent = 0;
int failed = 0;
for (String email : recipients)
{
try
{
sendEmail(email, subject, body);
sent++;
}
catch (Exception e)
{
failed++;
logError(email, e);
}
statusLabel.setText("Sent: " + sent + " | Failed: " + failed);
sendUpdate();
}
showCompletionDialog(sent, failed);
sendUpdate();
});Best Practices
Use the Static Factory Method
Always use OorianWorkerThread.create() instead of subclassing. The factory method handles thread management correctly.
Call sendUpdate() to Push Changes
After modifying UI elements, call sendUpdate() to push changes to the browser. Without it, changes won't appear.
Batch Updates for Performance
Don't call sendUpdate() on every iteration. Batch updates (e.g., every 100 items) to reduce network overhead.
Handle Exceptions
Wrap your code in try-catch. Update the UI to show error states if something goes wrong.
Provide User Feedback
Always show progress indicators. Users should know something is happening and approximately how long it will take.