ApplicationEvent作用说明(帮助理解): ApplicationEvent以及Listener是Spring为我们提供的一个事件监听、订阅的实现,内部实现原理是观察者设计模式,设计初衷也是为了系统业务逻辑之间的解耦,提高可扩展性以及可维护性
自定义Event事件
import lombok.Getter;
import lombok.Setter;
import org.springframework.context.ApplicationEvent;
import java.util.Map;
@Setter
@Getter
public class MyEvent extends ApplicationEvent {
private Map<String, String> map;
public MyEvent(Object source, Map<String, String> map) {
super(source);
this.map = map;
}
}
Event事件处理方式
第一种 通过EventListener处理
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class MyEventListenerDeal {
@EventListener
public void handleMyEventMap(MyEvent myEvent) {
log.info("------------handleMyEvent -----------");
// 測試 模擬真是業務的處理
myEvent.getMap().put("city", "beijing");
}
}
第二种通过事件ApplicationListerner加自定义的事件进行处理
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class MyEventListenerDeal implements ApplicationListener<MyEvent> {
@Override
public void onApplicationEvent(MyEvent myEvent) {
log.info("已经监听到开始处理!!!");
myEvent.getMap().put("city", "河南太热了!!!");
}
}
模拟业务层进行事件发布
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class MyEventTriggerService {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
public Map<String, String> triggerMap() {
// 模拟实际项目业务
Map<String, String> map = new HashMap<>();
map.put("name", "zhangting");
map.put("city", "nanjing");
MyEvent myEvent = new MyEvent(this, map);
// 发布时间
applicationEventPublisher.publishEvent(myEvent);
return map;
}
}
测试
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.worth.listen.MyEventTriggerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
@Api(tags = "测试Controller")
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {
@Autowired
MyEventTriggerService myEventTriggerService;
@ApiOperation(value = "基金每日净值表-test1", notes = "基金每日净值表-test1")
@GetMapping(value = "/test1")
public ResponseEntity<?> test1(HttpServletRequest req, HttpServletResponse response) {
log.info("----------begin-------");
Map<String, String> map = myEventTriggerService.triggerMap();
log.info("----------end-------");
return new ResponseEntity<>(map, HttpStatus.OK);
}
}
效果
监听之后的操作进行替换 第一种处理 第二种处理
新增SpringEvent使用(异步使用)
提供了两种方式进行事件发布
一种是继承 ApplicationEvent
第二种是自定义事件
自定义Event 事件参数
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.Map;
@Data
@AllArgsConstructor
public class MyMsgEvent {
public Map<String, String> map;
}
监听器异步处理
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.Map;
@Slf4j
@Component
public class MyMsgListener {
@Async
@SneakyThrows
@EventListener
public Map<String, String> sendMsg(MyMsgEvent myMsgEvent) {
log.info("新的操作时事件");
Map<String, String> map = myMsgEvent.getMap();
map.put("city", "我叫宋雷雷!!!");
Thread.sleep(60000);
log.info("异步睡觉了!!!");
return map;
}
}
实际业务使用
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class MyEventTriggerService2 {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
public Map<String, String> triggerMap() {
Map<String, String> map = new HashMap<>();
map.put("name", "zhangting");
map.put("city", "nanjing");
applicationEventPublisher.publishEvent(new MyMsgEvent(map));
return map;
}
}
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.worth.listener1.MyEventTriggerService;
import org.jeecg.modules.worth.listener2.MyEventTriggerService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
@Api(tags = "测试Controller")
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {
@Autowired
MyEventTriggerService myEventTriggerService;
@Autowired
MyEventTriggerService2 myEventTriggerService2;
@ApiOperation(value = "基金每日净值表-test1", notes = "基金每日净值表-test1")
@GetMapping(value = "/test1")
public ResponseEntity<?> test1(HttpServletRequest req, HttpServletResponse response) {
log.info("----------begin-------");
Map<String, String> map = myEventTriggerService2.triggerMap();
log.info("----------end-------");
HashMap<String, String> mao2 = new HashMap<>();
mao2.put("我来了@@","22222");
mao2.put("我又来了@@","3333");
return new ResponseEntity<>(mao2, HttpStatus.OK);
}
}
实际效果 解释一下: 在controller 重新对map进行值自定义,由于service 是异步处理,所以自定义的值是立刻返回的,在接口成功返回之后,在观察控制台,一分钟后异步任务执行完毕。
使用的业务: 客户消费完,累计积分。 分拆两部 提高接口效率:第一步处理客户消费,接口处理完之后直接返回成功,之后异步任务处理积分逻辑。 当然也可以使用Mq实现。
|