[Solved] Notifications lifetime tied to current session duration [message #1862926] |
Sun, 07 January 2024 07:18  |
Eclipse User |
|
|
|
Hi there everyone,
I've activated notifications in my application and I need to know if I've done the right thing.
Firstly, I added the code to the Desktop's execOpened() method. See below. It works BUT should the code be in execOpened() in the first place?
@Override
protected void execOpened() {
// Schedule notifications
if (CONFIG.getPropertyValue(ClientStructureTypeProperty.class).equals("ACC")) {
// The job/work to be done
final IRunnable openFormRunnable = () -> {
String alertsSummary = BEANS.get(IEmployeeDeadlinesService.class).getAlerts();
if (StringUtility.hasText(alertsSummary)) {
IStatus status = new Status(alertsSummary, IStatus.INFO, null);
long duration = 20000;
boolean closeable = true;
DesktopNotification notification =
new DesktopNotification(status, duration, closeable);
ClientSessionProvider.currentSession().getDesktop().addNotification(notification);
}
};
// Start the job after 30 seconds, repeat it every 30 minutes forever? Client session duration?
//
int openingDelay = 30;
if (openingDelay == 0) {
ModelJobs.schedule(openFormRunnable, ModelJobs.newInput(ClientRunContexts.copyCurrent()));
} else {
Jobs.schedule(() -> {
ModelJobs.schedule(openFormRunnable, ModelJobs.newInput(ClientRunContexts.copyCurrent()));
}, Jobs.newInput().withName(TEXTS.get("Deadlines"))
.withRunContext(ClientRunContexts.copyCurrent()).withExecutionTrigger(
Jobs.newExecutionTrigger().withStartIn(openingDelay, TimeUnit.SECONDS)
.withSchedule(FixedDelayScheduleBuilder.repeatForever(30, TimeUnit.MINUTES)))); // <--- repeatForever????
}
}
}
The problem I have is .withSchedule(FixedDelayScheduleBuilder.repeatForever(30, TimeUnit.MINUTES)).
I don't want it to run forever, only for the duration of each user's session but I don't know how to do that.
Can anyone tell me how to do this?
Cheers,
JD
[Updated on: Fri, 26 January 2024 15:17] by Moderator
|
|
|
|
|
|
Re: Notifications lifetime tied to current session duration [message #1863315 is a reply to message #1862970] |
Thu, 25 January 2024 10:19   |
Eclipse User |
|
|
|
@Mr Robot
Yes I have a ModelJob insode a Job.Schedule. I modelled it after the Scout Widgets example for Desktop on GitHub: https://github.com/eclipse-scout/scout.docs/blob/releases/22.0/code/widgets/org.eclipse.scout.widgets.client/src/main/java/org/eclipse/scout/widgets/client/ui/forms/DesktopForm.java
@ Matthias Villiger
I've done that and now I can cancel the job when the user closes the session
This is what it looks like now
/**
* integrationProjectNotifications
*
* Fires a notofocation/alert on employee related deadlines at a fixed interval
*
* @param openingDelayInSeconds
* @param repeatFrequencyInMinutes
*
* Called by the execOpened() method of this class and by the "Alerts" menu button
*/
private void integrationProjectNotifications(int openingDelayInSeconds,
int repeatFrequencyInMinutes) {
// Schedule notifications
if (CONFIG.getPropertyValue(ClientStructureTypeProperty.class).equals("ACI")) {
// The job/work to be done
final IRunnable openFormRunnable = () -> {
String alertsSummary = BEANS.get(IEmployeeDeadlinesService.class).getAlerts();
if (StringUtility.hasText(alertsSummary)) {
IStatus status = new Status(alertsSummary, IStatus.INFO, null);
long duration = 20000; // 1000 Milliseconds = 1 secs
boolean closeable = true;
DesktopNotification notification =
new DesktopNotification(status, duration, closeable);
ClientSessionProvider.currentSession().getDesktop().addNotification(notification);
}
};
// Start the job after openingDelayInSeconds, repeat it every repeatFrequencyInMinutes forever
if (openingDelayInSeconds <= 1) {
// Called only from the "Alerts" menu button
ModelJobs.schedule(openFormRunnable, ModelJobs.newInput(ClientRunContexts.copyCurrent()));
} else {
// Fired automatically by the execOpened() method
if (Objects.isNull(futureDeadlines)) {
futureDeadlines = Jobs.schedule(() -> {
ModelJobs.schedule(openFormRunnable,
ModelJobs.newInput(ClientRunContexts.copyCurrent()));
}, Jobs.newInput().withName(TEXTS.get("Deadlines"))
.withExecutionHint(TEXTS.get("Deadlines"))
.withRunContext(ClientRunContexts.copyCurrent())
.withExecutionTrigger(
Jobs.newExecutionTrigger().withStartIn(openingDelayInSeconds, TimeUnit.SECONDS)
.withSchedule(FixedDelayScheduleBuilder
.repeatForever(repeatFrequencyInMinutes, TimeUnit.MINUTES)))
.withExceptionHandling(new ExceptionHandler() {
// "false" below cancels the job
@Override
public void handle(Throwable t) {
LOG.error(
"Exception occurred while running employee deadlines notifications job. The job will now be canceled");
super.handle(t);
futureDeadlines.cancel(true);
}
}, false));
futureDeadlines.addListener(
Jobs.newEventFilterBuilder().andMatchEventType(JobEventType.JOB_STATE_CHANGED)
.andMatchState(JobState.RUNNING)
.andMatch(new SessionJobEventFilter(ISession.CURRENT.get())).toFilter(),
event -> {
LOG.info("{} job commences execution at {}",
futureDeadlines.getJobInput().getName(), LocalDateTime.now());
});
}
}
}
}
However, I now want the job to be cancelled whenever there is an exception and the code above does not work. It keeps firing even after an exception.
The code in the .withExceptionHandling part is never fired.
Can anyone help me fix this problem? Thanks a lot for your kind assistance.
Cheers,
JD
|
|
|
|
Powered by
FUDForum. Page generated in 0.03925 seconds