Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from eamtalu/develop
#33 OpenAccountUseCase - Publishing events
- Loading branch information
Showing
8 changed files
with
140 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
version: '3.8' | ||
services: | ||
postgres: | ||
image: postgres:latest | ||
container_name: postgres | ||
db: | ||
image: postgres:14.1-alpine | ||
restart: always | ||
environment: | ||
- POSTGRES_USER=postgres | ||
- POSTGRES_PASSWORD=postgres | ||
ports: | ||
- "5432:5432" | ||
restart: unless-stopped | ||
- '5432:5432' | ||
volumes: | ||
- ./db_persist:/var/lib/postgresql/data | ||
environment: | ||
POSTGRES_USER: ${POSTGRES_USER} | ||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} | ||
POSTGRES_DB: ${POSTGRES_DB} | ||
- db:/var/lib/postgresql/data | ||
volumes: | ||
db: | ||
driver: local |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/optivem/kata/banking/core/domain/common/events/EventPublisher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.optivem.kata.banking.core.domain.common.events; | ||
|
||
/** | ||
* Event Publisher interface will provide eventpublishing functionality to usecases | ||
*/ | ||
public interface EventPublisher { | ||
public void publishEvent(UseCaseEvent event); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/optivem/kata/banking/core/domain/common/events/EventPublisherToQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.optivem.kata.banking.core.domain.common.events; | ||
|
||
|
||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.stereotype.Component; | ||
|
||
|
||
@Component | ||
public class EventPublisherToQueue implements EventPublisher { | ||
|
||
private ApplicationEventPublisher applicationEventPublisher; | ||
|
||
public EventPublisherToQueue(ApplicationEventPublisher applicationEventPublisher){ | ||
this.applicationEventPublisher = applicationEventPublisher; | ||
} | ||
|
||
@Override | ||
public void publishEvent(UseCaseEvent event) { | ||
applicationEventPublisher.publishEvent(event); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/optivem/kata/banking/core/domain/common/events/UseCaseEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.optivem.kata.banking.core.domain.common.events; | ||
|
||
import com.optivem.kata.banking.core.domain.accounts.AccountId; | ||
import lombok.Getter; | ||
import org.springframework.context.ApplicationEvent; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
/** | ||
* This class serves as the Event Object that to be puslished by ApplicationEventPublisher | ||
*/ | ||
@Getter | ||
public class UseCaseEvent extends ApplicationEvent { | ||
|
||
private AccountId id; | ||
private String eventName; | ||
private LocalDateTime localDateTime; | ||
public UseCaseEvent(AccountId id, String eventName) { | ||
super(id); | ||
this.id = id; | ||
this.eventName = eventName; | ||
this.localDateTime = LocalDateTime.now(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/optivem/kata/banking/core/domain/common/events/UseCaseEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.optivem.kata.banking.core.domain.common.events; | ||
|
||
import com.optivem.kata.banking.infra.real.inmemory.EventQueue; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* EventListener class that will observe any event of type UseCaseEvent and act accordingly | ||
* It will also insert the event into queue. | ||
*/ | ||
@Component | ||
public class UseCaseEventHandler{ | ||
|
||
private final EventQueue<UseCaseEvent> queue; | ||
|
||
public UseCaseEventHandler(EventQueue<UseCaseEvent> eventEventQueue){ | ||
this.queue = eventEventQueue; | ||
} | ||
|
||
@EventListener | ||
public void handleEvent(UseCaseEvent event){ | ||
queue.addEvent(event); | ||
|
||
// var eventFromQueue = queue.next(); | ||
// System.out.println(eventFromQueue.getId()); | ||
// System.out.println(eventFromQueue.getEventName()); | ||
// System.out.println(eventFromQueue.getLocalDateTime()); | ||
|
||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...tivem/kata/banking/core/domain/common/events/UseCaseEvents/AccountOpenedUseCaseEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.optivem.kata.banking.core.domain.common.events.UseCaseEvents; | ||
|
||
|
||
import com.optivem.kata.banking.core.domain.accounts.AccountId; | ||
import com.optivem.kata.banking.core.domain.common.events.UseCaseEvent; | ||
|
||
public class AccountOpenedUseCaseEvent { | ||
|
||
public static UseCaseEvent generateEventOnSuccess(AccountId id){ | ||
return new UseCaseEvent(id,"AccountOpened"); | ||
} | ||
|
||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/optivem/kata/banking/infra/real/inmemory/EventQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.optivem.kata.banking.infra.real.inmemory; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.NoSuchElementException; | ||
import java.util.Queue; | ||
|
||
|
||
@Component | ||
public class EventQueue<T> { | ||
|
||
private final Queue<T> queue; | ||
|
||
public EventQueue(){ | ||
this.queue = new ArrayDeque<>(); | ||
} | ||
|
||
public void addEvent(T event){ | ||
queue.add(event); | ||
} | ||
|
||
public T next(){ | ||
if(queue.isEmpty()){ | ||
throw new NoSuchElementException(); | ||
} | ||
return queue.remove(); | ||
} | ||
|
||
} |