使用阿里云消息队列

控制台地址:http://ons.console.aliyun.com/#/home/topic

Demo:

支付消息mq工厂类:

public class DfacePayConsumerFactory {

    public static String        CID = "CID-";
  
  //监听执行实例
@Autowired
private DfacePayConsumerListener dfacePayConsumerListener; private String topic;
private String pTag;
private String accessKey;
private String secretKey;
private String tag; private Consumer consumer; /**
* @return the topic
*/
public String getTopic() {
return topic;
} /**
* @param topic the topic to set
*/
public void setTopic(String topic) {
this.topic = topic;
} /**
* @return the tag
*/
public String getTag() {
return tag;
} /**
* @param tag the tag to set
*/
public void setTag(String tag) {
this.tag = tag;
} /**
* @return the pTag
*/
public String getpTag() {
return pTag;
} /**
* @param pTag the pTag to set
*/
public void setpTag(String pTag) {
this.pTag = pTag;
} /**
* @return the accessKey
*/
public String getAccessKey() {
return accessKey;
} /**
* @param accessKey the accessKey to set
*/
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
} /**
* @return the secretKey
*/
public String getSecretKey() {
return secretKey;
} /**
* @param secretKey the secretKey to set
*/
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
} public void initConsumer() { Properties properties = new Properties();
String consumerId = CID + this.topic
+ (StringUtils.hasText(this.pTag) ? "-" + this.pTag : "");
properties.put(PropertyKeyConst.ConsumerId, consumerId);
properties.put(PropertyKeyConst.AccessKey, this.accessKey);
properties.put(PropertyKeyConst.SecretKey, this.secretKey);      /**
     ( 
     //相关属性介绍:
     //Properties properties = new Properties();
     //properties.put(PropertyKeyConst.ConsumerId, consumerLocal.getConsumerId());
     // AccessKey 阿里云身份验证,在阿里云服务器管理控制台创建
     //properties.put(PropertyKeyConst.AccessKey, consumerLocal.getAccessKey());
     // SecretKey 阿里云身份验证,在阿里云服务器管理控制台创建
     //properties.put(PropertyKeyConst.SecretKey, consumerLocal.getSecreKey());
     //消息处理失败后多久重新发送消息
     properties.put(PropertyKeyConst.SuspendTimeMillis, consumerLocal.getSuspendTimeMillis());
     //重发的次数
     //properties.put(PropertyKeyConst.MaxReconsumeTimes, consumerLocal.getMaxReconsumeTimes());
     //消费者的线程数
     //properties.put(PropertyKeyConst.ConsumeThreadNums,"1");
     //消费者的介入地址
     //properties.put(PropertyKeyConst.ONSAddr, consumerLocal.getOnsAddress());
     )
     **/
 
consumer = ONSFactory.createConsumer(properties);
consumer.subscribe(topic, tag, this.dfacePayConsumerListener);
new Thread(new Runnable() { @Override
public void run() {
try {
Thread.sleep(90000);
} catch (InterruptedException e) {
e.printStackTrace();
}
consumer.start();
}
}).start(); Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
shutDown();
}
});
} /**
* 停止监听
*
* @return
*/
public boolean shutDown() {
if (null != this.consumer) {
this.consumer.shutdown();
return true;
}
return false;
}
}
//dPay支付监听  执行mq consume消息接收(通过topic订阅)
@Component("dfacePayConsumerListener")
public class DfacePayConsumerListener implements MessageListener {
private static Logger logger = LoggerFactory.getLogger(DfacePayConsumerListener.class); @Autowired
private ...; @Override
public Action consume(Message message, ConsumeContext context) {
String msg = new String(message.getBody());
String tag = message.getTag();
logger.info(LogUtils.builder().append("mq", "接受mq").append("mqTag", tag)
.append("mqMsg", msg).toString());
return tagHandle(tag, msg, message);
} public Action tagHandle(String tag, String msg, Message message) {
if (MqTagEnum.PAY.name().equals(tag)) {
try {
PayBackBo payBackBo = JSON.parse(msg, PayBackBo.class);
//检查订单号
if (!payBackBo.getOrderNo().startsWith(ApplicationConstant.APP_NO)) {
logger.info(LogUtils.format("订单支付失败: orderNo 前缀 ", payBackBo.getOrderNo()));
return Action.CommitMessage;
}
return giftOrderHandle(payBackBo);
} catch (Exception e) {
e.printStackTrace();
logger.info(LogUtils.format("paid_error", e.getMessage()));
return Action.ReconsumeLater;
}
} else {
logger.info(LogUtils.builder().append("tag error", tag).append("msg", msg)
.append("message", message).toString());
}
return Action.CommitMessage;
} /**
* 支付订单处理
*
* @param payBackBo
* @return
*/
private Action giftOrderHandle(PayBackBo payBackBo) {
//处理支付业务逻辑
} }

