2014-11-21 39 views
0

我正在構建一個可以存儲數據集的基於GAE的應用程序。外部應用程序可以使用Google Cloud Endpoints訪問此數據集。Google App Engine:對數據存儲的操作已更改

當外部應用程序編輯此數據集(PUT POST DELETE)時,我想在我的GAE上運行腳本。我可以通過將此代碼添加到PUT/POST/DELETE api方法來實現此目的;例如:

/** 
* This inserts a new entity into App Engine datastore. If the entity already 
* exists in the datastore, an exception is thrown. 
* It uses HTTP POST method. 
* 
* @param sensor the entity to be inserted. 
* @return The inserted entity. 
*/ 
@ApiMethod(name = "insertSensor") 
public Sensor insertSensor(Sensor sensor) { 
    PersistenceManager mgr = getPersistenceManager(); 
    try { 
     if (containsSensor(sensor)) { 
      throw new EntityExistsException("Object already exists"); 
     } 
     mgr.makePersistent(sensor); 
     ########RUN SCRIPT HERE######## 
    } finally { 
     mgr.close(); 
    } 
    return sensor; 
} 

但我想知道GAE是否能提供更好的解決方案來解決這類問題?

+0

你在找Datastore PrePut&PostPut回調([docs](https://cloud.google.com/appengine/docs/java/datastore/callbacks))嗎? – tx802 2014-11-21 10:56:29

+0

是的。雖然他們不提供PostGET的回調,並且PrePOST和PostPOST都是? – Wouter 2014-11-21 15:11:48

+0

也許我誤解了你的問題:這些回調用於數據存儲_put_和_get_操作。那不是你所追求的? – tx802 2014-11-21 15:39:16

回答

0

如果您希望在路由處理程序或Endpoints API函數I'd recommend strongly that you look into Task Queues上收到請求時執行其他處理或操作,以解決該問題。這使您可以執行您定義的後臺處理/掛鉤,同時還可以將請求延遲保持在較低水平,確保客戶端獲得所需的數據,而不受您的應用程序干擾,無需執行與提供請求無關的額外處理。

相關問題