如何保持应用程序引擎的Servlet听火力地堡 [英] How to keep App Engine Servlet Listening to Firebase
问题描述
我在下面的教程//云.google.com /解决方案/移动/火力-APP-引擎机器人工作室
我拥有的一切工作和电子邮件每隔2分钟,因为它应该发送。不过,我现在想扩大这种触发只有在得到火力地堡节点上的数据变化,不可以发送邮件每2分钟发送电子邮件。
要测试我更换了 cron.xml
文件
来自:
<?XML版本=1.0编码=UTF-8&GT?;
< cronentries>
<&cron的GT;
< URL> /你好< / URL>
<描述>送我早上&LT优秀项目的电子邮件; /描述>
<&时间表GT;每2分钟< /时间表>
< / cron的>
< / cronentries>
到
<?XML版本=1.0编码=UTF-8&GT?;
< cronentries />
要清除出计划任务。
但是,现在在做的火力地堡分贝的改变,邮件永远不会发送....
的如何将我的应用程序引擎服务器监听保持到了火力点,随后产生 onDataChanged
实时给出一个动作?的
MyServlet类:
公共类MyServlet延伸的HttpServlet {
静态记录器记录= Logger.getLogger(com.example.username.myapplication.backend.MyServlet); @覆盖
公共无效的doGet(HttpServletRequest的REQ,HttpServletResponse的RESP)
抛出IOException
Log.info(拿到的cron信息,构建邮件。); //创建一个新的火力地堡实例,并认购儿童的事件。
火力地堡火力点=新的火力点([火力参考]);
firebase.addValueEventListener(新ValueEventListener(){
@覆盖
公共无效onDataChange(DataSnapshot dataSnapshot){
//生成使用各个领域,从火力地堡电子邮件内容。
最后StringBuilder的newItemMessage =新的StringBuilder();
newItemMessage.append(这应该非常密切地改变数据后抵达);
//现在发送电子邮件
属性道具=新特性();
会话的会话=作为Session.getDefaultInstance(道具,NULL);
尝试{
消息味精=新的MimeMessage(会话);
//确保您替换您的项目-ID在电子邮件From字段
msg.setFrom(新网际地址(任何@ [应用引擎] .appspotmail.com
藤堂Nagger));
msg.addRecipient(Message.RecipientType.TO,
新的网际地址(myEmail@gmail.com,收件人));
msg.setSubject(盛宴电子邮件测试);
msg.setText(newItemMessage.toString());
Transport.send(MSG);
}赶上(MessagingException | UnsupportedEncodingException五){
Log.warning(e.getMessage());
}
} 公共无效onCancelled(FirebaseError firebaseError){
}
});
}}
您的问题实际上是一个有关的AppEngine以及如何创建自动启动并自动执行一些初始化一个Servlet的问题。
您想保留手动缩放比例,但按照下列步骤进行:
的https://cloud.google.com/appengine/docs/java/config/appconfig#using_a_load-on-startup_servlet
有关初始化设置你的听众(),而不是一个HTTP请求。
你正在尝试绝对是可能的,看到它在其他地方运行。
I am following the tutorial at: https://cloud.google.com/solutions/mobile/firebase-app-engine-android-studio
I have everything working and the email is sending every 2 minutes as it should. However, I now wish to extend this to trigger sending an email only upon data change on the Firebase node, not sending a message every 2 minutes.
To test I replaced the cron.xml
file
from:
<?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>
to:
<?xml version="1.0" encoding="UTF-8"?>
<cronentries/>
To clear out the scheduled tasks.
But now upon making a change in the Firebase db, the email is never sent....
How can I keep my app engine server "listening" to the firebase node and subsequently produce an action given onDataChanged
in real-time?
MyServlet class:
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("anything@[app-engine].appspotmail.com",
"Todo Nagger"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress("myEmail@gmail.com", "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) {
}
});
}
}
Your question is actually a question about AppEngine and how to create a Servlet that starts automatically and automatically performs some initialization.
You will want to keep manual scaling on, but follow the steps here: https://cloud.google.com/appengine/docs/java/config/appconfig#using_a_load-on-startup_servlet
for setting up your listeners on init() instead of an http request. What you are trying is definitely possible and have seen it run elsewhere.
这篇关于如何保持应用程序引擎的Servlet听火力地堡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!