2016-02-26 33 views
4

,我在下面的教程:https://cloud.google.com/solutions/mobile/firebase-app-engine-android-studio如何保持應用程序引擎的Servlet聽火力

我所擁有的一切工作和電子郵件,每2分鐘,因爲它應該發送。但是,我現在希望將此擴展爲只觸發Firebase節點上的數據更改時發送電子郵件,而不是每2分鐘發送一條消息。

<?xml version="1.0" encoding="UTF-8"?> 
<cronentries> 
    <cron> 
     <url>/hello</url> 
     <description>Send me an email of outstanding items in the morning</description> 
     <schedule>every 2 minutes</schedule> 
    </cron> 
</cronentries> 

到:

爲了測試我從替換cron.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<cronentries/> 

要清除的計劃任務。

但現在在做的火力地堡分貝的變化,郵件永遠不會發送....

如何將我的應用程序引擎服務器「監聽」保持到了火力點,隨後產生給動作onDataChanged在實時?

MyServlet類:

public class MyServlet extends HttpServlet { 
    static Logger Log = Logger.getLogger("com.example.username.myapplication.backend.MyServlet"); 

    @Override 
    public void doGet(HttpServletRequest req, HttpServletResponse resp) 
      throws IOException { 
     Log.info("Got cron message, constructing email."); 

     //Create a new Firebase instance and subscribe on child events. 
     Firebase firebase = new Firebase("[firebase ref]"); 
     firebase.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       // Build the email message contents using every field from Firebase. 
       final StringBuilder newItemMessage = new StringBuilder(); 
       newItemMessage.append("This should arrive very closely after changing the data"); 


       //Now Send the email 
       Properties props = new Properties(); 
       Session session = Session.getDefaultInstance(props, null); 
       try { 
        Message msg = new MimeMessage(session); 
        //Make sure you substitute your project-id in the email From field 
        msg.setFrom(new InternetAddress("[email protected][app-engine].appspotmail.com", 
          "Todo Nagger")); 
        msg.addRecipient(Message.RecipientType.TO, 
          new InternetAddress("[email protected]", "Recipient")); 
        msg.setSubject("Feast Email Test"); 
        msg.setText(newItemMessage.toString()); 
        Transport.send(msg); 
       } catch (MessagingException | UnsupportedEncodingException e) { 
        Log.warning(e.getMessage()); 
       } 
      } 

      public void onCancelled(FirebaseError firebaseError) { 
      } 
     }); 
    } 

} 
+0

或者我應該只使用node.js ...? – Sauron

+1

您已經提到了教程,但是在啓用自動縮放時(在tut中涵蓋)幾乎總是會出現此錯誤。請參閱[此主題](https://groups.google.com/forum/#!searchin/firebase-talk/app$20engine/firebase-talk/CZYPteFV1Xw/BZ_DooKlDAAJ)。 – Kato

+0

另外,您可能需要記錄onCancelled()處理程序以查看您的偵聽器是否被拒絕。 – Kato

回答

3

你的問題實際上是一個服務引擎,以及如何創建自動啓動並自動執行一些初始化一個Servlet的問題。

你會想保留手動縮放,但按照下列步驟進行: https://cloud.google.com/appengine/docs/java/config/appconfig#using_a_load-on-startup_servlet

對初始化設置你的聽衆(),而不是一個HTTP請求。 你正在嘗試的是絕對有可能的,並且看到它在其他地方運行。

+0

但總體而言,在縮放方面,這是不是更容易與node.js ...? – Sauron

+0

Firebase甚至明確提供了針對節點的明確文檔:https://www.firebase.com/docs/web/quickstart.html – Sauron

+0

當然,nodejs是一個選項。 AppEngine更像是一種確保您的服務正在運行的PaaS。您不需要設置實例或集羣,也不必擔心重新啓動。 –

1

簡單的答案是你還不能這樣做。 Google雲端點的暫停時間爲1分鐘。克倫職位也有超時。

你需要的是觸發器。此功能在Google io 16上演示。無論何時數據發生變化,休息請求都會從Firebase發送到您選擇的服務器和路徑。

即使我在等待這個快到了。

相關問題