Springboot集成ActiveMQ可以实现异步发送邮件
一、创建生产者
1.1导入maven依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot Activemq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
</dependencies>
<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
1.2.引入application.yml配置
server:
port: 8080
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
queue: gothic-queue2
1.3.创建Queue配置
@Component
public class ConfigQueue {
@Value("${queue}")
private String queueName;
//将队列注入到Springboot容器
@Bean
public Queue queue(){
return new ActiveMQQueue(queueName);
}
}
1.4.创建producer
@Component
public class QueueProducer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
private int count = 1;
//设置五秒钟往邮箱发送一次消息
@Scheduled(fixedDelay = 5000)
public void send() {
JSONObject jsonObject = new JSONObject();
String userName = System.currentTimeMillis() + "";
jsonObject.put("userName", userName);
//设置目标邮箱
jsonObject.put("email", "12345678@qq.com");
String msg = jsonObject.toJSONString();
System.out.println("生产者向消费者发送内容:" + msg);
jmsMessagingTemplate.convertAndSend(queue, msg);
}
1.5.创建启动类
@EnableScheduling
@SpringBootApplication
public class ProducerApp {
public static void main(String[] args) {
SpringApplication.run(ProducerApp.class,args);
}
}
二、创建消费者
2.1.导入maven依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot Activemq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
2.2.引入application.yml文件
server:
port: 8081
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
mail:
host: smtp.163.com
username: 13245678910@163.com
##此处并非发送者邮箱的密码,而是授权码
password: liumiao123
enable: true
smtp:
auth: true
starttls:
enable: true
required: true
queue: gothic-queue2
关于163邮箱在配置文件中的password为授权码,该授权码实在163邮箱设置中开启POP3
1.进入163设置
2.设置POP3
3.按照流程获取客户端授权码
2.3.创建consumer
@Component
public class QueueConsumer {
@Autowired
JavaMailSender javaMailSender;
//获取发送邮箱账号
@Value("${spring.mail.username}")
private String fromEmail;
@JmsListener(destination = "${queue}")
public void receive(String msg) {
System.out.println("消费者接收到消息为:" + msg);
if (StringUtils.isEmpty(msg)) {
return;
}
JSONObject jsonObject = JSONObject.parseObject(msg);
String userName = jsonObject.getString("userName");
String toEmail = jsonObject.getString("email");
sendSimpleEmail(toEmail, userName);
}
/**
* @Description:
* @Param: toEmail 目标邮箱
* @Return:
* @Auther:
* @Date: 2019/11/24 14:47
*/
public void sendSimpleEmail(String toEmail, String userName) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(fromEmail);
simpleMailMessage.setTo(toEmail);
simpleMailMessage.setSubject("测试发邮件");
simpleMailMessage.setText("用户名为:" + userName);
javaMailSender.send(simpleMailMessage);
System.out.println("邮件发送完成");
}
}
2.4.创建启动类
@SpringBootApplication
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class,args);
}
}