Skip to content

Commit 4b95b74

Browse files
committed
1 parent 53b6164 commit 4b95b74

5 files changed

Lines changed: 93 additions & 47 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Fault response for `txt/plain` response #456
88
* Support `X-Forwarded-For` to allow app get real remote ip when app is behind a reverse proxy #454
99
* Param binding failure for `java.sql.TimeStamp` typed parameter #452
10+
* `@Every` without specifying the time failed #450
1011
* Some view engine caused browser always loading when running in prod mode #447
1112
* Param binding - it shall not try to get provider for simple types. #449
1213

src/main/java/act/job/JobAnnotationProcessor.java

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import act.app.App;
2424
import act.app.AppHolderBase;
2525
import act.app.event.AppEventId;
26+
import act.job.bytecode.JobAnnoInfo;
2627
import act.job.bytecode.ReflectedJobInvoker;
2728
import act.job.meta.JobClassMetaInfo;
2829
import act.job.meta.JobMethodMetaInfo;
29-
import org.osgl.$;
3030
import org.osgl.logging.LogManager;
3131
import org.osgl.logging.Logger;
3232
import org.osgl.util.E;
@@ -46,52 +46,44 @@ public JobAnnotationProcessor(App app) {
4646
manager = app.jobManager();
4747
}
4848

