Strust2 및 최대 절전 모드로 작업하고 있습니다. 환율 (USD to INR)을 찾아야합니다. 이 정보를 두 곳 이상에서 사용해야합니다. 이를 위해 ThreadLocal을 사용하고 있습니다.ThreadLocal을 사용하여 데이터를 저장하는 방법
public class GetExchangeRate{
private ThreadLocal<Double> threadLocalRate = new ThreadLocal<Double>();
public double getCurrencyRate(UserDet userDet){
LOG.info("Thread id is ---------------->"+Thread.currentThread().getId());
Double currencyRate = (Double) threadLocalRate.get();
if(currencyRate == null){
LOG.info("Object does not exist");
---//my code which is used to find USD --> INR exchange rate
threadLocalRate.set(currencyRate);
}
return currencyRate ;
}
}
나는 네 다른 방법에서 위의 메소드를 호출 할 필요가있다. 위 메서드를 다른 메서드에서 호출하면 위 코드가 실행 중입니다. 내 요구 사항은 단지 입니다. 전체 메소드를 실행해야합니다. 그리고 나머지는 세 번 총 메서드를 실행해서는 안됩니다. ThreadLocal 오브젝트에 격납되고있는 값이 돌려 주어집니다.
위의 전체 방법이 실행되었음을 보여주는 로그 보고서입니다.
[ INFO] 2012-09-20 10:20:04,611 [CommonFormats] (CommonFormats.java:getCurrencyRate:159)
Thread id is ---------------------------->54
[ INFO] 2012-09-20 10:20:04,611 [CommonFormats] (CommonFormats.java:getCurrencyRate:163)
Object does not exist
[ INFO] 2012-09-20 10:20:49,529 [CommonFormats] (CommonFormats.java:getCurrencyRate:159)
Thread id is ---------------------------->54
[ INFO] 2012-09-20 10:20:49,529 [CommonFormats] (CommonFormats.java:getCurrencyRate:163)
Object does not exist
내가 뭘 잘못하고 있는지 제안하십시오. 위의 방법은 네 개의 메소드에서 호출됩니다. 두 개의 메소드은 Action 클래스에 속하며 두 개의 메소드는 서비스 계층 클래스에 속합니다.
내 샘플 코드는
//Action class
public class StrutsAction1{
public String method1(){
// my code
CommonFormats commonFormats= new CommonFormats();
System.out.println(commonFormats.getCurrencyRate());
// my code
}
public String method2(){
// my code
CommonFormats commonFormats= new CommonFormats();
System.out.println(commonFormats.getCurrencyRate());
// my code
} }
//Business class
public class BussinessLogic{
public String method1(){
// my code
CommonFormats commonFormats= new CommonFormats();
System.out.println(commonFormats.getCurrencyRate());
// my code
}
public String method2(){
// my code
CommonFormats commonFormats= new CommonFormats();
System.out.println(commonFormats.getCurrencyRate());
// my code
} }
위의 611과 529는 위의 전화 번호 (발신자 번호)를 나타내는 라인 번호를 나타냅니다. – Daya
요구 사항이 명확하지 않습니다. –
@DaveNewton threadLocalRate 변수가 두 개 이상의 메소드가 필요합니다 (거의 4 개의 메소드에서). 그것은 환율 (USD -> INR)을 갖습니다. 환율을 알아내는 데 사용되는 코드는 한 번만 실행해야합니다. 나머지 3 번은 실행해서는 안됩니다. 그래서 ThreadLocale 개념을 사용했습니다. 그러나 그것은 작동하지 않습니다. 나는 그 질문을 갱신했다. – Daya