为什么我的拦截器 class 不适用于调度方法?
Why my interceptor class doesn't work with schedule methods?
我的 WildFly 10.2.0 服务器上有很多 Stateless
个 bean。每次我尝试使用 @Interceptors({LogService.class})
它都适用于任何方法,除了只有 2 个 @Schedule(second = "*/2", minute = "*", hour = "*")
方法的 Stateless
bean。我查找了文档,但没有找到任何线索。谁能帮我?我正在使用 Java 8.
这是我的拦截器 class:
public class LogService {
@AroundInvoke
public Object interceptsAngLog(InvocationContext context) throws Exception {
Long millis = System.currentTimeMillis();
LocalTime now = new LocalTime();
Object object = context.proceed();
String method = context.getMethod().getName();
String className = context.getTarget().getClass().getName();
Long millisSpent = System.currentTimeMillis() - millis;
System.out.println("[LOG] " + now.toString() + "-" + className + "-" + method + ": " + millisSpent);
return object;
}
}
这是我的时间表 class:
@Stateless
@Interceptors({LogService.class})
public class ScoreTimerService {
@EJB
private AccountDao accountDao;
@Schedule(second = "*/3", minute = "*", hour = "*")
public void addPointsDueOnlineState() {
List<Account> list = accountDao.findOnline();
for (Account account : list) {
account.addScore(5);
accountDao.update(account);
}
}
@Schedule(second = "*/2", minute = "*", hour = "*")
public void removePointsDueTime() {
List<Account> list = accountDao.findAll();
for (Account account : list) {
account.removeScore(1);
accountDao.update(account);
}
}
}
我尝试使用方法,class,将 @Interceptors({LogService.class})
替换为 @Interceptors(LogService.class)
,但 none 有效。
我发现只有 @AroundTimeout
符号适用于 @Schedule
EJB Bean。所以我添加了这样写我的方法:
@AroundInvoke
public Object intercept(InvocationContext context) throws Exception {
return monitor(context);
}
@AroundTimeout
public Object interceptSchedule(InvocationContext context) throws Exception {
return monitor(context);
}
private Object monitor(InvocationContext context) throws Exception {
long millis = System.currentTimeMillis();
String method = context.getMethod().getName();
String className = context.getTarget().getClass().getSimpleName();
Object object = context.proceed();
long newMillis = System.currentTimeMillis() - millis;
System.out.println("[LOG]-" + method + "." + className + "-" + newMillis + "ms");
return object;
}
对于@Schedule 和非@Schedule bean,它就像一个魅力
我的 WildFly 10.2.0 服务器上有很多 Stateless
个 bean。每次我尝试使用 @Interceptors({LogService.class})
它都适用于任何方法,除了只有 2 个 @Schedule(second = "*/2", minute = "*", hour = "*")
方法的 Stateless
bean。我查找了文档,但没有找到任何线索。谁能帮我?我正在使用 Java 8.
这是我的拦截器 class:
public class LogService {
@AroundInvoke
public Object interceptsAngLog(InvocationContext context) throws Exception {
Long millis = System.currentTimeMillis();
LocalTime now = new LocalTime();
Object object = context.proceed();
String method = context.getMethod().getName();
String className = context.getTarget().getClass().getName();
Long millisSpent = System.currentTimeMillis() - millis;
System.out.println("[LOG] " + now.toString() + "-" + className + "-" + method + ": " + millisSpent);
return object;
}
}
这是我的时间表 class:
@Stateless
@Interceptors({LogService.class})
public class ScoreTimerService {
@EJB
private AccountDao accountDao;
@Schedule(second = "*/3", minute = "*", hour = "*")
public void addPointsDueOnlineState() {
List<Account> list = accountDao.findOnline();
for (Account account : list) {
account.addScore(5);
accountDao.update(account);
}
}
@Schedule(second = "*/2", minute = "*", hour = "*")
public void removePointsDueTime() {
List<Account> list = accountDao.findAll();
for (Account account : list) {
account.removeScore(1);
accountDao.update(account);
}
}
}
我尝试使用方法,class,将 @Interceptors({LogService.class})
替换为 @Interceptors(LogService.class)
,但 none 有效。
我发现只有 @AroundTimeout
符号适用于 @Schedule
EJB Bean。所以我添加了这样写我的方法:
@AroundInvoke
public Object intercept(InvocationContext context) throws Exception {
return monitor(context);
}
@AroundTimeout
public Object interceptSchedule(InvocationContext context) throws Exception {
return monitor(context);
}
private Object monitor(InvocationContext context) throws Exception {
long millis = System.currentTimeMillis();
String method = context.getMethod().getName();
String className = context.getTarget().getClass().getSimpleName();
Object object = context.proceed();
long newMillis = System.currentTimeMillis() - millis;
System.out.println("[LOG]-" + method + "." + className + "-" + newMillis + "ms");
return object;
}
对于@Schedule 和非@Schedule bean,它就像一个魅力