Creating a Replication Based Push Application

The device application must meet these requirements to utilize the Replication-Based Push Synchronization APIs described in this section.

Develop the push application directly from generated mobile business object (MBO) code.

  1. Properly configure and deploy the mobile business objects (MBOs).
    1. Create a Cache Group (or use the default) and set the cache policy to Scheduled and set some value for the Cache interval, 30 seconds for example.
    2. Create a Synchronization Group and set some value for the Change detection level, one minute for example.
    3. Place all Mobile Application project MBOs in the same Cache Group and Synchronization Group.
    4. Deploy the Mobile Application Project as Replication-based in the Deployment wizard.
  2. Develop the push application.
    • Develop the application directly from MBO code:
      1. Generate the Object API code.
      2. Write a push listener to listen to SIS notification sent from server
        public class PushListener
        implements Runnable
        {
          Connection conn = null;
        
          private static String url  = "http://:100;deviceside=false";
        
          /**
           * Constructor
           */
          public PushListener()
          {
          }
        
          public void run()
          {
             System.out.println("++++++  Started Push Listener ++++++++");
                try
                {
                   conn = Connector.open(url);
                   while (true)
                   {
                      String syncRequestStr = null;
                      try
                      {
                         if ( conn instanceof StreamConnectionNotifier )
                         {
                     // Open an InputStream.
                          StreamConnectionNotifier scn = 
                          (StreamConnectionNotifier) conn;
                          StreamConnection sc = scn.acceptAndOpen();
                          InputStream input = sc.openInputStream();
                          // Extract the data from the InputStream.
                          StringBuffer sb = new StringBuffer();
                          byte[] data = new byte[256];
                          int chunk = 0;
                          while (-1 != (chunk = input.read(data)))
                          {
                          sb.append(new String(data, 0, chunk));
                          }
        
                      // Close the InputStream and StreamConnection.
                          input.close();
                          String s = sb.toString();
                      // Display the received data.
                          syncRequestStr = s.trim();
                          System.out.println(">>Received: " + syncRequestStr);
                            }
                        }
                        catch (Exception ex)
                        {
                            System.out.println(ex);
                        }
        
        // Clients can parse the syncRequestStr to find client application 
        // name, package name, sync group name(publication), launch client 
        //application and perform sync.
        
        // format of the push message sent by the server:
        // notification_timestamp=<datetime>;app=<client app name>;
        // device_id=<device id>;package=<sup package name with version>;
        // publication=<comma separated list of syncGroup names>
        
           TestDB.registerCallbackHandler(new MyCallbackHandler());
           com.sybase.collections.ObjectList sgs = new com.sybase.
           collections.ObjectList()
        // Assume you have notification to sync two syncGroups(publications), 
        // sg1 and sg2:
                sgs.add(TestDB.getSynchronizationGroup("sg1"));
                sgs.add(TestDB.getSynchronizationGroup("sg2"));
                TestDB.beginSynchronize(sgs, new Object());
        
                  }
              }
              catch (Exception ex)
              {
                System.out.println("HttpPushListener - ERROR : " + ex);
              }
          }
        
            /*
             * Define callback handler for handling SIS notifications
             */
            public class MyCallbackHandler extends com.sybase.
            persistence.DefaultCallbackHandler
            {
             public int onSynchronize(ObjectList arg0, SynchronizationContext arg1)
              {
                System.out.println("Called on Synchronize");
                return SynchronizationAction.CONTINUE; 
               // returns SynchronizationAction.CONTINUE to proceed this sync
                }
        
                public void onSynchronizeFailure(ObjectList arg0)
                {
                    System.out.println("Called onSynchronizeFailure");
                }
        
                public void onSynchronizeSuccess(ObjectList arg0)
                {
                    System.out.println("Called onSynchronizeSuccess");
                }
            }
        }
        
      3. In the application, start the push listener, set up the connection profile for SIS and synchronize SIS subscription to server:
        public class PushClientApp extends Application
        {
         public static String MDSSERVER                 = "localhost";
         public static String MDSSERVERPORT             = "8080";
        
          static String   PROFILE_HTTP_PUSH_PROTOCOL    = "HTTPPUSH";
          static String   PROFILE_KEY_ADDRESS           = "address";
          static String   PROFILE_KEY_PROTOCOL          = "protocol";
          static String   PROFILE_KEY_APPNAME           = "appname";
          static String   PROFILE_KEY_DEVICE_ID         = "deviceId";
          static String   PUSH_HTTP_DEFAULT_DEVICE_PORT = "100";
          static String   DEVICE_ID                     = "2100000a";
        
            public static void main(String[] args)
            {
              PushClientApp app = new PushClientApp();
              app.enterEventDispatcher();
            }
        
            Thread pushThread;
        
            PushClientApp()
            {
              // Set the connection profile information
              System.out.println("++++++++  Starting the client  ++++++++++");
              ConnectionProfile syncprofile = 
              TestDB.getSynchronizationProfile();
              syncprofile.setServerName("kpatilxp");
              syncprofile.setPortNumber(2480);
              syncprofile.save();
        
                // Login to the SUP server
                TestDB.loginToSync("supAdmin", "s3pAdmin");
        
                // Start the http push listener thread
                pushThread = new Thread(new PushListener());
                pushThread.start();
        
                setPushConnectionProfile("Test:1.0", DEVICE_ID, 
                syncprofile, "PushClientApp");
        
                // Enable SIS on the synchronization group
                SynchronizationGroup sg = 
                TestDB.getSynchronizationGroup("PushEnabled");
                sg.setEnableSIS(true);
                sg.setInterval(3);
                sg.save(); // this will update the local db
        
                // This will synchronize the SIS subscription to the server
                TestDB.synchronize();
                System.out.println("++++ Synchronization succeeded ++++++");
            }
        
         /*
         * For now this assumes MDS is running on localhost 
         * Creates the URL for PUSH
         * 
         * @param deviceid for SUP client
         */
            public static String getHTTPPushAddress(String deviceid)
            {
                String mdsServer = MDSSERVER;
        
                String mdsPort = MDSSERVERPORT;
        
                StringBuffer result = new StringBuffer("http://");
                result.append(mdsServer);
                result.append(":");
                result.append(mdsPort);
                result.append("/push?DESTINATION=");
                result.append(deviceid);
                result.append("&PORT=");
                result.append(PUSH_HTTP_DEFAULT_DEVICE_PORT);
                return result.toString();
            }
        
            /**
             * Sets up push settings for specified package's 
             * synchronization profile.
             * 
             * @param packageName
             *            the specified package name
             * @return true if set up succesfully.
             */
            private boolean setPushConnectionProfile(String packageName, 
            String deviceId, ConnectionProfile syncProfile,
                    String appId)
            {
        
              try
              {
                String httpPushAddress = getHTTPPushAddress(deviceId);
        
                syncProfile.setProperty(PROFILE_KEY_ADDRESS, 
                httpPushAddress);
        
                syncProfile.setProperty(PROFILE_KEY_PROTOCOL, 
                PROFILE_HTTP_PUSH_PROTOCOL);
        
                  syncProfile.setProperty(PROFILE_KEY_APPNAME, appId);
        
                  syncProfile.setProperty(PROFILE_KEY_DEVICE_ID, deviceId);
        
                  syncProfile.save();
            }
            catch (Exception e)
            {
             System.out.println(">> setPushConnectionProfile - 
             Exception e : " + e);
             return false;
             }
        
             return true;
        }