Guava 의 Event Bus 실전

4448 단어 차단 대기 열
EventBus 는 Guava 의 사건 처리 체제 로 디자인 모델 중의 관찰자 모델 (생산 / 소비자 프로 그래 밍 모델) 의 우아 한 실현 이다.이벤트 감청 과 게시 구독 모드 에 대해 EventBus 는 매우 우아 하고 간단 한 해결 방안 으로 복잡 한 클래스 와 인터페이스 차원 구 조 를 만 들 필요 가 없습니다.정의 EventBus
@Configuration
public class EventBusConfig {

    @Autowired
    private ApplicationContext context;

    /**
     *     : 
* ConditionalOnMissingBean , bean, */ @Bean @ConditionalOnMissingBean(AsyncEventBus.class) AsyncEventBus createEventBus() { // EventBus AsyncEventBus eventBus = new AsyncEventBus(Executors.newFixedThreadPool(5)); Reflections reflections = new Reflections("com.google.guava.listener", new MethodAnnotationsScanner()); Set methods = reflections.getMethodsAnnotatedWith(Subscribe.class); if (null != methods) { for (Method method : methods) { try { System.out.println(" :" + context.getBean(method.getDeclaringClass())); eventBus.register(context.getBean(method.getDeclaringClass())); } catch (Exception e) { e.printStackTrace(); } } } return eventBus; } }

이벤트 정의
public interface IEvent {
    void print();
}

public class LoginEvent implements IEvent{
    @Override
    public void print() {
        System.out.println("login ......");
    }
}

public class LogoutEvent implements IEvent {
    @Override
    public void print() {
        System.out.println("logout ......");
    }
}

public class BusinessEvent implements IEvent{
    @Override
    public void print() {
        System.out.println("business ......");
    }
}

정의 Listener
public interface IEventListener {
    void login(LoginEvent event);
    void business(BusinessEvent event);
    void logout(LogoutEvent event);
}

/**
 *                  
 */
@Component
public class LoginEventListener extends EventListenerAdapter {

    @Subscribe
    @Override
    public void login(LoginEvent event) {
        System.out.println("login receive event ...");
        event.print();
    }

    @Subscribe
    @Override
    public void business(BusinessEvent event) {
        System.out.println("business receive event ...");
        event.print();
    }
}

/**
 *                
 */
@Component
public class LogoutEventListener extends EventListenerAdapter{

    @Subscribe
    @Override
    public void logout(LogoutEvent event) {
        System.out.println("logout receive event ...");
        event.print();
    }

    @Subscribe
    public void logoutT(LogoutEvent event) {
        System.out.println("logoutT receive event ...");
        event.print();
    }
}


public abstract class EventListenerAdapter implements IEventListener{

    @Override
    public void login(LoginEvent event) {
    }

    @Override
    public void business(BusinessEvent enent) {
    }

    @Override
    public void logout(LogoutEvent event) {
    }
}

정의 이벤트 발표 인터페이스
public interface IBusinessService {
    void post(IEvent event);
}

@Service
public class BusinessServiceImpl implements IBusinessService {

    @Autowired
    private AsyncEventBus eventBus;

    @Override
    public void post(IEvent event) {
        if (null != event) {
            //     
            eventBus.post(event);
        }
    }
}

시작 트리거 이벤트
@Component
public class StartRunner implements CommandLineRunner {

    @Autowired
    private IBusinessService businessService;

    @Override
    public void run(String... args) throws Exception {
        IEvent loginEvent = new LoginEvent();
        businessService.post(loginEvent);
        IEvent logoutEvent = new LogoutEvent();
        businessService.post(logoutEvent);
        IEvent businessEvent = new BusinessEvent();
        businessService.post(businessEvent);
    }
}

시작 클래스
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class EventBusApplication {

    public static void main(String[] args) {
        try {
            SpringApplication.run(EventBusApplication.class, args);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

원리 분석https://www.jianshu.com/p/4bddd45a8e7a

좋은 웹페이지 즐겨찾기