49-
public void register(final JobMethodMetaInfo method, final Class<? extends Annotation> anno, final Object v) {
49+
public void register(final JobMethodMetaInfo method, final Class<? extends Annotation> anno, final JobAnnoInfo info) {
5050
if (LOGGER.isTraceEnabled()) {
51-
LOGGER.trace("register job[%s] on anno[%s] with arg[%s]", method, anno, v);
51+
LOGGER.trace("register job[%s] on anno[%s] with arg[%s]", method, anno, info);
5252
}
5353
if (isAbstract(method)) {
5454
app().jobManager().on(AppEventId.SINGLETON_PROVISIONED, new Runnable() {
5555
@Override
5656
public void run() {
5757
List<JobMethodMetaInfo> list = method.extendedJobMethodMetaInfoList(app());
5858
for (JobMethodMetaInfo subMethodInfo : list) {
59-
register(subMethodInfo, anno, v);
59+
register(subMethodInfo, anno, info);
6060
}
6161
}
6262
});
6363
return;
6464
}
6565
Job job = createMethodJob(method);
66+
String value = info.value;
6667
if (Cron.class.isAssignableFrom(anno)) {
67-
registerCron(job, evaluateExpression(v.toString(), anno));
68+
registerCron(job, evaluateExpression(value, anno));
6869
} else if (AlongWith.class.isAssignableFrom(anno)) {
69-
registerAlongWith(job, v.toString());
70+
registerAlongWith(job, value);
7071
} else if (Every.class.isAssignableFrom(anno)) {
71-
registerEvery(job, evaluateExpression(v.toString(), anno));
72+
registerEvery(job, evaluateExpression(value, anno));
7273
} else if (FixedDelay.class.isAssignableFrom(anno)) {
73-
registerFixedDelay(job, evaluateExpression(v.toString(), anno));
74+
registerFixedDelay(job, evaluateExpression(value, anno));
7475
} else if (InvokeAfter.class.isAssignableFrom(anno)) {
75-
registerInvokeAfter(job, v.toString());
76+
registerInvokeAfter(job, value);
7677
} else if (InvokeBefore.class.isAssignableFrom(anno)) {
77-
registerInvokeBefore(job, v.toString());
78+
registerInvokeBefore(job, value);
7879
} else if (OnAppStart.class.isAssignableFrom(anno)) {
79-
boolean async = null == v ? false : (Boolean)v;
80+
boolean async = info.async;
8081
registerOnAppStart(job, async);
8182
} else if (OnAppStop.class.isAssignableFrom(anno)) {
82-
boolean async = null == v ? false : (Boolean) v;
83+
boolean async = info.async;
8384
registerOnAppStop(job, async);
8485
} else if (OnAppEvent.class.isAssignableFrom(anno)) {
85-
boolean async = false;
86-
AppEventId appEventId;
87-
if (v instanceof $.T2) {
88-
$.T2<AppEventId, Boolean> t2 = $.cast(v);
89-
appEventId = t2._1;
90-
async = t2._2;
91-
} else {
92-
appEventId = $.cast(v);
93-
}
94-
registerOnAppEvent(job, appEventId, async);
86+
registerOnAppEvent(job, info.appEventId, info.async);
9587
} else {
9688
throw E.unsupport("Unknown job annotation class: %s", anno.getName());
9789
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package act.job.bytecode;
2+
3+
/*-
4+
* #%L
5+
* ACT Framework
6+
* %%
7+
* Copyright (C) 2014 - 2018 ActFramework
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import act.app.event.AppEventId;
24+
import act.util.AnnotationUtil;
25+
import com.alibaba.fastjson.JSON;
26+
import org.osgl.$;
27+
28+
import java.lang.annotation.Annotation;
29+
30+
public class JobAnnoInfo {
31+
public String value;
32+
public AppEventId appEventId;
33+
public boolean async;
34+
public String id;
35+
public Class<? extends Annotation> annotationType;
36+
37+
JobAnnoInfo (Class <? extends Annotation> annoType) {
38+
this.annotationType = annoType;
39+
Object v = AnnotationUtil.tryGetDefaultValue(annoType, "value");
40+
if (v instanceof String) {
41+
this.value = (String) v;
42+
} else if (v instanceof AppEventId) {
43+
this.appEventId = (AppEventId) v;
44+
}
45+
this.async = $.bool((Boolean) AnnotationUtil.tryGetDefaultValue(annoType, "async"));
46+
this.id = (String) AnnotationUtil.tryGetDefaultValue(annoType, "id");
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return JSON.toJSONString(this);
52+
}
53+
}

src/main/java/act/job/bytecode/JobByteCodeScanner.java

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ private boolean requireScan() {
196196

197197
private class ActionAnnotationVisitor extends AnnotationVisitor implements Opcodes {
198198

199-
List<AnnoInfo> annoInfos = new ArrayList<>();
200-
AnnoInfo currentInfo;
199+
List<JobAnnoInfo> annoInfos = new ArrayList<>();
200+
JobAnnoInfo currentInfo;
201201
JobMethodMetaInfo method;
202202

203203
public ActionAnnotationVisitor(AnnotationVisitor av, Class<? extends Annotation> c, JobMethodMetaInfo methodMetaInfo) {
@@ -207,55 +207,38 @@ public ActionAnnotationVisitor(AnnotationVisitor av, Class<? extends Annotation>
207207
}
208208

209209
void add(Class<? extends Annotation> annotationClass) {
210-
currentInfo = new AnnoInfo(annotationClass);
210+
currentInfo = new JobAnnoInfo(annotationClass);
211211
annoInfos.add(currentInfo);
212212
}
213213

214214
@Override
215215
public void visitEnum(String name, String desc, String value) {
216216
if (desc.contains("AppEventId")) {
217-
this.currentInfo.value = AppEventId.valueOf(value);
217+
this.currentInfo.appEventId = AppEventId.valueOf(value);
218218
}
219219
super.visitEnum(name, desc, value);
220220
}
221221

222222
@Override
223223
public void visit(String name, Object value) {
224224
if ("value".equals(name)) {
225-
this.currentInfo.value = value;
225+
this.currentInfo.value = value.toString();
226226
} else if ("async".equals(name)) {
227-
this.currentInfo.async = value;
227+
this.currentInfo.async = $.bool(value);
228228
} else if ("id".equals(name)) {
229229
this.method.id(S.string(value));
230230
}
231231
super.visit(name, value);
232232
}
233233

234234
public void doRegistration() {
235-
for (AnnoInfo info : annoInfos) {
236-
Object value = info.value;
237-
Object async = info.async;
238-
if (value != null && async != null) {
239-
value = $.T2(value, async);
240-
} else if (value == null) {
241-
value = async;
242-
}
243-
annotationProcessor.register(method, info.annotationType, value);
235+
for (JobAnnoInfo info : annoInfos) {
236+
annotationProcessor.register(method, info.annotationType, info);
244237
}
245238
}
246239
}
247240
}
248241
}
249242

250-
private static class AnnoInfo {
251-
Object value;
252-
Object async;
253-
Class<? extends Annotation> annotationType;
254-
255-
AnnoInfo(Class <? extends Annotation> annoType) {
256-
this.annotationType = annoType;
257-
}
258-
}
259-
260243

261244
}

src/main/java/act/util/AnnotationUtil.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
* #L%
2121
*/
2222

23+
import org.osgl.$;
24+
2325
import java.lang.annotation.Annotation;
26+
import java.lang.reflect.Method;
2427

2528
public class AnnotationUtil {
2629
public static <T extends Annotation> T declaredAnnotation(Class c, Class<T> annoClass) {
@@ -72,4 +75,18 @@ public static <T extends Annotation> T getAnnotation(Class<?> targetClass, Class
7275
targetClass = targetClass.getSuperclass();
7376
return null != targetClass ? getAnnotation(targetClass, annotationClass) : null;
7477
}
78+
79+
public static Object defaultValue(Class<? extends Annotation> annoType, String methodName) {
80+
Method method = $.getMethod(annoType, methodName);
81+
return method.getDefaultValue();
82+
}
83+
84+
public static Object tryGetDefaultValue(Class<? extends Annotation> annoType, String methodName) {
85+
try {
86+
return defaultValue(annoType, methodName);
87+
} catch (Exception e) {
88+
// ignore it
89+
return null;
90+
}
91+
}
7592
}

0 commit comments

Comments
 (0)