簡體   English   中英

Google App Engine數據存儲區無法實時運行,而是在本地運行

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

我已將一個Java項目部署到Google App Engine,並且數據存儲無法像在本地那樣工作。 該項目將產生一個包含各種新聞摘要的網頁。 這些文章摘自數據存儲區。 數據存儲區通過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);
        }
    }
}

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>

日志文件 日志文件

我看不到Successfully updated database日志條目的信息,這可能意味着該servlet從未執行-否則您會看到一些錯誤消息。 確保將其在web.xml中正確映射到此URL。

只是一個猜測:如果你清理你的項目之前上傳,你可能已經重挫了數據存儲索引配置信息,如所描述這里

如果是這種情況,請在本地運行devserver並執行所有相關的DataStore操作,然后再次上載該應用程序(首先不進行清理)應該可以解決您的問題。

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