0

我有一个 BlackBerry 应用程序,它以固定的时间间隔向服务器发送消息。使用任何可用的连接方法通过网络服务发送消息;Wifi、BIS、TCP/IP 等

由于消息是连续发送的,我需要一种机制来排队消息以防互联网不可用,并在互联网可用时发送消息。出于这个原因,我希望首先将任何传出消息保存在 Persistent Store 中,然后读取 Persistent Store 并循环通过它来发送所有待处理的消息。任何新消息都应保存在持久存储中的最后一个位置。

单击“发送”时,我正在调用以下两种方法:

    public static void saveMessage(String msg){
        Hashtable hashtable=new Hashtable();
        persistentObject = PersistentStore.getPersistentObject(KEY);
        hashtable.put("MessageToSend", msg);
        persistentObject.commit();
    }

    public static void sendMessage(String msg){
        Hashtable hashtable=new Hashtable();
        persistentObject = PersistentStore.getPersistentObject(KEY);
        Vector msgVector = (Vector)persistentObject.getContents();
        Enumeration eMsgs=msgVector.elements();;
            /*synchronized(poObject )*/{
                persistentObject.setContents(msgVector);
                persistentObject.commit();
            }
            int i=0;
            while(eMsgs.hasMoreElements()){
                hashtable=(Hashtable)eMsgs.nextElement();
                String encryptedMessage=(String)hashtable.get("MessageToSend");
                if(!encryptedMessage.equals("")){
                    //check internet connection
                    String C0NNECTION_EXTENSION = checkInternetConnection();
                    if(C0NNECTION_EXTENSION==null)
                    {
                        Dialog.alert("Check internet connection and try again");
                        return;
                    }
                    else
                    {
                        MyScreen.PostMsgToServer(encryptedMessage);
                        hashtable.remove(encryptedMessage);
                    }
                }
                i++;
            }
    }

这只是我遇到的教程/示例的尝试。请帮忙。

4

1 回答 1

1

您显示的保存方法实际上并未将哈希表放入PersistentObject. 尝试这样的事情:

public static void saveMessage(String msg){
    Hashtable hashtable = new Hashtable();
    persistentObject = PersistentStore.getPersistentObject(KEY);
    hashtable.put("MessageToSend", msg);
    persistentObject.setContents(hashtable);   // <- you were missing this
    persistentObject.commit();
}

这可能仍然不是一个很好的实现,因为我假设您可能想要saveMessage()多次调用,并向持久存储添加多个消息(?)。是这样吗,还是您只能保存一条消息?(如果这是真的,你可以忽略下一个建议saveMessage()

public static void saveMessage(String msg){
    persistentObject = PersistentStore.getPersistentObject(KEY);
    Hashtable hashtable = (Hashtable) persistentObject.getContents();
    if (hashtable == null) {
        // lazily initialize the store contents
        hashtable = new Hashtable();
        hashtable.put("MessagesToSend", new Vector());
    }
    Vector queuedMessages = (Vector) hashtable.get("MessagesToSend");
    queuedMessages.addElement(msg);
    // write the store contents to device storage
    persistentObject.setContents(hashtable);
    persistentObject.commit();
}


/** 
 * @param msg TODO: I'm not sure why msg needs to be passed, if 
 *                  saveMessage(msg) was called first? 
 */
public static void sendMessage(String msg){
    // TODO: you could choose to save the message here, so that the caller
    //   need not remember to call both sendMessage() and saveMessage()
    // saveMessage(msg);
    persistentObject = PersistentStore.getPersistentObject(KEY);
    Hashtable hashtable = (Hashtable) persistentObject.getContents();
    if (hashtable != null) {
         // check for saved messages first, and send them
         Vector msgVector = (Vector) hashtable.get("MessagesToSend");
         Enumeration eMsgs = msgVector.elements();
         Vector toDelete = new Vector();
         while (eMsgs.hasMoreElements()) {
              String encryptedMessage = (String)eMsgs.nextElement();

              // if the send was successful, you should delete message from the store
              toDelete.addElement(encryptedMessage);
         }

         eMsgs = toDelete.elements();
         while (eMsgs.hasMoreElements()) {
              // we can delete this sent message now
              msgVector.removeElement((String)eMsgs.nextElement());
         }
         // re-write the persistent store to the device
         persistentObject.setContents(hashtable);
         persistentObject.commit();
    }
}

我通常还希望引导您远离一切static……也就是说,这确实是一个更大的、不相关的问题,而且您的持久存储对象很可能是您应用程序中的一个独特的全局对象(然而,更好的实现可能会避免所有这些static声明)。

更新:我有点不清楚您希望如何调用这两种方法。根据您的描述,您似乎同时调用了两者 saveMessage(msg),然后sendMessage(msg)在用户单击发送时调用。如果您首先使用我的实现保存消息,则无需传入msgto sendMessage(),因为sendMessage()将发送队列(向量)中所有已保存的、未发送的消息。因此,API forsendMessage()有一个不必要的参数。或者,我想你可以sendMessage(String)作为唯一的公共方法离开,并sendMessage(String)首先调用saveMessage(String).

无论如何,这取决于您,以及您希望方法调用语义如何工作。存储和检索持久对象的基本问题应该由上面的代码解决。

于 2013-02-13T11:03:31.347 回答