Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 33 additions & 28 deletions server-session/src/main/java/com/iluwatar/sessionserver/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;

/**
Expand All @@ -57,6 +60,10 @@ public class App {
private static Map<String, Instant> sessionCreationTimes = new HashMap<>();
private static final long SESSION_EXPIRATION_TIME = 10000;

private static final ScheduledExecutorService sessionScheduler =
Executors.newSingleThreadScheduledExecutor(
Thread.ofVirtual().name("session-scheduler-", 1).factory());

/**
* Main entry point.
*
Expand All @@ -81,36 +88,34 @@ public static void main(String[] args) throws IOException {
}

private static void sessionExpirationTask() {
new Thread(
() -> {
while (true) {
try {
LOGGER.info("Session expiration checker started...");
Thread.sleep(SESSION_EXPIRATION_TIME); // Sleep for expiration time
Instant currentTime = Instant.now();
synchronized (sessions) {
synchronized (sessionCreationTimes) {
Iterator<Map.Entry<String, Instant>> iterator =
sessionCreationTimes.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Instant> entry = iterator.next();
if (entry
.getValue()
.plusMillis(SESSION_EXPIRATION_TIME)
.isBefore(currentTime)) {
sessions.remove(entry.getKey());
iterator.remove();
}
}
}
sessionScheduler.scheduleAtFixedRate(
() -> {
try {
LOGGER.info("Session expiration checker started...");
Instant currentTime = Instant.now();
synchronized (sessions) {
synchronized (sessionCreationTimes) {
Iterator<Map.Entry<String, Instant>> iterator =
sessionCreationTimes.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Instant> entry = iterator.next();
if (entry
.getValue()
.plusMillis(SESSION_EXPIRATION_TIME)
.isBefore(currentTime)) {
sessions.remove(entry.getKey());
iterator.remove();
}
LOGGER.info("Session expiration checker finished!");
} catch (InterruptedException e) {
LOGGER.error("An error occurred: ", e);
Thread.currentThread().interrupt();
}
}
})
.start();
}
LOGGER.info("Session expiration checker finished!");
} catch (Exception e) {
LOGGER.error("An error occurred: ", e);
}
},
0,
SESSION_EXPIRATION_TIME,
TimeUnit.MILLISECONDS);
}
}
9 changes: 6 additions & 3 deletions value-object/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ tag:

## Also known as

* Embedded Value
* Immutable Object
* Embedded Value
* Inline Value
* Integrated Value

## Intent of Value Object Design Pattern
## Intent of Value Object / Immutable Object Design Pattern

The Value Object pattern in Java creates immutable objects that represent a descriptive aspect of the domain with no conceptual identity. It aims to enhance performance and reduce memory overhead by storing frequently accessed immutable data directly within the object that uses it, rather than separately.
The Value Object pattern (also known as the **Immutable Object pattern**) in Java creates immutable objects that represent a descriptive aspect of the domain with no conceptual identity. It aims to enhance performance and reduce memory overhead by storing frequently accessed immutable data directly within the object that uses it, rather than separately.

The Immutable Object pattern ensures that an object's state cannot be modified after construction, providing thread-safety and predictability in concurrent scenarios.

## Detailed Explanation of Value Object Pattern with Real-World Examples

Expand Down Expand Up @@ -146,3 +148,4 @@ Trade-offs:
* [J2EE Design Patterns](https://amzn.to/4dpzgmx)
* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
* [ValueObject (Martin Fowler)](https://martinfowler.com/bliki/ValueObject.html)

Loading