mq:默认使用 event 替代 spring cloud stream

pull/64/head
YunaiV 2023-11-02 12:57:25 +08:00
parent fdb479ef49
commit 02693836b2
18 changed files with 95 additions and 89 deletions

View File

@ -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());
}
}

View File

@ -1,29 +0,0 @@
package cn.iocoder.yudao.module.promotion.mq.consumer.coupon;
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;
import java.util.function.Consumer;
/**
* {@link UserCreateMessage}
*
* @author owen
*/
@Component
@Slf4j
public class UserCreateConsumer implements Consumer<UserCreateMessage> {
@Resource
private CouponService couponService;
@Override
public void accept(UserCreateMessage message) {
log.info("[onMessage][消息内容({})]", message);
couponService.takeCouponByRegister(message.getUserId());
}
}

View File

@ -0,0 +1,4 @@
/**
*
*/
package cn.iocoder.yudao.module.promotion.mq.consumer;

View File

@ -1,21 +0,0 @@
package cn.iocoder.yudao.module.promotion.mq.message.coupon;
import lombok.Data;
import javax.validation.constraints.NotNull;
/**
*
*
* @author owen
*/
@Data
public class UserCreateMessage {
/**
*
*/
@NotNull(message = "用户编号不能为空")
private Long userId;
}

View File

@ -0,0 +1,4 @@
/**
*
*/
package cn.iocoder.yudao.module.promotion.mq.message;

View File

@ -0,0 +1,4 @@
/**
*
*/
package cn.iocoder.yudao.module.promotion.mq.producer;

View File

@ -0,0 +1,4 @@
/**
*
*/
package cn.iocoder.yudao.module.member.message;

View File

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.member.mq.message.user;
package cn.iocoder.yudao.module.member.message.user;
import lombok.Data;
@ -10,7 +10,7 @@ import javax.validation.constraints.NotNull;
* @author owen
*/
@Data
public class UserCreateMessage {
public class MemberUserCreateMessage {
/**
*

View File

@ -0,0 +1,4 @@
/**
*
*/
package cn.iocoder.yudao.module.member.mq.consumer;

View File

@ -0,0 +1,4 @@
/**
*
*/
package cn.iocoder.yudao.module.member.mq.message;

View File

@ -0,0 +1,4 @@
/**
*
*/
package cn.iocoder.yudao.module.member.mq.producer;

View File

@ -1,9 +1,8 @@
package cn.iocoder.yudao.module.member.mq.producer.user;
import cn.iocoder.yudao.framework.mq.core.bus.AbstractBusProducer;
import cn.iocoder.yudao.module.member.mq.message.user.UserCreateMessage;
import cn.iocoder.yudao.module.member.message.user.MemberUserCreateMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@ -15,19 +14,18 @@ import javax.annotation.Resource;
*/
@Slf4j
@Component
public class MemberUserProducer extends AbstractBusProducer {
public class MemberUserProducer {
@Resource
private StreamBridge streamBridge;
private ApplicationContext applicationContext;
// TODO 芋艿:后续要在细看下;
/**
* {@link UserCreateMessage}
* {@link MemberUserCreateMessage}
*
* @param userId
*/
public void sendUserCreateMessage(Long userId) {
streamBridge.send("member-create-out-0",new UserCreateMessage().setUserId(userId));
applicationContext.publishEvent(new MemberUserCreateMessage().setUserId(userId));
}
}

View File

@ -6,9 +6,9 @@ tenant-id: {{adminTenentId}}
{
"templateCode": "test_01",
"mobile": "156016913900",
"params": {
"key01": "value01",
"key02": "value02"
"mobile": "15601691390",
"templateParams": {
"operation": "value01",
"code": "value02"
}
}

View File

@ -3,10 +3,11 @@ package cn.iocoder.yudao.module.system.mq.consumer.mail;
import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage;
import cn.iocoder.yudao.module.system.service.mail.MailSendService;
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;
import java.util.function.Consumer;
/**
* {@link MailSendMessage}
@ -15,14 +16,15 @@ import java.util.function.Consumer;
*/
@Component
@Slf4j
public class MailSendConsumer implements Consumer<MailSendMessage> {
public class MailSendConsumer {
@Resource
private MailSendService mailSendService;
@Override
public void accept(MailSendMessage message) {
log.info("[accept][消息内容({})]", message);
@EventListener
@Async // Spring Event 默认在 Producer 发送的线程,通过 @Async 实现异步
public void onMessage(MailSendMessage message) {
log.info("[onMessage][消息内容({})]", message);
mailSendService.doSendMail(message);
}

View File

@ -3,10 +3,11 @@ package cn.iocoder.yudao.module.system.mq.consumer.sms;
import cn.iocoder.yudao.module.system.mq.message.sms.SmsSendMessage;
import cn.iocoder.yudao.module.system.service.sms.SmsSendService;
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;
import java.util.function.Consumer;
/**
* {@link SmsSendMessage}
@ -15,14 +16,16 @@ import java.util.function.Consumer;
*/
@Component
@Slf4j
public class SmsSendConsumer implements Consumer<SmsSendMessage> {
public class SmsSendConsumer {
@Resource
private SmsSendService smsSendService;
@Override
public void accept(SmsSendMessage message) {
log.info("[accept][消息内容({})]", message);
@EventListener
@Async // Spring Event 默认在 Producer 发送的线程,通过 @Async 实现异步
public void onMessage(SmsSendMessage message) {
log.info("[onMessage][消息内容({})]", message);
smsSendService.doSendSms(message);
}
}

View File

@ -1,9 +1,8 @@
package cn.iocoder.yudao.module.system.mq.producer.mail;
import cn.iocoder.yudao.framework.mq.core.bus.AbstractBusProducer;
import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@ -16,10 +15,10 @@ import javax.annotation.Resource;
*/
@Slf4j
@Component
public class MailProducer extends AbstractBusProducer {
public class MailProducer {
@Resource
private StreamBridge streamBridge;
private ApplicationContext applicationContext;
/**
* {@link MailSendMessage}
@ -36,7 +35,7 @@ public class MailProducer extends AbstractBusProducer {
MailSendMessage message = new MailSendMessage()
.setLogId(sendLogId).setMail(mail).setAccountId(accountId)
.setNickname(nickname).setTitle(title).setContent(content);
streamBridge.send("mailSend-out-0", message);
applicationContext.publishEvent(message);
}
}

View File

@ -1,4 +0,0 @@
/**
*
*/
package cn.iocoder.yudao.module.system.mq.producer;

View File

@ -1,10 +1,9 @@
package cn.iocoder.yudao.module.system.mq.producer.sms;
import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.mq.core.bus.AbstractBusProducer;
import cn.iocoder.yudao.module.system.mq.message.sms.SmsSendMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@ -14,14 +13,14 @@ import java.util.List;
* Sms Producer
*
* @author zzf
* @date 2021/3/9 16:35
* @since 2021/3/9 16:35
*/
@Slf4j
@Component
public class SmsProducer extends AbstractBusProducer {
public class SmsProducer {
@Resource
private StreamBridge streamBridge;
private ApplicationContext applicationContext;
/**
* {@link SmsSendMessage}
@ -36,7 +35,7 @@ public class SmsProducer extends AbstractBusProducer {
Long channelId, String apiTemplateId, List<KeyValue<String, Object>> templateParams) {
SmsSendMessage message = new SmsSendMessage().setLogId(logId).setMobile(mobile);
message.setChannelId(channelId).setApiTemplateId(apiTemplateId).setTemplateParams(templateParams);
streamBridge.send("smsSend-out-0", message);
applicationContext.publishEvent(message);
}
}