code optimize

master
xueli.xue 3 years ago
parent 8e102f8c3e
commit f7fa721690
  1. 1
      xxl-job-admin/src/main/java/com/xxl/job/admin/core/alarm/impl/EmailJobAlarm.java
  2. 105
      xxl-job-core/src/main/java/com/xxl/job/core/executor/XxlJobExecutor.java
  3. 5
      xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSimpleExecutor.java
  4. 5
      xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSpringExecutor.java
  5. 2
      xxl-job-core/src/main/java/com/xxl/job/core/thread/ExecutorRegistryThread.java
  6. 2
      xxl-job-core/src/main/java/com/xxl/job/core/thread/JobLogFileCleanThread.java
  7. 4
      xxl-job-core/src/main/java/com/xxl/job/core/thread/TriggerCallbackThread.java
  8. 4
      xxl-job-core/src/main/java/com/xxl/job/core/util/XxlJobRemotingUtil.java
  9. 4
      xxl-job-executor-samples/xxl-job-executor-sample-frameless/src/main/java/com/xxl/job/executor/sample/frameless/FramelessApplication.java
  10. 4
      xxl-job-executor-samples/xxl-job-executor-sample-frameless/src/main/java/com/xxl/job/executor/sample/frameless/config/FrameLessXxlJobConfig.java
  11. 2
      xxl-job-executor-samples/xxl-job-executor-sample-frameless/src/main/java/com/xxl/job/executor/sample/frameless/jobhandler/SampleXxlJob.java
  12. 20
      xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java

