使用SpringEvent业务解耦

说明

用于系统业务之间解耦。
需要使用的知识点:
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 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业务解耦)