1
votes

I followed the tutorial for loading plain text files given here : https://github.com/spring-cloud/spring-cloud-config/blob/master/docs/src/main/asciidoc/spring-cloud-config.adoc#serving-plain-text

When I test the following url in the config serve it works perfectly and returns the plain text file : http://localhost:8888/icullen-webapp/default/master/messages_en_US.properties

However when I use in the config client, it takes ages to load my page as I have way too many translations in the same page. The code finds the translations from config repo but for each translation it makes the call to the server and hence its quite slow.

Find below my code :

<bean id="messageSource"
        class="com.icullen.site.utils.TolerantReloadableResourceBundleMessageSource">
        <property name="basenames" value="http://localhost:8888/icullen-webapp/default/master/messages" />
        <property name="defaultEncoding" value="UTF-8" />
        <property name="cacheSeconds">
            <value>5</value>
        </property>
    </bean>

public class TolerantReloadableResourceBundleMessageSource extends
ReloadableResourceBundleMessageSource {

private static final Logger logger = LoggerFactory.getLogger(TolerantReloadableResourceBundleMessageSource.class);

@Override
protected String getMessageInternal(String code, Object[] args,
        Locale locale) {
    String messageInternal = super.getMessageInternal(code, args, locale);
    if(messageInternal == null){
        logger.warn("No translation for : {}", code);
    }
    return messageInternal != null ? messageInternal : "?" + code + "?";
}
}

The translations are called in the jsp using spring messages like this

<spring:message htmlEscape="true" code="category.${region.name}"/>

what am i doing wrong ? what should i do to load the page fast. Please help as we are stuck.

1

1 Answers

0
votes

I found the solution : it was my caching value. I had to put the caching to -1 i.e. - cache forever. When I changed my properties file I called a controller which refreshed the cache manually.

@Controller
public class RefreshTranslationsController {

private static final Logger logger = LoggerFactory.getLogger(RefreshTranslationsController.class);

@Autowired
private TolerantReloadableResourceBundleMessageSource messageSource;


 @RequestMapping(value = "/product/refresh/translations", method = RequestMethod.GET)
 public String refreshTranslations(HttpServletRequest request, Locale locale, Model model) {
     if(userIsLoggedIn()){   
             logger.info("Loaded message source before refreshing : "+messageSource.toString());
             messageSource.clearCache();
             logger.info("Cleared message cache : ");
             messageSource.getMessage("category.FLASH", null, locale);
             logger.info("Translations Reloaded !!! ");
         }
     }else{
         throw new CullenSecurityException();
     }
     return "redirect:/product/";
 }

}