Exemple #1
0
        static void Main(string[] args)
        {
            //producer创建和正常工作的参数,必须输入
            ONSFactoryProperty factoryInfo = new ONSFactoryProperty();

            factoryInfo.setFactoryProperty(factoryInfo.getConsumerIdName(), "CID_xxxxxxxxx"); //在ONS控制台申请的consumerId
            factoryInfo.setFactoryProperty(factoryInfo.getPublishTopicsName(), "xxxxxxxx");   // 在ONS 控制台申请的msg topic
            factoryInfo.setFactoryProperty(factoryInfo.getAccessKeyName(), "xxxxxxx");        //ONS AccessKey
            factoryInfo.setFactoryProperty(factoryInfo.getSecretKeyName(), "xxxxxxxxxxx");    // ONS SecretKey
            factoryInfo.setOnsChannel(ONSChannel.CLOUD);                                      //默认值为ONSChannel.ALIYUN,聚石塔用户必须设置为CLOUD,阿里云用户不需要设置(如果设置,必须设置为ALIYUN)

            //创建consumer
            ONSFactory   onsfactory = new ONSFactory();
            PushConsumer pConsumer  = onsfactory.getInstance().createPushConsumer(factoryInfo);

            //register msg listener and subscribe msg topic
            MessageListener msgListener = new MyMsgListener();

            pConsumer.subscribe(factoryInfo.getPublishTopics(), "*", ref msgListener);

            //在拉取消息前,必须调用start方法来启动consumer,只需调用一次即可。
            pConsumer.start();
            //consumer启动后,会自动拉取消息,拉取到消息后,会自动调用MyMsgListener实例的consume函数;

            //确定消费完成后,调用shutdown函数;在应用退出前,必须销毁Consumer 对象,否则会导致内存泄露等问题
            pConsumer.shutdown();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            //producer创建和正常工作的参数,必须输入
            ONSFactoryProperty factoryInfo = new ONSFactoryProperty();

            factoryInfo.setFactoryProperty(factoryInfo.getProducerIdName(), "PID_xxxxxxx");       //在ONS控制台申请的producerId
            factoryInfo.setFactoryProperty(factoryInfo.getPublishTopicsName(), "xxxxxxxxx");      // 在ONS 控制台申请的msg topic
            factoryInfo.setFactoryProperty(factoryInfo.getMsgContentName(), "input msg content"); //消息内容
            factoryInfo.setFactoryProperty(factoryInfo.getAccessKeyName(), "xxxxx");              //ONS AccessKey
            factoryInfo.setFactoryProperty(factoryInfo.getSecretKeyName(), "xxxxxxxxxxxxxxxx");   // ONS SecretKey
            factoryInfo.setOnsChannel(ONSChannel.CLOUD);                                          //默认值为ONSChannel.ALIYUN,聚石塔用户必须设置为CLOUD,阿里云用户不需要设置(如果设置,必须设置为ALIYUN)

            //创建producer
            ONSFactory onsfactory = new ONSFactory();
            Producer   pProducer  = onsfactory.getInstance().createProducer(factoryInfo);

            //在发送消息前,必须调用start方法来启动Producer,只需调用一次即可。
            pProducer.start();

            Message msg = new Message(
                //Message Topic
                factoryInfo.getPublishTopics(),
                //Message Tag,可理解为Gmail中的标签,对消息进行再归类,方便Consumer指定过滤条件在ONS服务器过滤
                "TagA",
                //Message Body,任何二进制形式的数据,ONS不做任何干预,需要Producer与Consumer协商好一致的序列化和反序列化方式
                factoryInfo.getMessageContent()
                );

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

            //发送消息,只要不抛异常就是成功
            try
            {
                SendResultONS sendResult = pProducer.send(msg);
            }
            catch (ONSClientException e)
            {
                //异常处理
            }

            // 在应用退出前,必须销毁Producer对象,否则会导致内存泄露等问题
            pProducer.shutdown();
        }