@ -32,6 +32,7 @@ public class EmailJobAlarm implements JobAlarm {
* *
* @param jobLog * @param jobLog
*/ */
@Override
public boolean doAlarm(XxlJobInfo info, XxlJobLog jobLog){ public boolean doAlarm(XxlJobInfo info, XxlJobLog jobLog){
boolean alarmResult = true; boolean alarmResult = true;

@ -84,10 +84,10 @@ public class XxlJobExecutor {
initEmbedServer(address, ip, port, appname, accessToken); initEmbedServer(address, ip, port, appname, accessToken);
} }
public void destroy(){ public void destroy(){
// destory executor-server // destroy executor-server
stopEmbedServer(); stopEmbedServer();
// destory jobThreadRepository // destroy jobThreadRepository
if (jobThreadRepository.size() > 0) { if (jobThreadRepository.size() > 0) {
for (Map.Entry<Integer, JobThread> item: jobThreadRepository.entrySet()) { for (Map.Entry<Integer, JobThread> item: jobThreadRepository.entrySet()) {
JobThread oldJobThread = removeJobThread(item.getKey(), "web container destroy and kill the job."); JobThread oldJobThread = removeJobThread(item.getKey(), "web container destroy and kill the job.");
@ -105,58 +105,14 @@ public class XxlJobExecutor {
jobHandlerRepository.clear(); jobHandlerRepository.clear();
// destory JobLogFileCleanThread // destroy JobLogFileCleanThread
JobLogFileCleanThread.getInstance().toStop(); JobLogFileCleanThread.getInstance().toStop();
// destory TriggerCallbackThread // destroy TriggerCallbackThread
TriggerCallbackThread.getInstance().toStop(); TriggerCallbackThread.getInstance().toStop();
} }
protected void registerJobHandler(XxlJob xxlJob, Object bean, Method executeMethod){
if (xxlJob == null) {
return;
}
String name = xxlJob.value();
//make and simplify the variables since they'll be called several times later
Class<?> clazz = bean.getClass();
String methodName = executeMethod.getName();
if (name.trim().length() == 0) {
throw new RuntimeException("xxl-job method-jobhandler name invalid, for[" + clazz + "#" + methodName + "] .");
}
if (loadJobHandler(name) != null) {
throw new RuntimeException("xxl-job jobhandler[" + name + "] naming conflicts.");
}
executeMethod.setAccessible(true);
// init and destroy
Method initMethod = null;
Method destroyMethod = null;
if (xxlJob.init().trim().length() > 0) {
try {
initMethod = clazz.getDeclaredMethod(xxlJob.init());
initMethod.setAccessible(true);
} catch (NoSuchMethodException e) {
throw new RuntimeException("xxl-job method-jobhandler initMethod invalid, for[" + clazz + "#" + methodName + "] .");
}
}
if (xxlJob.destroy().trim().length() > 0) {
try {
destroyMethod = clazz.getDeclaredMethod(xxlJob.destroy());
destroyMethod.setAccessible(true);
} catch (NoSuchMethodException e) {
throw new RuntimeException("xxl-job method-jobhandler destroyMethod invalid, for[" + clazz + "#" + methodName + "] .");
}
}
// registry jobhandler
registJobHandler(name, new MethodJobHandler(bean, executeMethod, initMethod, destroyMethod));
}
// ---------------------- admin-client (rpc invoker) ---------------------- // ---------------------- admin-client (rpc invoker) ----------------------
private static List<AdminBiz> adminBizList; private static List<AdminBiz> adminBizList;
@ -225,6 +181,59 @@ public class XxlJobExecutor {
logger.info(">>>>>>>>>>> xxl-job register jobhandler success, name:{}, jobHandler:{}", name, jobHandler); logger.info(">>>>>>>>>>> xxl-job register jobhandler success, name:{}, jobHandler:{}", name, jobHandler);
return jobHandlerRepository.put(name, jobHandler); return jobHandlerRepository.put(name, jobHandler);
} }
protected void registJobHandler(XxlJob xxlJob, Object bean, Method executeMethod){
if (xxlJob == null) {
return;
}
String name = xxlJob.value();
//make and simplify the variables since they'll be called several times later
Class<?> clazz = bean.getClass();
String methodName = executeMethod.getName();
if (name.trim().length() == 0) {
throw new RuntimeException("xxl-job method-jobhandler name invalid, for[" + clazz + "#" + methodName + "] .");
}
if (loadJobHandler(name) != null) {
throw new RuntimeException("xxl-job jobhandler[" + name + "] naming conflicts.");
}
// execute method
/*if (!(method.getParameterTypes().length == 1 && method.getParameterTypes()[0].isAssignableFrom(String.class))) {
throw new RuntimeException("xxl-job method-jobhandler param-classtype invalid, for[" + bean.getClass() + "#" + method.getName() + "] , " +
"The correct method format like \" public ReturnT<String> execute(String param) \" .");
}
if (!method.getReturnType().isAssignableFrom(ReturnT.class)) {
throw new RuntimeException("xxl-job method-jobhandler return-classtype invalid, for[" + bean.getClass() + "#" + method.getName() + "] , " +
"The correct method format like \" public ReturnT<String> execute(String param) \" .");
}*/
executeMethod.setAccessible(true);
// init and destroy
Method initMethod = null;
Method destroyMethod = null;
if (xxlJob.init().trim().length() > 0) {
try {
initMethod = clazz.getDeclaredMethod(xxlJob.init());
initMethod.setAccessible(true);
} catch (NoSuchMethodException e) {
throw new RuntimeException("xxl-job method-jobhandler initMethod invalid, for[" + clazz + "#" + methodName + "] .");
}
}
if (xxlJob.destroy().trim().length() > 0) {
try {
destroyMethod = clazz.getDeclaredMethod(xxlJob.destroy());
destroyMethod.setAccessible(true);
} catch (NoSuchMethodException e) {
throw new RuntimeException("xxl-job method-jobhandler destroyMethod invalid, for[" + clazz + "#" + methodName + "] .");
}
}
// registry jobhandler
registJobHandler(name, new MethodJobHandler(bean, executeMethod, initMethod, destroyMethod));
}
// ---------------------- job thread repository ---------------------- // ---------------------- job thread repository ----------------------

@ -30,6 +30,7 @@ public class XxlJobSimpleExecutor extends XxlJobExecutor {
} }
@Override
public void start() { public void start() {
// init JobHandler Repository (for method) // init JobHandler Repository (for method)
@ -43,6 +44,7 @@ public class XxlJobSimpleExecutor extends XxlJobExecutor {
} }
} }
@Override
public void destroy() { public void destroy() {
super.destroy(); super.destroy();
} }
@ -62,7 +64,8 @@ public class XxlJobSimpleExecutor extends XxlJobExecutor {
} }
for (Method executeMethod : methods) { for (Method executeMethod : methods) {
XxlJob xxlJob = executeMethod.getAnnotation(XxlJob.class); XxlJob xxlJob = executeMethod.getAnnotation(XxlJob.class);
registerJobHandler(xxlJob, bean, executeMethod); // registry
registJobHandler(xxlJob, bean, executeMethod);
} }
} }

@ -105,7 +105,8 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC
for (Map.Entry<Method, XxlJob> methodXxlJobEntry : annotatedMethods.entrySet()) { for (Map.Entry<Method, XxlJob> methodXxlJobEntry : annotatedMethods.entrySet()) {
Method executeMethod = methodXxlJobEntry.getKey(); Method executeMethod = methodXxlJobEntry.getKey();
XxlJob xxlJob = methodXxlJobEntry.getValue(); XxlJob xxlJob = methodXxlJobEntry.getValue();
registerJobHandler(xxlJob, bean, executeMethod); // regist
registJobHandler(xxlJob, bean, executeMethod);
} }
} }
} }
@ -115,7 +116,7 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC
@Override @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext; XxlJobSpringExecutor.applicationContext = applicationContext;
} }
public static ApplicationContext getApplicationContext() { public static ApplicationContext getApplicationContext() {

@ -102,7 +102,7 @@ public class ExecutorRegistryThread {
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
} }
} }
logger.info(">>>>>>>>>>> xxl-job, executor registry thread destory."); logger.info(">>>>>>>>>>> xxl-job, executor registry thread destroy.");
} }
}); });

