底层线程模型统一;destory逻辑优化;

pull/1/head
xueli.xue 8 years ago
parent b8e196606d
commit 79f9317ffe

@ -78,10 +78,10 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware, In
// destroy
public void destroy(){
// admin registry stop
JobRegistryHelper.getInstance().stop();
JobRegistryHelper.getInstance().toStop();
// admin monitor stop
JobMonitorHelper.getInstance().stop();
JobMonitorHelper.getInstance().toStop();
serverFactory.destroy();
}

@ -36,7 +36,7 @@ public class JobMonitorHelper {
@Override
public void run() {
while (true) {
while (!toStop) {
try {
logger.debug(">>>>>>>>>>> job monitor beat ... ");
Integer jobLogId = JobMonitorHelper.instance.queue.take();
@ -81,7 +81,7 @@ public class JobMonitorHelper {
monitorThread.start();
}
public void stop(){
public void toStop(){
toStop = true;
//monitorThread.interrupt();
}

@ -70,7 +70,7 @@ public class JobRegistryHelper {
registryThread.start();
}
public void stop(){
public void toStop(){
toStop = true;
//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.registry.RegistHelper;
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.TriggerCallbackThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
@ -46,11 +48,33 @@ public class XxlJobExecutor implements ApplicationContextAware, ApplicationListe
// ---------------------------------- job server ------------------------------------
private NetComServerFactory serverFactory = new NetComServerFactory();
public void start() throws Exception {
// executor start
NetComServerFactory.putService(ExecutorBiz.class, new ExecutorBizImpl());
serverFactory.start(port, ip, appName, registHelper);
// trigger callback thread start
TriggerCallbackThread.getInstance().start();
}
public void destroy(){
// executor stop
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 ------------------------------------
@ -99,7 +123,8 @@ public class XxlJobExecutor implements ApplicationContextAware, ApplicationListe
return jobThread;
}
public static JobThread loadJobThread(String jobKey){
return JobThreadRepository.get(jobKey);
JobThread jobThread = JobThreadRepository.get(jobKey);
return jobThread;
}
public static void removeJobThread(String jobKey){
JobThreadRepository.remove(jobKey);

@ -1,7 +1,7 @@
package com.xxl.job.core.rpc.netcom.jetty.server;
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.Handler;
import org.eclipse.jetty.server.Server;
@ -11,8 +11,6 @@ import org.eclipse.jetty.util.thread.ExecutorThreadPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.TimeUnit;
/**
* rpc jetty server
* @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 Server server;
private Thread thread;
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
public void run() {
server = new Server();
@ -43,7 +41,7 @@ public class JettyServer {
try {
server.start();
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
logger.info(">>>>>>>>>>> xxl-rpc server start success, netcon={}, port={}", JettyServer.class.getName(), port);
} catch (Exception e) {
@ -65,43 +63,10 @@ public class JettyServer {
logger.error("", e);
}
}
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;
if (thread.isAlive()) {
thread.interrupt();
}
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();
logger.info(">>>>>>>>>>> xxl-rpc server destroy success, netcon={}", JettyServer.class.getName());
}
}

@ -1,8 +1,53 @@
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.
*/
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 {
private static Logger logger = LoggerFactory.getLogger(TriggerCallbackThread.class);
private static LinkedBlockingQueue<HandleCallbackParam> callBackQueue = new LinkedBlockingQueue<HandleCallbackParam>();
static {
new Thread(new Runnable() {
private static TriggerCallbackThread instance = new TriggerCallbackThread();
public static TriggerCallbackThread getInstance(){
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
public void run() {
while(true){
while(!toStop){
try {
HandleCallbackParam callback = callBackQueue.take();
HandleCallbackParam callback = getInstance().callBackQueue.take();
if (callback != null) {
for (String address : callback.getLogAddress()) {
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){
callBackQueue.add(callback);
getInstance().callBackQueue.add(callback);
logger.debug(">>>>>>>>>>> xxl-job, push callback request, logId:{}", callback.getLogId());
}

Loading…
Cancel
Save