Tuesday, April 24, 2018

Useful Java EE patterns

If you need a self-refreshing cache - with a timer to fetch updates - visible to all users:

annotate a class ApplicationCache with:


initialize it with a method annotated with
public void startup()

provide a scheduled method:

@javax.ejb.Schedule(dayOfWeek = "1-5", hour = "*", minute = "*/30", persistent = false)
public void refreshCache()

Wherever you want to invoke a service method on the above class, you acquire a reference with

private ApplicationCache applicationCache;


If you need a logger:

private org.slf4j.Logger logger;


if you need a thread-safe collection:

private final List employees = Collections.synchronizedList(new ArrayList());
private final Map employeesMap = Collections.synchronizedMap(new HashMap());


No comments: