2012-07-31 32 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

相關問題