Monday 4 October 2021

ExpiringMap: listeners: notify when the entry expires

Expiration listeners are notified when an entry expires.

 

Is the expiration listeners called synchronously?

Yes,  Expiration listeners are called synchronously by default as the entries expire, and write operations to the map are blocked until the listeners complete.

 

Example

Below snippet create a synchronous listener.

Map<Integer, Employee> expiringMap = ExpiringMap.builder().expiration(5, TimeUnit.SECONDS)
.expirationListener((key, connection) -> ((Employee) connection).logOnExpire())
.expirationPolicy(ExpirationPolicy.CREATED).build(

Find the below working application.

 

Employee.java

package com.sample.app.model;

public class Employee {

	private int id;
	private String name;

	public Employee(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + "]";
	}
	
	public void logOnExpire() {
		System.out.println(this.toString() + " is expired");
	}

}


ExpiringMapListeners.java

package com.sample.app;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.sample.app.model.Employee;

import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;

public class ExpiringMapListeners {
	private static void sleepNSeconds(int n) throws InterruptedException {
		System.out.println("\nSleeping for " + n + " seconds");
		TimeUnit.SECONDS.sleep(n);
	}

	public static void main(String args[]) throws InterruptedException {
		Map<Integer, Employee> expiringMap = ExpiringMap.builder().expiration(5, TimeUnit.SECONDS)
				.expirationListener((key, employee) -> ((Employee) employee).logOnExpire())
				.expirationPolicy(ExpirationPolicy.CREATED).build();

		expiringMap.put(1, new Employee(1, "Ram"));
		sleepNSeconds(10);

	}
}


Output

Sleeping for 10 seconds
Employee [id=1, name=Ram] is expired


How to define asynchronous listener?

Using ‘asyncExpirationListener’ method, you can add an asynchronous listeners to the ExpiringMap.

 

ExpiringMapListeners.java

package com.sample.app;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.sample.app.model.Employee;

import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;

public class ExpiringMapListeners {
	private static void sleepNSeconds(int n) throws InterruptedException {
		System.out.println("\nSleeping for " + n + " seconds");
		TimeUnit.SECONDS.sleep(n);
	}

	public static void main(String args[]) throws InterruptedException {
		Map<Integer, Employee> expiringMap = ExpiringMap.builder().expiration(5, TimeUnit.SECONDS)
				.expirationListener((key, employee) -> ((Employee) employee).logOnExpire())
				.expirationPolicy(ExpirationPolicy.CREATED).build();

		expiringMap.put(1, new Employee(1, "Ram"));
		sleepNSeconds(10);

	}
}


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment