Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Scout » [Solved] Notifications lifetime tied to current session duration(Limit the firing of notifications to the duration of the current user's session)
[Solved] Notifications lifetime tied to current session duration [message #1862926] Sun, 07 January 2024 12:18 Go to next message
J D is currently offline J DFriend
Messages: 100
Registered: February 2021
Senior Member
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 20:17]

Report message to a moderator

Re: Notifications lifetime tied to current session duration [message #1862928 is a reply to message #1862926] Sun, 07 January 2024 13:07 Go to previous messageGo to next message
Mr Robot is currently offline Mr RobotFriend
Messages: 71
Registered: March 2020
Member
You have ModelJobs inside Jobs.schedule?

Only ModelJobs is enough, with ScheduleBuilder.

I think it will stop working if session is closed.
Re: Notifications lifetime tied to current session duration [message #1862929 is a reply to message #1862928] Sun, 07 January 2024 13:11 Go to previous messageGo to next message
Mr Robot is currently offline Mr RobotFriend
Messages: 71
Registered: March 2020
Member
Run job each 5 seconds for testing and write system.out when triggered.

Then logout/close browserto see if system.out will write to output.
Re: Notifications lifetime tied to current session duration [message #1862970 is a reply to message #1862926] Tue, 09 January 2024 09:19 Go to previous messageGo to next message
Matthias Villiger is currently offline Matthias VilligerFriend
Messages: 232
Registered: September 2011
Senior Member
Hi J D

You could store the future returned by schedule() and then cancel it as soon as you no longer need it.

Does this work for you?

Kind regards
Mat
Re: Notifications lifetime tied to current session duration [message #1863315 is a reply to message #1862970] Thu, 25 January 2024 15:19 Go to previous messageGo to next message
J D is currently offline J DFriend
Messages: 100
Registered: February 2021
Senior Member
@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
Re: Notifications lifetime tied to current session duration [message #1863334 is a reply to message #1863315] Fri, 26 January 2024 14:59 Go to previous message
J D is currently offline J DFriend
Messages: 100
Registered: February 2021
Senior Member
Hi there everyone,

I think I figured it out. I did not throw the exception where it occurred and then catch it in calling class.

It works now.

Thanks a lot for your assistance.

Cheers,

JD

[Updated on: Fri, 26 January 2024 15:00]

Report message to a moderator

Previous Topic:Icons missing after font change
Next Topic:please help me figure out this error
Goto Forum:
  


Current Time: Wed May 08 21:26:24 GMT 2024

Powered by FUDForum. Page generated in 0.03366 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top