jobhandler加载方式更新

v1.3
xueli.xue 9 years ago
parent 7b402a3c8b
commit 0f4c412fcd

@ -24,7 +24,7 @@ import com.xxl.job.core.util.MailUtil;
public class JobMonitorHelper { public class JobMonitorHelper {
private static Logger logger = LoggerFactory.getLogger(JobMonitorHelper.class); 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 ExecutorService executor = Executors.newCachedThreadPool();
private LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>(0xfff8); private LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>(0xfff8);
private ConcurrentHashMap<String, Integer> countMap = new ConcurrentHashMap<String, Integer>(); 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_NAME, jobDataMap.get(HandlerRepository.HANDLER_NAME));
params.put(HandlerRepository.HANDLER_PARAMS, jobDataMap.get(HandlerRepository.HANDLER_PARAMS)); 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); String handler_address = jobDataMap.get(HandlerRepository.HANDLER_ADDRESS);
if (!handler_address.startsWith("http")){ if (!handler_address.startsWith("http")){
handler_address = "http://" + handler_address + "/"; handler_address = "http://" + handler_address + "/";

@ -1,5 +1,5 @@
# for trigger log callback # for trigger log callback
trigger_log_url=http://localhost:8080/joblog/save trigger_log_url=http://localhost/joblog/save
# for email # for email
mail.host=smtp.163.com mail.host=smtp.163.com

@ -6,25 +6,22 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; 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.IJobHandler;
import com.xxl.job.client.handler.JobHander;
/** /**
* demo job handler * demo job handler
* @author xuxueli 2015-12-19 19:43:36 * @author xuxueli 2015-12-19 19:43:36
*/ */
@JobHander(jobName="demoJobHandler")
@Service @Service
public class DemoJobHandler extends IJobHandler { public class DemoJobHandler extends IJobHandler {
private static transient Logger logger = LoggerFactory.getLogger(DemoJobHandler.class); private static transient Logger logger = LoggerFactory.getLogger(DemoJobHandler.class);
public DemoJobHandler() {
HandlerRepository.regist("demoJobHandler", this);
}
@Override @Override
public JobHandleStatus handle(String... params) throws Exception { public JobHandleStatus handle(String... params) throws Exception {
logger.info(" ... params:" + params); logger.info(" ... params:" + params);
for (int i = 0; i < 60; i++) { for (int i = 0; i < 5; i++) {
TimeUnit.SECONDS.sleep(1); TimeUnit.SECONDS.sleep(1);
logger.info("handler run:{}", i); logger.info("handler run:{}", i);
} }

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

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

@ -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; package com.xxl.job.client.netcom.jetty;
import java.util.Map;
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;
@ -8,11 +10,18 @@ import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.util.thread.ExecutorThreadPool; import org.eclipse.jetty.util.thread.ExecutorThreadPool;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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. * 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 static final Logger logger = LoggerFactory.getLogger(XxlJobJettyServer.class);
private int port = 9999; 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; //package com.xxl.job.client.netcom.servlet;
//
//
import java.io.IOException; //import java.io.IOException;
import java.util.HashMap; //import java.util.HashMap;
import java.util.Map; //import java.util.Map;
//
import javax.servlet.ServletException; //import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; //import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; //import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; //import javax.servlet.http.HttpServletResponse;
//
import com.xxl.job.client.handler.HandlerRepository; //import com.xxl.job.client.handler.HandlerRepository;
//
//
/** ///**
* remote job client on http // * remote job client on http
* @author xuxueli 2015-12-19 18:36:47 // * @author xuxueli 2015-12-19 18:36:47
*/ // */
public class XxlJobServlet extends HttpServlet { //@Deprecated
private static final long serialVersionUID = 1L; //public class XxlJobServlet extends HttpServlet {
// private static final long serialVersionUID = 1L;
/** //
* Default constructor. // /**
*/ // * Default constructor.
public XxlJobServlet() { // */
// TODO Auto-generated constructor stub // public XxlJobServlet() {
} // // TODO Auto-generated constructor stub
// }
/** //
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) // /**
*/ // * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // */
request.setCharacterEncoding("UTF-8"); // protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8"); // request.setCharacterEncoding("UTF-8");
// response.setCharacterEncoding("UTF-8");
Map<String, String> _param = new HashMap<String, String>(); //
if (request.getParameterMap()!=null && request.getParameterMap().size()>0) { // Map<String, String> _param = new HashMap<String, String>();
for (Object paramKey : request.getParameterMap().keySet()) { // if (request.getParameterMap()!=null && request.getParameterMap().size()>0) {
if (paramKey!=null) { // for (Object paramKey : request.getParameterMap().keySet()) {
String paramKeyStr = paramKey.toString(); // if (paramKey!=null) {
_param.put(paramKeyStr, request.getParameter(paramKeyStr)); // String paramKeyStr = paramKey.toString();
} // _param.put(paramKeyStr, request.getParameter(paramKeyStr));
} // }
} // }
// }
String resp = HandlerRepository.service(_param); //
response.getWriter().append(resp); // String resp = HandlerRepository.service(_param);
return; // response.getWriter().append(resp);
} // return;
// }
/** //
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) // /**
*/ // * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // */
// TODO Auto-generated method stub // protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response); // // TODO Auto-generated method stub
} // doGet(request, response);
// }
} //
//}

Loading…
Cancel
Save