Skip to content
Snippets Groups Projects
Commit 33d0dbfa authored by Gritsch Philipp - Studierendenaccount's avatar Gritsch Philipp - Studierendenaccount
Browse files

Merge branch 'philipp-session-demo' into 'master'

adds session demo

See merge request !3
parents 122de451 6ff26adb
No related branches found
No related tags found
1 merge request!3adds session demo
...@@ -26,6 +26,12 @@ ...@@ -26,6 +26,12 @@
<artifactId>jakarta.jakartaee-api</artifactId> <artifactId>jakarta.jakartaee-api</artifactId>
<version>9.1.0</version> <version>9.1.0</version>
<scope>provided</scope> <scope>provided</scope>
<exclusions>
<exclusion>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>jakarta.ws.rs</groupId> <groupId>jakarta.ws.rs</groupId>
...@@ -40,6 +46,12 @@ ...@@ -40,6 +46,12 @@
<artifactId>apache-jena-libs</artifactId> <artifactId>apache-jena-libs</artifactId>
<type>pom</type> <type>pom</type>
<version>4.2.0</version> <version>4.2.0</version>
<exclusions>
<exclusion>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId> <groupId>org.apache.logging.log4j</groupId>
......
package com.kgshape.app.rest.demo;
import com.kgshape.app.service.demo.DemoSessionCache;
import jakarta.enterprise.context.RequestScoped;
import jakarta.enterprise.context.SessionScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
/**
* The name of this bean is misleading, then contrary to its name, the offered endpoints
* (all which start with /sessionTest) don't need to know there statefulness
*
* This can also be seen with the {@link RequestScoped} annotation, meaning
* that this object is created again for each object
*/
@RequestScoped
@Path("/sessionTest")
public class SessionAwareEndpoint {
/**
* The only thing which makes the whole endpoint session aware is the injection of this {@link SessionScoped}
* bean
*
* But also note that the scope of the injected Bean should be irrelevant to the development of your endpoint.
* Think of this principle like that: The injected object fulfills a contract, in this case a session wide cache
* The technique used to achieve this contract is not the business of the endpoint, but the implementation of this object
* (it could possibly be a more sophisticated service, which synchronizes sessions across multiple cluster nodes, or something
* else)
*/
@Inject
private DemoSessionCache demoSessionCache;
/**
* Upon request contains a JSESSIONID cookie used to identify the session
*
* As long as the cookie isnt changed this session is alive (also regard session time out)
*/
@GET
public String getCounter() {
return String.valueOf(this.demoSessionCache.getAtomicInteger().getAndIncrement());
}
}
package com.kgshape.app.service.demo;
import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.SessionScoped;
/**
* This object exists once per session
*/
@SessionScoped
public class DemoSessionCache implements Serializable {
private AtomicInteger atomicInteger;
/**
* Always use {@link PostConstruct} to initialize the state of your bean
*
* Reason: Objects may be recycled, leading to inconsistencies, when initializing logic is in the constructor
* (as the constructor wouldn't be called if the object would be recycled)
*/
@PostConstruct
protected void init() {
this.atomicInteger = new AtomicInteger();
}
public AtomicInteger getAtomicInteger() {
return this.atomicInteger;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment