Skip to content

Commit f7fa721

Browse files
author
xueli.xue
committed
code optimize
1 parent 8e102f8 commit f7fa721

File tree

12 files changed

+78
-80
lines changed

12 files changed

+78
-80
lines changed

xxl-job-admin/src/main/java/com/xxl/job/admin/core/alarm/impl/EmailJobAlarm.java

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class EmailJobAlarm implements JobAlarm {
3232
*
3333
* @param jobLog
3434
*/
35+
@Override
3536
public boolean doAlarm(XxlJobInfo info, XxlJobLog jobLog){
3637
boolean alarmResult = true;
3738

xxl-job-core/src/main/java/com/xxl/job/core/executor/XxlJobExecutor.java

+57-48
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ public void start() throws Exception {
8484
initEmbedServer(address, ip, port, appname, accessToken);
8585
}
8686
public void destroy(){
87-
// destory executor-server
87+
// destroy executor-server
8888
stopEmbedServer();
8989

90-
// destory jobThreadRepository
90+
// destroy jobThreadRepository
9191
if (jobThreadRepository.size() > 0) {
9292
for (Map.Entry<Integer, JobThread> item: jobThreadRepository.entrySet()) {
9393
JobThread oldJobThread = removeJobThread(item.getKey(), "web container destroy and kill the job.");
@@ -105,58 +105,14 @@ public void destroy(){
105105
jobHandlerRepository.clear();
106106

107107

108-
// destory JobLogFileCleanThread
108+
// destroy JobLogFileCleanThread
109109
JobLogFileCleanThread.getInstance().toStop();
110110

111-
// destory TriggerCallbackThread
111+
// destroy TriggerCallbackThread
112112
TriggerCallbackThread.getInstance().toStop();
113113

114114
}
115115

116-
protected void registerJobHandler(XxlJob xxlJob, Object bean, Method executeMethod){
117-
if (xxlJob == null) {
118-
return;
119-
}
120-
121-
String name = xxlJob.value();
122-
//make and simplify the variables since they'll be called several times later
123-
Class<?> clazz = bean.getClass();
124-
String methodName = executeMethod.getName();
125-
if (name.trim().length() == 0) {
126-
throw new RuntimeException("xxl-job method-jobhandler name invalid, for[" + clazz + "#" + methodName + "] .");
127-
}
128-
if (loadJobHandler(name) != null) {
129-
throw new RuntimeException("xxl-job jobhandler[" + name + "] naming conflicts.");
130-
}
131-
132-
executeMethod.setAccessible(true);
133-
134-
// init and destroy
135-
Method initMethod = null;
136-
Method destroyMethod = null;
137-
138-
if (xxlJob.init().trim().length() > 0) {
139-
try {
140-
initMethod = clazz.getDeclaredMethod(xxlJob.init());
141-
initMethod.setAccessible(true);
142-
} catch (NoSuchMethodException e) {
143-
throw new RuntimeException("xxl-job method-jobhandler initMethod invalid, for[" + clazz + "#" + methodName + "] .");
144-
}
145-
}
146-
if (xxlJob.destroy().trim().length() > 0) {
147-
try {
148-
destroyMethod = clazz.getDeclaredMethod(xxlJob.destroy());
149-
destroyMethod.setAccessible(true);
150-
} catch (NoSuchMethodException e) {
151-
throw new RuntimeException("xxl-job method-jobhandler destroyMethod invalid, for[" + clazz + "#" + methodName + "] .");
152-
}
153-
}
154-
155-
// registry jobhandler
156-
registJobHandler(name, new MethodJobHandler(bean, executeMethod, initMethod, destroyMethod));
157-
158-
}
159-
160116

161117
// ---------------------- admin-client (rpc invoker) ----------------------
162118
private static List<AdminBiz> adminBizList;
@@ -225,6 +181,59 @@ public static IJobHandler registJobHandler(String name, IJobHandler jobHandler){
225181
logger.info(">>>>>>>>>>> xxl-job register jobhandler success, name:{}, jobHandler:{}", name, jobHandler);
226182
return jobHandlerRepository.put(name, jobHandler);
227183
}
184+
protected void registJobHandler(XxlJob xxlJob, Object bean, Method executeMethod){
185+
if (xxlJob == null) {
186+
return;
187+
}
188+
189+
String name = xxlJob.value();
190+
//make and simplify the variables since they'll be called several times later
191+
Class<?> clazz = bean.getClass();
192+
String methodName = executeMethod.getName();
193+
if (name.trim().length() == 0) {
194+
throw new RuntimeException("xxl-job method-jobhandler name invalid, for[" + clazz + "#" + methodName + "] .");
195+
}
196+
if (loadJobHandler(name) != null) {
197+
throw new RuntimeException("xxl-job jobhandler[" + name + "] naming conflicts.");
198+
}
199+
200+
// execute method
201+
/*if (!(method.getParameterTypes().length == 1 && method.getParameterTypes()[0].isAssignableFrom(String.class))) {
202+
throw new RuntimeException("xxl-job method-jobhandler param-classtype invalid, for[" + bean.getClass() + "#" + method.getName() + "] , " +
203+
"The correct method format like \" public ReturnT<String> execute(String param) \" .");
204+
}
205+
if (!method.getReturnType().isAssignableFrom(ReturnT.class)) {
206+
throw new RuntimeException("xxl-job method-jobhandler return-classtype invalid, for[" + bean.getClass() + "#" + method.getName() + "] , " +
207+
"The correct method format like \" public ReturnT<String> execute(String param) \" .");
208+
}*/
209+
210+
executeMethod.setAccessible(true);
211+
212+
// init and destroy
213+
Method initMethod = null;
214+
Method destroyMethod = null;
215+
216+
if (xxlJob.init().trim().length() > 0) {
217+
try {
218+
initMethod = clazz.getDeclaredMethod(xxlJob.init());
219+
initMethod.setAccessible(true);
220+
} catch (NoSuchMethodException e) {
221+
throw new RuntimeException("xxl-job method-jobhandler initMethod invalid, for[" + clazz + "#" + methodName + "] .");
222+
}
223+
}
224+
if (xxlJob.destroy().trim().length() > 0) {
225+
try {
226+
destroyMethod = clazz.getDeclaredMethod(xxlJob.destroy());
227+
destroyMethod.setAccessible(true);
228+
} catch (NoSuchMethodException e) {
229+
throw new RuntimeException("xxl-job method-jobhandler destroyMethod invalid, for[" + clazz + "#" + methodName + "] .");
230+
}
231+
}
232+
233+
// registry jobhandler
234+
registJobHandler(name, new MethodJobHandler(bean, executeMethod, initMethod, destroyMethod));
235+
236+
}
228237

229238

230239
// ---------------------- job thread repository ----------------------

xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSimpleExecutor.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public void setXxlJobBeanList(List<Object> xxlJobBeanList) {
3030
}
3131

3232

33+
@Override
3334
public void start() {
3435

3536
// init JobHandler Repository (for method)
@@ -43,6 +44,7 @@ public void start() {
4344
}
4445
}
4546

47+
@Override
4648
public void destroy() {
4749
super.destroy();
4850
}
@@ -62,7 +64,8 @@ private void initJobHandlerMethodRepository(List<Object> xxlJobBeanList) {
6264
}
6365
for (Method executeMethod : methods) {
6466
XxlJob xxlJob = executeMethod.getAnnotation(XxlJob.class);
65-
registerJobHandler(xxlJob, bean, executeMethod);
67+
// registry
68+
registJobHandler(xxlJob, bean, executeMethod);
6669
}
6770

6871
}

xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSpringExecutor.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ public XxlJob inspect(Method method) {
105105
for (Map.Entry<Method, XxlJob> methodXxlJobEntry : annotatedMethods.entrySet()) {
106106
Method executeMethod = methodXxlJobEntry.getKey();
107107
XxlJob xxlJob = methodXxlJobEntry.getValue();
108-
registerJobHandler(xxlJob, bean, executeMethod);
108+
// regist
109+
registJobHandler(xxlJob, bean, executeMethod);
109110
}
110111
}
111112
}
@@ -115,7 +116,7 @@ public XxlJob inspect(Method method) {
115116

116117
@Override
117118
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
118-
this.applicationContext = applicationContext;
119+
XxlJobSpringExecutor.applicationContext = applicationContext;
119120
}
120121

121122
public static ApplicationContext getApplicationContext() {

xxl-job-core/src/main/java/com/xxl/job/core/thread/ExecutorRegistryThread.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void run() {
102102
logger.error(e.getMessage(), e);
103103
}
104104
}
105-
logger.info(">>>>>>>>>>> xxl-job, executor registry thread destory.");
105+
logger.info(">>>>>>>>>>> xxl-job, executor registry thread destroy.");
106106

107107
}
108108
});

