美文网首页
使用SpringEvent业务解耦

使用SpringEvent业务解耦

作者: 离别刀 | 来源:发表于2020-02-10 20:37 被阅读0次

说明

用于系统业务之间解耦。
需要使用的知识点:
1.ApplicationContext,2.ApplicationEventPublisher,3.EventListener

1.创建事件对象,普通java类

public class EventObject {
    public String name;
    public Date gmtDate= new Date();
    public Object data;

    public EventObject(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "EventObject{" +
                "name='" + name + '\'' +
                ", gmtDate=" + gmtDate +
                ", data=" + data +
                '}';
    }
}

2.创建service类,实现事件发布

@Service
public class EventService {

    @Resource
    private ApplicationContext applicationContext;

    public void sendEvent(String name){
        EventObject eventObject= new EventObject(name);
        Map<String,String> maps= new HashMap<>();
        maps.put("address","china");
        eventObject.data= maps;
        applicationContext.publishEvent(eventObject);
    }
}

3.创建事件监听器,监听该事件

@Service
public class EventObjectListener {

    @EventListener
    private void EventObjectHandler(EventObject eventObject){
        String thName= Thread.currentThread().getName()+"-"+Thread.currentThread().getId();
        System.out.println("thName:"+thName+" get eventObject: "+ JSON.toJSONString(eventObject));
    }
}

4.运行单元测试

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class EventServiceTest {

    @Autowired
    private EventService eventService;

    @Test
    public void testPublishEvent(){
        eventService.sendEvent("bruce");
        eventService.sendEvent("jack");
        Thread.sleep(5000L);
    }
}

运行结果如下:
thName:taskExecutor-4-19 get eventObject: {"data":{"address":"china"},"gmtDate":1581337936934,"name":"bruce"}
thName:taskExecutor-2-17 get eventObject: {"data":{"address":"china"},"gmtDate":1581337936937,"name":"jack"}

5.可选项,配置接收事件处理的类是否启用新线程处理,需要配置线程池,并手动初始化SimpleApplicationEventMulticaster类


import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 *
 * @author Administrator
 * @date 2019-12-30 0030
 */
@Component
public class ThreadPollConfiguration {

    @Bean("taskExecutor")
    public Executor initTaskExecutor(){
        ThreadPoolTaskExecutor executor= new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(50);
        executor.setQueueCapacity(10000);
        executor.setThreadNamePrefix("taskExecutor-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }

    @Bean("applicationEventMulticaster")
    public SimpleApplicationEventMulticaster init(@Qualifier("taskExecutor")Executor executor){
        SimpleApplicationEventMulticaster simpleApplicationEventMulticaster= new SimpleApplicationEventMulticaster();
        simpleApplicationEventMulticaster.setTaskExecutor(executor);
        return simpleApplicationEventMulticaster;
    }
}

相关文章

  • 使用SpringEvent业务解耦

    说明 用于系统业务之间解耦。需要使用的知识点:1.ApplicationContext,2.Application...

  • Spring Event使用

    SpringEvent 自定义事件链,实用性很强的一种设计,可以利用它来做业务剥离,复杂场景解耦、代码独立等,也是...

  • Spring Boot 解耦之事件驱动

    参考:SpringBoot使用ApplicationEvent&Listener完成业务解耦[https://se...

  • 青团社拆包依赖-QDepend

    青团社业务发展愈发多样,Android使用模块化设计进行业务的解耦,在代码都解耦上我们已经有一套方案,但在工程的模...

  • android 业务解耦

    android 业务解耦 当一个超级android应用由几个甚至几十个大模块组成,需要几十个人同时开发时,业务模块...

  • 业务解耦组件

  • RabbitMQ使用总结

    MQ是互联网业务比较常见的解耦利器,一下简单介绍下RabbitMQ的一些使用。 解耦 典型的一个场景就是订单系统和...

  • 架构设计原则

    架构坚持组件化,持续重构,小而美。架构设计十大原则: 1.全面解耦原则:对业务进行抽象建模,业务数据与业务逻辑解耦...

  • Guava-EventBus使用详解

    在使用ApplicationEvent和Listener快速实现业务解耦中提到了用Spring提供的观察者设计模式...

  • RocketMQ-入门

    一、MQ应用场景 业务解耦解耦是消息队列要解决的最本质问题。所谓解耦,简单点讲就是一个事务,只关心核心的流程。而需...

网友评论

      本文标题:使用SpringEvent业务解耦

      本文链接:https://www.haomeiwen.com/subject/fczaxhtx.html