quartz异常之:org.quartz.SchedulerException

搭建 quartz 集群的时候碰到的揪心的bug。

一、问题

    原来的项目是单服务,现在要部署在多服务器上,也就是需要搭建一个集群。

    还好之前 quartz 就已经是由 mysql 来管理,所以只需要开启集群的开关即可:org.quartz.jobStore.isClustered=true

    项目在本地完美运行,但是上了测试机就报异常,如下:
</p>

1
2018-01-30&nbsp;14:23:47.234&nbsp;ERROR&nbsp;6101&nbsp;---&nbsp;[main]&nbsp;org.quartz.impl.StdSchedulerFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;Couldn&#39;t&nbsp;generate&nbsp;instance&nbsp;Id!


1
2
3
4
5
6
7
org.quartz.SchedulerException:&nbsp;Couldn&#39;t&nbsp;get&nbsp;host&nbsp;name!
at&nbsp;org.quartz.simpl.SimpleInstanceIdGenerator.generateInstanceId(SimpleInstanceIdGenerator.java:36)
at&nbsp;org.quartz.impl.StdSchedulerFactory.instantiate(StdSchedulerFactory.java:1211)
at&nbsp;org.quartz.impl.StdSchedulerFactory.getScheduler(StdSchedulerFactory.java:1519)
at&nbsp;org.springframework.scheduling.quartz.SchedulerFactoryBean.createScheduler(SchedulerFactoryBean.java:597)
at&nbsp;org.springframework.scheduling.quartz.SchedulerFactoryBean.afterPropertiesSet(SchedulerFactoryBean.java:480)
...


1
2
3
4
Caused&nbsp;by:&nbsp;java.net.UnknownHostException:&nbsp;WLSHASRV007:&nbsp;WLSHASRV007:&nbsp;域名解析暂时失败
at&nbsp;java.net.InetAddress.getLocalHost(InetAddress.java:1505)
at&nbsp;org.quartz.simpl.SimpleInstanceIdGenerator.generateInstanceId(SimpleInstanceIdGenerator.java:34)
...&nbsp;108&nbsp;common&nbsp;frames&nbsp;omitted


    很糟糕,第一次碰到,查看了项目中的各处配置文件都没问题,最后 google 给我解决了,原来是 org.quartz.simpl.SimpleInstanceIdGenerator 中获取主机名异常了。

    下面是源码:

1
2
3
4
5
6
7
8
9
public&nbsp;class&nbsp;SimpleInstanceIdGenerator&nbsp;implements&nbsp;InstanceIdGenerator&nbsp;{
&nbsp;&nbsp;public&nbsp;String&nbsp;generateInstanceId()&nbsp;throws&nbsp;SchedulerException&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;InetAddress.getLocalHost().getHostName()&nbsp;+&nbsp;System.currentTimeMillis();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(Exception&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;SchedulerException("Couldn&#39;t&nbsp;get&nbsp;host&nbsp;name!",&nbsp;e);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
}


二、解决

    其实解决是很简单,

    ① 查看主机名:

1
2
[root@WLSHASRV007&nbsp;~]#&nbsp;hostname
WLSHASRV007

    ② 查看 hosts 文件,vi /etc/hosts ,确保无乱码等异常,并确认 127.0.0.1 后面有上命令查询出的主机名

    

    ③ 如图,发现没有,则添加即可

    








------ 本文结束 感谢阅读 ------
0%