2015-02-06 105 views
0

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

代码

@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> 

日志文件 Log File

回答

0

只是一个猜测:如果你上传之前清洗你的项目,你可能已经重挫了数据存储的索引配置信息,如here所述。

如果是这样的情况下,本地运行您的devserver和行使所有相关的资料储存库作业,然后再上传应用程序(不清洗第一)应该解决您的问题。

1

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

0

1)计划任务可以访问admin-only 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> 
相关问题