listner not acting as expected
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
Which version are you using?
I am using 0.5.7
@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.
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,
- git pull https://github.com/spring-guides/gs-rest-service.git
- replace greeting controller file with attached file
- mvn spring-boot:run
- 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
@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 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.
@rbkasat I tried this - works fine for me. perhaps this issue no longer exists?