在控制器中检索由AOP类计算的值

我创建了一个AspectClass:

@Aspect
@Component
public class TimeOutAspect {

    private Date startDate;
    private Date endDate;
    private long duration;


    // check delay bank contact
    @Before("execution(com.app.ws.shared.dto.BankaccountDto com.app.ws.service.BankaccountService.checkaccount())")
    public Date bankCheckLaunch() {
        startDate = new Date(System.currentTimeMillis());
        System.out.println("Checking engine - Before : " + startDate);
        return startDate;
    }


    @After("execution(com.app.ws.shared.dto.BankaccountDto com.app.ws.service.BankaccountService.checkaccount())")
    public Date bankCheckEnd() {
        endDate = new Date(System.currentTimeMillis());
        System.out.println("Checking engine - After : " + endDate);
        setDuration(endDate,startDate);
        return endDate;
    }

    /// period.getMillis => long
    public Long setDuration(Date endDate,Date startDate) {
        Interval interval = new Interval(startDate.getTime(),endDate.getTime());
        Duration period = interval.toDuration();
        long duration = period.getMillis();
        System.out.println("Duration = " + period.getMillis());
        return duration;

    }

    public long getDuration() {
        return duration;
    }
}

此类旨在计算由控制器调用的服务类的执行持续时间。 我尝试检索此持续时间并在控制器上设置条件(超时)。

public BankaccountResponseModel validateTransactionByaccountNumber(@RequestBody BankaccountRestRequestModel transactionInformationRestRequestModel) {
    // objet attendu en retour
    BankaccountResponseModel returnValue = new BankaccountResponseModel();

    ModelMapper modelMaper = new ModelMapper();
    BankaccountDto accountToCheck = modelMaper.map(transactionInformationRestRequestModel,BankaccountDto.class);

    BankaccountDto bankaccountDto = bankaccountService.checkaccount(accountToCheck);
    // convertir en response
    returnValue = modelMaper.map(bankaccountDto,BankaccountResponseModel.class);

    // chek delay of response
    System.out.println(timeOut.getDuration());
    if (timeOut.getDuration() > 2) {
        returnValue.setStatus(false);
        returnValue.setMessage("Failure - Time out");
    }

    return returnValue;
}

不幸的是,返回值等于0,但应该为正。

jujishou1984 回答:在控制器中检索由AOP类计算的值

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2987610.html

大家都在问