2017-10-21 88 views
1

我的WildFly 10.2.0服務器上有大量的Stateless豆子。每次嘗試使用@Interceptors({LogService.class})時,它都可以使用任何方法,但只有2個@Schedule(second = "*/2", minute = "*", hour = "*")方法的Stateless bean除外。我查找了文檔,但沒有找到任何線索。誰能幫我?我使用的Java 8爲什麼我的攔截器類不適用於調度方法?

這裏是我的攔截器類:

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

} 

我試着用的方法,階級,關於更換@Interceptors({LogService.class})@Interceptors(LogService.class)但沒有人工作。

+0

ü可以分享一些代碼嗎? – Vahid

+0

對不起,我剛剛加了 – GabrielRado

+0

是你用javax.interceptor.Interceptors註解的嗎? – Vahid

回答

1

我發現只有@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豆