Refresh / restart bean application area

I had a problem updating or rebooting an application-driven bean. It behaves like a cached bean. Therefore, as soon as the data changes on the db side, I want to reload the list in the bean. Is there anyway, to update / reload the list, say once a day, depending on the given time? Thanks

+8
java jsf jsf-2
source share
1 answer

Just add a method to the bean application area, which does just that.

public void reload() { list = dao.list(); } 

Then grab / paste this bean into another bean and call the method.

 data.reload(); 

Update Sorry, I missed the "once a day" bit. Do you mean automatic reboot in the background? This is best achieved with a background thread controlled by a ScheduledExecutorService . Create a ServletContextListener as follows:

 @WebListener public class Config implements ServletContextListener { private ScheduledExecutorService scheduler; @Override public void contextInitialized(ServletContextEvent event) { Reloader reloader = new Reloader(event.getServletContext()); scheduler = Executors.newSingleThreadScheduledExecutor(); scheduler.scheduleAtFixedRate(reloader, 1, 1, TimeUnit.DAYS); } @Override public void contextDestroyed(ServletContextEvent event) { scheduler.shutdownNow(); } } 

If the Reloader class looks like this (assuming the managed name is bean data )

 public class Reloader implements Runnable { private ServletContext context; public Reloader(ServletContext context) { this.context = context; } @Override public void run() { Data data = (Data) context.getAttribute("data"); if (data != null) { data.reload(); } } } 
+14
source share

All Articles