底层线程模型统一;destory逻辑优化;
This commit is contained in:
parent
b8e196606d
commit
79f9317ffe
|
@ -78,10 +78,10 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware, In
|
||||||
// destroy
|
// destroy
|
||||||
public void destroy(){
|
public void destroy(){
|
||||||
// admin registry stop
|
// admin registry stop
|
||||||
JobRegistryHelper.getInstance().stop();
|
JobRegistryHelper.getInstance().toStop();
|
||||||
|
|
||||||
// admin monitor stop
|
// admin monitor stop
|
||||||
JobMonitorHelper.getInstance().stop();
|
JobMonitorHelper.getInstance().toStop();
|
||||||
|
|
||||||
serverFactory.destroy();
|
serverFactory.destroy();
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ public class JobMonitorHelper {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (true) {
|
while (!toStop) {
|
||||||
try {
|
try {
|
||||||
logger.debug(">>>>>>>>>>> job monitor beat ... ");
|
logger.debug(">>>>>>>>>>> job monitor beat ... ");
|
||||||
Integer jobLogId = JobMonitorHelper.instance.queue.take();
|
Integer jobLogId = JobMonitorHelper.instance.queue.take();
|
||||||
|
@ -81,7 +81,7 @@ public class JobMonitorHelper {
|
||||||
monitorThread.start();
|
monitorThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop(){
|
public void toStop(){
|
||||||
toStop = true;
|
toStop = true;
|
||||||
//monitorThread.interrupt();
|
//monitorThread.interrupt();
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,7 +70,7 @@ public class JobRegistryHelper {
|
||||||
registryThread.start();
|
registryThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop(){
|
public void toStop(){
|
||||||
toStop = true;
|
toStop = true;
|
||||||
//registryThread.interrupt();
|
//registryThread.interrupt();
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,9 @@ import com.xxl.job.core.handler.IJobHandler;
|
||||||
import com.xxl.job.core.handler.annotation.JobHander;
|
import com.xxl.job.core.handler.annotation.JobHander;
|
||||||
import com.xxl.job.core.registry.RegistHelper;
|
import com.xxl.job.core.registry.RegistHelper;
|
||||||
import com.xxl.job.core.rpc.netcom.NetComServerFactory;
|
import com.xxl.job.core.rpc.netcom.NetComServerFactory;
|
||||||
|
import com.xxl.job.core.thread.ExecutorRegistryThread;
|
||||||
import com.xxl.job.core.thread.JobThread;
|
import com.xxl.job.core.thread.JobThread;
|
||||||
|
import com.xxl.job.core.thread.TriggerCallbackThread;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
|
@ -46,11 +48,33 @@ public class XxlJobExecutor implements ApplicationContextAware, ApplicationListe
|
||||||
// ---------------------------------- job server ------------------------------------
|
// ---------------------------------- job server ------------------------------------
|
||||||
private NetComServerFactory serverFactory = new NetComServerFactory();
|
private NetComServerFactory serverFactory = new NetComServerFactory();
|
||||||
public void start() throws Exception {
|
public void start() throws Exception {
|
||||||
|
// executor start
|
||||||
NetComServerFactory.putService(ExecutorBiz.class, new ExecutorBizImpl());
|
NetComServerFactory.putService(ExecutorBiz.class, new ExecutorBizImpl());
|
||||||
serverFactory.start(port, ip, appName, registHelper);
|
serverFactory.start(port, ip, appName, registHelper);
|
||||||
|
|
||||||
|
// trigger callback thread start
|
||||||
|
TriggerCallbackThread.getInstance().start();
|
||||||
}
|
}
|
||||||
public void destroy(){
|
public void destroy(){
|
||||||
|
// executor stop
|
||||||
serverFactory.destroy();
|
serverFactory.destroy();
|
||||||
|
|
||||||
|
// job thread repository destory
|
||||||
|
if (JobThreadRepository.size() > 0) {
|
||||||
|
for (Map.Entry<String, JobThread> item: JobThreadRepository.entrySet()) {
|
||||||
|
JobThread jobThread = item.getValue();
|
||||||
|
jobThread.toStop("Web容器销毁终止");
|
||||||
|
jobThread.interrupt();
|
||||||
|
|
||||||
|
}
|
||||||
|
JobThreadRepository.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// trigger callback thread stop
|
||||||
|
TriggerCallbackThread.getInstance().toStop();
|
||||||
|
|
||||||
|
// executor registry thread stop
|
||||||
|
ExecutorRegistryThread.getInstance().toStop();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------- init job handler ------------------------------------
|
// ---------------------------------- init job handler ------------------------------------
|
||||||
|
@ -99,7 +123,8 @@ public class XxlJobExecutor implements ApplicationContextAware, ApplicationListe
|
||||||
return jobThread;
|
return jobThread;
|
||||||
}
|
}
|
||||||
public static JobThread loadJobThread(String jobKey){
|
public static JobThread loadJobThread(String jobKey){
|
||||||
return JobThreadRepository.get(jobKey);
|
JobThread jobThread = JobThreadRepository.get(jobKey);
|
||||||
|
return jobThread;
|
||||||
}
|
}
|
||||||
public static void removeJobThread(String jobKey){
|
public static void removeJobThread(String jobKey){
|
||||||
JobThreadRepository.remove(jobKey);
|
JobThreadRepository.remove(jobKey);
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package com.xxl.job.core.rpc.netcom.jetty.server;
|
package com.xxl.job.core.rpc.netcom.jetty.server;
|
||||||
|
|
||||||
import com.xxl.job.core.registry.RegistHelper;
|
import com.xxl.job.core.registry.RegistHelper;
|
||||||
import com.xxl.job.core.util.IpUtil;
|
import com.xxl.job.core.thread.ExecutorRegistryThread;
|
||||||
import org.eclipse.jetty.server.Connector;
|
import org.eclipse.jetty.server.Connector;
|
||||||
import org.eclipse.jetty.server.Handler;
|
import org.eclipse.jetty.server.Handler;
|
||||||
import org.eclipse.jetty.server.Server;
|
import org.eclipse.jetty.server.Server;
|
||||||
|
@ -11,8 +11,6 @@ import org.eclipse.jetty.util.thread.ExecutorThreadPool;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* rpc jetty server
|
* rpc jetty server
|
||||||
* @author xuxueli 2015-11-19 22:29:03
|
* @author xuxueli 2015-11-19 22:29:03
|
||||||
|
@ -21,9 +19,9 @@ public class JettyServer {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(JettyServer.class);
|
private static final Logger logger = LoggerFactory.getLogger(JettyServer.class);
|
||||||
|
|
||||||
private Server server;
|
private Server server;
|
||||||
|
private Thread thread;
|
||||||
public void start(final int port, final String ip, final String appName, final RegistHelper registHelper) throws Exception {
|
public void start(final int port, final String ip, final String appName, final RegistHelper registHelper) throws Exception {
|
||||||
Thread thread = new Thread(new Runnable() {
|
thread = new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
server = new Server();
|
server = new Server();
|
||||||
|
@ -43,7 +41,7 @@ public class JettyServer {
|
||||||
try {
|
try {
|
||||||
server.start();
|
server.start();
|
||||||
logger.info(">>>>>>>>>>>> xxl-job jetty server start success at port:{}.", port);
|
logger.info(">>>>>>>>>>>> xxl-job jetty server start success at port:{}.", port);
|
||||||
executorRegistryBeat(port, ip, appName, registHelper);
|
ExecutorRegistryThread.getInstance().start(port, ip, appName, registHelper);
|
||||||
server.join(); // block until thread stopped
|
server.join(); // block until thread stopped
|
||||||
logger.info(">>>>>>>>>>> xxl-rpc server start success, netcon={}, port={}", JettyServer.class.getName(), port);
|
logger.info(">>>>>>>>>>> xxl-rpc server start success, netcon={}, port={}", JettyServer.class.getName(), port);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -65,43 +63,10 @@ public class JettyServer {
|
||||||
logger.error("", e);
|
logger.error("", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (thread.isAlive()) {
|
||||||
|
thread.interrupt();
|
||||||
|
}
|
||||||
logger.info(">>>>>>>>>>> xxl-rpc server destroy success, netcon={}", JettyServer.class.getName());
|
logger.info(">>>>>>>>>>> xxl-rpc server destroy success, netcon={}", JettyServer.class.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* registry beat
|
|
||||||
* @param port
|
|
||||||
* @param ip
|
|
||||||
* @param appName
|
|
||||||
* @param registHelper
|
|
||||||
*/
|
|
||||||
private void executorRegistryBeat(final int port, final String ip, final String appName, final RegistHelper registHelper){
|
|
||||||
if (registHelper==null && appName==null || appName.trim().length()==0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Thread registryThread = new Thread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
while (true) {
|
|
||||||
try {
|
|
||||||
// generate addredd = ip:port
|
|
||||||
String address = null;
|
|
||||||
if (ip != null && ip.trim().length()>0) {
|
|
||||||
address = ip.trim().concat(":").concat(String.valueOf(port));
|
|
||||||
} else {
|
|
||||||
address = IpUtil.getIpPort(port);
|
|
||||||
}
|
|
||||||
|
|
||||||
registHelper.registry(RegistHelper.RegistType.EXECUTOR.name(), appName, address);
|
|
||||||
TimeUnit.SECONDS.sleep(RegistHelper.TIMEOUT);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
registryThread.setDaemon(true);
|
|
||||||
registryThread.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,53 @@
|
||||||
package com.xxl.job.core.thread;
|
package com.xxl.job.core.thread;
|
||||||
|
|
||||||
|
import com.xxl.job.core.registry.RegistHelper;
|
||||||
|
import com.xxl.job.core.util.IpUtil;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by xuxueli on 17/3/2.
|
* Created by xuxueli on 17/3/2.
|
||||||
*/
|
*/
|
||||||
public class ExecutorRegistryThread extends Thread {
|
public class ExecutorRegistryThread extends Thread {
|
||||||
|
|
||||||
|
private static ExecutorRegistryThread instance = new ExecutorRegistryThread();
|
||||||
|
public static ExecutorRegistryThread getInstance(){
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Thread registryThread;
|
||||||
|
private boolean toStop = false;
|
||||||
|
public void start(final int port, final String ip, final String appName, final RegistHelper registHelper){
|
||||||
|
if (registHelper==null && appName==null || appName.trim().length()==0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
registryThread = new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (!toStop) {
|
||||||
|
try {
|
||||||
|
// generate addredd = ip:port
|
||||||
|
String address = null;
|
||||||
|
if (ip != null && ip.trim().length()>0) {
|
||||||
|
address = ip.trim().concat(":").concat(String.valueOf(port));
|
||||||
|
} else {
|
||||||
|
address = IpUtil.getIpPort(port);
|
||||||
|
}
|
||||||
|
|
||||||
|
registHelper.registry(RegistHelper.RegistType.EXECUTOR.name(), appName, address);
|
||||||
|
TimeUnit.SECONDS.sleep(RegistHelper.TIMEOUT);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
registryThread.setDaemon(true);
|
||||||
|
registryThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toStop() {
|
||||||
|
toStop = true;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,14 +15,23 @@ import java.util.concurrent.LinkedBlockingQueue;
|
||||||
public class TriggerCallbackThread {
|
public class TriggerCallbackThread {
|
||||||
private static Logger logger = LoggerFactory.getLogger(TriggerCallbackThread.class);
|
private static Logger logger = LoggerFactory.getLogger(TriggerCallbackThread.class);
|
||||||
|
|
||||||
private static LinkedBlockingQueue<HandleCallbackParam> callBackQueue = new LinkedBlockingQueue<HandleCallbackParam>();
|
private static TriggerCallbackThread instance = new TriggerCallbackThread();
|
||||||
static {
|
public static TriggerCallbackThread getInstance(){
|
||||||
new Thread(new Runnable() {
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
private LinkedBlockingQueue<HandleCallbackParam> callBackQueue = new LinkedBlockingQueue<HandleCallbackParam>();
|
||||||
|
|
||||||
|
private Thread triggerCallbackThread;
|
||||||
|
private boolean toStop = false;
|
||||||
|
public void start() {
|
||||||
|
triggerCallbackThread = new Thread(new Runnable() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while(true){
|
while(!toStop){
|
||||||
try {
|
try {
|
||||||
HandleCallbackParam callback = callBackQueue.take();
|
HandleCallbackParam callback = getInstance().callBackQueue.take();
|
||||||
if (callback != null) {
|
if (callback != null) {
|
||||||
for (String address : callback.getLogAddress()) {
|
for (String address : callback.getLogAddress()) {
|
||||||
try {
|
try {
|
||||||
|
@ -44,10 +53,16 @@ public class TriggerCallbackThread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
});
|
||||||
|
triggerCallbackThread.setDaemon(true);
|
||||||
|
triggerCallbackThread.start();
|
||||||
}
|
}
|
||||||
|
public void toStop(){
|
||||||
|
toStop = true;
|
||||||
|
}
|
||||||
|
|
||||||
public static void pushCallBack(HandleCallbackParam callback){
|
public static void pushCallBack(HandleCallbackParam callback){
|
||||||
callBackQueue.add(callback);
|
getInstance().callBackQueue.add(callback);
|
||||||
logger.debug(">>>>>>>>>>> xxl-job, push callback request, logId:{}", callback.getLogId());
|
logger.debug(">>>>>>>>>>> xxl-job, push callback request, logId:{}", callback.getLogId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue