merge
This commit is contained in:
@@ -8,17 +8,17 @@ package com.xwbing.rabbit;
|
||||
*/
|
||||
public class RabbitConstant {
|
||||
/**
|
||||
* 服务器调用
|
||||
* 邮件队列
|
||||
*/
|
||||
public static final String SERVER_INVOKE_QUEUE = "server.invoke";
|
||||
public static final String EMAIL_QUEUE = "email";
|
||||
/**
|
||||
* http请求
|
||||
* http请求队列
|
||||
*/
|
||||
public static final String HTTP_REQUEST_QUEUE = "http.request";
|
||||
/**
|
||||
* 服务器调用路由键(*表示一个词,#表示零个或多个词)
|
||||
* 邮件队列路由键(*表示一个词,#表示零个或多个词)
|
||||
*/
|
||||
public static final String SERVER_INVOKE_ROUTING_KEY = "server.invoke.key";
|
||||
public static final String EMAIL_ROUTING_KEY = "email.key";
|
||||
/**
|
||||
* http请求路由键
|
||||
*/
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
package com.xwbing.rabbit;
|
||||
|
||||
import com.xwbing.service.rest.MailService;
|
||||
import com.xwbing.util.RestMessage;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.text.MessageFormat;
|
||||
|
||||
import static com.xwbing.rabbit.RabbitConstant.EMAIL_QUEUE;
|
||||
import static com.xwbing.rabbit.RabbitConstant.HTTP_REQUEST_QUEUE;
|
||||
import static com.xwbing.rabbit.RabbitConstant.SERVER_INVOKE_QUEUE;
|
||||
|
||||
/**
|
||||
* 项目名称: boot-module-demo
|
||||
@@ -18,18 +21,22 @@ import static com.xwbing.rabbit.RabbitConstant.SERVER_INVOKE_QUEUE;
|
||||
*/
|
||||
@Component
|
||||
public class Receiver {
|
||||
@Resource
|
||||
private MailService mailService;
|
||||
private final Logger logger = LoggerFactory.getLogger(Receiver.class);
|
||||
|
||||
/**
|
||||
* 处理server队列信息
|
||||
* 处理邮件队列信息
|
||||
*
|
||||
* @param msg
|
||||
* @return
|
||||
*/
|
||||
@RabbitListener(queues = SERVER_INVOKE_QUEUE)
|
||||
public String processServer(String msg) {
|
||||
String response = MessageFormat.format("收到{0}队列的消息:{1}", SERVER_INVOKE_QUEUE, msg);
|
||||
return response.toUpperCase();
|
||||
@RabbitListener(queues = EMAIL_QUEUE)
|
||||
public String processEmail(String[] msg) {
|
||||
String format = MessageFormat.format("你的用户名是:{0},密码是:{1}", msg[1], msg[2]);
|
||||
RestMessage restMessage = mailService.sendSimpleMail(msg[0], "注册成功", format);
|
||||
boolean success = restMessage.isSuccess();
|
||||
return MessageFormat.format("成功发送邮件给{0}:{1}", msg[1], success);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,7 +45,7 @@ public class Receiver {
|
||||
* @param msg
|
||||
*/
|
||||
@RabbitListener(queues = HTTP_REQUEST_QUEUE)
|
||||
public String processHttp(String msg) {
|
||||
public String processHttp(String[] msg) {
|
||||
String response = MessageFormat.format("收到{0}队列的消息:{1}", HTTP_REQUEST_QUEUE, msg);
|
||||
return response.toUpperCase();
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@@ -53,12 +54,12 @@ public class Sender implements RabbitTemplate.ConfirmCallback, RabbitTemplate.Re
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送信息到server队列
|
||||
* 发送信息到email队列
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
public void sendServerInvoke(String msg) {
|
||||
send(msg, RabbitConstant.CONTROL_EXCHANGE, RabbitConstant.SERVER_INVOKE_ROUTING_KEY);
|
||||
public void sendEmail(String[] msg) {
|
||||
send(msg, RabbitConstant.CONTROL_EXCHANGE, RabbitConstant.EMAIL_ROUTING_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,7 +67,7 @@ public class Sender implements RabbitTemplate.ConfirmCallback, RabbitTemplate.Re
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
public void sendHttpRequest(String msg) {
|
||||
public void sendHttpRequest(String[] msg) {
|
||||
send(msg, RabbitConstant.CONTROL_EXCHANGE, RabbitConstant.HTTP_REQUEST_ROUTING_KEY);
|
||||
}
|
||||
|
||||
@@ -77,14 +78,14 @@ public class Sender implements RabbitTemplate.ConfirmCallback, RabbitTemplate.Re
|
||||
* @param exchange 交换器
|
||||
* @param routingKey 路由键
|
||||
*/
|
||||
private void send(String msg, String exchange, String routingKey) {
|
||||
private void send(String[] msg, String exchange, String routingKey) {
|
||||
CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
|
||||
logger.info("开始发送消息:{}", msg.toLowerCase());
|
||||
logger.info("开始发送消息:{}", Arrays.toString(msg));
|
||||
//转换并发送消息,且等待消息者返回响应消息。
|
||||
Object response = rabbitTemplate.convertSendAndReceive(exchange, routingKey, msg, correlationId);
|
||||
if (response != null) {
|
||||
logger.info("消费者响应:{}", response.toString());
|
||||
}
|
||||
logger.info("结束发送消息:{}", msg.toLowerCase());
|
||||
logger.info("{}消息发送结束", Arrays.toString(msg));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,8 @@ import org.springframework.context.annotation.Configuration;
|
||||
public class TopicExchangeConfig {
|
||||
//声明队列
|
||||
@Bean
|
||||
public Queue serverInvokeQueue() {
|
||||
return new Queue(RabbitConstant.SERVER_INVOKE_QUEUE, true);//true表示持久化该队列
|
||||
public Queue emailQueue() {
|
||||
return new Queue(RabbitConstant.EMAIL_QUEUE, true);//true表示持久化该队列
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -34,12 +34,12 @@ public class TopicExchangeConfig {
|
||||
|
||||
//绑定
|
||||
@Bean
|
||||
public Binding bindingExchangeMessage() {
|
||||
return BindingBuilder.bind(serverInvokeQueue()).to(topicExchange()).with(RabbitConstant.SERVER_INVOKE_ROUTING_KEY);//*表示一个词,#表示零个或多个词
|
||||
public Binding bindingEmail() {
|
||||
return BindingBuilder.bind(emailQueue()).to(topicExchange()).with(RabbitConstant.EMAIL_ROUTING_KEY);//*表示一个词,#表示零个或多个词
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Binding bindingExchangeMessages() {
|
||||
public Binding bindingHttpRequest() {
|
||||
return BindingBuilder.bind(httpRequestQueue()).to(topicExchange()).with(RabbitConstant.HTTP_REQUEST_ROUTING_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.xwbing.exception.BusinessException;
|
||||
import com.xwbing.util.RestMessage;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.mail.MailException;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
@@ -41,7 +42,13 @@ public class MailService {
|
||||
message.setTo(to);
|
||||
message.setSubject(subject);
|
||||
message.setText(content);
|
||||
mailSender.send(message);
|
||||
try {
|
||||
mailSender.send(message);
|
||||
} catch (MailException e) {
|
||||
restMessage.setSuccess(false);
|
||||
restMessage.setMessage("发送邮件失败");
|
||||
return restMessage;
|
||||
}
|
||||
restMessage.setSuccess(true);
|
||||
restMessage.setMessage("纯文本邮件已经发送");
|
||||
return restMessage;
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.xwbing.domain.entity.model.EmailModel;
|
||||
import com.xwbing.domain.entity.sys.*;
|
||||
import com.xwbing.domain.repository.sys.SysUserRepository;
|
||||
import com.xwbing.exception.BusinessException;
|
||||
import com.xwbing.rabbit.Sender;
|
||||
import com.xwbing.util.*;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
@@ -41,6 +42,8 @@ public class SysUserService {
|
||||
private SysAuthorityService sysAuthorityService;
|
||||
@Resource
|
||||
private SysUserRoleService sysUserRoleService;
|
||||
@Resource
|
||||
private Sender sender;
|
||||
|
||||
/**
|
||||
* 增
|
||||
@@ -51,7 +54,8 @@ public class SysUserService {
|
||||
public RestMessage save(SysUser sysUser) {
|
||||
RestMessage result = new RestMessage();
|
||||
//检查用户名是否存在
|
||||
SysUser old = getByUserName(sysUser.getUserName());
|
||||
String userName = sysUser.getUserName();
|
||||
SysUser old = getByUserName(userName);
|
||||
if (old != null) {
|
||||
throw new BusinessException("已经存在此用户名");
|
||||
}
|
||||
@@ -68,12 +72,14 @@ public class SysUserService {
|
||||
if (one == null) {
|
||||
throw new BusinessException("新增用户失败");
|
||||
}
|
||||
String[] msg = {sysUser.getMail(), userName, res[0]};
|
||||
//发送邮件
|
||||
boolean send = sendEmail(sysUser, res[0]);
|
||||
// 发送邮件结束
|
||||
if (!send) {
|
||||
throw new BusinessException("发送密码邮件失败");
|
||||
}
|
||||
sender.sendEmail(msg);
|
||||
// boolean send = sendEmail(sysUser, res[0]);
|
||||
// // 发送邮件结束
|
||||
// if (!send) {
|
||||
// throw new BusinessException("发送密码邮件失败");
|
||||
// }
|
||||
result.setSuccess(true);
|
||||
result.setId(id);
|
||||
return result;
|
||||
@@ -220,10 +226,9 @@ public class SysUserService {
|
||||
if (save == null) {
|
||||
throw new BusinessException("重置密码失败");
|
||||
}
|
||||
boolean send = sendEmail(old, str[0]);
|
||||
if (!send) {
|
||||
throw new BusinessException("发送密码邮件失败");
|
||||
}
|
||||
//发送邮件
|
||||
String[] msg = {old.getMail(), userName, str[0]};
|
||||
sender.sendEmail(msg);
|
||||
result.setSuccess(true);
|
||||
result.setMessage("重置密码成功");
|
||||
return result;
|
||||
@@ -442,20 +447,21 @@ public class SysUserService {
|
||||
/**
|
||||
* 发送邮件
|
||||
*
|
||||
* @param sysUser
|
||||
* @param email
|
||||
* @param userName
|
||||
* @param passWord
|
||||
* @return
|
||||
*/
|
||||
private boolean sendEmail(SysUser sysUser, String passWord) {
|
||||
private boolean sendEmail(String email, String userName, String passWord) {
|
||||
SysConfig sysConfig = sysConfigService.getByCode(CommonConstant.EMAIL_KEY);
|
||||
if (sysConfig == null) {
|
||||
throw new BusinessException("没有查找到邮件配置项");
|
||||
}
|
||||
JSONObject jsonObject = JSONObject.parseObject(sysConfig.getValue());
|
||||
EmailModel emailModel = JSONObject.toJavaObject(jsonObject, EmailModel.class);
|
||||
emailModel.setToEmail(sysUser.getMail());
|
||||
emailModel.setToEmail(email);
|
||||
emailModel.setSubject(emailModel.getSubject());
|
||||
emailModel.setCentent(emailModel.getCentent() + " 你的用户名是:" + sysUser.getUserName() + ",密码是:" + passWord);
|
||||
emailModel.setCentent(emailModel.getCentent() + " 你的用户名是:" + userName + ",密码是:" + passWord);
|
||||
return EmailUtil.sendTextEmail(emailModel);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ public class TestControl {
|
||||
private final Logger logger = LoggerFactory.getLogger(TestControl.class);
|
||||
|
||||
@GetMapping("send")
|
||||
public void send(@RequestParam String msg) {
|
||||
public void send(@RequestParam String[] msg) {
|
||||
sender.sendHttpRequest(msg);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user