如何为 Spring 框架 Web 应用程序中的 Quartz 计划作业执行的代码配置编码 (UTF-8)?
我想知道如何配置 Quartz 调度作业线程以反映正确的编码。在 Springframework 注入加载的 Web 应用程序 (java) 中正常执行的代码在 Quartz 调度的线程中运行时将出现编码问题。
有人可以帮助我吗?所有源代码均使用 maven2 编译,源代码和文件编码配置为 UTF-8。
在石英线程中,如果超出 ISO 8859-1 字符,任何字符串都会出现编码错误:
示例配置
<bean name="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="example.ExampleJob" />
</bean>
<bean id="jobTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="jobDetail" />
<property name="startDelay" value="1000" />
<property name="repeatCount" value="0" />
<property name="repeatInterval" value="1" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="jobTrigger"/>
</list>
</property>
</bean>
示例实现
public class ExampleJob extends QuartzJobBean {
private Log log = LogFactory.getLog(ExampleJob.class);
protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {
log.info("ÅÄÖ");
log.info(Charset.defaultCharset());
}
}
示例输出
2010-05-20 17:04:38,285 1342 INFO [QuartzScheduler_Worker-9] ExampleJob - ÅÄÖ
2010-05-20 17:04:38,286 1343 INFO [QuartzScheduler_Worker-9] ExampleJob - UTF-8
相同的代码行在 web 容器中的 servlet 引用的 spring 注入 bean 中执行将输出正确的编码。
是什么使得 Quartz 线程编码依赖?
I wonder how to configure Quartz scheduled job threads to reflect proper encoding. Code which otherwise executes fine within Springframework injection loaded webapps (java) will get encoding issues when run in threads scheduled by quartz.
Is there anyone who can help me out? All source is compiled using maven2 with source and file encodings configured as UTF-8.
In the quartz threads any string will have encoding errors if outside ISO 8859-1 characters:
Example config
<bean name="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="example.ExampleJob" />
</bean>
<bean id="jobTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="jobDetail" />
<property name="startDelay" value="1000" />
<property name="repeatCount" value="0" />
<property name="repeatInterval" value="1" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="jobTrigger"/>
</list>
</property>
</bean>
Example implementation
public class ExampleJob extends QuartzJobBean {
private Log log = LogFactory.getLog(ExampleJob.class);
protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {
log.info("ÅÄÖ");
log.info(Charset.defaultCharset());
}
}
Example output
2010-05-20 17:04:38,285 1342 INFO [QuartzScheduler_Worker-9] ExampleJob - ÅÄÖ
2010-05-20 17:04:38,286 1343 INFO [QuartzScheduler_Worker-9] ExampleJob - UTF-8
The same lines of code executed within spring injected beans referenced by servlets in the web-container will output proper encoding.
What is it that make Quartz threads encoding dependent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我以前没有见过
ÅÄÖ
模式。这不符合使用我所知道的任何 ISO-8859 字符集的常见错误模式。由于您在评论中谈论了 Mac OS Roman,因此我调查了其代码页并得出结论是这种编码在某个地方被错误地使用了。字符串
ÅÄÖ
由以下 UTF-8 字节组成:代码页告诉我
0xC3
确实代表√
并且0x85
、0x84
和0x96
分别代表Ö
、Ñ
和ñ
分别采用 Mac OS 罗马编码。由于您告诉过它在 servlet 中使用时工作正常,并且两者都使用相同的日志附加程序,因此可以排除日志输出的可疑之处。我现在只能想到一个原因:包含这些字符的文件是使用 Mac OS 罗马编码而不是 UTF-8 保存的。目前尚不清楚您在 Mac 上使用的是哪个编辑器,但记事本和 Eclipse 都会显示一条警告消息,并且在编辑器中重新打开文件应该会显示相同的格式错误的字符。
您使用哪个编辑器?是否明确配置为使用 UTF-8 编码保存文件?
更新:既然这似乎不是问题的原因,那么让我们回到使用 servlet 时它工作正常的事实。你具体是如何测试的?您是否不小心使用 Mac OS Roman 编码输入了这些字符,以便当记录器最终可能配置为使用 Mac OS Roman 时它会正确结束?记录器记录到哪里?命令控制台还是日志文件?它们是如何编码的?编码检测器会告诉您有关文件编码的哪些信息? (抱歉,我不使用 Mac,但 Windows 中的 Editplus/Notepad++ 可以检测/自动猜测文件编码并告诉它)。
I haven't seen the
ÅÄÖ
pattern before. This doesn't fit in the usual malform patterns using any of the ISO-8859 charsets I am aware of. Since you were talking about Mac OS Roman in one of your comments, I investigated its codepage and came to the conclusion that this encoding is been used incorrectly somewhere.The string
ÅÄÖ
is composed of the following UTF-8 bytes:The codepage learns me that
0xC3
indeed stands for√
and that0x85
,0x84
and0x96
stands forÖ
,Ñ
andñ
respectively in the Mac OS Roman encoding.Since you told that it works fine when used in servlets and that both uses the same logging appender, the logging output can be excluded from being suspect. I can now then think of only one cause: the file with those characters is been saved using the Mac OS Roman encoding instead of UTF-8. It's unclear which editor you're using on Mac, but both Notepad and Eclipse would have displayed a warning message about that and reopening the file in the editor should have shown the same malformed characters.
Which editor are you using? Is it explicitly configured to save files using UTF-8 encoding?
Update: since that doesn't seem to be the cause of the problem, let's go back to the fact that it works fine when using servlets. How exactly did you test it? Didn't you accidently enter those characters using Mac OS Roman encoding so that it would end up correctly when the logger is after all probably configured to use Mac OS Roman? Where does the logger log to? The command console or a logfile? How are they encoded? What does encoding detectors tell about the file encoding? (sorry, I don't do Mac, but Editplus/Notepad++ in windows for example can detect/autoguess file encoding and tell about it).