欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Spring事件機(jī)制實(shí)現(xiàn)異步的方法

 更新時(shí)間:2018年06月21日 10:37:44   作者:Joepis  
這篇文章主要介紹了使用Spring事件機(jī)制實(shí)現(xiàn)異步的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

當(dāng)把一個(gè)事件發(fā)布到Spring提供的ApplicationContext中,被監(jiān)聽(tīng)器偵測(cè)到,就會(huì)執(zhí)行對(duì)應(yīng)的處理方法。

事件本身
事件是一個(gè)自定義的類(lèi),需要繼承Spring提供的ApplicationEvent。

@Data
public class MyEvent extends ApplicationEvent {
  private String msg;

  public MyEvent(Object source, String msg) {
    super(source);
    this.msg = msg;
  }
}

事件監(jiān)聽(tīng)

基本方法是實(shí)現(xiàn)ApplicationListener接口,自定義一個(gè)監(jiān)聽(tīng)器,實(shí)現(xiàn)onApplicationEvent()方法,然后添加到ApplicationContext

比如:

public class MyListener implements ApplicationListener<MyEvent> { 

  @Override 
  public void onApplicationEvent(MyEvent event) { 
    System.out.print("監(jiān)聽(tīng)到MyEvent事件"); 
  } 
} 
...
// SpringBoot的啟動(dòng)類(lèi)中添加監(jiān)聽(tīng)器
    public static void main(String[] args) {
    SpringApplication application = new SpringApplication(MyApplication.class);
    application.addListeners(new MyListener());
    application.run(args);
  }

也可以使用注解@EventListener(推薦):原理就是通過(guò)掃描這個(gè)注解,創(chuàng)建監(jiān)聽(tīng)器并添加到ApplicationContext。

@Component
@Slf4j
public class MyEventHandler {

  @EventListener
  public void handleEvent(MyEvent event) {
    log.info("------------處理事件:{}", event.getMsg());
    try {
      Thread.sleep(5 * 1000L);
      log.info("事件1(5s)處理完成");
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

}

事件發(fā)布

可以通過(guò)上下文對(duì)象的發(fā)布方法ConfigurableApplicationContext::publishEvent()來(lái)發(fā)布。

也可以實(shí)現(xiàn)ApplicationEventPublisherAware接口來(lái)發(fā)布(推薦)。

@Component
@Slf4j
public class EventService implements ApplicationEventPublisherAware {
  public ApplicationEventPublisher publisher;

  @Override
  public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
    this.publisher = applicationEventPublisher;
  }

  public String doEventWork(String msg) {
    log.info("------------publish event:" + msg);
    MyEvent event = new MyEvent(this, msg);
    publisher.publishEvent(event);
    return "OK";
  }
}

測(cè)試代碼

@SpringBootTest
@RunWith(SpringRunner.class)
public class EventServiceTest {
  @Autowired
  private EventService service;

  @Test
  public void eventTest() {
    String msg="Java Code";
    service.doEventWork(msg);
  }
}


注意

如果2個(gè)事件之間是繼承關(guān)系,會(huì)先監(jiān)聽(tīng)到子類(lèi)事件,處理完再監(jiān)聽(tīng)父類(lèi)。

// MyEvent2 extends MyEvent

@Component
@Slf4j
public class MyEventHandler {

  @EventListener
  public void handleEvent(MyEvent event) {
    log.info("------------處理事件:{}", event.getMsg());
    try {
      Thread.sleep(5 * 1000L);
      log.info("事件1(5s)處理完成");
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  @EventListener
  public void handleEvent2(MyEvent2 event) {
    log.info("------------處理事件2:{}", event.getMsg());
    try {
      Thread.sleep(10 * 1000L);
      log.info("事件2(10s)處理完成");
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

當(dāng)我publish一個(gè)子類(lèi)事件MyEvent2時(shí),日志如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論