记录学习,spring + Guava EventBus

@Component
public class ConsumerEvent001 implements EventAbstract<TestEvent001> {

    @Subscribe
    @Override
    public void handle(TestEvent001 event) {
        System.out.println("处理代码001:" + event.getName() + "------------------");
    }
}


@Component
public class ConsumerEvent002 implements  EventAbstract<TestEvent002>{

    @Subscribe
    @Override
    public void handle(TestEvent002 event) {
        //ThreadUtil.sleep(3000);
        System.out.println("处理代码002:" + event.getName()+ "---------------------");
    }
}



/**
 * Event 事件处理抽象接口类
 * @author zhangxun
 */
public interface EventAbstract<E> {

    /**
     *  event
     * @param event
     */
    void handle(E event);
}

/**
 * @Author: ZX
 * @Date: 
 */

@Configuration
public class EventConfig {

    @Bean
    public EventBus eventBus(){
        EventBus eventBus = new EventBus();
        return eventBus;
    }

    @Bean
    public AsyncEventBus asyncEventBus(){
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3 , 10 , 3000L , TimeUnit.SECONDS,  new ArrayBlockingQueue<>(3));
        return new AsyncEventBus(threadPoolExecutor);
    }

}





/**
 * @Author: ZX
 * @Date: 
 */

@Component
public class EventListenerRegist {

    @Autowired
    private EventBus eventBus;

//    @Qualifier(value = "asyncEventBus")
//    @Autowired
//    private AsyncEventBus asyncEventBus;

    @Autowired
    private ConsumerEvent001 consumerEvent001;

    @Autowired
    private ConsumerEvent002 consumerEvent002;

    @PostConstruct
    public void init(){
        eventBus.register(consumerEvent001);
        eventBus.register(consumerEvent002);
    }

}



@Component
public class ProviderEvent {

    @Autowired
    private EventBus eventBus;

    @Autowired
    private AsyncEventBus asyncEventBus;

    public <T> void  post(T t){
        eventBus.post(t);
    }

    public <T> void   asyncPost(T t){
        eventBus.post(t);
    }
}

public class TestEvent001 {}

public class TestEvent002 {}



public class TestApp {


    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(TestApp.class);
        annotationConfigApplicationContext.scan("com");
        TestEvent001 testEvent001 = new TestEvent001("test001");

        ProviderEvent bean =   annotationConfigApplicationContext.getBean(ProviderEvent.class);
        for(int i = 0 ;i < 100 ; i++){
            bean.asyncPost(new TestEvent002("test002--" + i));
        }
        //bean.post(testEvent001);

    }


}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值