Springboot event Listener 事件监听 异步处理

Springboot event Listener 事件监听 异步处理,第1张

Springboot event Listener 事件监听 异步处理

编写SpringContextHolder

package com.cf.user.server.event;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;


@Slf4j
@Service
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {

	private static ApplicationContext applicationContext = null;

	
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) {
		SpringContextHolder.applicationContext = applicationContext;
	}

	
	@SuppressWarnings("unchecked")
	public static  T getBean(String name) {
		return (T) applicationContext.getBean(name);
	}

	
	public static  T getBean(Class requiredType) {
		return applicationContext.getBean(requiredType);
	}

	
	public static void clearHolder() {
		if (log.isDebugEnabled()) {
			log.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
		}
		applicationContext = null;
	}

	
	public static void publishEvent(ApplicationEvent event) {
		if (applicationContext == null) {
			return;
		}
		applicationContext.publishEvent(event);
	}

	
	@Override
	@SneakyThrows
	public void destroy() {
		SpringContextHolder.clearHolder();
	}

}

定义事件 SplitEvent

package com.cf.user.server.event;

import org.springframework.context.ApplicationEvent;


public class SplitEvent extends ApplicationEvent {
    public SplitEvent(Object source) {
        super(source);
    }
}

事件监听

package com.cf.user.server.event;

import cn.hutool.core.date.DateUtil;
import com.cf.user.server.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;


@Slf4j
@RequiredArgsConstructor
public class SplitListener {

    private final UserService service;

    @Async
    @Order
    @EventListener(SplitEvent.class)
    public void doSplit(SplitEvent event) throws InterruptedException {
        log.info("事件处理---");
        Thread.sleep(10000);
        // event.getSource 接收Object类型的参数
        Integer num = (Integer) event.getSource();
        for (int i = 0; i < num; i++) {
            String name = service.getName();
            log.info(name);
        }
    }
}

UserService方法

package com.cf.user.server.service.impl;

import com.cf.user.server.service.UserService;
import org.apache.catalina.User;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Override
    public String getName() {
        return "张三";
    }
}

Application 启动类配置

package com.cf.user.server;

import com.cf.user.server.event.SplitListener;
import com.cf.user.server.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;

@EnableAsync
@ComponentScan(basePackages = {
        "com.cf.user.server.*"
})
@SpringBootApplication
public class UserServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserServerApplication.class, args);
    }

    
    @Bean
    public SplitListener sysLogListener(UserService userService) {
        return new SplitListener(userService);
    }
}

DemoController

package com.cf.user.server.controller;

import com.cf.user.server.event.SplitEvent;
import com.cf.user.server.event.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("demo")
public class DemoController {

    @GetMapping
    public String test(){
    	// 给SplitEvent传入数字参数
        SpringContextHolder.publishEvent(new SplitEvent(2));
        log.info("return-----");
        return "abc";
    }

}


摘自Pig项目
Gitee地址

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5684803.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存