1

I want to be able to display news on the login page. (e.g. downtime caused by server maintenance etc.)

The news will be loaded from a DB. However, because the news will change very rarely, it should be cached and not be loaded everytime the application is loaded.

Here is where i get stucked: How can I cache the news ? In addition, I want to load the news every 10 min from the DB,else from the cache. Is this possible?

Or maybe you could recommand me a better soultion.

Many thanks in advance.

Leostiw

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
leostiw
  • 1,125
  • 3
  • 12
  • 28

2 Answers2

0

If you want to store the data across pages you can use @SessionScoped bean to hold the data for that purpose.
You can use Cookies to store the data between multiple sessions.
As far as requesting DB every 10 minutes is concerned..,
Usually one will send the ajax call every 10 minutes, instead you can use PRIEMFACE's PRIME PUSH technology[Link].
Which is build based on reverse ajax technology where the reverse ajax call will be sent to update JSF component from Managed bean when data is updated.
You can use Data Base triggers along with that.

EDIT: As Luiggi Mendoza suggested in the following comments using @ApplicationScope will fix the issue.

Kishor Prakash
  • 8,011
  • 12
  • 61
  • 92
  • Thanks for the answer! Regardin the scope, i don't want to store data across pages, I need the news to be loaded just once/session, namely on login. After I process the login, I won't need the news anymore. I actually though using a **@RequestScoped** bean. But that is not my main point here. I dont want to access the DB every time a session is created. The news should be loaded from the Cache. And the cache should be updated every 10 min. Regarding **primefaces push**, i don't think i can use it with this purpouse, right? (session independent) – leostiw Jul 11 '13 at 11:34
  • @leostiw I won't recommend using a `@SessionScoped` since the news should be available for all the users in the application and no need to be recovered on each new session start. You could use `@ApplicationScoped` to store the news or even better, use a real cache like [ehcache](http://stackoverflow.com/q/14712626/1065197) or [infinispan](http://www.jboss.org/infinispan/) to store and retrieve the news from your `@ViewScoped` managed bean. – Luiggi Mendoza Jul 11 '13 at 14:44
  • @LuiggiMendoza thanks for the answer! I will use the `@ApplicationScoped` bean,seems best in this case. One question though, where will be the data stored in this case? The reloading cycle, should I use a thread, what would you recommand? – leostiw Jul 12 '13 at 08:43
  • @leostiw as ssid in my previous comment, it would be better using a real cache implementation like ehcache or infinispan since they already have taken care about concurrency issues and other related problems that you will have to face and test by doing it manually – Luiggi Mendoza Jul 12 '13 at 13:28
0

finally I decided to do the refreshing cycle backed trough the EJB Timer Service.

This is my solution:

The @ApplicationScoped Bean (NewsController):

@ApplicationScoped
public class NewsController extends BaseController implements Serializable {
private static final long serialVersionUID = 1L;

@PersistenceContext
protected EntityManager em;

private List<News> newsList;

public NewsController() {
}

@PostConstruct
public void init() throws ExecutionException {
    getAvailableNews();
}

public void getAvailableNews() {
    if (newsList == null) {
        newsList = new LinkedList<News>();
        Query q = em.createNativeQuery(
                "SELECT * FROM news WHERE sysdate BETWEEN ab AND bis+1",
                News.class);
        @SuppressWarnings("unchecked")
        List<News> result = q.getResultList();
        for (News n : result)
            newsList.add(n);
    }
}

public List<News> getNewsList() {
    return newsList;
}

public void setNewsList(List<News> newsList) {
    this.newsList = newsList;
}

}

I addede additionally a @Stateless Bean (NewsScheduler) which calls my getAvailableNews() every 10 miuntes in the NewsController:

@Stateless
public class NewsScheduler {

@Inject
NewsController newsController;

@Schedule(hour = "*", minute = "*/5")
public void reload() {
    System.out.println("---------------- Laden der News aus der DB --------------");
    newsController.setNewsList(null);
    newsController.getAvailableNews();
}

}
leostiw
  • 1,125
  • 3
  • 12
  • 28