码迷,mamicode.com
首页 > 编程语言 > 详细

阿里云-ONS-Help-SDK参考-TCP协议-JavaSDK-Demo-[java-tcp-demo]:InitlliJIDEA

时间:2020-03-29 22:44:25      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:message   The   文件夹   示例   pack   any   amp   may   end   

ylbtech-阿里云-ONS-Help-SDK参考-TCP协议-JavaSDK-Demo-[java-tcp-demo]:InitlliJIDEA

 

1.返回顶部

将 Demo 工程文件导入 IntelliJ IDEA
·在 IntelliJ IDEA 界面,选择 Import Project,选择 mq-demo-master 文件夹。

技术图片

·选择 Import 类型为 Maven。

技术图片

·

技术图片

·

技术图片

··

2. 发送消息返回顶部

·src/main/java/com/aliyun/openservices/tcp/example/MqConfig.java

/**
 * Copyright (C) 2010-2016 Alibaba Group Holding Limited
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aliyun.openservices.tcp.example;

/**
 * MQ 配置
 */
public class MqConfig {
    /**
     * 启动测试之前请替换如下 XXX 为您的配置
     */
    public static final String TOPIC = "XXX";
    public static final String GROUP_ID = "XXX";
    public static final String ORDER_TOPIC = "XXX";
    public static final String ORDER_GROUP_ID = "XXX";
    public static final String ACCESS_KEY = "XXX";
    public static final String SECRET_KEY = "XXX";
    public static final String TAG = "mq_test_tag";

    /**
     * NAMESRV_ADDR, 请在mq控制台 https://ons.console.aliyun.com 通过"实例管理--获取接入点信息--TCP协议接入点"获取
     */
    public static final String NAMESRV_ADDR = "XXX";

}

·src/main/java/com/aliyun/openservices/tcp/example/producer/SimpleMQProducer.java

/**
 * Copyright (C) 2010-2016 Alibaba Group Holding Limited
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aliyun.openservices.tcp.example.producer;

import java.util.Date;
import java.util.Properties;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.exception.ONSClientException;
import com.aliyun.openservices.tcp.example.MqConfig;

/**
 * MQ发送普通消息示例 Demo
 */
public class SimpleMQProducer {


    public static void main(String[] args) {
        Properties producerProperties = new Properties();
        producerProperties.setProperty(PropertyKeyConst.GROUP_ID, MqConfig.GROUP_ID);
        producerProperties.setProperty(PropertyKeyConst.AccessKey, MqConfig.ACCESS_KEY);
        producerProperties.setProperty(PropertyKeyConst.SecretKey, MqConfig.SECRET_KEY);
        producerProperties.setProperty(PropertyKeyConst.NAMESRV_ADDR, MqConfig.NAMESRV_ADDR);
        Producer producer = ONSFactory.createProducer(producerProperties);
        producer.start();
        System.out.println("Producer Started");

        for (int i = 0; i < 10; i++) {
            Message message = new Message(MqConfig.TOPIC, MqConfig.TAG, "mq send transaction message test".getBytes());
            try {
                SendResult sendResult = producer.send(message);
                assert sendResult != null;
                System.out.println(new Date() + " Send mq message success! Topic is:" + MqConfig.TOPIC + " msgId is: " + sendResult.getMessageId());
            } catch (ONSClientException e) {
                // 消息发送失败,需要进行重试处理,可重新发送这条消息或持久化这条数据进行补偿处理
                System.out.println(new Date() + " Send mq message failed! Topic is:" + MqConfig.TOPIC);
                e.printStackTrace();
            }
        }
    }
}

·

3.返回顶部
 
4.返回顶部
 
5.返回顶部
 
 
6.返回顶部
 
技术图片 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

阿里云-ONS-Help-SDK参考-TCP协议-JavaSDK-Demo-[java-tcp-demo]:InitlliJIDEA

标签:message   The   文件夹   示例   pack   any   amp   may   end   

原文地址:https://www.cnblogs.com/storebook/p/12595565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!