TransactionalEventListener 实战场景
创建一个 EventEntity 用于event-listener 之间传输对象
@Data
public class EventEntity extends ApplicationEvent {
private static final long serialVersionUID = -6206752641309458207L;
private String id;
private String tag;
public EventEntity (Object source, String tag, String id) {
super(source);
this.tag = tag;
this.id = id;
}
}
定义个枚举 区分 不同的事件类别
public enum GoodsTagsEnum {
GENERATOR_GOODS_INDEX("生成商品索引"),
GENERATOR_STORE_GOODS_INDEX("生成店铺商品索引"),
UPDATE_GOODS_INDEX("更新商品索引"),
UPDATE_GOODS_INDEX_PROMOTIONS("更新商品索引促销信息"),
DELETE_GOODS_INDEX_PROMOTIONS("更新商品索引促销信息"),
UPDATE_GOODS_INDEX_FIELD("更新商品索引部分字段"),
RESET_GOODS_INDEX("重置商品索引"),
STORE_GOODS_DELETE("删除店铺商品索引"),
GOODS_DELETE("删除商品"),
GOODS_AUDIT("审核商品"),
GOODS_COLLECTION("收藏商品"),
BUY_GOODS_COMPLETE("购买商品完成"),
SKU_DELETE("删除商品SKU"),
VIEW_GOODS("查看商品"),
GOODS_COMMENT_COMPLETE("商品评价");
private final String description;
GoodsTagsEnum(String description) {
this.description = description;
}
public String description() {
return description;
}
}
创建一个事件发布者
@Service
public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> implements GoodsSkuService {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
@Override
@Transactional(rollbackFor = Exception.class)
public void updateGoodsSkuStatusByStoreId(String storeId, String marketEnable, String authFlag) {
LambdaUpdateWrapper<GoodsSku> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(GoodsSku::getStoreId, storeId);
updateWrapper.set(CharSequenceUtil.isNotEmpty(marketEnable), GoodsSku::getMarketEnable, marketEnable);
updateWrapper.set(CharSequenceUtil.isNotEmpty(authFlag), GoodsSku::getAuthFlag, authFlag);
boolean update = this.update(updateWrapper);
if (Boolean.TRUE.equals(update)) {
if (GoodsStatusEnum.UPPER.name().equals(marketEnable)) {
applicationEventPublisher.publishEvent(new EventEntity ("生成店铺商品", GoodsTagsEnum.GENERATOR_STORE_GOODS_INDEX.name(), storeId));
} else if (GoodsStatusEnum.DOWN.name().equals(marketEnable)) {
cache.vagueDel(CachePrefix.GOODS_SKU.getPrefix());
applicationEventPublisher.publishEvent(new EventEntity ("删除店铺商品", GoodsTagsEnum.STORE_GOODS_DELETE.name(), storeId));
}
}
}
@Override
public void generateEs(Goods goods) {
if (!GoodsStatusEnum.UPPER.name().equals(goods.getMarketEnable()) || !GoodsAuthEnum.PASS.name().equals(goods.getAuthFlag())) {
return;
}
applicationEventPublisher.publishEvent(new EventEntity ("生成商品", GoodsTagsEnum.GENERATOR_GOODS_INDEX.name(), goods.getId()));
}
定义 @TransactionalEventListener 来监听事件
@Component
public class GeneratorEsGoodsIndexListener {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@Autowired
private RocketmqCustomProperties rocketmqCustomProperties;
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void generatorEsGoodsIndex(EventEntity esGoodsIndexEvent) {
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + esGoodsIndexEvent.getTag();
rocketMQTemplate.asyncSend(destination, esGoodsIndexEvent.getId(), RocketmqSendCallbackBuilder.commonCallback());
}
}
总结
- 先定义传输模型 以及 type
- 新增或者更改 商品 成功后 发布 Event 事件
- 在@TransactionalEventListener 中接收事件回调
- 最后发送mq 到远程消费 ,例如商品信息发送mq 后洗到Elastichsearch