阿里云 消息队列mq的更多相关文章

  1. 使用java实现阿里云消息队列简单封装

    一.前言 最近公司有使用阿里云消息队列的需求,为了更加方便使用,本人用了几天时间将消息队列封装成api调用方式以方便内部系统的调用,现在已经完成,特此记录其中过程和使用到的相关技术,与君共勉. 现在阿 ...

  2. Sping Boot入门到实战之实战篇(一):实现自定义Spring Boot Starter——阿里云消息队列服务Starter

    在 Sping Boot入门到实战之入门篇(四):Spring Boot自动化配置 这篇中,我们知道Spring Boot自动化配置的实现,主要由如下几部分完成: @EnableAutoConfigu ...

  3. 阿里云消息队列(MQ)服务

    A.首先在阿里云上申请消息队列MQ服务: B.然后创建一个Topic(主题,一级主题):然后创建生产者与消费者: C.不过此时还没有结束 ,还需要创建一个AccessKey和AccessSecret( ...

  4. 阿里云消息队列MQ_HTTP接入 for .NetCore 简单例子

    , , )).TotalMilliseconds;                 , , )).TotalMilliseconds;                 )                ...

  5. 阿里云消息队列的C#使用http接口发送消息实例

    app.config <appSettings> <clear/> <add key="Ons_Topic" value="XXX_Fini ...

  6. 消息队列 MQ 入门理解

    功能特性: 应用场景: 消息队列 MQ 可应用于如下几个场景: 分布式事务 在传统的事务处理中,多个系统之间的交互耦合到一个事务中,响应时间长,影响系统可用性.引入分布式事务消息,交易系统和消息队列之 ...

  7. 详解RPC远程调用和消息队列MQ的区别

    PC(Remote Procedure Call)远程过程调用,主要解决远程通信间的问题,不需要了解底层网络的通信机制. RPC框架 知名度较高的有Thrift(FB的).dubbo(阿里的). RP ...

  8. 消息队列MQ简介

    项目中要用到RabbitMQ,领导让我先了解一下.在之前的公司中,用到过消息队列MQ,阿里的那款RocketMQ,当时公司也做了简单的技术分享,自己也看了一些博客.自己在有道云笔记上,做了一些整理,但 ...

  9. 为什么会需要消息队列(MQ)?

    为什么会需要消息队列(MQ)? #################################################################################### ...

随机推荐

  1. VUE小案例--跑马灯效果

    自学Vue课程中学到的一个小案例,跑马灯效果 <!DOCTYPE html> <html lang="zh-CN"> <head> <me ...

  2. openstack stein部署手册 2. 基础应用

    1. chrony # 安装程序包 yum install -y chrony # 变更配置文件 /etc/chrony.conf 增加 server 192.168.123.200 iburst # ...

  3. linux100day(day5)--编程原理和shell脚本

    通过前面的学习,我们对于linux文件系统有了一定的了解,我们接下来会初步接触编程原理和尝试编写shell脚本来实现功能. day05--编程原理和shell脚本初步认识 编程原理 在早期编程中,因为 ...

  4. 自己关于SSM框架的搭建

    第一步 导入相应的包 spring springmvc 需要的包 spring-webmvc spring-aop spring-beans apring-context spring-core sp ...

  5. mysql官网下载安装

    1.官网下载zip压缩文件 2.解压到指定文件夹 3.配置环境变量 4.新建my.ini 5.管理员权限打开cmd命令行,mysqld install mysql,mysql --initialize ...

  6. 牛客网NOIP赛前集训营-提高组(第六场) C-树

    题目描述 有一棵有 n 个结点的树,每条边有编号为 0,1,2 的三种颜色,刚开始每条边颜色都为 0 . 现在有 3 种操作: \(1\ x\ y\ col\) ,表示询问 \(x\) 到 \(y\) ...

  7. Java使用文件通道复制文件

    两种文件通道复制文件方式的性能比较 import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IO ...

  8. boost location-independent times

    The class boost::posix_time::ptime defindes a location-independent time. It uses the type boost::gre ...

  9. 01 spring security入门篇

    1. 环境搭建 使用SpringBoot搭建开发环境,只需在pom.xml添加如下依赖即可. <?xml version="1.0" encoding="UTF-8 ...

  10. Dataphin数据服务系列之--API 配置、管理和消费

    研发小哥哥还在为公司里大量 API 只上不下,不可查不可用, 想找的 API 找不到而苦恼吗?业务方小姐姐还在为 API 开发时间长,业务相应不及时而抱怨吐槽吗? 铛铛铛,Dataphin 数据服务 ...