Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的拦截器类没有';你不能使用计划方法吗?_Java_Interceptor_Ejb 3.1 - Fatal编程技术网

Java 为什么我的拦截器类没有';你不能使用计划方法吗?

Java 为什么我的拦截器类没有';你不能使用计划方法吗?,java,interceptor,ejb-3.1,Java,Interceptor,Ejb 3.1,我的WildFly 10.2.0服务器上有很多无状态的bean。每次我尝试使用@Interceptors({LogService.class})时,它对任何方法都有效,除了对只有2个@Schedule(second=“*/2”,minute=“*”,hour=“*”方法的无状态bean。我找了文件,但没有找到任何线索。有人能帮我吗?我正在使用Java8 这是我的拦截器类: public class LogService { @AroundInvoke public Object interce

我的WildFly 10.2.0服务器上有很多无状态的bean。每次我尝试使用
@Interceptors({LogService.class})
时,它对任何方法都有效,除了对只有2个
@Schedule(second=“*/2”,minute=“*”,hour=“*”
方法的
无状态
bean。我找了文件,但没有找到任何线索。有人能帮我吗?我正在使用Java8

这是我的拦截器类:

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;
}
}
这是我的课程表:

@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);
    }
}

}
@无状态
@拦截器({LogService.class})
公共类ScoreTimerService{
@EJB
私人帐户;
@时间表(第二个“*/3”,分钟“*”,小时“*”)
public void addPointsDueOnlineState(){
List List=accountDao.findOnline();
用于(帐户:列表){
账户积分(5分);
accountDao.update(account);
}
}
@时间表(秒=“*/2”,分钟=“*”,小时=“*”)
public void removePointsDueTime(){
List=accountDao.findAll();
用于(帐户:列表){
账户。removeScore(1);
accountDao.update(account);
}
}
}

我尝试在类上使用on方法替换
@Interceptors({LogService.class})
@Interceptors(LogService.class)
,但是没有一个有效。

我发现只有
@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

你能分享一些代码吗?对不起,我刚刚用javax.interceptor.Interceptors注释添加了你?是的,我的导入就像
import javax.ejb.ejb;导入javax.ejb.Schedule;导入javax.ejb.Stateless;导入javax.interceptor.Interceptors只是为了测试,你可以从其中一个方法中删除@Schedule注释,看看它是否被调用。你可以试试吗?你只能使用一种方法和两个注释,即删除
截取计划
,将
监视器
内联到
截取
,并将
@AroundTimeout
置于
截取
上(以及
@AroundInvoke