spring-事件发布器

实现:异步监听两个事件,update事件和delete事件
上代码:
删除事件定义:

public class DeleteEvent extends ApplicationEvent {
    private String eventId;

    public DeleteEvent(Object source) {
        super(source);
    }

    public DeleteEvent(Object source, Clock clock) {
        super(source, clock);
    }

    public String getEventId() {
        return eventId;
    }

    public void setEventId(String eventId) {
        this.eventId = eventId;
    }
}

更新事件定义:

public class UpdateEvent extends ApplicationEvent {
    private String eventId;

    public UpdateEvent(Object source) {
        super(source);
    }

    public UpdateEvent(Object source, Clock clock) {
        super(source, clock);
    }

    public String getEventId() {
        return eventId;
    }

    public void setEventId(String eventId) {
        this.eventId = eventId;
    }
}

自定义事件多波器,实现异步处理:

@Component
public class ListenerConfig {
   //异步执行的关键,如果不配置Multicaster,则是同步执行
   //ApplicationEvent默认是同步执行
    @Bean
    public SimpleApplicationEventMulticaster applicationEventMulticaster() {
        SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster();
        BlockingQueue blockingQueue = new LinkedBlockingDeque<>(1000);
        //根据实际需要调整线程数
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 20, 1, TimeUnit.SECONDS, blockingQueue);
        simpleApplicationEventMulticaster.setTaskExecutor(threadPoolExecutor);
        return simpleApplicationEventMulticaster;
    }

    @Bean
    public ApplicationListener updateListener(){
        return updateEvent -> {
            System.out.println("收到更新事件"+updateEvent);
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("输出更新事件id:"+updateEvent.getEventId());
        };
    }

    @Bean
    public ApplicationListener deleteListener(){
        return deleteEvent -> {
            System.out.println("收到删除事件"+deleteEvent);
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("输出删除事件id:"+deleteEvent.getEventId());
        };
    }
}

测试:
方式一:

    @Autowired
    private ApplicationEventPublisher eventPublisher;

    @Test
    public void tset(){
        System.out.println("事件测试开始。。。。");
        UpdateEvent update = new UpdateEvent("update");
        update.setEventId("update-1");
        eventPublisher.publishEvent(update);
        DeleteEvent deleteEvent = new DeleteEvent("delete");
        deleteEvent.setEventId("delete-1");
        eventPublisher.publishEvent(deleteEvent);
        System.out.println("事件测试结束。。。。。");
    }

方式二:

   @Test
    public void tset(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ListenerConfig.class);
        System.out.println("事件测试开始。。。。");
        UpdateEvent update = new UpdateEvent("update");
        update.setEventId("update-1");
        applicationContext.publishEvent(update);
        DeleteEvent deleteEvent = new DeleteEvent("delete");
        deleteEvent.setEventId("delete-1");
        applicationContext.publishEvent(deleteEvent);
        applicationContext.close();
        System.out.println("事件测试结束。。。。。");
    }

结果输出:


image.png

你可能感兴趣的:(spring-事件发布器)