1.引入pom依赖
<!--activemq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
2.application.properties配置文件中
#activemq
spring.activemq.broker-url=tcp://192.168.0.110:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.pool.enabled=false
3.创建类
Producer 类(生产者)
@Service
@Slf4j
public class Producer {
@Autowired
private JmsMessagingTemplate JmsMessagingTemplate;//红线运行无错,貌似是idea的问题 ,可忽略 public void sendMsg(String destinationName,String message){
log.info("发送消息:"+message);
Destination Destination = new ActiveMQQueue(destinationName);
JmsMessagingTemplate.convertAndSend(Destination,message);
}
}
Consumer 类(消费者)
@Service
@Slf4j
public class Consumer {
@Autowired
private RedisService redisService; @JmsListener(destination = "test.queue")
public void receiveMsg(String msg){
log.info("收到消息:"+msg);
} @JmsListener(destination = "order")
public void order(String msg){
log.info("收到消息:"+msg);
} @JmsListener(destination = "sendmsg")
public void sendmsg(String msg)throws Exception{
log.info("消息队列收到消息:"+msg);
JSONObject jo = JSONObject.parseObject(msg);
String mobile=jo.getString("mobile");
String msg1 = "{\"code\":\"" + jo.getString("code") + "\"}";
SendSmsResponse response = AliyunSms.sendSms(mobile, "SMS_146801291", msg1);
}
}
4.测试
@ResponseBody
@RequestMapping(value = "/getIdenCode1",method = RequestMethod.POST)
public ReturnVO getIdenCode1(HttpServletRequest request) {
String mobile = request.getParameter("mobile");
String code="123456";
if (StringUtils.isBlank(mobile)) {
return new ReturnVO(0,"缺少必要参数");
}
//消息队列
producer.sendMsg("sendmsg","{\"mobile\":\""+mobile+"\",\"code\":\""+code+"\"}");
return new ReturnVO(true);
}
5.附件类
<!--阿里短信-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.7.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.1.0</version>
</dependency> <!-- fastjson阿里巴巴jSON处理器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.13</version>
</dependency>
阿里云短信工具类
public class AliyunSms {
//产品名称:云通信短信API产品,开发者无需替换
static final String product = "Dysmsapi";
//产品域名,开发者无需替换
static final String domain = "dysmsapi.aliyuncs.com"; // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
static final String accessKeyId = "accessKeyId";
static final String accessKeySecret = "accessKeySecret"; public static SendSmsResponse sendSms(String phone, String template, String msg) throws ClientException {
//可自助调整超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象-具体描述见控制台-文档部分内容
SendSmsRequest request = new SendSmsRequest();
//必填:待发送手机号
request.setPhoneNumbers(phone);
//必填:短信签名-可在短信控制台中找到
request.setSignName("短信签名");
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode(template);
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam(msg);
//选填-上行短信扩展码(无特殊需求用户请忽略此字段)
//request.setSmsUpExtendCode("90997");
//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
// request.setOutId("yourOutId");
//hint 此处可能会抛出异常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
return sendSmsResponse;
} public static QuerySendDetailsResponse querySendDetails(String bizId) throws ClientException {
//可自助调整超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象
QuerySendDetailsRequest request = new QuerySendDetailsRequest();
//必填-号码
request.setPhoneNumber("12300000000");
//可选-流水号
request.setBizId(bizId);
//必填-发送日期 支持30天内记录查询,格式yyyyMMdd
SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
request.setSendDate(ft.format(new Date()));
//必填-页大小
request.setPageSize(10L);
//必填-当前页码从1开始计数
request.setCurrentPage(1L);
//hint 此处可能会抛出异常,注意catch
QuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request);
return querySendDetailsResponse;
}
}

												

