struts2 인터셉터(initerceptor) – Thread
Local
오라클자바커뮤니티에서 설립한 오엔제이프로그래밍
실무교육센터
(신입사원채용무료교육, 오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷)
어떤
Object가 Thread별로 유일할 때 그 오브젝트를
Thread-Local 오브젝트라고 한다.
Struts2는 Action을 실행하는데 필요한 정보를
ActionContext에 저장을 하는데 이 ActionContext는 애플리케이션
맵, 세션 맵, 요청 파라미터 맵, locale, ActionInvocation, ValueStack, ServletRequest,
ServletResponses 등 액션을 실행하는데 필요한 모든 정보를 가지고 있는
Thread-local 오브젝트 이다.
ThreadLocal 클래스는 어떤 Thread에 어떤 오브젝트를 리턴 했는지 저장하고 어떤
Thread가 오브젝트를 요청하면 새로운 쓰레드이거나 기존 쓰레드지만 오브젝트가 삭제된 경우에만 오브젝트를 새로 생성하고 아니면
기존 Object를 리턴한다.
ThreadLocal<T> 클래스에는
4개의 메소드가 있다.
public T get() : 이 thread-local 변수의 현재 Thread 값을 리턴
protected T initialValue() :
이 thread-local 변수에 대한
현재 Thread의 초기값을 리턴
remove() : 이 thread-local 변수의 현재 Thread의 값을 제거
set(T value) : 이 thread-local 변수의 현재 Thread 값을 세팅
Thread별로 유일한 오브젝트를 리턴하는 Thread-Local 예제를 작성해 보도록 하자.
[ThreadLocalTest.java]
/**
*
Thread별로 생성되는
TestClass
*/
class TestClass
{
// HashCode를 16진수로 출력
public String toString() {
return String.format("[TestClass, hashcode=%s]",
Integer
.toHexString(this.hashCode()));
}
}
/**
*
TestClass의 thread-local 인스턴스를 만들어주는 클래스 ThreadLocal 클래스의 인스턴스는 Thread별로
생성한
*
Object를 저장 ThreadLocal 클래스의
get() 메소드는 해당 쓰레드에서 생성한 Object가 있으면 리턴 없으면
*
initialValue를 실행해서 TestClass의 인스턴스를 새로 생성한다.
*/
class TestClassThreadLocal extends
ThreadLocal<ThreadLocalTest> {
public ThreadLocalTest initialValue()
{
return new ThreadLocalTest();
}
}
/**
*
TestClassThreadLocal 클래스의 인스턴스를 만들고 그 인스턴스의 get
메소드를 통해 Thread-Local 오브젝트를
*
가져온다.
*/
class TestClassFactory
{
/**
* thread-local 인스턴스를
만드는 ThreadLocal 클래스 초기화
*/
static ThreadLocal<ThreadLocalTest> testClassThreadLocal = new
TestClassThreadLocal();
/**
* TestClass의 thread-local Object를 리턴
*
*
@return
*/
public static ThreadLocalTest getThreadLocalObject()
{
return testClassThreadLocal.get();
}
}
/**
*
위에서 만든 TestClassFactory.getThreadLocalObject 메소드를
통해
*
thread-local 인스턴스를 가져와 이름과 인스턴스 해쉬코드를 출력하는 프로그램
*/
class ThreadClass implements Runnable
{
public void run() {
ThreadLocalTest testClass =
TestClassFactory.getThreadLocalObject();
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + ":" +
testClass);
try
{
Thread.sleep(200);
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
*
Thread를 여러개 생성하는 메인 클래스
* @author
Administrator
*
*/
public class ThreadLocalTest
{
public static void main(String[] args)
{
Thread t1 = new Thread(new
ThreadClass());
Thread t2 = new Thread(new
ThreadClass());
Thread t3 = new Thread(new
ThreadClass());
t1.start();
t2.start();
t3.start();
}
}
[결과]
Thread별로 TestClass의
오브젝트가 유일함을 알 수 있다.
Thread-0:ThreadLocalTest@9cab16
Thread-2:ThreadLocalTest@1a46e30
Thread-1:ThreadLocalTest@3e25a5
Thread-0:ThreadLocalTest@9cab16
Thread-1:ThreadLocalTest@3e25a5
Thread-2:ThreadLocalTest@1a46e30
Thread-0:ThreadLocalTest@9cab16
Thread-1:ThreadLocalTest@3e25a5
Thread-2:ThreadLocalTest@1a46e30
사실 ActionContext는 다음과 같이 생성된다.
ActionContext actionContext =
ActionContext.getActionContext()
getActionContext() 메소드를 통해
thread-local Object로 생성되는 것이다.
WAS는 사용자의 요청이 있으면
Thread를 생성하여 요청을 처리하는데 웹 애플리케이션에서
thread-lcoal은 request scope와 수명을 같이한다. 요청 처리를 위한 모든
정보를 ActionContext에 저장하고
ActionContext를 thread-local Object로 만들어 놓은
것이다.
댓글 없음:
댓글 쓰기