理解 Spring ApplicationListener -- 持续更新

如果不懂ApplicationListener,可以看看这篇入门文章

https://blog.csdn.net/liyantianmin/article/details/81017960

 

其原理,就是一个观察者模式,注册--监听--触发

 

在实际的项目中,可以对一些常用的操作进行一个归纳,如数据同步时下发

目前用到数据同步下发这块

当然也可以配置事件线程池,异步操作,毕竟是可以把一些耗时的操作放到主线程外,而不让前端请求超时

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.scheduling.annotation.EnableAsync;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

import java.util.concurrent.ThreadPoolExecutor;

 

/**

 * EventThreadConfig

 * 配置事件线程池

 */

 

@Configuration

@EnableAsync

@ComponentScan("com.cn.demo.event")

public class EventThreadConfig {

    /** Set the ThreadPoolExecutor's core pool size. */

    private int corePoolSize = 10;

    /** Set the ThreadPoolExecutor's maximum pool size. */

    private int maxPoolSize = 20;

    /** Set the capacity for the ThreadPoolExecutor's BlockingQueue. */

    private int queueCapacity = 10000;

 

    @Bean

    public Executor ctp2PosUserAsync() {

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

        executor.setCorePoolSize(corePoolSize);

        executor.setMaxPoolSize(maxPoolSize);

        executor.setQueueCapacity(queueCapacity);

        executor.setThreadNamePrefix("Ctp2PosExecutor-");

 

        // rejection-policy:当pool已经达到max size的时候,如何处理新任务

        // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行

        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

        executor.initialize();

 

        // 等待所有任务结束后再关闭线程池

        executor.setWaitForTasksToCompleteOnShutdown(true);

        return executor;

    }

}

 

事件处理

/**

 * 事件监听类

 */

@Component

public class UserListener implements ApplicationListener<UserEvent> {

 

    @Autowired

    private Rop2CtpDataHandleService rop2CtpDataHandleService;

 

    @Override

    @Async("ctp2PosUserAsync")

    //@Async

    public void onApplicationEvent(UserEvent event) {

        //System.out.println("UserListener:" + Thread.currentThread().getName()+event.getSource());

        try {

            rop2CtpDataHandleService.Rop2CtpUserHandle(event.getPreUserRelatedData(), event.getAfterUserRelatedData());//同步新/老数据

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

 

}

/**

 * 用户角色员工ctp数据下发到pos

 */

public class UserEvent extends ApplicationEvent {

    public UserEvent(Object obj) {

        super(obj);

    }

 

    public UserEvent(Object obj, UserRelatedData pre, UserRelatedData after) {

        super(obj);

        this.preUserRelatedData = pre;

        this.afterUserRelatedData = after;

    }

 

    private UserRelatedData preUserRelatedData;

 

    private UserRelatedData afterUserRelatedData;

 

    public UserRelatedData getPreUserRelatedData() {

        return preUserRelatedData;

    }

 

    public void setPreUserRelatedData(UserRelatedData preUserRelatedData) {

        this.preUserRelatedData = preUserRelatedData;

    }

 

    public UserRelatedData getAfterUserRelatedData() {

        return afterUserRelatedData;

    }

 

    public void setAfterUserRelatedData(UserRelatedData afterUserRelatedData) {

        this.afterUserRelatedData = afterUserRelatedData;

    }

}

新、老数据的获取,目前采取的方案比较简单

1. 先获取要操作的数据

2. 操作数据

3. 再获取操作后的数据

这种方式比较简单,不知道有没有其他高效的方法,以后遇到再补充

 

发布了51 篇原创文章 · 获赞 1 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/panlongbao_918/article/details/104221884