mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-04 20:28:44 +08:00 
			
		
		
		
	mq 重构:默认的 mq 实现,采用 event,减少对 redis、rocketmq 等消息队列的依赖
This commit is contained in:
		@@ -0,0 +1,31 @@
 | 
			
		||||
package cn.iocoder.yudao.module.promotion.mq.consumer.coupon;
 | 
			
		||||
 | 
			
		||||
import cn.iocoder.yudao.module.member.message.user.MemberUserCreateMessage;
 | 
			
		||||
import cn.iocoder.yudao.module.promotion.service.coupon.CouponService;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.context.event.EventListener;
 | 
			
		||||
import org.springframework.scheduling.annotation.Async;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Resource;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 用户注册时,发送优惠劵的消费者,基 {@link MemberUserCreateMessage} 消息
 | 
			
		||||
 *
 | 
			
		||||
 * @author owen
 | 
			
		||||
 */
 | 
			
		||||
@Component
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class CouponTakeByRegisterConsumer {
 | 
			
		||||
 | 
			
		||||
    @Resource
 | 
			
		||||
    private CouponService couponService;
 | 
			
		||||
 | 
			
		||||
    @EventListener
 | 
			
		||||
    @Async // Spring Event 默认在 Producer 发送的线程,通过 @Async 实现异步
 | 
			
		||||
    public void onMessage(MemberUserCreateMessage message) {
 | 
			
		||||
        log.info("[onMessage][消息内容({})]", message);
 | 
			
		||||
        couponService.takeCouponByRegister(message.getUserId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,29 +0,0 @@
 | 
			
		||||
package cn.iocoder.yudao.module.promotion.mq.consumer.coupon;
 | 
			
		||||
 | 
			
		||||
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessageListener;
 | 
			
		||||
import cn.iocoder.yudao.module.promotion.mq.message.coupon.UserCreateMessage;
 | 
			
		||||
import cn.iocoder.yudao.module.promotion.service.coupon.CouponService;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Resource;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 针对 {@link UserCreateMessage} 的消费者
 | 
			
		||||
 *
 | 
			
		||||
 * @author owen
 | 
			
		||||
 */
 | 
			
		||||
@Component
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class UserCreateConsumer extends AbstractStreamMessageListener<UserCreateMessage> {
 | 
			
		||||
 | 
			
		||||
    @Resource
 | 
			
		||||
    private CouponService couponService;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onMessage(UserCreateMessage message) {
 | 
			
		||||
        log.info("[onMessage][消息内容({})]", message);
 | 
			
		||||
        couponService.takeCouponByRegister(message.getUserId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,4 @@
 | 
			
		||||
/**
 | 
			
		||||
 * 消息队列的消费者
 | 
			
		||||
 */
 | 
			
		||||
package cn.iocoder.yudao.module.promotion.mq.consumer;
 | 
			
		||||
@@ -1,29 +0,0 @@
 | 
			
		||||
package cn.iocoder.yudao.module.promotion.mq.message.coupon;
 | 
			
		||||
 | 
			
		||||
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessage;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.EqualsAndHashCode;
 | 
			
		||||
 | 
			
		||||
import javax.validation.constraints.NotNull;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 会员用户创建消息
 | 
			
		||||
 *
 | 
			
		||||
 * @author owen
 | 
			
		||||
 */
 | 
			
		||||
@Data
 | 
			
		||||
@EqualsAndHashCode(callSuper = true)
 | 
			
		||||
public class UserCreateMessage extends AbstractStreamMessage {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 用户编号
 | 
			
		||||
     */
 | 
			
		||||
    @NotNull(message = "用户编号不能为空")
 | 
			
		||||
    private Long userId;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String getStreamKey() {
 | 
			
		||||
        return "member.user.create";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,4 @@
 | 
			
		||||
/**
 | 
			
		||||
 * 消息队列的消息
 | 
			
		||||
 */
 | 
			
		||||
package cn.iocoder.yudao.module.promotion.mq.message;
 | 
			
		||||
@@ -0,0 +1,4 @@
 | 
			
		||||
/**
 | 
			
		||||
 * 消息队列的生产者
 | 
			
		||||
 */
 | 
			
		||||
package cn.iocoder.yudao.module.promotion.mq.producer;
 | 
			
		||||
		Reference in New Issue
	
	Block a user