简体   繁体   English

Google App Engine数据存储区无法实时运行,而是在本地运行

[英]Google App Engine Datastore does not run live, does locally

I have deployed a Java project to Google App Engine, and the Datastore is not working as it does locally. 我已将一个Java项目部署到Google App Engine,并且数据存储无法像在本地那样工作。 The project produces a webpage with a summary of various news articles. 该项目将产生一个包含各种新闻摘要的网页。 These articles are taken from the Datastore. 这些文章摘自数据存储区。 The Datastore is updated with new articles every 30 mins via a cron job and Java Servlet. 数据存储区通过cron作业和Java Servlet每30分钟更新一次新文章。 At least this is what happens locally. 至少这是本地发生的情况。 When deployed live the cron job runs, successfully according to the logs, but there are no entries in the datastore. 实时部署后,cron作业会根据日志成功运行,但是数据存储中没有任何条目。 Has anybody come across a similar problem and have a solution? 是否有人遇到过类似的问题并有解决方案?

Code

@SuppressWarnings("serial")
public class CronServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(CronServlet.class.getName());

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            ArrayList<String> feeds = new ArrayList<String>();

            feeds.add("http://feeds.reuters.com/reuters/technologyNews");
            feeds.add("http://feeds.reuters.com/reuters/companyNews");

            RunTagger tagger = new RunTagger();
            tagger.loadRssStreams(feeds.toArray(new String[feeds.size()]));

            ArrayList<Story> articles = tagger.tagArticles();
            uploadArticles(articles);

            log.info("Succesfully updated database");

        } catch (Exception e) {
            log.error("Failed to update database.");
            e.printStackTrace();
        }
    }

    private void uploadArticles(ArrayList<Story> articles) {
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

        for(Story article : articles){
            String url = article.getUrl();
            String headline = article.getHeadline();
            String summary = article.getSummary();
            String tags = article.getTags().toString();
            String feed = article.getFeed();
            String image = article.getImage();

            DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
            Date date;

            try {
                date = format.parse(article.getDate());
            } catch (ParseException e) {
                date = new Date();
                e.printStackTrace();
            }

            Key k = KeyFactory.createKey("Articles", url+feed);

            Entity entity = new Entity("Article", k);

            entity.setProperty("url", url);
            entity.setProperty("headline", headline);
            entity.setProperty("summary", summary);
            entity.setProperty("tags", tags);
            entity.setProperty("feed", feed);
            entity.setProperty("image", image);
            entity.setProperty("date", date);

            datastore.put(entity);
        }
    }
}

Cron Job Cron Job

<?xml version="1.0" encoding="UTF-8"?>
<cronentries>
    <cron>
        <url>/cron/mycronjob</url>
        <description>Update DB</description>
        <schedule>every 1 hours</schedule>
    </cron>
</cronentries>

Log File 日志文件 日志文件

I don't see Successfully updated database log entry, which probably means that this servlet never executed - otherwise you would have seen some error message. 我看不到Successfully updated database日志条目的信息,这可能意味着该servlet从未执行-否则您会看到一些错误消息。 Make sure it is correctly mapped in web.xml to this URL. 确保将其在web.xml中正确映射到此URL。

Just a guess: if you cleaned your project prior to uploading it, you may have clobbered the DataStore index configuration info, as described here . 只是一个猜测:如果你清理你的项目之前上传,你可能已经重挫了数据存储索引配置信息,如所描述这里

If this is the case, running your devserver locally and exercising all of the relevant DataStore operations, then uploading the app again (without cleaning first) should fix your problem. 如果是这种情况,请在本地运行devserver并执行所有相关的DataStore操作,然后再次上载该应用程序(首先不进行清理)应该可以解决您的问题。

1)Scheduled tasks can access admin-only URLs as. 1)计划的任务可以访问admin专用URL。

<security-constraint>
    <web-resource-collection>
        <web-resource-name>cron</web-resource-name>
        <url-pattern>/cron</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>admin</role-name>
    </auth-constraint>
</security-constraint>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM