2013-10-15 3 views
0

클래스 중 하나에 주입되는 EJB가 있습니다. EJB에는 주입 된 클래스에서 자원 모니터링을 시작하는 메소드가 있습니다. 모니터 메소드에는 변수 중 하나가 업데이트되면 깨져 있어야하는 while 루프가 있습니다. 코드는 다음과 같이 보입니다 : 내 로그에서상태 변수 EJB의 변수 업데이트

public class MyObject() 
{ 
    @EJB 
    private MyEJB myEjb; 

    private Collection stuffToMonitor; 

    public MyObject() 
    { 
     //empty 
    } 

    public void doSomething() 
    { 
     // create collection of stuffToMonitor 

     myEjb.startMonitoring(stuffToMonitor); 

     // code that does something 

     if(conditionsAreMet) 
     { 
      myEjb.stopMonitoring(); 
     } 

     // more code 
    } 
} 

@Stateful 
public class MyEJB() 
{ 
    private volatile boolean monitoringStopped = false; 

    public MyEJB() 
    { 
     //empty 
    } 

    public void startMonitoring(Collection stuffToMonitor) 
    { 
     int completed = 0; 
     int total = stuffToMonitor.size(); 

     while(completed < total) 
     { 
      // using Futures, track completed stuff in collection 

      // log the value of this.monitoringStopped  
      if (this.monitoringStopped) 
      { 
       break; 
      } 
     } 
    } 

    public voide stopMonitoring() 
    { 
     this.monitoringStopped = true; 
     // log the value of this.monitoringStopped 
    } 
} 

, 나는 this.monitoringStopped의 값이 stopMonitoring 메소드가 호출 된 후 true 것을 볼 수 있지만, 항상 while 루프에서 false로 기록합니다.

원래 MyEJB는 상태 비 저장이며 상태가 변경되었으며 변수도 volatile로 변경되었지만 변경 내용은 while 루프에서 선택되지 않았습니다.

monitoringStopped 변수의 업데이트 된 값을 얻기 위해 코드를 가져 오려면 무엇이 누락 되었습니까?

답변

0

나는 누군가가 더 잘 알고 있다면 그들로부터 듣는 것에 감사 할지라도, 내가하려고 한 것은 EJBS에서는 불가능하다고 생각한다.

대신 다른 방법을 발견했습니다. MyObject가 myEjb.stopMonitoring();을 호출하는 대신 설정하는 상태 변수를 보유하고있는 MyStatus라는 세 번째 클래스를 추가했습니다. MyEJB를 stateless bean으로 설정하고 startMonitoring 메소드에서 MyStatus 객체를 전달한다. while 루프가 반복 될 때마다 상태를 확인하고이를 기반으로 중단됩니다.

업데이트 코드 :

public class MyObject() 
{ 
    @EJB 
    private MyEJB myEjb; 

    @EJB 
    private MyStatus myStatus; 

    private Collection stuffToMonitor; 

    public MyObject() 
    { 
     //empty 
    } 

    public void doSomething() 
    { 
     // create collection of stuffToMonitor 

     myEjb.startMonitoring(stuffToMonitor); 

     // code that does something 

     if(conditionsAreMet) 
     { 
      myStatus.stopMonitor(); 
     } 

     // more code 
    } 
} 

@Stateless 
public class MyEJB() 
{ 
    private volatile boolean monitoringStopped = false; 

    public MyEJB() 
    { 
     //empty 
    } 

    public void startMonitoring(Collection stuffToMonitor, MyStatus myStatus) 
    { 
     int completed = 0; 
     int total = stuffToMonitor.size(); 

     while((completed < total) && myStatus.shouldMonitor()) 
     { 
      // using Futures, track completed stuff in collection 
     } 
    } 
} 

@Stateless 
public class MyStatus 
{ 
    private boolean shouldMonitor = true; 

    public void stopMonitor() 
    { 
     this.shouldMonitor = false; 
    } 

    public boolean shouldMonitor() 
    { 
     return this.shouldMonitor; 
    } 
}