-
Notifications
You must be signed in to change notification settings - Fork 1
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 #46 from dnd-side-project/feat/ddd
이벤트 처리
- Loading branch information
Showing
14 changed files
with
262 additions
and
63 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
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.dnd.gooding.common.event; | ||
|
||
public abstract class Event { | ||
|
||
private long timestamp; | ||
|
||
public Event() { | ||
this.timestamp = System.currentTimeMillis(); | ||
} | ||
|
||
public long getTimestamp() { | ||
return timestamp; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/dnd/gooding/common/event/EventHandler.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,19 @@ | ||
package com.dnd.gooding.common.event; | ||
|
||
import net.jodah.typetools.TypeResolver; | ||
|
||
public interface EventHandler<T> { | ||
|
||
void handle(T event); | ||
|
||
/** | ||
* 핸들러가 이벤트를 처리할 수 있는지 여부를 검사 | ||
* | ||
* @param event | ||
* @return | ||
*/ | ||
default boolean canHandle(Object event) { | ||
Class<?>[] typeArgs = TypeResolver.resolveRawArguments(EventHandler.class, this.getClass()); | ||
return typeArgs[0].isAssignableFrom(event.getClass()); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/dnd/gooding/common/event/EventResetProcessor.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,35 @@ | ||
package com.dnd.gooding.common.event; | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Aspect | ||
@Order(0) | ||
@Component | ||
public class EventResetProcessor { | ||
|
||
private ThreadLocal<Integer> nestedCount = | ||
new ThreadLocal<Integer>() { | ||
|
||
@Override | ||
protected Integer initialValue() { | ||
return 0; | ||
} | ||
}; | ||
|
||
@Around("@within(org.springframework.stereotype.Service)") | ||
public Object doReset(ProceedingJoinPoint joinPoint) throws Throwable { | ||
nestedCount.set(nestedCount.get() + 1); | ||
try { | ||
return joinPoint.proceed(); | ||
} finally { | ||
nestedCount.set(nestedCount.get() - 1); | ||
if (nestedCount.get() == 0) { | ||
Events.reset(); | ||
} | ||
} | ||
} | ||
} |
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,57 @@ | ||
package com.dnd.gooding.common.event; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Events { | ||
|
||
private static ThreadLocal<List<EventHandler<?>>> handlers = new ThreadLocal<>(); | ||
private static ThreadLocal<Boolean> publishing = | ||
new ThreadLocal<Boolean>() { | ||
@Override | ||
protected Boolean initialValue() { | ||
return Boolean.FALSE; | ||
} | ||
}; | ||
|
||
public static void raise(Object event) { | ||
if (publishing.get()) { | ||
return; | ||
} | ||
|
||
try { | ||
publishing.set(Boolean.TRUE); | ||
|
||
List<EventHandler<?>> eventHandlers = handlers.get(); | ||
if (eventHandlers == null) { | ||
return; | ||
} | ||
for (EventHandler handler : eventHandlers) { | ||
if (handler.canHandle(event)) { | ||
handler.handle(event); | ||
} | ||
} | ||
} finally { | ||
publishing.set(Boolean.FALSE); | ||
} | ||
} | ||
|
||
public static void handle(EventHandler<?> handler) { | ||
if (publishing.get()) { | ||
return; | ||
} | ||
|
||
List<EventHandler<?>> eventHandlers = handlers.get(); | ||
if (eventHandlers == null) { | ||
eventHandlers = new ArrayList<>(); | ||
handlers.set(eventHandlers); | ||
} | ||
eventHandlers.add(handler); | ||
} | ||
|
||
public static void reset() { | ||
if (!publishing.get()) { | ||
handlers.remove(); | ||
} | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
src/main/java/com/dnd/gooding/oauth/infra/MemberCreatedHandler.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.dnd.gooding.oauth.infra; | ||
|
||
import com.dnd.gooding.common.event.EventHandler; | ||
import com.dnd.gooding.user.command.application.CreateMemberService; | ||
import com.dnd.gooding.user.command.domain.MemberCreatedEvent; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class MemberCreatedHandler implements EventHandler<MemberCreatedEvent> { | ||
|
||
private CreateMemberService createMemberService; | ||
|
||
public MemberCreatedHandler(CreateMemberService createMemberService) { | ||
this.createMemberService = createMemberService; | ||
} | ||
|
||
@Override | ||
public void handle(MemberCreatedEvent event) { | ||
createMemberService.create(event.getEmail(), event.getoAuthId().getId()); | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
src/main/java/com/dnd/gooding/user/command/domain/MemberCreatedEvent.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,26 @@ | ||
package com.dnd.gooding.user.command.domain; | ||
|
||
import com.dnd.gooding.common.event.Event; | ||
import com.dnd.gooding.oauth.command.domain.OAuthId; | ||
|
||
public class MemberCreatedEvent extends Event { | ||
|
||
private String email; | ||
private OAuthId oAuthId; | ||
|
||
private MemberCreatedEvent() {} | ||
|
||
public MemberCreatedEvent(String email, OAuthId oAuthId) { | ||
super(); | ||
this.email = email; | ||
this.oAuthId = oAuthId; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public OAuthId getoAuthId() { | ||
return oAuthId; | ||
} | ||
} |
60 changes: 32 additions & 28 deletions
60
...est/java/com/dnd/gooding/integration/user/command/CreateMemberServiceIntegrationTest.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 |
---|---|---|
@@ -1,40 +1,44 @@ | ||
package com.dnd.gooding.integration.user.command; | ||
|
||
import com.dnd.gooding.integration.IntegrationTest; | ||
import com.dnd.gooding.user.command.application.CreateMemberService; | ||
import com.dnd.gooding.user.query.application.MemberQueryService; | ||
import com.dnd.gooding.user.query.dto.MemberData; | ||
import javax.persistence.EntityManager; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import com.dnd.gooding.integration.IntegrationTest; | ||
import com.dnd.gooding.user.command.application.CreateMemberService; | ||
import com.dnd.gooding.user.query.application.MemberQueryService; | ||
import com.dnd.gooding.user.query.dto.MemberData; | ||
|
||
class CreateMemberServiceIntegrationTest extends IntegrationTest { | ||
|
||
@Autowired private CreateMemberService createMemberService; | ||
@Autowired private MemberQueryService memberQueryService; | ||
@Autowired private EntityManager entityManager; | ||
|
||
@DisplayName("멤버를 생성한다.") | ||
@Test | ||
void createMember() { | ||
// given | ||
String id = "[email protected]"; | ||
String oAuthId = "12356"; | ||
|
||
// when | ||
createMemberService.createMember(id, oAuthId); | ||
entityManager.flush(); | ||
entityManager.clear(); | ||
|
||
// then | ||
MemberData memberData = memberQueryService.getMember(id); | ||
|
||
Assertions.assertEquals(1, 1); | ||
Assertions.assertNotNull(memberData); | ||
Assertions.assertEquals(id, memberData.getId()); | ||
Assertions.assertEquals(oAuthId, memberData.getoAuthId()); | ||
} | ||
@Autowired | ||
private CreateMemberService createMemberService; | ||
@Autowired | ||
private MemberQueryService memberQueryService; | ||
@Autowired | ||
private EntityManager entityManager; | ||
|
||
@DisplayName("멤버를 생성한다.") | ||
@Test | ||
void create() { | ||
// given | ||
String id = "[email protected]"; | ||
String oAuthId = "12356"; | ||
|
||
// when | ||
createMemberService.create(id, oAuthId); | ||
entityManager.flush(); | ||
entityManager.clear(); | ||
|
||
// then | ||
MemberData memberData = memberQueryService.getMember(id); | ||
|
||
Assertions.assertEquals(1, 1); | ||
Assertions.assertNotNull(memberData); | ||
Assertions.assertEquals(id, memberData.getId()); | ||
Assertions.assertEquals(oAuthId, memberData.getoAuthId()); | ||
} | ||
} |
Oops, something went wrong.