expiringmap icon indicating copy to clipboard operation
expiringmap copied to clipboard

listner not acting as expected

Open rbkasat opened this issue 9 years ago • 7 comments

I am trying to use listeners , I have very simple code, map = ExpiringMap.builder() .variableExpiration() .build(); TokenGen t1= new T1("T1","m1","m2"); map.put("T1",t1, ExpirationPolicy.CREATED, 20, TimeUnit.SECONDS); TokenGen t2= new T2("T2","m1","m2","m3"); map.put("T2",t2, ExpirationPolicy.CREATED, 20, TimeUnit.SECONDS); ExpirationListener<String, TokenGen> myListner = (key, val) -> { try { System.out.println("******* this is refresh for "+key); val.reload(); map.put(key,val) map.setExpiration(key, 20, TimeUnit.SECONDS); map.setExpirationPolicy(key, ExpirationPolicy.CREATED); } catch (Exception e) { e.printStackTrace(); } }; map.addExpirationListener(myListner); }

output: [T1, T2] Value of T1>>m1m258 Value of T2>>m2m1m358 ******* this is refresh for T1 ******* this is refresh for T2 ******* this is refresh for T2

For some reason, My listeners are executing twice for t2 and once for t1 every 20 sec..Any Idea why t2 executes twice? Do you have any documentation on listeners and how they work and manages multithreading

rbkasat avatar Oct 07 '16 20:10 rbkasat

Which version are you using?

jhalterman avatar Oct 07 '16 20:10 jhalterman

I am using 0.5.7

rbkasat avatar Oct 08 '16 15:10 rbkasat

@rbkasat Can you provide a test case that demonstrates what you're seeing that doesn't depend on other code? I can't run what you provided.

jhalterman avatar Oct 09 '16 02:10 jhalterman

Please find attached rest controller for spring boot application. its fairly straight forward , when you run spring boot app the controller will be initialized and then cache will be initialized.

to run this sample,

  1. git pull https://github.com/spring-guides/gs-rest-service.git
  2. replace greeting controller file with attached file
  3. mvn spring-boot:run
  4. hit the url http://localhost:8080/greeting1

in terminal logs you will see the T2 listener firing 2 times whereas t1 is firing one time. Expected is both should be executing just one time at every interval

GreetingController.java.txt

rbkasat avatar Oct 09 '16 19:10 rbkasat

@rbkasat I gotta ask - do you have a more straightforward way of reproducing this, that doesn't involve cloning a repo and firing up a web server? :)

jhalterman avatar Oct 10 '16 00:10 jhalterman

@jhalterman ok now just run the attached java class :) you will see the T2 listener firing two times in the logs every 15 sec and t1 once.

GreetingController.java.txt

rbkasat avatar Oct 10 '16 00:10 rbkasat

@rbkasat I tried this - works fine for me. perhaps this issue no longer exists?

jeacott1 avatar Sep 18 '19 00:09 jeacott1