xxl-job 報:java.net.BindException: Address already in use: bind

猿芯發表於2020-12-29

錯誤資訊

初看的時候還以為是埠占用:Address already in use: bind,不要被錯誤提示矇蔽了,實則是 xxl-job 的執行器註解 @Bean 配置的問題。

2020-12-29 09:24:00.494  INFO 9284,// --- [      Thread-40] com.xxl.rpc.remoting.net.Server          : >>>>>>>>>>> xxl-rpc remoting server start success, nettype = com.xxl.rpc.remoting.net.impl.netty_http.server.NettyHttpServer, port = 9991
Exception in thread "Thread-44" java.net.BindException: Address already in use: bind
	at sun.nio.ch.Net.bind0(Native Method)
	at sun.nio.ch.Net.bind(Net.java:433)
	at sun.nio.ch.Net.bind(Net.java:425)
	at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
	at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:134)
	at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:550)
	at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1334)
	at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:504)
	at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:489)
	at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:973)
	at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:248)
	at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:348)
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute$$$capture(AbstractEventExecutor.java:164)
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java)
	at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.lang.Thread.run(Thread.java:745)

問題定位

在低版本的 xxl-job 中, 初始化 XxlJobSpringExecutor 執行器需要在 @Bean 中加上 initMethod = "start", destroyMethod = "destroy",但是在高版本的 xxl-job(如 2.1.2)則需要刪除 initMethod = "start", destroyMethod = "destroy"

報錯前

@Bean(initMethod = "start", destroyMethod = "destroy")
public XxlJobSpringExecutor xxlJobExecutor() {
    log.info(">>>>>>>>>>> xxl-job config init.");
    XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
    xxlJobSpringExecutor.setAdminAddresses(jobProperties.getAdminAddresses());
    xxlJobSpringExecutor.setAppName(jobProperties.getAppName());
    xxlJobSpringExecutor.setPort(jobProperties.getPort());
    xxlJobSpringExecutor.setLogPath(jobProperties.getLogPath());
    xxlJobSpringExecutor.setLogRetentionDays(jobProperties.getLogRetentionDays());
    xxlJobSpringExecutor.setIp(jobProperties.getIp());
    xxlJobSpringExecutor.setAccessToken(jobProperties.getAccessToken());
    return xxlJobSpringExecutor;
}

解決

@Bean 的屬性 initMethod = "start", destroyMethod = "destroy" 刪除即可,程式碼如下:

@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
    log.info(">>>>>>>>>>> xxl-job config init.");
    XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
    xxlJobSpringExecutor.setAdminAddresses(jobProperties.getAdminAddresses());
    xxlJobSpringExecutor.setAppName(jobProperties.getAppName());
    xxlJobSpringExecutor.setPort(jobProperties.getPort());
    xxlJobSpringExecutor.setLogPath(jobProperties.getLogPath());
    xxlJobSpringExecutor.setLogRetentionDays(jobProperties.getLogRetentionDays());
    xxlJobSpringExecutor.setIp(jobProperties.getIp());
    xxlJobSpringExecutor.setAccessToken(jobProperties.getAccessToken());
    return xxlJobSpringExecutor;
}

相關文章