消息服务平台 处理公司内部各种消息业务
比如 发送邮件 发送短信 微信推送
接口有两种类型 异步 同步
同步需求: 当调用消息服务平台,需要返回消息服务平台调用第三方平台接口是否成功
异步需求: 采用MQ异步发送消息(没有返回结果)
发送邮件参数:
邮件账号 内容信息
格式是JSON
需要使用 fastJson:
<!--fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.49</version> </dependency>
存入消息: 消息参数格式是Json 所以需要 fastjson
package com.toov5.Producer; import javax.jms.Queue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import com.alibaba.fastjson.JSONObject; @Component public class P2PProducer { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; //把队列注入进来 @Autowired //此注解默认是以类型找 在配置文件中 已经注入的 @Bean private Queue queue; //每隔5s时间向队列发送消息 @Scheduled(fixedDelay=5000) //每间隔2s向队列发送消息 public void send() { String userName = System.currentTimeMillis()+" "; JSONObject jsonObject = new JSONObject(); jsonObject.put("username", userName); jsonObject.put("email", "[email protected]"); String msgString = jsonObject.toJSONString(); jmsMessagingTemplate.convertAndSend(queue,msgString); System.out.println("点对点通讯,msg"+msgString); } }
主要的依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
注意163邮件需要开通pop3协议!
其他遵循之前 点对点demo
package com.toov5.Producer; import javax.jms.Queue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import com.alibaba.fastjson.JSONObject; @Component public class P2PProducer { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; //把队列注入进来 @Autowired //此注解默认是以类型找 在配置文件中 已经注入的 @Bean private Queue queue; //每隔5s时间向队列发送消息 @Scheduled(fixedDelay=5000) //每间隔2s向队列发送消息 public void send() { String userName = System.currentTimeMillis()+" "; JSONObject jsonObject = new JSONObject(); jsonObject.put("username", userName); jsonObject.put("email", "[email protected]"); String msgString = jsonObject.toJSONString(); jmsMessagingTemplate.convertAndSend(queue,msgString); System.out.println("点对点通讯,msg"+msgString); } }
配置:
package com.toov5.config; import javax.jms.Queue; import org.apache.activemq.command.ActiveMQQueue; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; @Component public class ConfigQueue { @Value("${my_queue}") private String myQueue; //首先将队列注入到SpringBoot容器中去 @Bean public Queue queue() { return new ActiveMQQueue(myQueue); } }
启动:
package com.toov5; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling //开启定时任务 public class AppProducer { public static void main(String[] args) { SpringApplication.run(AppProducer.class, args); } }
yml:
spring: activemq: broker-url: tcp://192.168.91.6:61616 user: admin password: admin my_queue: springboot-queue-toov5 server: port: 8081
pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.toov5.springboot.avtivemq</groupId> <artifactId>springboot.avtivemq</artifactId> <version>0.0.1-SNAPSHOT</version> <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> <!--fastjson --> <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> </project>
consumer:
package com.toov5.activemqConsumer; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.jms.annotation.JmsListener; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Component; import com.alibaba.fastjson.JSONObject; @Component public class P2PConsumer { @Autowired private JavaMailSender javaMailSender; @Value("${spring.mail.username}") private String toEmail; @JmsListener(destination = "${my_queue}") // 用这个注解去监听 监听的队列 public void receiver(String msg) throws Exception { if (StringUtils.isEmpty(msg)) { return; } // 解析json JSONObject parseObject = JSONObject.parseObject(msg); //转成对象 String userName = parseObject.getString("username"); String email = parseObject.getString("email"); sendSimpleMail(email, userName); System.out.println("消费者成功获取到生产者的消息,msg" + msg); } public void sendSimpleMail(String email, String userName) throws Exception { SimpleMailMessage message = new SimpleMailMessage(); // 邮件的来源 自己发自己 message.setFrom(email); // 发送给谁 message.setTo(toEmail); // 邮件标题 message.setSubject("toov5博客园提醒"); // 邮件内容 message.setText("hello:" + userName + "我们成为好朋友!"); // 发送邮件 javaMailSender.send(message); System.out.println("邮件发送完成," + JSONObject.toJSONString(message)); } }
启动类:
package com.toov5.activemqConsumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class AppConsumer { public static void main(String[] args) { SpringApplication.run(AppConsumer.class, args); } }
yml:
spring: activemq: broker-url: tcp://192.168.91.6:61616 user: admin password: admin mail: ##163邮件服务 host: smtp.163.com ##发送邮件账号 username: acmchangyuan@163.com ##授权码 password: yugongv5 enable: true smtp: auth: true starttls: enable: true required: true my_queue: springboot-queue-toov5 server: port: 8080
pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.toov5.activemqConsumer</groupId> <artifactId>activemqConsumer</artifactId> <version>0.0.1-SNAPSHOT</version> <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> <!--fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.49</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </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> </project>
先启动consumer!
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/16136.html