From 2e3a2c0ab725233b7a18ef8b599c115834d1301a Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Fri, 4 May 2018 16:31:19 +0200 Subject: [PATCH] [2.3.3] Interruption timers. We have them. --- .../ass2/aop/management/ManagementAspect.java | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/ass2-aop/src/main/java/dst/ass2/aop/management/ManagementAspect.java b/ass2-aop/src/main/java/dst/ass2/aop/management/ManagementAspect.java index aba3eab..39b0667 100644 --- a/ass2-aop/src/main/java/dst/ass2/aop/management/ManagementAspect.java +++ b/ass2-aop/src/main/java/dst/ass2/aop/management/ManagementAspect.java @@ -1,7 +1,41 @@ package dst.ass2.aop.management; +import dst.ass2.aop.IPluginExecutable; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.reflect.MethodSignature; + +import java.util.Timer; +import java.util.TimerTask; + +@Aspect public class ManagementAspect { + @Around("execution(* dst.ass2.aop.IPluginExecutable.execute(..)) && @annotation(Timeout)") + public Object around(ProceedingJoinPoint point) { + // setup interruptionTimer. + Timeout timeoutAnn = ((MethodSignature) point.getSignature()).getMethod().getAnnotation(Timeout.class); + System.out.println("ManagementAspect - sched"); + + Timer interruptionTimer = new Timer(true); + interruptionTimer.schedule(new TimerTask() { + @Override + public void run() { + System.out.println("ManagementAspect - interrupt"); + ((IPluginExecutable) point.getTarget()).interrupted(); + } + }, timeoutAnn.value()); - // TODO + //execute actual function + Object result = null; + try { + result = point.proceed(); + } catch (Throwable throwable) { + throwable.printStackTrace(); + } + // once the function completed, cancel interruptionTimer and return results. + interruptionTimer.cancel(); + return result; + } } -- 2.43.0