jobhandler加载方式更新

master
xueli.xue 9 years ago
parent 7b402a3c8b
commit 0f4c412fcd
  1. 2
      xxl-job-admin/src/main/java/com/xxl/job/core/thread/JobMonitorHelper.java
  2. 2
      xxl-job-admin/src/main/java/com/xxl/job/service/job/RemoteHttpJobBean.java
  3. 2
      xxl-job-admin/src/main/resources/config.properties
  4. 9
      xxl-job-client-demo/src/main/java/com/xxl/job/service/handler/DemoJobHandler.java
  5. 2
      xxl-job-client-demo/src/main/webapp/WEB-INF/web.xml
  6. 8
      xxl-job-client/pom.xml
  7. 18
      xxl-job-client/src/main/java/com/xxl/job/client/handler/JobHander.java
  8. 34
      xxl-job-client/src/main/java/com/xxl/job/client/netcom/jetty/XxlJobJettyServer.java
  9. 121
      xxl-job-client/src/main/java/com/xxl/job/client/netcom/servlet/XxlJobServlet.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<Integer> queue = new LinkedBlockingQueue<Integer>(0xfff8);
private ConcurrentHashMap<String, Integer> countMap = new ConcurrentHashMap<String, Integer>();

@ -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 + "/";

@ -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

@ -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);
}

@ -20,7 +20,7 @@
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Servlet任务执行器,推荐使用Jetty任务执行器 -->
<!-- [@Deprecated] Servlet任务执行器,推荐使用Jetty任务执行器 -->
<!--
<servlet>
<servlet-name>XxlJobServlet</servlet-name>

@ -52,6 +52,14 @@
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<!-- spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.14.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>

@ -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();
}

@ -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<String, Object> 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);
}
}
}
}
}

@ -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<String, String> _param = new HashMap<String, String>();
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<String, String> _param = new HashMap<String, String>();
// 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);
// }
//
//}

Loading…
Cancel
Save