From 0f4c412fcdf027b4376d49ce5c56d4010cf34db2 Mon Sep 17 00:00:00 2001 From: "xueli.xue" Date: Tue, 17 May 2016 21:35:24 +0800 Subject: [PATCH] =?UTF-8?q?jobhandler=E5=8A=A0=E8=BD=BD=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xxl/job/core/thread/JobMonitorHelper.java | 2 +- .../job/service/job/RemoteHttpJobBean.java | 2 +- .../src/main/resources/config.properties | 2 +- .../job/service/handler/DemoJobHandler.java | 9 +- .../src/main/webapp/WEB-INF/web.xml | 2 +- xxl-job-client/pom.xml | 8 ++ .../com/xxl/job/client/handler/JobHander.java | 18 +++ .../netcom/jetty/XxlJobJettyServer.java | 34 ++++- .../client/netcom/servlet/XxlJobServlet.java | 121 +++++++++--------- 9 files changed, 127 insertions(+), 71 deletions(-) create mode 100644 xxl-job-client/src/main/java/com/xxl/job/client/handler/JobHander.java diff --git a/xxl-job-admin/src/main/java/com/xxl/job/core/thread/JobMonitorHelper.java b/xxl-job-admin/src/main/java/com/xxl/job/core/thread/JobMonitorHelper.java index 6cf209b7..c55d492d 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/core/thread/JobMonitorHelper.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/core/thread/JobMonitorHelper.java @@ -24,7 +24,7 @@ import com.xxl.job.core.util.MailUtil; public class JobMonitorHelper { private static Logger logger = LoggerFactory.getLogger(JobMonitorHelper.class); - public static JobMonitorHelper helper = new JobMonitorHelper(); + private static JobMonitorHelper helper = new JobMonitorHelper(); private ExecutorService executor = Executors.newCachedThreadPool(); private LinkedBlockingQueue queue = new LinkedBlockingQueue(0xfff8); private ConcurrentHashMap countMap = new ConcurrentHashMap(); diff --git a/xxl-job-admin/src/main/java/com/xxl/job/service/job/RemoteHttpJobBean.java b/xxl-job-admin/src/main/java/com/xxl/job/service/job/RemoteHttpJobBean.java index dab63033..07fba476 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/service/job/RemoteHttpJobBean.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/service/job/RemoteHttpJobBean.java @@ -62,7 +62,7 @@ public class RemoteHttpJobBean extends QuartzJobBean { params.put(HandlerRepository.HANDLER_NAME, jobDataMap.get(HandlerRepository.HANDLER_NAME)); params.put(HandlerRepository.HANDLER_PARAMS, jobDataMap.get(HandlerRepository.HANDLER_PARAMS)); - // handler address, jetty or servlet + // handler address, jetty (servlet dead) String handler_address = jobDataMap.get(HandlerRepository.HANDLER_ADDRESS); if (!handler_address.startsWith("http")){ handler_address = "http://" + handler_address + "/"; diff --git a/xxl-job-admin/src/main/resources/config.properties b/xxl-job-admin/src/main/resources/config.properties index 574549dc..56968991 100644 --- a/xxl-job-admin/src/main/resources/config.properties +++ b/xxl-job-admin/src/main/resources/config.properties @@ -1,5 +1,5 @@ # for trigger log callback -trigger_log_url=http://localhost:8080/joblog/save +trigger_log_url=http://localhost/joblog/save # for email mail.host=smtp.163.com diff --git a/xxl-job-client-demo/src/main/java/com/xxl/job/service/handler/DemoJobHandler.java b/xxl-job-client-demo/src/main/java/com/xxl/job/service/handler/DemoJobHandler.java index 611596e6..7106b0d4 100644 --- a/xxl-job-client-demo/src/main/java/com/xxl/job/service/handler/DemoJobHandler.java +++ b/xxl-job-client-demo/src/main/java/com/xxl/job/service/handler/DemoJobHandler.java @@ -6,25 +6,22 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; -import com.xxl.job.client.handler.HandlerRepository; import com.xxl.job.client.handler.IJobHandler; +import com.xxl.job.client.handler.JobHander; /** * demo job handler * @author xuxueli 2015-12-19 19:43:36 */ +@JobHander(jobName="demoJobHandler") @Service public class DemoJobHandler extends IJobHandler { private static transient Logger logger = LoggerFactory.getLogger(DemoJobHandler.class); - public DemoJobHandler() { - HandlerRepository.regist("demoJobHandler", this); - } - @Override public JobHandleStatus handle(String... params) throws Exception { logger.info(" ... params:" + params); - for (int i = 0; i < 60; i++) { + for (int i = 0; i < 5; i++) { TimeUnit.SECONDS.sleep(1); logger.info("handler run:{}", i); } diff --git a/xxl-job-client-demo/src/main/webapp/WEB-INF/web.xml b/xxl-job-client-demo/src/main/webapp/WEB-INF/web.xml index 1c00c952..60e82ff8 100644 --- a/xxl-job-client-demo/src/main/webapp/WEB-INF/web.xml +++ b/xxl-job-client-demo/src/main/webapp/WEB-INF/web.xml @@ -20,7 +20,7 @@ org.springframework.web.context.ContextLoaderListener - + + + org.springframework + spring-context + 3.2.14.RELEASE + compile + diff --git a/xxl-job-client/src/main/java/com/xxl/job/client/handler/JobHander.java b/xxl-job-client/src/main/java/com/xxl/job/client/handler/JobHander.java new file mode 100644 index 00000000..3009f62a --- /dev/null +++ b/xxl-job-client/src/main/java/com/xxl/job/client/handler/JobHander.java @@ -0,0 +1,18 @@ +package com.xxl.job.client.handler; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * annotation for job handler + * @author 2016-5-17 21:06:49 + */ +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface JobHander { + + String jobName(); + +} diff --git a/xxl-job-client/src/main/java/com/xxl/job/client/netcom/jetty/XxlJobJettyServer.java b/xxl-job-client/src/main/java/com/xxl/job/client/netcom/jetty/XxlJobJettyServer.java index 29756b24..7e96eb23 100644 --- a/xxl-job-client/src/main/java/com/xxl/job/client/netcom/jetty/XxlJobJettyServer.java +++ b/xxl-job-client/src/main/java/com/xxl/job/client/netcom/jetty/XxlJobJettyServer.java @@ -1,5 +1,7 @@ package com.xxl.job.client.netcom.jetty; +import java.util.Map; + import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Server; @@ -8,11 +10,18 @@ import org.eclipse.jetty.server.nio.SelectChannelConnector; import org.eclipse.jetty.util.thread.ExecutorThreadPool; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +import com.xxl.job.client.handler.HandlerRepository; +import com.xxl.job.client.handler.IJobHandler; +import com.xxl.job.client.handler.JobHander; /** * Created by xuxueli on 2016/3/2 21:14. */ -public class XxlJobJettyServer { +public class XxlJobJettyServer implements ApplicationContextAware { private static final Logger logger = LoggerFactory.getLogger(XxlJobJettyServer.class); private int port = 9999; @@ -52,4 +61,27 @@ public class XxlJobJettyServer { } + public static ApplicationContext applicationContext; + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + XxlJobJettyServer.applicationContext = applicationContext; + initJobHandler(); + } + + /** + * init job handler service + */ + public void initJobHandler(){ + Map serviceBeanMap = XxlJobJettyServer.applicationContext.getBeansWithAnnotation(JobHander.class); + if (serviceBeanMap!=null && serviceBeanMap.size()>0) { + for (Object serviceBean : serviceBeanMap.values()) { + String jobName = serviceBean.getClass().getAnnotation(JobHander.class).jobName(); + if (jobName!=null && jobName.trim().length()>0 && serviceBean instanceof IJobHandler) { + IJobHandler handler = (IJobHandler) serviceBean; + HandlerRepository.regist(jobName, handler); + } + } + } + } + } diff --git a/xxl-job-client/src/main/java/com/xxl/job/client/netcom/servlet/XxlJobServlet.java b/xxl-job-client/src/main/java/com/xxl/job/client/netcom/servlet/XxlJobServlet.java index 47f42df1..7051bb18 100644 --- a/xxl-job-client/src/main/java/com/xxl/job/client/netcom/servlet/XxlJobServlet.java +++ b/xxl-job-client/src/main/java/com/xxl/job/client/netcom/servlet/XxlJobServlet.java @@ -1,60 +1,61 @@ -package com.xxl.job.client.netcom.servlet; - - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import com.xxl.job.client.handler.HandlerRepository; - - -/** - * remote job client on http - * @author xuxueli 2015-12-19 18:36:47 - */ -public class XxlJobServlet extends HttpServlet { - private static final long serialVersionUID = 1L; - - /** - * Default constructor. - */ - public XxlJobServlet() { - // TODO Auto-generated constructor stub - } - - /** - * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) - */ - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - request.setCharacterEncoding("UTF-8"); - response.setCharacterEncoding("UTF-8"); - - Map _param = new HashMap(); - if (request.getParameterMap()!=null && request.getParameterMap().size()>0) { - for (Object paramKey : request.getParameterMap().keySet()) { - if (paramKey!=null) { - String paramKeyStr = paramKey.toString(); - _param.put(paramKeyStr, request.getParameter(paramKeyStr)); - } - } - } - - String resp = HandlerRepository.service(_param); - response.getWriter().append(resp); - return; - } - - /** - * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) - */ - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - // TODO Auto-generated method stub - doGet(request, response); - } - -} +//package com.xxl.job.client.netcom.servlet; +// +// +//import java.io.IOException; +//import java.util.HashMap; +//import java.util.Map; +// +//import javax.servlet.ServletException; +//import javax.servlet.http.HttpServlet; +//import javax.servlet.http.HttpServletRequest; +//import javax.servlet.http.HttpServletResponse; +// +//import com.xxl.job.client.handler.HandlerRepository; +// +// +///** +// * remote job client on http +// * @author xuxueli 2015-12-19 18:36:47 +// */ +//@Deprecated +//public class XxlJobServlet extends HttpServlet { +// private static final long serialVersionUID = 1L; +// +// /** +// * Default constructor. +// */ +// public XxlJobServlet() { +// // TODO Auto-generated constructor stub +// } +// +// /** +// * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) +// */ +// protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { +// request.setCharacterEncoding("UTF-8"); +// response.setCharacterEncoding("UTF-8"); +// +// Map _param = new HashMap(); +// if (request.getParameterMap()!=null && request.getParameterMap().size()>0) { +// for (Object paramKey : request.getParameterMap().keySet()) { +// if (paramKey!=null) { +// String paramKeyStr = paramKey.toString(); +// _param.put(paramKeyStr, request.getParameter(paramKeyStr)); +// } +// } +// } +// +// String resp = HandlerRepository.service(_param); +// response.getWriter().append(resp); +// return; +// } +// +// /** +// * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) +// */ +// protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { +// // TODO Auto-generated method stub +// doGet(request, response); +// } +// +//}