Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[20210220] (Spring) BeanUtils.copyProperties(), Event Handling(Pub-Sub) #45

Open
JuHyun419 opened this issue Feb 20, 2021 · 0 comments
Open

Comments

@JuHyun419
Copy link
Owner

JuHyun419 commented Feb 20, 2021

BeanUtils.copyProperties()


Guava의 EventBus ... Event Driven(Comment<댓글> Event)

// Service Layer 
private final EventBus eventBus;

eventBus.post(new CommentCreatedEvent(comment));


// CommentCreatedEvent는 댓글 관련 Entity
public class CommentCreatedEvent {

  private final Id<Post, Long> postId;
  private final Id<User, Long> userId;
  private final Writer commentWriter;
  private final Id<Comment, Long> commentId;
  ...


// CommentCreatedEventListener
public class CommentCreateEventListener implements AutoCloseable {

  private final EventBus eventBus;

  private final PostService postService;

  private final KafkaTemplate<String, CommentCreatedMessage> kafkaTemplate;

  public CommentCreateEventListener(EventBus eventBus,
                                    PostService postService,
                                    KafkaTemplate<String, CommentCreatedMessage> kafkaTemplate) {
    this.eventBus = eventBus;
    this.postService = postService;
    this.kafkaTemplate = kafkaTemplate;
    eventBus.register(this);  // 등록.. 이벤트를 구독할 수 있는 상태
  }

  /**
   * EventBus를 등록하고 EventBus에 특정 Event를 발송하면
   * EventBus가 그 Event를 SubScribe하고 있는 대상에게 알아서 전달해주는 역할
   * 컨트롤러나 서비스가 CommentCreateEventListener에 대한 존재를 알 필요가 없이, EventBus만 바라보고 있으면 된다.
   * Service의 eventBus.post(new CommentCreateEvent(comment));를 통해 아래 메서드가 호출됨
   */
  @Subscribe
  public void handleCommentCreateEvent(CommentCreatedEvent event) throws Exception {
    Id<Post, Long> postId = event.getPostId();

    // Kafka 에게 발송(send) 로직
    ....
  }


  // Event Configure 설정
  @Bean
  public EventBus eventBus(TaskExecutor eventTaskExecutor, EventExceptionHandler eventExceptionHandler) {
    return new AsyncEventBus(eventTaskExecutor, eventExceptionHandler);
  }

  @Bean()
  public CommentCreateEventListener commentCreateEventListener(EventBus eventBus, PostService postService, KafkaTemplate kafkaTemplate) {
    return new CommentCreateEventListener(eventBus, postService, kafkaTemplate);
  }

References

https://www.baeldung.com/guava-eventbus

@JuHyun419 JuHyun419 changed the title [20210220] (Spring) BeanUtils.copyProperties(), Event Driven(guava EventBus) [20210220] (Spring) BeanUtils.copyProperties(), Event Handling(Pub-Sub) Feb 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant