|  | @@ -1,8 +1,10 @@
 | 
	
		
			
				|  |  |  package com.xxl.job.core.executor.jetty;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -import com.xxl.job.core.router.HandlerRouter;
 | 
	
		
			
				|  |  |  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.router.HandlerRouter;
 | 
	
		
			
				|  |  | +import com.xxl.job.core.util.IpUtil;
 | 
	
		
			
				|  |  |  import org.eclipse.jetty.server.Connector;
 | 
	
		
			
				|  |  |  import org.eclipse.jetty.server.Handler;
 | 
	
		
			
				|  |  |  import org.eclipse.jetty.server.Server;
 | 
	
	
		
			
				|  | @@ -16,6 +18,7 @@ import org.springframework.context.ApplicationContext;
 | 
	
		
			
				|  |  |  import org.springframework.context.ApplicationContextAware;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import java.util.Map;
 | 
	
		
			
				|  |  | +import java.util.concurrent.TimeUnit;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /**
 | 
	
		
			
				|  |  |   * Created by xuxueli on 2016/3/2 21:14.
 | 
	
	
		
			
				|  | @@ -24,15 +27,23 @@ public class XxlJobExecutor implements ApplicationContextAware {
 | 
	
		
			
				|  |  |      private static final Logger logger = LoggerFactory.getLogger(XxlJobExecutor.class);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      private int port = 9999;
 | 
	
		
			
				|  |  | +    private String appName;
 | 
	
		
			
				|  |  | +    private RegistHelper registHelper;
 | 
	
		
			
				|  |  |      public void setPort(int port) {
 | 
	
		
			
				|  |  |          this.port = port;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | +    public void setAppName(String appName) {
 | 
	
		
			
				|  |  | +        this.appName = appName;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    public void setRegistHelper(RegistHelper registHelper) {
 | 
	
		
			
				|  |  | +        this.registHelper = registHelper;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      // ---------------------------------- job server ------------------------------------
 | 
	
		
			
				|  |  |      Server server = null;
 | 
	
		
			
				|  |  |      public void start() throws Exception {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        new Thread(new Runnable() {
 | 
	
		
			
				|  |  | +        Thread executorTnread = new Thread(new Runnable() {
 | 
	
		
			
				|  |  |              @Override
 | 
	
		
			
				|  |  |              public void run() {
 | 
	
		
			
				|  |  |                  server = new Server();
 | 
	
	
		
			
				|  | @@ -52,14 +63,16 @@ public class XxlJobExecutor implements ApplicationContextAware {
 | 
	
		
			
				|  |  |                  try {
 | 
	
		
			
				|  |  |                      server.start();
 | 
	
		
			
				|  |  |                      logger.info(">>>>>>>>>>>> xxl-job jetty server start success at port:{}.", port);
 | 
	
		
			
				|  |  | -                    server.join();  // block until server ready
 | 
	
		
			
				|  |  | +                    registryBeat();
 | 
	
		
			
				|  |  | +                    server.join();  // block until thread stopped
 | 
	
		
			
				|  |  |                      logger.info(">>>>>>>>>>>> xxl-job jetty server join success at port:{}.", port);
 | 
	
		
			
				|  |  |                  } catch (Exception e) {
 | 
	
		
			
				|  |  |                      e.printStackTrace();
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | -        }).start();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +        executorTnread.setDaemon(true); // daemon, service jvm, user thread leave >>> daemon leave >>> jvm leave
 | 
	
		
			
				|  |  | +        executorTnread.start();
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |      
 | 
	
		
			
				|  |  |      public void destroy(){
 | 
	
	
		
			
				|  | @@ -72,6 +85,28 @@ public class XxlJobExecutor implements ApplicationContextAware {
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    private void registryBeat(){
 | 
	
		
			
				|  |  | +        if (registHelper==null && appName==null || appName.trim().length()==0) {
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        Thread registryThread = new Thread(new Runnable() {
 | 
	
		
			
				|  |  | +            @Override
 | 
	
		
			
				|  |  | +            public void run() {
 | 
	
		
			
				|  |  | +                while (true) {
 | 
	
		
			
				|  |  | +                    try {
 | 
	
		
			
				|  |  | +                        String address = IpUtil.getIp().concat(":").concat(String.valueOf(port));
 | 
	
		
			
				|  |  | +                        registHelper.registry(RegistHelper.RegistType.EXECUTOR.name(), appName, address);
 | 
	
		
			
				|  |  | +                        TimeUnit.SECONDS.sleep(15);
 | 
	
		
			
				|  |  | +                    } catch (Exception e) {
 | 
	
		
			
				|  |  | +                        e.printStackTrace();
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +        registryThread.setDaemon(true);
 | 
	
		
			
				|  |  | +        registryThread.start();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      // ---------------------------------- init job handler ------------------------------------
 | 
	
		
			
				|  |  |      public static ApplicationContext applicationContext;
 | 
	
		
			
				|  |  |  	@Override
 |