springboot中activeMQ消息队列的引入与使用(发送短信)的更多相关文章

  1. SpringBoot集成ActiveMq消息队列实现即时和延迟处理

    原文链接:https://blog.csdn.net/My_harbor/article/details/81328727 一.安装ActiveMq 具体安装步骤:自己谷歌去 二.新建springbo ...

  2. SpringBoot配置activemq消息队列

    1.配置pom相关依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  3. 工业物联网或系统集成中应用消息队列(ActiveMQ,C#的demo)的场景全面分析

    1.[连载]<C#通讯(串口和网络)框架的设计与实现> 2.[开源]C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 2.应用SuperIO(SIO)和开源跨平台物联网框 ...

  4. JAVA的设计模式之观察者模式----结合ActiveMQ消息队列说明

    1----------------------观察者模式------------------------------ 观察者模式:定义对象间一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的 ...

  5. ActiveMQ消息队列从入门到实践(4)—使用Spring JMS收发消息

    Java消息服务(Java Message Service ,JMS)是一个Java标准,定义了使用消息代理的通用API .在JMS出现之前,每个消息代理都有私有的API,这就使得不同代理之间的消息代 ...

  6. C#实现ActiveMQ消息队列

    本文使用C#实现ActiveMQ消息队列功能. 一.首先需要导入两个包,分别是:Apache.NMS 和 Apache.NMS.ActiveMQ 二.创建Winform程序实现生产者功能. 三.Pro ...

  7. ActiveMQ 消息队列服务

      1 ActiveMQ简介 1.1 ActiveMQ是什么 ActiveMQ是一个消息队列应用服务器(推送服务器).支持JMS规范. 1.1.1 JMS概述 全称:Java Message Serv ...

  8. ActiveMQ基础教程(四):.net core集成使用ActiveMQ消息队列

    接上一篇:ActiveMQ基础教程(三):C#连接使用ActiveMQ消息队列 这里继续说下.net core集成使用ActiveMQ.因为代码比较多,所以放到gitee上:https://gitee ...

  9. C#中使用消息队列RabbitMQ

    在C#中使用消息队列RabbitMQ 2014-10-27 14:41 by qy1141, 745 阅读, 2 评论, 收藏, 编辑 1.什么是RabbitMQ.详见 http://www.rabb ...

随机推荐

  1. Oracle sql判断一个字段是否全数字 或含有中文

    update (select length(t.name), t.* -- name,length(name) from g_enterprise_info t where nvl2(translat ...

  2. 堡垒机介绍及实现 (使用python django实现)(一)

    堡垒机介绍及实现 (使用python django实现)(一) 堡垒机的功能 我们在使用服务器的时候,通常的方式是 ssh user@ip 然后输入password 多人同时使用,就需要多个账号.这时 ...

  3. go modules学习

    https://github.com/golang/go/wiki/Modules https://tonybai.com/2018/07/15/hello-go-module/ https://ww ...

  4. 配置Dubbo Demo遇到的坑之一---找不到dubbo.xsd文件

    原文地址:https://blog.csdn.net/qq_36654870/article/details/80603302 1.dubbo.xsd文件不能读取 因为阿里http://code.al ...

  5. POJ-1836-Alignment-双向LIS(最长上升子序列)(LIS+LSD)+dp

    In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are al ...

  6. 第36讲 谈谈MySQL支持的事务隔离级别,以及悲观锁和乐观锁的原理和应用场景

    在日常开发中,尤其是业务开发,少不了利用 Java 对数据库进行基本的增删改查等数据操作,这也是 Java 工程师的必备技能之一.做好数据操作,不仅仅需要对 Java 语言相关框架的掌握,更需要对各种 ...

  7. 14-MySQL-Ubuntu-数据表的查询-范围查询(三)

    范围查询 1,不连续查询-in, not in 查询年龄是12,18,34的学生姓名和年龄信息 select name,age from students where age in (12,18,34 ...

  8. Ajax.BeginForm 在 Chrome下的问题

    项目背景:MVC4 代码: @using (Ajax.BeginForm("Index", "GoingMeter", new AjaxOptions { On ...

  9. sysobjects syscolumns

    在sysobjects系统表中存储着数据库的所有对象,每个对象都有一个唯一的id号进行标识.object_id就是根据对象名称返回该对象的id.反之,object_name是根据对象id返回对象名称. ...

  10. Django项目基础配置和基本使用

    博文配置内容包括: django项目的创建 django项目下应用的创建及配置 数据库的配置 templates模板的配置 命令行创建项目: 在需要的目录下创建Django项目输入命令:django- ...