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:

@javax.enterprise.context.ApplicationScoped
@javax.ejb.Singleton
@javax.ejb.ConcurrencyManagement(ConcurrencyManagementType.BEAN)


initialize it with a method annotated with
@javax.annotation.PostConstruct
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

@javax.inject.Inject
private ApplicationCache applicationCache;


=================



If you need a logger:

@javax.inject.Inject
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: