Skip to content

Commit

Permalink
Feat: 멤버 생성 동기 이벤트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
haeyonghahn committed Feb 3, 2024
1 parent 7d6aca3 commit f2fc624
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 212 deletions.
14 changes: 7 additions & 7 deletions src/main/java/com/dnd/gooding/common/event/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

public abstract class Event {

private long timestamp;
private long timestamp;

public Event() {
this.timestamp = System.currentTimeMillis();
}
public Event() {
this.timestamp = System.currentTimeMillis();
}

public long getTimestamp() {
return timestamp;
}
public long getTimestamp() {
return timestamp;
}
}
22 changes: 11 additions & 11 deletions src/main/java/com/dnd/gooding/common/event/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

public interface EventHandler<T> {

void handle(T event);
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());
}
/**
* 핸들러가 이벤트를 처리할 수 있는지 여부를 검사
*
* @param event
* @return
*/
default boolean canHandle(Object event) {
Class<?>[] typeArgs = TypeResolver.resolveRawArguments(EventHandler.class, this.getClass());
return typeArgs[0].isAssignableFrom(event.getClass());
}
}
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();
}
}
}
}
99 changes: 49 additions & 50 deletions src/main/java/com/dnd/gooding/common/event/Events.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,53 @@

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();
}
}
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,55 +1,54 @@
package com.dnd.gooding.oauth.command.application;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dnd.gooding.common.event.Events;
import com.dnd.gooding.oauth.command.domain.ExternalLogin;
import com.dnd.gooding.oauth.command.domain.OAuth;
import com.dnd.gooding.oauth.command.domain.OAuthId;
import com.dnd.gooding.oauth.command.domain.OAuthRepository;
import com.dnd.gooding.oauth.command.model.OAuthMember;
import com.dnd.gooding.user.command.application.CreateMemberService;
import com.dnd.gooding.user.command.domain.MemberCreatedEvent;
import com.dnd.gooding.oauth.infra.MemberCreatedHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CreateOAuthService {

private CreateMemberService createMemberService;
private OAuthRepository oAuthRepository;

private ExternalLogin externalLogin;

public CreateOAuthService(CreateMemberService createMemberService,
OAuthRepository oAuthRepository, ExternalLogin externalLogin) {
this.createMemberService = createMemberService;
this.oAuthRepository = oAuthRepository;
this.externalLogin = externalLogin;
}

@Transactional
public OAuth create(String code) {
OAuthMember oAuthMember = externalLogin.getOauthToken(code);
Events.handle((MemberCreatedEvent evt) ->
createMemberService.create(evt.getEmail(), evt.getoAuthId().getId()));

OAuth oAuth = new OAuth(
new OAuthId(oAuthMember.getoAuthId()),
oAuthMember.getImageUrl(),
oAuthMember.getProvider(),
oAuthMember.getEmail());
createOAuth(oAuth);
return oAuth;
}

@Transactional
public void createOAuth(OAuth oAuth) {
oAuthRepository
.findByoAuthId(oAuth.getoAuthId())
.ifPresentOrElse(x -> {
}, () -> {
oAuthRepository.save(oAuth);
});
}
private OAuthRepository oAuthRepository;

private ExternalLogin externalLogin;

@Autowired private MemberCreatedHandler memberCreatedHandler;

public CreateOAuthService(OAuthRepository oAuthRepository, ExternalLogin externalLogin) {
this.oAuthRepository = oAuthRepository;
this.externalLogin = externalLogin;
}

@Transactional
public OAuth create(String code) {
OAuthMember oAuthMember = externalLogin.getOauthToken(code);
Events.handle(memberCreatedHandler);

OAuth oAuth =
new OAuth(
new OAuthId(oAuthMember.getoAuthId()),
oAuthMember.getImageUrl(),
oAuthMember.getProvider(),
oAuthMember.getEmail());

createOAuth(oAuth);
return oAuth;
}

@Transactional
public void createOAuth(OAuth oAuth) {
oAuthRepository
.findByoAuthId(oAuth.getoAuthId())
.ifPresentOrElse(
x -> {},
() -> {
oAuthRepository.save(oAuth);
});
}
}
67 changes: 32 additions & 35 deletions src/main/java/com/dnd/gooding/oauth/command/domain/OAuth.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,43 @@
package com.dnd.gooding.oauth.command.domain;

import com.dnd.gooding.common.event.Events;
import com.dnd.gooding.user.command.domain.MemberCreatedEvent;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

import com.dnd.gooding.common.event.Events;
import com.dnd.gooding.user.command.domain.MemberCreatedEvent;

@Entity
@Table(name = "oauth")
public class OAuth {

@EmbeddedId
private OAuthId oAuthId;
private String imageUrl;
private String provider;
private String email;

protected OAuth() {
}

public OAuth(OAuthId oAuthId, String imageUrl, String provider, String email) {
this.oAuthId = oAuthId;
this.imageUrl = imageUrl;
this.provider = provider;
this.email = email;
Events.raise(new MemberCreatedEvent(email, oAuthId));
}

public OAuthId getoAuthId() {
return oAuthId;
}

public String getImageUrl() {
return imageUrl;
}

public String getProvider() {
return provider;
}

public String getEmail() {
return email;
}
@EmbeddedId private OAuthId oAuthId;
private String imageUrl;
private String provider;
private String email;

protected OAuth() {}

public OAuth(OAuthId oAuthId, String imageUrl, String provider, String email) {
this.oAuthId = oAuthId;
this.imageUrl = imageUrl;
this.provider = provider;
this.email = email;
Events.raise(new MemberCreatedEvent(email, oAuthId));
}

public OAuthId getoAuthId() {
return oAuthId;
}

public String getImageUrl() {
return imageUrl;
}

public String getProvider() {
return provider;
}

public String getEmail() {
return email;
}
}
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());
}
}
Loading

0 comments on commit f2fc624

Please sign in to comment.