百度智能云

All Product Document

          Database Transmission Server

          Use the Data Subscription SDK

          Create Demo File

          import com.bce.dts.client.ConsumerClient;
          import com.bce.dts.client.ConsumerClientImpl;
          import com.bce.dts.client.ConsumerListener;
          import com.bce.dts.client.RegionContext;
          import com.bce.dts.model.message.data.DataMessage;
          public class Demo {
              public static void main(String[] args) {
                  // Initialize user identity
                  String akString = "Fill in your Access Key ID";
                  String akString = "Fill in your Secret Access Key";
                  String dtsId = “Fill in the data subscription task ID you release in the DTS console";
                    
                  // Initialize RegionContext
                  RegionContext regionContext = new RegionContext();
                  regionContext.setUsePublicIp(false);
                  regionContext.setAccessKey(akString);
                  regionContext.setSecretKey(skString);
                    
                  ConsumerClient consumerClient = new ConsumerClientImpl(regionContext);
                  consumerClient.addConcurrentListener(new ConsumerListener() { 
                      @Override
                      public void notify(DataMessage dataMsg) throws Exception {
                              // TODO: process DataMessage as ur wish
                          System.out.println(dataMsg);
                      }
                        
                      @Override
                      public void noException(Exception paramException) {
                              // TODO: handle exception when process DataMessage
                              System.out.println("Listener exception");
                      }
                  });
          
                  consumerClient.start();  
              }
          }

          The user shall add a different ConsumerListener to the ConsumerClient and override the notify and the noException methods.

          Method Name Method Type Method Parameter Return Value Method Description
          notify public abstract DataMessage dataMsg void Execute a consumption method of user-specified subscription data
          noException public abstract Exception paramException void Exception handling method for subscription data consumption process

          After creating the file and filling in akString, skString and dtsId, execute Demo. For details of SDK interface methods, please see: Introduction of SDK Interface.

          Previous
          Install-DTS-Java-SDK
          Next
          SDK Download Page