일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- spring-cloud-stream
- 카프카
- avo
- schema registry
- spring-batch
- Spring
- 플레이 프레임워크
- Slick
- gradle
- kafkastreams
- kafkastream
- spring-kafka
- 한빛미디어
- reactive
- springboot
- scala 2.10
- Elasticsearch
- Logstash
- statestore
- scala
- coursera
- aws
- RabbitMQ
- kafka streams
- Kafka
- confluent
- play framework
- Elk
- kafka interactive query
- enablekafkastreams
- Today
- Total
b
tomcat session 의 생성과 Tomcat session Manager 본문
이 글은 StandardSession 과 StandardManager 만 대상으로 확인하였다.
톰캣의 세션은 결국 메모리 기반의 HashMap 이다. 어떻게 이 HashMap 에 Session 들이 저장되어 있고, 각 Key(SessionId)는 어떻게 발급 및 반환되는지 확인해본다.
spring boot v2.6.1 에서 링크 를 보면 Lazy Session Id Generator 를 ID 생성기로 사용하는 것을 확인 할 수 있다. spring Boot 에 포함된 이 클래스는 크게 변경되는 내용은 없고, 기존의 org.apache.catalina.StandardSessionIdGenerator 를 lazy 형태로 변경한 것에 지나지 않는다
로직은 링크 를 보면 확인할 수 있지만, sessionIdLength=16 일때 32 byte 길이의 sessionId 가 생성된다.
StandardSessionIdGenerator
이 SessionID 는 tomcat의 StandardManager 의 createSession 메소드 (사실은 ManagerBase 클래스에 구현) 에서 사용된다.
ManagerBase.java
그럼 이후에 HttpServeltRequest 에서 어떻게 세션을 획득 할까?
- RequestFacade.java 의 getSession 링크 을 확인
- connector.Request.java 에서 doGetSession 을 확인하면 findSession 을 찾을 수 있다.
- 이 findSession 은 Manager 의 구현체에서 찾을 수 있고
public abstract class ManagerBase extends LifecycleMBeanBase implements Manager {
...
protected Map<String, Session> sessions = new ConcurrentHashMap();
...
public Session findSession(String id) throws IOException {
return id == null ? null : (Session)this.sessions.get(id);
}
...
만약에 session 에 Manager 에 존재하지 않는다면 다음의 링크 의해서 새로운 세션을 만들어 준다.
그리고 아래처럼 응답 쿠키로 반환해준다.
this.session = manager.createSession(sessionId);
if (this.session != null && trackModesIncludesCookie) {
Cookie cookie = ApplicationSessionCookieConfig.createSessionCookie(context, this.session.getIdInternal(), this.isSecure());
this.response.addSessionCookieInternal(cookie);
}
createSession 을 이용해 세션을 계속 생성 할 수 있는데 아래처럼 maxActiveSessions 의 값보다 더 많은 세션을 만들 수는 없다. 이 값은 https://tomcat.apache.org/tomcat-9.0-doc/config/manager.html 처럼 설정으로 변경 가능하다.
public Session createSession(String sessionId) {
if (this.maxActiveSessions >= 0 && this.getActiveSessions() >= this.maxActiveSessions) {
++this.rejectedSessions;
throw new TooManyActiveSessionsException(sm.getString("managerBase.createSession.ise"), this.maxActiveSessions);
} else {
그리고 Session 은 링크 와 같은 StandardSession 구현체가 있다.