admin管理员组文章数量:1530085
一、前言
ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成ApplicationContext的事件机制。
如果容器中存在ApplicationListener的Bean,当ApplicationContext调用publishEvent方法时,对应的Bean会被触发。这一过程是典型的观察者模式的实现。
一、目的
在开发过程中,需要涉及跨平台数据推送,因此使用自定义事件。
二、ApplicationListener源码
@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
void onApplicationEvent(E var1);
static <T> ApplicationListener<PayloadApplicationEvent<T>> forPayload(Consumer<T> consumer) {
return (event) -> {
consumer.accept(event.getPayload());
};
}
}
三、项目代码实现
1.所需的maven包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
<version>5.7.4</version>
</dependency>
</dependencies>
2.自定义事件及监听
@Getter
public class PushEvent extends ApplicationEvent {
private IPushData.PushDataType dataType;
public PushEvent(PushTask pushTask, IPushData.PushDataType dataType){
super(pushTask);
this.dataType = dataType;
}
}
@Slf4j
@Component
public class PushEventListener implements ApplicationListener<PushEvent> {
@Override
public void onApplicationEvent(PushEvent pushEvent) {
PushTask pushTask = (PushTask) pushEvent.getSource();
final String data = pushTask.getPushData();
final String url = "http://192.168.169.218:8080/pushEvent";
//3.发送数据
String result = null;
try {
result = HttpUtil.createPost(url)
.body(String.format("data=%s", data))
.execute()
.body();
recordSuccessTime("推送成功");
} catch (Throwable t) {
log.error("推送数据异常——>", t);
result = t.getMessage();
} finally {
log.info("Push task execution -> type:{} | to : {} | result : {} | data : {}", pushEvent.getDataType(), url, result, data);
}
}
/**
* 记录成功信息
*
*/
private void recordSuccessTime(String message) {
System.out.println(message);
}
}
3.Controller层代码
@Controller
public class HelloController {
@Resource
OrgPushPublisher orgPushPublisher;
@ResponseBody
@RequestMapping("/hello")
public String hello(){
orgPushPublisher.pushCreateEvent(new OrgSynchronizationIncrementDTO("1","北京办事处"));
return "Hello World!";
}
@ResponseBody
@RequestMapping("/pushEvent")
public String pushEvent(String data){
System.out.println("接收到的数据data="+data);
return "接收成功";
}
}
4.访问地址
http://localhost:8080/hello
5.执行结果
6.项目链接
https://gitee/chaoren_me/ApplicationListenerDemo.git
版权声明:本文标题:Spring中的ApplicationListener的使用详解案例(观察者模式) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1726220155a1061008.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论