본문 바로가기
슬기로운 자바 개발자 생활/Java more

Google Guava EventBus 라이브러리 사용법

by 슬기로운 동네 형 2023. 1. 22.
반응형

Google Guava EventBus 라이브러리 사용법

Google Guava Event Bus


 구글 구아바는 자바용 공통 라이브러리 오픈 소스 집합이다. 구글 개발자들이 개발했다.

 아파치 커먼즈 Apache Commons와 목적이 비슷하다.

 

https://github.com/google/guava/wiki

 

GitHub - google/guava: Google core libraries for Java

Google core libraries for Java. Contribute to google/guava development by creating an account on GitHub.

github.com

https://commons.apache.org/

 

Apache Commons – Apache Commons

Welcome to Apache Commons Apache Commons is an Apache project focused on all aspects of reusable Java components. The Apache Commons project is composed of three parts: The Commons Proper - A repository of reusable Java components. The Commons Sandbox - A

commons.apache.org

 

오늘 소개할 구아바 안에 여러 유틸들이 존재하는데 그중 하나인 EventBus다.

구독자 패턴 또는 옵저버 패턴을 구현해 놓은 기능이다.

관련 패턴에 대한 경험이나 이해가 있다면 구아바는 꽤 간편하게 사용할 수 있다.

 

 내 경우에는 주로 어디선가 데이터를 연계 받은 후, 후처리 기능(메일 보내기, 결재 결과 반영하기, Todo 발생시키기)에 사용하곤 했다.

 

안드로이드 쪽에서는 otto 라이브러리가 비슷한 개념이다. 

 시나리오 / 상황

 예로 내가 맡은 시스템이 비용처리를 하는 시스템이라고 가정해본다. 전자결재 시스템 같은 외부 시스템에서 결재 결과를 수신받은 후, 수신받은 결재의 품의서 종류에 따라 이후 작업(후 처리)이 다를 경우에 사용해 본다.

 예제와 비슷한 상황은 각자 판단 하면 될것이다.

 소스

  • ReceivedDataVo : 타 시스템으로 받은 결과 데이터
  • DoSomeThingWorkEventListner1 : 받은 데이터로 어떤 처리를 하는 객체/리스트너 (메일 보내기, ToDo 등록, DB 영)
  • DoSomeThingWorkEventListner2 : 받은 데이터로 어떤 처리를 하는 객체/리스트너 (메일보내기, ToDo 등록, DB 반영)
  • EventBustTest : 리스트너를 등록하고 이벤트 발행

 

pom.xml

<dependency>
    <groupId>org.sonatype.nexus</groupId>
    <artifactId>google-guava-eventbus</artifactId>
    <version>3.2.0-01</version>
</dependency>

 

ReceivedDataVo.java

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@AllArgsConstructor
public class ReceivedDataVo {
    private String interfaceName;
    private String resultCode;
    private String value01;
    private String value02;
    private String value03;
}

DoSomeThingWorkEventListner1.java

import com.google.common.eventbus.Subscribe;
public class DoSomethingWorkEventListener1 {
    @Subscribe
    public void handler(ReceivedDataVo receivedDataVo){
        System.out.println("1. ===================================================");
        System.out.println("1. 받은 데이터로 어떤 작업을 합니다.");
        System.out.println(receivedDataVo.toString());
    }
}

DoSomeThingWorkEventListner2.java

import com.google.common.eventbus.Subscribe;
public class DoSomethingWorkEventListener2 {
    @Subscribe
    public void handler(ReceivedDataVo receivedDataVo){
        System.out.println("2. ***************************************************");
        System.out.println("2. 받은 데이터로 어떤 작업을 합니다.");
        System.out.println(receivedDataVo.toString());
    }
}

EventBusTest.java

import com.google.common.eventbus.EventBus;
public class EventBustTest {
    public static void main(String[] args) {

        //이벤트버스 생성
        EventBus eventBus = new EventBus("myEventBus Example");

        //이벤트버스 등록
        eventBus.register(new DoSomethingWorkEventListener1());
        eventBus.register(new DoSomethingWorkEventListener2());

        // 1. 데이터를 다른 곳에서 받는다. ReceivedDataVo를 받았다고 가정함.
        // 2. 등록된 이벤트리스너가 실행 된다.(DoSomethingWorkEventListener1, DoSomethingWorkEventListener2)
        eventBus.post(new ReceivedDataVo("결재결과:인건비처리","C:승인","최종결재자:홍길동","사번:20110001","시간:202212310911"));
    }
}

결과

실행 결과

반응형

댓글