事件对象:
继承自java.util.EventObject
对象,由开发者自行定义实现。
事件源:
就是触发事件的源头,不同的事件源会触发不同的事件类型。
事件监听器:
事件监听器负责监听事件源发出的事件,事件监听器可以通过实现java.util.EventListener
这个标识接口.,实现事件监听。
事件源可以注册事件监听器对象,并可以向事件监听器对象发送事件对象,事件发生后,事件源将事件对象发给已经注册的所有事件监听器,监听器对象随后会根据事件对象内的相应方法响应这个事件。
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-bus-amqpartifactId>
dependency>
添加自定义事件源pojo
/**
* @ClassName: User
* @Description: 自定义事件源pojo
* @Author: 尚先生
* @CreateDate: 2019/3/1 10:08
* @Version: 1.0
*/
public class User implements Serializable {
private static final long serialVersionUID = -6789036732613612349L;
/**
* ID
*/
private Long id;
/**
* 用户名
*/
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
添加自定义事件
/**
* @ClassName: UserRemoteApplicationEvent
* @Description: 自定义事件
* @Author: 尚先生
* @CreateDate: 2019/3/1 10:10
* @Version: 1.0
*/
public class UserRemoteApplicationEvent extends RemoteApplicationEvent {
private UserRemoteApplicationEvent() {
}
//自定义User
public UserRemoteApplicationEvent(User user, String originService,
String destinationService) {
super(user, originService, destinationService);
}
}
自定义@Configuration
,添加 @RemoteApplicationEventScan
/**
* @ClassName: BusConfiguration
* @Description: 自定义配置扫描类
* @Author: 尚先生
* @CreateDate: 2019/3/1 10:15
* @Version: 1.0
*/
@Configuration
@RemoteApplicationEventScan(basePackageClasses = UserRemoteApplicationEvent.class)
public class BusConfiguration {
}
发布事件
/**
* @ClassName: BusEventController
* @Description: 自定义发布事件
* @Author: 尚先生
* @CreateDate: 2019/3/1 10:23
* @Version: 1.0
*/
@RestController
public class BusEventController implements ApplicationContextAware, ApplicationEventPublisherAware {
// 时间发布器
private ApplicationEventPublisher eventPublisher;
//Spring上下文
private ApplicationContext applicationContext;
// 发布事件
@PostMapping("/bus/event/publish/user")
public boolean publishUserEvent(@RequestBody User user,
@RequestParam(value = "destination", required = false) String destination) {
String serviceInstanceId = applicationContext.getId();
UserRemoteApplicationEvent event = new UserRemoteApplicationEvent(user, serviceInstanceId, destination);
try {
eventPublisher.publishEvent(event);
return true;
} catch (Exception e) {
return false;
}
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.eventPublisher = applicationEventPublisher;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
自定义监听实现
@EventListener
public void onUserRemoteApplicationEvent(UserRemoteApplicationEvent event) {
System.out.printf("UserRemoteApplicationEvent - " +
" Source : %s , originService : %s , destinationService : %s \n",
event.getSource(),
event.getOriginService(),
event.getDestinationService());
}
测试代码实现结果
http://localhost:8080/bus/event/publish/user?destination=user-service-client:8081
{
"id":1,
"name":"sxs"
}
测试结果
true
博客地址:https://blog.csdn.net/shang_xs
微信公众号地址:http://mp.weixin.qq.com/mp/homepage?__biz=MzUxMzk4MDc1OQ==&hid=2&sn=c6c58c06f6f8403af27b6743648b3055&scene=18#wechat_redirect
具体依赖及相关源码参照
https://github.com/dwyanewede/segmentfault-lessons/tree/master/spring-cloud
事件监听机制(一)Java事件监听
https://blog.csdn.net/shang_xs/article/details/87911756
事件监听机制(二)Spring事件监听
https://blog.csdn.net/shang_xs/article/details/88048545
事件监听机制(三)Spring Cloud Bus流程分析
https://blog.csdn.net/shang_xs/article/details/88050196
事件监听机制(四)Spring Cloud Bus自定义实现RemoteApplicationEvent
https://blog.csdn.net/shang_xs/article/details/88051207