Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8182447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:51:58+00:00 2026-06-07T00:51:58+00:00

In a Java EE application built with the Spring Framework I need to do

  • 0

In a Java EE application built with the Spring Framework I need to do some really expensive operations in a DAO that can take up to several minutes. Using Spring MVC, I hit the DAO through my Controller, when the user’s request maps to the controller method:

@RequestMapping(value = "/categories.do")
public ModelAndView categories(
        @PathVariable("cc") String cc,
        @PathVariable("ll") String ll) {
    Locale locale = new Locale(ll, cc);
    ModelAndView result = getView("categories", locale);
    Map<Category, List<Product>> allProducts = supplyDao.getAllProducts(locale);
    result.addObject("products", allProducts);
    return result;
}

The getAllProducts method makes a large number of requests to an external web service to draw all the data needed for the products. The method is cached via Spring’s @Cacheable annotation, over an underlying Ehcache implementation:

@Cacheable(value = CACHE_NAME, key = CACHE_KEY_PREFIX + "'(' + #p0 + ')'")
public Map<Category, List<Product>> getAllProducts(Locale locale) {
        // a lot of HTTP requests firing from here
}

The problem with this approach is that while the cache is empty the page is basically unaccessible. Moreover, if more than one request hits the page when the cache is empty, the DAO method will fire again and all the requests would be repeated in parallel. As far as I understand, a solution to the second problem would be to use a BlockingCache, but I haven’t had the chance to implement it yet.

What I want is to have the controller method always draw its results from the cache. I want to implement a @PostConstruct method that triggers cache population for all locales. Something like:

@PostConstruct
public void populateCaches() {
    for (Locale locale : localeList) {
        getAllProducts(locale);
    }
}

I don’t mind the initial population taking a while since the server is seldomly restarted. I also will set the cache expiration to something like three days – the data does not get updated frequently and there is no danger in not serving the most up to date version.

What I would like to do is run a TimerTask at set intervals, let’s say two days and 23 hours, which would force a DAO method to draw all the product data from the web services. This data would then replace the data in the cache, without it having been expired. The cache expiration counter would then be reset – the data would expire in three days time again. This way, the controller method would always get the product data from the cache and the page would be responsive.

My question is, how would I implement such a method given that I am using Spring’s cache abstraction? Do I need to handle the CacheManager directly in my methods?

Another question is: am I approaching the problem correctly? Is there a better way of doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:52:00+00:00Added an answer on June 7, 2026 at 12:52 am

    To block calls until a cache is available

    @Cacheable(cacheName="yourCache", decoratedCacheType= DecoratedCacheType.SELF_POPULATING_CACHE)
    public List<String> getWhatever(int id) {
    //call database
    }
    

    Instead of a timertask you could use an autorefreshing cache :

    @Cacheable(cacheName="yourCache", refreshInterval=1000, decoratedCacheType= DecoratedCacheType.REFRESHING_SELF_POPULATING_CACHE)
    public List<String> getWhatever(int id) {
      //call database
    }
    

    kudos

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built a Java application that has some dependencies (~10). I would like
I have a Spring framework based Java web application, which has been built in
I have a web application, built with Maven, consisting of a Java (Spring) backend
I've created a simple console Java application that is built with Maven. Is there
I have built a web application using Java EE platform that sells one of
I have a web application that use Sring IoC framework. I use the Java
I am switching an enterprise application built on Spring 3.1.0, Hibernate 3.6.0 from Java
I'm following a tutorial to set up a skeleton application for tomcat : http://maestric.com/doc/java/spring/setup#build_files
I am trying to migrate a Java application built by Ant to Maven2. Among
i implemented a small web application to create Rss feeds in java(spring mvc) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.