Servlet在App Engine中不起作用

时间:2016-10-25 14:30:57

标签: java android google-app-engine servlets firebase-realtime-database

我的servlet和Google App Engine存在问题。 问题是本地我的代码工作正常,当在GAE中部署时它会记录到LOG.warning("already exists...");。我在appengine-web.xml中设置了手动缩放,因为我使用的是Firebase。 这里使用的教程是:https://cloud.google.com/solutions/mobile/firebase-app-engine-android-studio

这是我的doGet servlet方法:

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException{
    LOG.info("I'm in get method");
    FirebaseOptions options = new FirebaseOptions.Builder()
            .setServiceAccount(getServletContext().getResourceAsStream(Constants.JSON_FILENAME))
            .setDatabaseUrl(Constants.FIREBASE_PROJECT_ID).build();

    try {
        FirebaseApp.getInstance();
    } catch (Exception error) {
        LOG.warning("doesn't exist...");
    }

    try {
        FirebaseApp.initializeApp(options);
    } catch (Exception error) {
        LOG.warning("already exists...");
    }

    // As an admin, the app has access to read and write all data, regardless of Security Rules
    DatabaseReference ref = FirebaseDatabase
            .getInstance()
            .getReference("IDs");

    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            LOG.info("There are " + snapshot.getChildrenCount() + " users");
            for (DataSnapshot userSnapshot : snapshot.getChildren()) {
                int notificationHour = 0, notificationMinute = 0;
                Informations informations = userSnapshot.getValue(Informations.class);
                if ("on".equals(informations.getNotifiche())
                        && !informations.isFlagImportanza()
                        && informations.getOrarioNotifica().trim().length() > 0) {
                    String[] notificationTime = informations.getOrarioNotifica().split(":");

                    if (notificationTime[0].trim().length() > 0)
                        notificationHour = Integer.valueOf(notificationTime[0]);
                    if (notificationTime[1].trim().length() > 0)
                        notificationMinute = Integer.valueOf(notificationTime[1]);

                    Calendar calendar = Calendar.getInstance();
                    int hour = calendar.get(Calendar.HOUR_OF_DAY);
                    int minute = calendar.get(Calendar.MINUTE);
                    if (hour == notificationHour && minute == notificationMinute)
                        executePost(Constants.URL, userSnapshot.getRef().getKey());
                }
                LOG.info(informations.toString());
            }
        }

        @Override
        public void onCancelled(DatabaseError firebaseError) {
            LOG.warning("The read failed: " + firebaseError.getMessage());
        }
    });
}

0 个答案:

没有答案