Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Scout » Beans with CreateImmediately or which implement IPlatformListener
Beans with CreateImmediately or which implement IPlatformListener [message #1858121] Thu, 16 March 2023 12:53 Go to next message
Eclipse UserFriend
Hi,
we are using LookupCalls/LookupServices, which query our ERP system via REST. Since the response from the erp is rather slow and the data doesn't change often, we implemented a simple caching mechanism.

In order to schedule a refresh job I annoted the LooupService with @CreateImmediately. Unfortunately the Service gets created during the unit test run too. I tried, not to use @CreateImmediately and let the LookupService implement IPlatformListener. But the Platform is startet during the tests, too.
So this is the "solution" I've come up with. Is there a better way to check if the application is in "normal" and not in testing mode? Or is there another way to achieve what I am trying to do?

@ApplicationScoped
public abstract class SimpleCachingLookupService<ID> implements ILookupService<ID>, IPlatformListener {

  private IFuture refreshFuture = null;
	
  public SimpleCachingLookupService(boolean enableScheduledRefresh) {
    this.scheduledRefreshEnabled = enableScheduledRefresh;
  }

  @Override
  public void stateChanged(PlatformEvent event) {
    if (isScheduledRefreshEnabled() &&
      event.getState() == IPlatform.State.PlatformStarted &&
      refreshFuture == null
      && !"GlobalTestingPlatform".equals(event.getSource().getClass().getSimpleName())
    ) {
      addScheduledRefreshJob();
      return;
    }
    if (isScheduledRefreshEnabled() &&
      event.getState() == IPlatform.State.PlatformStopping &&
      refreshFuture != null &&
      !refreshFuture.isCancelled()
    ) {
        refreshFuture.cancel(false);
    }
  }
  
  private void addScheduledRefreshJob() {
    if (isScheduledRefreshEnabled()) {
      refreshFuture = Jobs.schedule(() -> {
          if (RunMonitor.CURRENT.get().isCancelled()) {
            log.info("Job {} is cancelled", IFuture.CURRENT.get().getJobInput().getName());
            return;
          }
          forceRefresh();
        },
        Jobs.newInput()
          .withName(String.format("SimpleCachingLookupService(%s) Refresh Job", this.getClass().getSimpleName()))
          .withRunContext(ServerRunContexts.copyCurrent(true))
          .withExecutionTrigger(Jobs.newExecutionTrigger()
            .withSchedule(SimpleScheduleBuilder.simpleSchedule()
              .withIntervalInSeconds(getCacheTimeout())
              .repeatForever())));
      log.info("Scheduled {}, interval {} s", refreshFuture.getJobInput().getName(), getCacheTimeout());
    }
  }
  
  /* ... */
}


Best,
Nils
Re: Beans with CreateImmediately or which implement IPlatformListener [message #1858129 is a reply to message #1858121] Thu, 16 March 2023 14:51 Go to previous message
Eclipse UserFriend
Hi Nils

You could use a config property to enable/disable your job, implement the config property as default value true and override it in your config.properties file of your test case to false.

Another solution would be to implement your REST service as Scout Bean with a default implementation in your production code and a second Mock-Implementation in your testcode, using a lower @Order(value) annotation in order to force the test environment to use the mocked implementation.
Benefit of the second solution is, that your lookup caching could also be unit tested against the mocked REST client implementation.

Regards,
Paolo
Previous Topic:Eclipse Scout 23.1 is out now!
Next Topic:Accordion field, height
Goto Forum:
  


Current Time: Mon Feb 17 11:54:46 GMT 2025

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

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

Back to the top