Got bad characters in scheduled tasks

Refresh

April 2019

Views

9 time

0

I use Spring (4.2.9.RELEASE) for a website. It uses Quartz-Scheduler (2.2.1) to run a scheduled task and this task needs to retrieve Chinese strings by using MessageSource. Here is some declaration:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="cacheSeconds" value="5" />
    <property name="basenames">
        <list>
            <value>WEB-INF/i18n/messages</value>
        </list>
    </property>
    <property name="defaultEncoding" value="UTF-8" />
</bean>

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.2.1</version>
</dependency>

The program for the scheduled task has this declaration:

@Autowired
private MessageSource messageSource;

When the task program runs, it retrieves i18n strings the following way:

Locale locale = LocaleUtils.toLocale(lang); 
String msg = messageSource.getMessage("label.message.alert", new Object[]{}, locale);

The "lang" is a string. If it is zh_TW for Chinese, the string retrieved is bad characters:

æ人給æ¨ç¼äºæ¶æ¯ï¼

However, if the lang is en_US or es_MX, the retrieved strings look just right. How to fix this issue?

P.S. #1 My site uses JSP for page content. On a JSP page, the following shows English, Spanish, and Chinese characters correctly:

<spring:message code="label.message.alert"/>

Only the scheduled program has this issue.

I have three properties files having the i18n strings:

messages_en_US.properties
messages_es_MX.properties
messages_zh_TW.properties

Inside each property file, there is a string similar to the following for each language:

label.message.alert=You've got messages.

In the messages_zh_TW.properties, I have the following line:

label.message.alert=有人給您發了消息!

P.S. #2 An interesting observation. Inside a web controller, I did the following:

@Autowired
private MessageSource messageSource;

.....

Locale locale = LocaleUtils.toLocale("zh_TW"); 
String msg = messageSource.getMessage("label.message.alert", new Object[]{}, locale);

The retrieved Chinese string displays correctly.

0 answers