2012-07-31 34 views
0

在一个简单的RMI程序中,我设法在两个线程之间传递Context。现在我需要将设置/报告从上下文移动到AspectJ类。如何将方法移动到AspectJ类?

我的问题是:如何将背景下,如果我需要把它作为问候(上下文)的参数

HelloIF

public interface HelloIF extends Remote { 
    String greeting(Context c) throws RemoteException; 
} 

你好

public class Hello extends UnicastRemoteObject implements HelloIF { 

    public Hello() throws RemoteException { 
    } 

    public String greeting(Context c) throws RemoteException { 
     c.report(); 
     return "greeting"; 
    } 
} 

的RMIServer

public class RMIServer { 

    public static void main(String[] args) throws RemoteException, MalformedURLException { 
     LocateRegistry.createRegistry(1099); 
     HelloIF hello = new Hello(); 
     Naming.rebind("server.Hello", hello); 
     System.out.println("server.RMI Server is ready."); 
    } 
} 

RMIClient

public class RMIClient { 
    public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException { 

     Context context = new Context("request1", Thread.currentThread().getName()+System.currentTimeMillis()); 

     Registry registry = LocateRegistry.getRegistry("localhost"); 
     HelloIF hello = (HelloIF) registry.lookup("server.Hello"); 
     System.out.println(hello.greeting(context)); 

     context.report(); 

    } 
} 

语境

public class Context implements Serializable 
{ 
    private String requestType; 
    private String distributedThreadName; 

    public Context(String requestType, String distributedThreadName) 
    { 
     this.requestType = requestType; 
     this.distributedThreadName = distributedThreadName; 
    } 

    (...) 

    public void report() { 
     System.out.println("thread : " 
       + Thread.currentThread().getName() + " " 
       + Thread.currentThread().getId()); 

     System.out.println("context : " 
       + this.getDistributedThreadName() + " " + this.getRequestType()); 
    } 
} 

最后一个空的AspectJ类

@Aspect 
public class ReportingAspect { 
    @Before("call(void main(..))") 
    public void beforeReportClient(JoinPoint joinPoint) throws Throwable { 
    } 

    @After("call(void main(..))") 
    public void afterReportClient(JoinPoint joinPoint) throws Throwable { 
    } 

    @Before("call(String greeting(..))") 
    public void beforeReportGreeting(JoinPoint joinPoint) throws Throwable { 
    } 

    @After("call(String greeting(..))") 
    public void afterReportGreeting(JoinPoint joinPoint) throws Throwable { 
    } 

} 

如何从您好,RMIClient上下文()构造函数和C/context.report(移动)■报告方面?

回答

1

你也可以传递参数的函数,而底层对象,劝告,这样的:

@Before("execution(* greeting(..)) && target(target) && " + 
     "args(context)") 
public void beforeReportGreeting(HelloIF target, Context context) { 
     context.doSomething(); 
     target.doSomething(); 
} 

研究AspectJ的注释文档中的全部细节。它可以为所有的建议类型完成。

编辑阅读更详细的问题,它的声音,如果你想使由纵横构建和控制的Context对象的东西,同时还通过它作为参数传递给Hello.greeting()。

这并不合理。没有任何AOP进行,您的底层系统应该可以正常工作。因此,如果Context对象是该底层域的一部分,那么Aspect负责其构建和管理并不是一个好主意。

如果上下文是相关的方面,那么你会删除所有与来自域类的背景下(这样greeting()将不带任何参数),并建立在该方面,上下文对象(S)。

+0

好的,我明白了。我试图为连接到一个请求的所有线程设置相同的上下文(线程名称)。我的问题是,这只是一个简单的例子,我必须在庞大的系统中执行此操作。所以我的想法是在各个方面构建/管理上下文。有什么建议我应该做什么? – alicjasalamon 2012-08-01 07:01:55

相关问题