• TOPAPI 消息通知机制


    接收用户订阅消息

    
            public class UserSubMain {
    
                public static void main(String[] args ) throws ApiException  {
                    String appKey="1021036310";
                    String appSecret="sandboxdc954ee217f6ad0ac192f436f";
                    String serverUrl = "http://gw.api.tbsandbox.com/router/rest";
    
                    DefaultTaobaoClient client = new DefaultTaobaoClient(serverUrl , appKey , appSecret, "xml");
                    
                    IncrementCustomerPermitRequest req = new IncrementCustomerPermitRequest();
                    req.setStatus("all;all;all");
                    req.setTopics("trade;refund;item");
                    req.setType("get,syn,notify");
                    System.out.print((client.execute(req, "61017155c02b94c4f80e4611222c34f33b6076f29b063992074082786")).getBody());
                }
            }
        

    监听类

    
            import com.taobao.api.internal.stream.connect.ConnectionLifeCycleListener;
            public class DefaultConnectionLifeCycleListener implements ConnectionLifeCycleListener {
                public void onBeforeConnect() {
                    System.out.println("connect start ");
                }
                public void onConnect() {
                    System.out.println("connecting ");
                }
                public void onException(Throwable throwable) {
                    System.out.println("connecting error:  " + throwable);
                }
                public void onConnectError(Exception e) {
                    System.out.println("connecting error:  " + e);
                }
                public void onReadTimeout() {
                    System.out.println("connecting timeout");
                }
                public void onMaxReadTimeoutException() {
                    System.out.println("onMaxReadTimeoutException");
                }
                public void onSysErrorException(Exception e) {
                    System.out.println("onSysErrorException error:  " + e);
                }
            }
        

    监听类

    
            import com.taobao.api.internal.stream.message.TopCometMessageListener;
            public class DefaultTopCometMessageListener implements TopCometMessageListener{
                public void onConnectMsg(String message) {
                    System.out.println("connecting onConnectMsg:  " + message);
                }
                public void onHeartBeat() {}
    
                public void onReceiveMsg(String message) {
                    System.out.println("connecting onReceiveMsg:  " + message); 
                }
                public void onConnectReachMaxTime() {}
    
                public void onDiscardMsg(String message) {
                    System.out.println("connecting onDiscardMsg:  " + message);
                }
    
                public void onServerUpgrade(String message) {
                    System.out.println("connecting onServerUpgrade:  " + message);
                }
    
                public void onServerRehash() {  }
    
                public void onServerKickOff() { }
    
                public void onClientKickOff() { }
    
                public void onOtherMsg(String message) {
                    System.out.println("connecting onOtherMsg:  " + message);
                }
                public void onException(Exception ex) { }
            }
    
        

    订阅消息

    
            import com.taobao.api.internal.stream.Configuration;
            import com.taobao.api.internal.stream.TopCometStream;
            import com.taobao.api.internal.stream.TopCometStreamFactory;
            public class ComentMain {
                public static void main(String[] args )  {
                    String appKey="1021036310";
                    String appSecret="sandboxdc954ee217f6ad0ac192f436f";
                    Configuration conf = new Configuration(appKey,appSecret,null);
                    conf.setConnectUrl("http://stream.api.tbsandbox.com/stream");
                    TopCometStream stream = new TopCometStreamFactory(conf).getInstance();
                    stream.setConnectionListener(new DefaultConnectionLifeCycleListener());
                    stream.setMessageListener(new DefaultTopCometMessageListener());
                    stream.start();
                }
            }
  • 相关阅读:
    机器学习实践笔记3(树和随机森林)
    Cocos2d-x3.1回调函数具体解释
    base 64 编解码器
    HDU 4915 Parenthese sequence _(:зゝ∠)_ 哈哈
    跟我extjs5(03--在项目过程中加载文件)
    备份和还原数据库
    Android学习–Android app 语言切换功能
    Android app内语言环境切换
    Android学习–Android app 语言切换功能
    swift:自定义UICollectionViewFlowLayout
  • 原文地址:https://www.cnblogs.com/wmz/p/3272989.html
Copyright © 2020-2023  润新知