@ -96,7 +96,7 @@ public class JobLogFileCleanThread {
} }
} }
} }
logger.info(">>>>>>>>>>> xxl-job, executor JobLogFileCleanThread thread destory."); logger.info(">>>>>>>>>>> xxl-job, executor JobLogFileCleanThread thread destroy.");
} }
}); });

@ -95,7 +95,7 @@ public class TriggerCallbackThread {
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
} }
} }
logger.info(">>>>>>>>>>> xxl-job, executor callback thread destory."); logger.info(">>>>>>>>>>> xxl-job, executor callback thread destroy.");
} }
}); });
@ -125,7 +125,7 @@ public class TriggerCallbackThread {
} }
} }
} }
logger.info(">>>>>>>>>>> xxl-job, executor retry callback thread destory."); logger.info(">>>>>>>>>>> xxl-job, executor retry callback thread destroy.");
} }
}); });
triggerRetryCallbackThread.setDaemon(true); triggerRetryCallbackThread.setDaemon(true);

@ -34,17 +34,21 @@ public class XxlJobRemotingUtil {
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
} }
connection.setHostnameVerifier(new HostnameVerifier() { connection.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) { public boolean verify(String hostname, SSLSession session) {
return true; return true;
} }
}); });
} }
private static final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { private static final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() { public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{}; return new java.security.cert.X509Certificate[]{};
} }
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
} }
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
} }
}}; }};

@ -29,8 +29,8 @@ public class FramelessApplication {
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
} finally { } finally {
// destory // destroy
FrameLessXxlJobConfig.getInstance().destoryXxlJobExecutor(); FrameLessXxlJobConfig.getInstance().destroyXxlJobExecutor();
} }
} }

@ -56,9 +56,9 @@ public class FrameLessXxlJobConfig {
} }
/** /**
* destory * destroy
*/ */
public void destoryXxlJobExecutor() { public void destroyXxlJobExecutor() {
if (xxlJobExecutor != null) { if (xxlJobExecutor != null) {
xxlJobExecutor.destroy(); xxlJobExecutor.destroy();
} }

@ -244,7 +244,7 @@ public class SampleXxlJob {
logger.info("init"); logger.info("init");
} }
public void destroy(){ public void destroy(){
logger.info("destory"); logger.info("destroy");
} }

@ -245,29 +245,9 @@ public class SampleXxlJob {
public void init(){ public void init(){
logger.info("init"); logger.info("init");
} }
public void destroy(){ public void destroy(){
logger.info("destroy"); logger.info("destroy");
} }
/**
* 6生命周期任务示例任务初始化与销毁时支持private方法但不推荐
*/
@XxlJob(value = "demoJobHandler3", init = "initForDemoJobHandler3", destroy = "destroyForDemoJobHandler3")
public void demoJobHandler3() throws Exception {
XxlJobHelper.log("XXL-JOB, Hello World.");
}
private void initForDemoJobHandler3(){
logger.info("initForDemoJobHandler3");
}
/**
* 演示private方法也可以被访问且执行
*/
public void destroyForDemoJobHandler3(){
logger.info("destroyForDemoJobHandler3");
}
} }

Loading…
Cancel
Save