当前页面: 开发资料首页 → J2EE 专题 → JBOSS AOP学习笔记-标签使用(J2SE5.0)中的元数据
摘要:
import org.jboss.aspects.Oneway;
public class Foo
{
@Oneway public static void someMethod() {...}
public static void main(String[] args)
{
someMethod(); // executes in background
}
}
package org.jboss.aspects;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
public @interface Oneway {}
package org.jboss.aspects;
public OnewayAspect
{
private static class Task implements Runnable
{
private MethodInvocation invocation;
public Task(MethodInvocation invocation)
{
this.invocation = invocation;
}
public void run()
{
try { invocation.invokeNext(); }
catch (Throwable ignore) { }
}
}
public Object oneway(MethodInvocation invocation) throws Throwable
{
MethodInvocation copy = invocation.copy();
Thread t = new Thread(new Task(copy));
t.setDaemon(false);
t.start();
return null;
}
}
aspect="org.jboss.aspects.OnewayAspect"/>
import org.jboss.aspects.Threadbased;
public class Foo
{
@Threadbased private int counter;
}
package org.jboss.aspects;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target({ElementType.FIELD})
public @interface Threadbased {}
package org.jboss.aspects;
import org.jboss.aop.joinpoint.*;
import java.lang.reflect.Field;
public class ThreadbasedAspect
{
private ThreadLocal threadbased = new ThreadLocal();
public Object access(FieldReadInvocation invocation)
throws Throwable
{
// just in case we have a primitive,
// we can't return null
if (threadbased.get() == null)
return invocation.invokeNext();
return threadbased.get();
}
public Object access(FieldWriteInvocation invocation)
throws Throwable
{
threadbased.set(invocation.getValue());
return null;
}
}
scope="PER_JOINPOINT"/>
aspect="org.jboss.aspects.ThreadbasedAspect"/>