Skip to content

Commit 8d0b00a

Browse files
author
Ansj
committed
add schedule annotation
1 parent 686044b commit 8d0b00a

File tree

9 files changed

+107
-6
lines changed

9 files changed

+107
-6
lines changed

src/main/java/org/nlpcn/jcoder/domain/ClassDoc.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class ClassDoc extends ApiDoc {
1515
private String group;
1616
private boolean single = true;
1717
private boolean status = true;
18+
private String scheduleStr ;
1819
private String version;
1920
private String description;
2021

@@ -62,6 +63,14 @@ public void setDescription(String description) {
6263
this.description = description;
6364
}
6465

66+
public String getScheduleStr() {
67+
return scheduleStr;
68+
}
69+
70+
public void setScheduleStr(String scheduleStr) {
71+
this.scheduleStr = scheduleStr;
72+
}
73+
6574
@Override
6675
public ApiDoc createSubDoc(String name) {
6776
MethodDoc methodDoc = new MethodDoc(name);

src/main/java/org/nlpcn/jcoder/run/annotation/Execute.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @author ansj
99
*/
1010
@Retention(RetentionPolicy.RUNTIME)
11-
@Target({ElementType.TYPE, ElementType.METHOD})
11+
@Target({ElementType.METHOD})
1212
@Documented
1313
public @interface Execute {
1414

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.nlpcn.jcoder.run.annotation;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
/**
10+
* execute function use it by classname/funname
11+
*
12+
* @author ansj
13+
*/
14+
@Retention(RetentionPolicy.RUNTIME)
15+
@Target({ElementType.TYPE})
16+
@Documented
17+
public @interface Schedule {
18+
String value() default "" ;
19+
}

src/main/java/org/nlpcn/jcoder/run/annotation/Single.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @author ansj
99
*/
1010
@Retention(RetentionPolicy.RUNTIME)
11-
@Target({ElementType.TYPE, ElementType.METHOD})
11+
@Target({ElementType.TYPE})
1212
@Documented
1313
public @interface Single {
1414
boolean value() default true;

src/main/java/org/nlpcn/jcoder/util/GroupFileListener.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
44
import org.apache.commons.io.monitor.FileAlterationMonitor;
55
import org.apache.commons.io.monitor.FileAlterationObserver;
6+
import org.nlpcn.jcoder.domain.ClassDoc;
67
import org.nlpcn.jcoder.domain.Task;
78
import org.nlpcn.jcoder.run.CodeException;
89
import org.nlpcn.jcoder.run.java.JavaSourceUtil;
@@ -247,6 +248,7 @@ private synchronized void createTask(File file) throws CodeException {
247248

248249
JavaSourceUtil javaSourceUtil = new JavaSourceUtil(content);
249250

251+
250252
String className = javaSourceUtil.getClassName();
251253

252254
String fileName = fileName(file);
@@ -268,10 +270,26 @@ private synchronized void createTask(File file) throws CodeException {
268270
if (task != null) {
269271
this.onFileChange(file);
270272
} else {
273+
274+
ClassDoc parse = null ;
275+
276+
try {
277+
parse = JavaDocUtil.parse(content);
278+
} catch (Exception e) {
279+
throw new CodeException(e) ;
280+
}
281+
271282
task = new Task();
272283
task.setCode(content);
273284
task.setCreateUser("admin");
274-
task.setType(1);
285+
286+
if(parse.getScheduleStr()!=null){
287+
task.setType(2);
288+
task.setScheduleStr(parse.getScheduleStr());
289+
}else{
290+
task.setType(1);
291+
}
292+
275293
task.setStatus(1);
276294
task.setCreateTime(new Date());
277295
task.setUpdateTime(new Date());

src/main/java/org/nlpcn/jcoder/util/JavaDocUtil.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
import com.github.javaparser.ast.body.Parameter;
1010
import com.github.javaparser.ast.body.TypeDeclaration;
1111
import com.github.javaparser.ast.expr.AnnotationExpr;
12+
import com.github.javaparser.ast.expr.NormalAnnotationExpr;
1213
import com.github.javaparser.ast.expr.SingleMemberAnnotationExpr;
14+
import com.github.javaparser.ast.expr.StringLiteralExpr;
15+
1316
import org.nlpcn.jcoder.domain.ClassDoc;
1417
import org.nlpcn.jcoder.domain.ClassDoc.MethodDoc;
1518
import org.nlpcn.jcoder.domain.ClassDoc.MethodDoc.ParamDoc;
@@ -62,11 +65,25 @@ public static ClassDoc parse(String code) throws Exception {
6265
if (node instanceof SingleMemberAnnotationExpr) {
6366

6467
SingleMemberAnnotationExpr sma = (SingleMemberAnnotationExpr) node;
65-
6668
if (sma.getName().getName().equals("Single")) {
6769
if ("false".equals(sma.getMemberValue().toString())) {
6870
cd.setSingle(false);
6971
}
72+
}else if (sma.getName().getName().equals("Schedule")) {
73+
cd.setScheduleStr(((StringLiteralExpr)sma.getMemberValue()).getValue());
74+
}
75+
76+
} else if (node instanceof MethodDeclaration) {
77+
explainMethod(cd, (MethodDeclaration) node);
78+
}else if (node instanceof NormalAnnotationExpr) {
79+
80+
NormalAnnotationExpr sma = (NormalAnnotationExpr) node;
81+
if (sma.getName().getName().equals("Single")) {
82+
if("false".equals(sma.getPairs().get(0).getValue().toString())){
83+
cd.setSingle(false);
84+
}
85+
}else if (sma.getName().getName().equals("Schedule")) {
86+
cd.setScheduleStr(((StringLiteralExpr)sma.getPairs().get(0).getValue()).getValue());
7087
}
7188

7289
} else if (node instanceof MethodDeclaration) {

src/test/java/BootstrapTestAnsj.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public class BootstrapTestAnsj {
44

55
// private static String host = "192.168.31.107";
6-
private static String host = "192.168.31.227";
6+
// private static String host = "192.168.31.227";
77
// private static String zk = "192.168.31.227:2181";
88
// private static String zk = "192.168.3.137:2181|jcoder:jcoder";
99

@@ -20,7 +20,7 @@ public void test1() throws Exception {
2020
int port = 9095;
2121
Bootstrap.main(new String[]{
2222
// "--zk=" + zk,
23-
"--host=" + host,
23+
// "--host=" + host,
2424
"--home=jcoder_home_" + port,
2525
"--port=" + port,
2626
"--token=www.infcn.com.cn",

src/test/java/cn/com/infcn/api/test/TestEtlApi.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import com.alibaba.fastjson.JSONObject;
44
import org.nlpcn.jcoder.run.annotation.Execute;
5+
import org.nlpcn.jcoder.run.annotation.Schedule;
56

7+
@Schedule("while")
68
public class TestEtlApi {
79

810
@Execute
11+
912
public void test(JSONObject doc) {
1013
doc.put("aaa", "ccc");
1114
doc.remove("bbb");
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.nlpcn.jcoder.util;
2+
3+
import org.junit.Test;
4+
import org.nlpcn.jcoder.domain.ClassDoc;
5+
6+
/**
7+
* Created by Ansj on 29/03/2018.
8+
*/
9+
public class JavaDocUtilTest {
10+
11+
@Test
12+
public void testSchdule() throws Exception {
13+
String code = "package cn.com.infcn.irsp;\n" +
14+
"\n" +
15+
"import org.nlpcn.jcoder.run.annotation.Execute;\n" +
16+
"import org.nlpcn.jcoder.run.annotation.Schedule;\n" +
17+
"\n" +
18+
"\n" +
19+
"/**\n" +
20+
" * Created by Ansj on 29/03/2018.\n" +
21+
" */\n" +
22+
"@Schedule(\"0/3 * * * * ?\")\n" +
23+
"public class TestApi {\n" +
24+
"\n" +
25+
"\t@Execute\n" +
26+
"\tpublic void execute(){\n" +
27+
"\t\tSystem.out.println(\"100\");\n" +
28+
"\t}\n" +
29+
"}\n";
30+
31+
ClassDoc parse = JavaDocUtil.parse(code);
32+
System.out.println(parse.getScheduleStr());
33+
34+
}
35+
}

0 commit comments

Comments
 (0)