技術共有

春のイベントリスナー

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

ApplicationListener インターフェースを実装する

@Configuration
public class A48 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(A48.class);
        context.getBean(MyService.class).doBusiness();
        context.close();
    }
	
	// 定义任务
    static class MyEvent extends ApplicationEvent{
        public MyEvent(Object source) {
            super(source);
        }
    }
	
    @Component
    @Slf4j
    static class MyService{
        @Resource
        private ApplicationEventPublisher publisher;

        public void doBusiness(){
            log.info("主线业务");
            publisher.publishEvent(new MyEvent("myservice发送的事件"));
        }
    }
	
    @Component
    @Slf4j
    static class SmsApplicationListener implements ApplicationListener<MyEvent>{
        @Override
        public void onApplicationEvent(MyEvent event) {
            log.info("支线任务");
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

@EventListener アノテーション

@Component
@Slf4j
static class SmsApplicationListener{
    @EventListener
    public void listener(MyEvent event){
        log.info("发送短信....");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

@EventListener とスレッド プールを非同期的に組み合わせる

@Component
@Slf4j
static class SmsApplicationListener{
    @EventListener
    public void listener(MyEvent event){
        log.info("发送短信....");
    }
}

// 线程池异步发送事件
@Bean
public ThreadPoolTaskExecutor executor(){
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(3);
    executor.setMaxPoolSize(10);
    executor.setQueueCapacity(100);
    return executor;
}

@Bean
public SimpleApplicationEventMulticaster applicationEventMulticaster(ThreadPoolTaskExecutor executor){
    SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
    multicaster.setTaskExecutor(executor);
    return multicaster;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

解析用のカスタムリスナー

public static void main(String[] args) {
   AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(A48.class);
   SmsService bean = context.getBean(SmsService.class);
   for (Method method : SmsService.class.getMethods()) {
       if(method.isAnnotationPresent(MyListener.class)){
           ApplicationListener listener = new ApplicationListener() {
               @Override
               public void onApplicationEvent(ApplicationEvent event) {
                   try {
                       method.invoke(bean,event);
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
               }
           };
           context.addApplicationListener(listener);
       }
   }
   context.close();
}

@Component
@Slf4j
static class SmsService{
   @MyListener
   public void listener(ApplicationEvent event){
       log.info("发送短信....");
   }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29