【问题标题】:Google App Engine Datastore does not run live, does locallyGoogle App Engine Datastore 不会实时运行,而是在本地运行
【发布时间】:2015-02-08 16:38:33
【问题描述】:

我已将 Java 项目部署到 Google App Engine,但 Datastore 无法像在本地那样工作。该项目生成一个网页,其中包含各种新闻文章的摘要。这些文章取自数据存储区。数据存储区通过 cron 作业和 Java Servlet 每 30 分钟更新一次新文章。至少这是当地发生的事情。实时部署时,cron 作业根据日志成功运行,但数据存储中没有条目。有没有人遇到过类似的问题并有解决方案?

代码

@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);
        }
    }
}

定时任务

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

日志文件

【问题讨论】:

    标签: java google-app-engine


    【解决方案1】:

    我没有看到Successfully updated database 日志条目,这可能意味着这个servlet 从未执行过——否则你会看到一些错误消息。确保它在 web.xml 中正确映射到此 URL。

    【讨论】:

      【解决方案2】:

      只是猜测:如果您在上传项目之前对其进行了清理,您可能已经破坏了 DataStore 索引配置信息,如 here 所述。

      如果是这种情况,在本地运行您的开发服务器并执行所有相关的 DataStore 操作,然后再次上传应用程序(无需先清理)应该可以解决您的问题。

      【讨论】:

        【解决方案3】:

        1) 计划任务可以访问仅限管理员的 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>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-04
          • 1970-01-01
          • 2019-10-31
          • 2023-03-15
          相关资源
          最近更新 更多