使用SpringEvent業(yè)務(wù)解耦

說明

用于系統(tǒng)業(yè)務(wù)之間解耦。
需要使用的知識點:
1.ApplicationContext,2.ApplicationEventPublisher,3.EventListener

1.創(chuàng)建事件對象,普通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.創(chuàng)建service類,實現(xiàn)事件發(fā)布

@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.創(chuàng)建事件監(jiān)聽器,監(jiān)聽該事件

@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);
    }
}

運行結(jié)果如下:
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;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容