xxl-job-core/src/main/java/com/xxl/job/core/thread/JobLogFileCleanThread.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void run() {
9696
}
9797
}
9898
}
99-
logger.info(">>>>>>>>>>> xxl-job, executor JobLogFileCleanThread thread destory.");
99+
logger.info(">>>>>>>>>>> xxl-job, executor JobLogFileCleanThread thread destroy.");
100100

101101
}
102102
});

xxl-job-core/src/main/java/com/xxl/job/core/thread/TriggerCallbackThread.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void run() {
9595
logger.error(e.getMessage(), e);
9696
}
9797
}
98-
logger.info(">>>>>>>>>>> xxl-job, executor callback thread destory.");
98+
logger.info(">>>>>>>>>>> xxl-job, executor callback thread destroy.");
9999

100100
}
101101
});
@@ -125,7 +125,7 @@ public void run() {
125125
}
126126
}
127127
}
128-
logger.info(">>>>>>>>>>> xxl-job, executor retry callback thread destory.");
128+
logger.info(">>>>>>>>>>> xxl-job, executor retry callback thread destroy.");
129129
}
130130
});
131131
triggerRetryCallbackThread.setDaemon(true);

xxl-job-core/src/main/java/com/xxl/job/core/util/XxlJobRemotingUtil.java

