Solon

solon.cloud.eventplus

v2.7.5 </> markdown

此插件,由社区成员(浅念)贡献

<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon.cloud.eventplus</artifactId>
</dependency>

1、描述

分布式扩展插件。在 solon.cloud 插件的基础上,添加基于实体的事件处理方式。算是 Solon Cloud Event 使用接口的一种增强包装,使用时必须引入具体的 Solon Cloud Event 插件为基础。有两种小好处:

  • 使用基于实类型实体操作
  • 事件主题名的只需要标注在实体类上(就此一处)

2、使用示例

定义事件实体

//
//只需要在实体上关联主题,其它处不再出现主题
//
@CloudEvent("user.create.event")
public class UserCreatedEvent implements CloudEventEntity {
    public long userId;
}

订阅事件实体

//用代理模式订阅(。实体已申明主题相关信息)
@CloudEventSubscribe
public class UserCreatedEventHandler implements CloudEventHandlerPlus<UserCreatedEvent> {
    @Override
    public boolean handler(UserCreatedEvent event) throws Throwable {
        //业务处理
        return false;
    }
}


//或者,用类函数模式订阅
@Component
public class EventSubscriber{
    @CloudEventSubscribe
    public boolean onUserCreatedEvent(UserCreatedEvent event){
        //处理业务
        return false;
    }
}

发布事件实体

UserCreatedEvent event = new UserCreatedEvent();
event.userId  =1212;

//发布
event.publish();