??前言
本文主要是【Rabbitmq】——SpringBoot項目整合Redis,Rabbitmq發(fā)送、消費、存儲郵件的文章,如果有什么需要改進的地方還請大佬指出??
??作者簡介:大家好,我是聽風與他??
??博客首頁:CSDN主頁聽風與他
??每日一句:狠狠沉淀,頂峰相見
SpringBoot項目整合Redis,Rabbitmq發(fā)送、消費、存儲郵件
1.導入mail,redis,rabbitmq的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.配置application.yml文件
spring:
mail:
host: smtp.163.com
username: 15671190765@163.com
password: XXX #此為郵箱的snmp密碼
rabbitmq:
addresses: localhost
username: admin #rabbitmq的賬號名密碼均為admin
password: admin
virtual-host: / #虛擬主機采用默認的/
data:
redis:
port: 6379
host: localhost #redis均為默認配置及端口,不配置yml也可
3.Rabbitmq配置類:RabbitConfiguration
package com.rabbitmqemail.config;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfiguration {
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter converter) {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
template.setMessageConverter(converter);
return template;
}
//給Bean隊列取名為郵件隊列
@Bean("emailQueue")
public Queue emailQueue(){
return QueueBuilder
.durable("mail") //給郵件隊列取名為email
.build();
}
}
Rabbitmq監(jiān)聽類:MailQueueListener
package com.rabbitmqemail.listener;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@RabbitListener(queues = "mail") //指定一下消息隊列,該消息隊列是mail消息隊列
public class MailQueueListener{
@Autowired
JavaMailSender sender;
@Value("${spring.mail.username}")
String username;
@RabbitHandler
public void sendMailMessage(Map<String,Object> data){
// System.out.println(data.get("email")+" "+data.get("code"));
String email = (String) data.get("email");
Integer code = (Integer) data.get("code");
SimpleMailMessage message= createMessage("歡迎注冊我們的網(wǎng)站","您的驗證碼為"+code+",有效時間三分鐘,為了保障您的安全,請勿向他人泄露驗證碼信息。",email);
System.out.println("message1:"+message.getText());
if (message == null) return;
sender.send(message);
}
private SimpleMailMessage createMessage(String title,String content,String email){
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject(title); //主題
message.setText(content); //內容
message.setTo(email); //發(fā)送目標郵箱
message.setFrom(username); //源發(fā)送郵箱
return message;
}
}
接口類:emailService
package com.rabbitmqemail.service;
public interface emailService {
String EmailVerifyCode(String email);
}
接口實現(xiàn)類:emailServiceImpl
package com.rabbitmqemail.service.impl;
import ch.qos.logback.classic.pattern.MessageConverter;
import com.alibaba.fastjson2.JSONObject;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmqemail.service.emailService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@Service
public class emailServiceImpl implements emailService {
@Autowired
AmqpTemplate amqpTemplate; //將消息隊列注冊為bean
@Autowired
StringRedisTemplate redisTemplate;
@Override
public String EmailVerifyCode(String email) {
Random random = new Random();
int code = random.nextInt(899999)+100000; //生成六位數(shù)的驗證碼
// System.out.println("email:"+email+" code:"+code);
Map<String,Object> data = Map.of("email",email,"code",code);
amqpTemplate.convertAndSend("mail",data); //向消息隊列中發(fā)送數(shù)據(jù)
redisTemplate.opsForValue()
.set(email,String.valueOf(code),3, TimeUnit.MINUTES);
//用redis來存取數(shù)據(jù)
return null;
}
}
測試類:RabbitmqEmailApplicationTests
package com.rabbitmqemail;
import com.rabbitmqemail.service.impl.emailServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class RabbitmqEmailApplicationTests {
@Autowired
private emailServiceImpl emailService;
@Test
void contextLoads() {
emailService.EmailVerifyCode("2482893650@qq.com");
}
}
測試結果:此時指定郵箱已收到驗證碼
文章來源:http://www.zghlxwxcb.cn/news/detail-758998.html
測試項目開源倉庫:
https://gitee.com/zhang-zilong_zzl/Rabbitmq-email
??文章末尾
文章來源地址http://www.zghlxwxcb.cn/news/detail-758998.html
到了這里,關于SpringBoot項目整合Redis,Rabbitmq發(fā)送、消費、存儲郵件的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!