Skip to content

Commit 7c34500

Browse files
authored
Merge pull request quarkusio#34525 from manovotn/issue34332
Undertow - fire context events for session context
2 parents f21a955 + ed08d10 commit 7c34500

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.quarkus.undertow.test.sessioncontext;
2+
3+
import jakarta.enterprise.context.ApplicationScoped;
4+
import jakarta.enterprise.context.BeforeDestroyed;
5+
import jakarta.enterprise.context.Destroyed;
6+
import jakarta.enterprise.context.Initialized;
7+
import jakarta.enterprise.context.SessionScoped;
8+
import jakarta.enterprise.event.Observes;
9+
10+
@ApplicationScoped
11+
public class ObservingBean {
12+
13+
int timesInitObserved = 0;
14+
int timesBeforeDestroyedObserved = 0;
15+
int timesDestroyedObserved = 0;
16+
17+
public int getTimesInitObserved() {
18+
return timesInitObserved;
19+
}
20+
21+
public int getTimesBeforeDestroyedObserved() {
22+
return timesBeforeDestroyedObserved;
23+
}
24+
25+
public int getTimesDestroyedObserved() {
26+
return timesDestroyedObserved;
27+
}
28+
29+
public void observeInit(@Observes @Initialized(SessionScoped.class) Object event) {
30+
timesInitObserved++;
31+
}
32+
33+
public void observeBeforeDestroyed(@Observes @BeforeDestroyed(SessionScoped.class) Object event) {
34+
timesBeforeDestroyedObserved++;
35+
}
36+
37+
public void observeDestroyed(@Observes @Destroyed(SessionScoped.class) Object event) {
38+
timesDestroyedObserved++;
39+
}
40+
41+
public void resetState() {
42+
this.timesInitObserved = 0;
43+
this.timesBeforeDestroyedObserved = 0;
44+
this.timesDestroyedObserved = 0;
45+
}
46+
}

extensions/undertow/deployment/src/test/java/io/quarkus/undertow/test/sessioncontext/SessionContextTestCase.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
import static org.hamcrest.Matchers.is;
66
import static org.junit.jupiter.api.Assertions.assertNotEquals;
77

8+
import org.junit.jupiter.api.Assertions;
89
import org.junit.jupiter.api.Test;
910
import org.junit.jupiter.api.extension.RegisterExtension;
1011

12+
import io.quarkus.arc.Arc;
1113
import io.quarkus.test.QuarkusUnitTest;
1214
import io.restassured.response.Response;
1315

1416
public class SessionContextTestCase {
1517

1618
@RegisterExtension
1719
static QuarkusUnitTest runner = new QuarkusUnitTest()
18-
.withApplicationRoot((jar) -> jar.addClasses(TestServlet.class, Foo.class));
20+
.withApplicationRoot((jar) -> jar.addClasses(TestServlet.class, Foo.class, ObservingBean.class));
1921

2022
@Test
2123
public void testServlet() {
@@ -30,4 +32,18 @@ public void testServlet() {
3032
response.then().statusCode(200).body(is("count=1"));
3133
}
3234

35+
@Test
36+
public void testContextEvents() {
37+
ObservingBean observingBean = Arc.container().select(ObservingBean.class).get();
38+
39+
// make sure we start with zero events to keep this test method independent
40+
observingBean.resetState();
41+
42+
// following request creates a session and also destroys it by enforcing invalidation
43+
when().get("/foo?destroy=true").then().statusCode(200);
44+
Assertions.assertEquals(1, observingBean.getTimesInitObserved());
45+
Assertions.assertEquals(1, observingBean.getTimesBeforeDestroyedObserved());
46+
Assertions.assertEquals(1, observingBean.getTimesDestroyedObserved());
47+
}
48+
3349
}

extensions/undertow/runtime/src/main/java/io/quarkus/undertow/runtime/HttpSessionContext.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
import java.util.Objects;
77
import java.util.stream.Collectors;
88

9+
import jakarta.enterprise.context.BeforeDestroyed;
910
import jakarta.enterprise.context.ContextNotActiveException;
11+
import jakarta.enterprise.context.Destroyed;
12+
import jakarta.enterprise.context.Initialized;
1013
import jakarta.enterprise.context.SessionScoped;
1114
import jakarta.enterprise.context.spi.Contextual;
1215
import jakarta.enterprise.context.spi.CreationalContext;
16+
import jakarta.enterprise.event.Event;
1317
import jakarta.servlet.annotation.WebListener;
1418
import jakarta.servlet.http.HttpServletRequest;
1519
import jakarta.servlet.http.HttpSession;
@@ -205,12 +209,20 @@ public boolean equals(Object obj) {
205209
@Override
206210
public void sessionDestroyed(HttpSessionEvent se) {
207211
HttpSession session = se.getSession();
212+
Event<Object> event = Arc.container().beanManager().getEvent();
213+
event.select(HttpSession.class, BeforeDestroyed.Literal.SESSION).fire(session);
208214
try {
209215
DESTRUCT_SESSION.set(session);
210216
destroy(session);
217+
event.select(HttpSession.class, Destroyed.Literal.SESSION).fire(session);
211218
} finally {
212219
DESTRUCT_SESSION.remove();
213220
}
214221
}
215222

223+
@Override
224+
public void sessionCreated(HttpSessionEvent se) {
225+
Arc.container().beanManager().getEvent().select(HttpSession.class, Initialized.Literal.SESSION).fire(se.getSession());
226+
}
227+
216228
}

0 commit comments

Comments
 (0)