Package com.aliyun.openservices.ons.demo

Source Code of com.aliyun.openservices.ons.demo.ProducerTest

/***********************************************************
运行示例代码的前置条件:

通过下面两种方式可以引入依赖(任选一种)
    1、Maven方式引入依赖
        <dependency>
            <groupId>com.aliyun.openservices</groupId>
            <artifactId>ons-client</artifactId>
            <version>1.1.1</version>
        </dependency>

    2、下载依赖Jar包
        http://onsall.oss-cn-hangzhou.aliyuncs.com/aliyun-ons-client-java.tar.gz
       
注意:收发消息只能在阿里云ECS上进行
***********************************************************/

package com.aliyun.openservices.ons.demo;

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;


public class ProducerTest {

    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.ProducerId, "PID_001");
        properties.put(PropertyKeyConst.AccessKey, "F0000043D88CB7EF4");
        properties.put(PropertyKeyConst.SecretKey, "F0000043D900F191F");
        Producer producer = ONSFactory.createProducer(properties);
        // 在发送消息前,必须调用start方法来启动Producer,只需调用一次即可。
        producer.start();
        Message msg = new Message( //
            // Message Topic
            "TopicTestONS",
            // Message Tag,
            // 可理解为Gmail中的标签,对消息进行再归类,方便Consumer指定过滤条件在ONS服务器过滤
            "TagA",
            // Message Body
            // 任何二进制形式的数据,ONS不做任何干预,需要Producer与Consumer协商好一致的序列化和反序列化方式
            "Hello ONS".getBytes());

        // 设置代表消息的业务关键属性,请尽可能全局唯一。
        // 以方便您在无法正常收到消息情况下,可通过ONS Console查询消息并补发。
        // 注意:不设置也不会影响消息正常收发
        msg.setKey("ORDERID_100");

        // 发送消息,只要不抛异常就是成功
        SendResult sendResult = producer.send(msg);
        System.out.println(sendResult);

        // 在应用退出前,销毁Producer对象<br>
        // 注意:如果不销毁也没有问题
        producer.shutdown();
    }
}
TOP

Related Classes of com.aliyun.openservices.ons.demo.ProducerTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.