+4
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,21 @@ private static void trustAllHosts(HttpsURLConnection connection) {
3434
logger.error(e.getMessage(), e);
3535
}
3636
connection.setHostnameVerifier(new HostnameVerifier() {
37+
@Override
3738
public boolean verify(String hostname, SSLSession session) {
3839
return true;
3940
}
4041
});
4142
}
4243
private static final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
44+
@Override
4345
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
4446
return new java.security.cert.X509Certificate[]{};
4547
}
48+
@Override
4649
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
4750
}
51+
@Override
4852
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
4953
}
5054
}};

xxl-job-executor-samples/xxl-job-executor-sample-frameless/src/main/java/com/xxl/job/executor/sample/frameless/FramelessApplication.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public static void main(String[] args) {
2929
} catch (Exception e) {
3030
logger.error(e.getMessage(), e);
3131
} finally {
32-
// destory
33-
FrameLessXxlJobConfig.getInstance().destoryXxlJobExecutor();
32+
// destroy
33+
FrameLessXxlJobConfig.getInstance().destroyXxlJobExecutor();
3434
}
3535

3636
}

xxl-job-executor-samples/xxl-job-executor-sample-frameless/src/main/java/com/xxl/job/executor/sample/frameless/config/FrameLessXxlJobConfig.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public void initXxlJobExecutor() {
5656
}
5757

5858
/**
59-
* destory
59+
* destroy
6060
*/
61-
public void destoryXxlJobExecutor() {
61+
public void destroyXxlJobExecutor() {
6262
if (xxlJobExecutor != null) {
6363
xxlJobExecutor.destroy();
6464
}

xxl-job-executor-samples/xxl-job-executor-sample-frameless/src/main/java/com/xxl/job/executor/sample/frameless/jobhandler/SampleXxlJob.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public void init(){
244244
logger.info("init");
245245
}
246246
public void destroy(){
247-
logger.info("destory");
247+
logger.info("destroy");
248248
}
249249

250250

xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java

-20
Original file line numberDiff line numberDiff line change
@@ -245,29 +245,9 @@ public void demoJobHandler2() throws Exception {
245245
public void init(){
246246
logger.info("init");
247247
}
248-
249248
public void destroy(){
250249
logger.info("destroy");
251250
}
252251

253-
/**
254-
* 6、生命周期任务示例:任务初始化与销毁时,支持private方法(但不推荐);
255-
*/
256-
@XxlJob(value = "demoJobHandler3", init = "initForDemoJobHandler3", destroy = "destroyForDemoJobHandler3")
257-
public void demoJobHandler3() throws Exception {
258-
XxlJobHelper.log("XXL-JOB, Hello World.");
259-
}
260-
261-
private void initForDemoJobHandler3(){
262-
logger.info("initForDemoJobHandler3");
263-
}
264-
265-
/**
266-
* 演示private方法也可以被访问且执行
267-
*/
268-
public void destroyForDemoJobHandler3(){
269-
logger.info("destroyForDemoJobHandler3");
270-
}
271-
272252

273253
}

0 commit comments

Comments
 (0)