关于RokectMQ消息队列的运用

自己封装的RocketUtil

public class RocketMQUtils {

    public static Producer getProducer(String producerId){
        Properties properties = new Properties();
        //您在控制台创建的Producer ID
        properties.put(PropertyKeyConst.ProducerId,"PID_T_PaymentCallback_CMB");
        // AccessKey 阿里云身份验证,在阿里云服务器管理控制台创建
        properties.put(PropertyKeyConst.AccessKey,"LTAIIxdTlDBAT7N3");
        // SecretKey 阿里云身份验证,在阿里云服务器管理控制台创建
        properties.put(PropertyKeyConst.SecretKey, "HYeesDmlmFadiYCZ7TA0pIiCGu4PGk");
        //设置发送超时时间,单位毫秒
        properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, "3000");
        // 设置 TCP 接入域名(此处以公共云生产环境为例)
        properties.put(PropertyKeyConst.ONSAddr, "http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet");
        Producer producer = ONSFactory.createProducer(properties);
        // 在发送消息前,必须调用start方法来启动Producer,只需调用一次即可
        producer.start();
        System.out.println("haha");
        return producer;
    }
    //创建一个消息
    public static Message createMsg(String topic, String tags, Object msg){
        return new Message(topic,tags,msg.toString().getBytes());
    }


    //客户端订阅消息
    public static Consumer getCoustomer(String consumerId, String topic){
        Properties properties = new Properties();
        // 您在控制台创建的 Consumer ID
        properties.put(PropertyKeyConst.ConsumerId, "CID_T_PaymentCallback_CMB");
        // AccessKey 阿里云身份验证,在阿里云服务器管理控制台创建
        properties.put(PropertyKeyConst.AccessKey, "LTAIIxdTlDBAT7N3");
        // SecretKey 阿里云身份验证,在阿里云服务器管理控制台创建
        properties.put(PropertyKeyConst.SecretKey, "HYeesDmlmFadiYCZ7TA0pIiCGu4PGk");
        // 设置 TCP 接入域名(此处以公共云生产环境为例)
        properties.put(PropertyKeyConst.ONSAddr,
                "http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet");
        // 集群订阅方式 (默认)
        // properties.put(PropertyKeyConst.MessageModel, PropertyValueConst.CLUSTERING);
        // 广播订阅方式
        // properties.put(PropertyKeyConst.MessageModel, PropertyValueConst.BROADCASTING);
        Consumer consumer = ONSFactory.createConsumer(properties);
        consumer.subscribe(topic, "*", new MessageListener() { //订阅多个Tag
            public Action consume(Message message, ConsumeContext context) {
                System.out.println("Receive: " + message);
                System.out.println(new String(message.getBody()));
                return Action.CommitMessage;
            }
        });
        consumer.start();
        System.out.println("Consumer Started");
        return consumer;
    }
测试类

@RestController
public class WchatPayController {

    @RequestMapping(value = "/payResult", method = RequestMethod.POST)
    public void PayCallback(HttpServletRequest request, String producerId) {
        Map<String, String> params = new TreeMap<String, String>();
        // 取出所有参数是为了验证签名
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String parameterName = parameterNames.nextElement();
            params.put(parameterName, request.getParameter(parameterName));
        }
        JSONObject paramsObject = JSONObject.fromObject(params);
        TLinx2Util.verifySign(paramsObject);

        String ord_no = paramsObject.getString("ord_no");
        String out_no = paramsObject.getString("out_no");
        String rand_str = paramsObject.getString("rand_str");
        String status = paramsObject.getString("status");
        String timestamp = paramsObject.getString("timestamp");
        String open_key = paramsObject.getString("open_key");

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("ord_no", ord_no);
        map.put("out_no", out_no);
        map.put("rand_str", rand_str);
        map.put("status", status);
        map.put("timestamp", timestamp);
        map.put("open_key", open_key);


        Producer producer = RocketMQUtils.getProducer(producerId);
        Message msg = RocketMQUtils.createMsg("T_PaymentCallback_CMB", "tags", map);
        System.out.println(new String(msg.getBody()));
        SendResult result = producer.send(msg);
        System.out.println(result);

        System.err.println("============回调参数是=============" + paramsObject.toString());


    }

    @RequestMapping(value = "/getCustorm", method = RequestMethod.POST)
    public void getCustorm(String consumerId, String topic) {
        Consumer consumer = RocketMQUtils.getCoustomer(consumerId, topic);
        System.err.print(consumer + "已经启动");
    }
	compile group: 'com.aliyun.openservices', name: 'ons-client', version: '1.7.1.Final'




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
队列在消息队列方面的运用非常广泛,它是一种常见的数据结构,用于实现消息的异步传输和处理。消息队列是一种基于生产者-消费者模型的通信机制,通过将消息发送到队列中,实现了解耦和异步处理的目的。 在消息队列中,队列被用作存储消息的容器。生产者将消息发送到队列的末尾,而消费者则从队列的头部获取消息进行处理。这种方式可以实现生产者和消费者之间的解耦,使得它们可以独立地进行工作。 消息队列在分布式系统中有着广泛的应用,它可以用于以下方面: 1. 异步任务处理:生产者将任务发送到队列中,消费者从队列中获取任务并进行处理。这样可以实现任务的异步执行,提高系统的吞吐量和响应速度。 2. 应用解耦:不同的应用之间可以通过消息队列进行通信,将消息发送到队列中,其他应用可以从队列中获取消息并进行相应的处理。这样可以实现应用之间的解耦,提高系统的可扩展性和灵活性。 3. 流量削峰:当系统面临高并发请求时,可以将请求发送到消息队列中进行缓冲,然后由消费者按照自己的处理能力进行处理。这样可以平滑处理高峰期的请求,避免系统的过载。 4. 日志收集:将系统的日志信息发送到消息队列中,然后由消费者进行处理和存储。这样可以实现日志的集中管理和分析,方便故障排查和系统优化。 总之,队列在消息队列方面的运用可以提供异步处理、解耦、流量控制和日志收集等功能,为分布式系统的设计和实现提供了很大的便利性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值