TOPAPI 消息通知机制

时间:2021-09-09 22:17:48

接收用户订阅消息


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();
}
}