我有一个servlet,它可以从休息服务或jsp表单发布请求,它将调用内部方法(internalAddPodcast())将实体添加到数据存储区。
当我从jsp页面点击internalAddPodcast()时,它工作正常,我可以看到通过在添加后立即查询实体已成功添加实体。但是,当我从rest方法addPodcast()执行此操作时,datastore.put()似乎并没有实际添加到数据存储区,因为我尝试在put()之后立即检索它,并且没有任何东西返回。向下看这个类的底部附近,我把评论“//这个从REST服务添加时是空的:(”这是我希望有一些结果回来的地方,特别是我刚刚放入的实体数据存储。
package com.aol.sharepodder;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
@Path("/add/podcast/")
public class AddPodcastServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger log = Logger.getLogger(AddPodcastServlet.class
.getName());
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
String email = req.getParameter("email");
String collectionName = req.getParameter("collectionName");
String podcast_url = req.getParameter("podcast_url");
String podcast_description = req.getParameter("podcast_description");
String podcast_title = req.getParameter("podcast_title");
log.info("--post adding " + collectionName);
internalAddPodcast(email, collectionName, podcast_url,
podcast_description, podcast_title);
resp.sendRedirect("/collection_details.jsp?collectionName="
+ collectionName + "&email=" + email);
}
@POST
@Produces("text/plain")
@Consumes("application/x-www-form-urlencoded")
public String addPodcast(
@DefaultValue("barrand@gmail.com") @FormParam("email") String email,
@DefaultValue("default") @FormParam("collectionName") String collectionName,
@DefaultValue("") @FormParam("podcast_url") String podcast_url,
@DefaultValue("") @FormParam("podcast_description") String podcast_description,
@DefaultValue("") @FormParam("podcast_title") String podcast_title) {
try {
internalAddPodcast(email, collectionName, podcast_url,
podcast_description, podcast_title);
if (podcast_url == "") {
return "No url supplied";
}
return "true";
} catch (Exception e) {
return e.getMessage();
}
}
private void internalAddPodcast(String email, String collectionName,
String podcast_url, String podcast_description, String podcast_title) {
log.info("--INTERNAL ADD ");
log.info("--email " + email);
log.info("--collectionName " + collectionName);
log.info("--podcast_url " + podcast_url);
log.info("--podcast_description " + podcast_description);
log.info("--podcast_title " + podcast_title);
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
Entity podcast = new Entity("Podcast");
podcast.setProperty("collectionName", collectionName);
podcast.setProperty("user", user);
podcast.setProperty("email", email);
Date date = new Date();
podcast.setProperty("date", date);
podcast.setProperty("podcast_title", podcast_title);
podcast.setProperty("podcast_description", podcast_description);
podcast.setProperty("podcast_url", podcast_url);
DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
datastore.put(podcast);
//try to log the podcast that I just got done adding to the datastore
Query query = new Query("Podcast");
PreparedQuery pq = datastore.prepare(query);
//THIS QUERY IS EMPTY WHEN ADDED FROM THE REST SERVICE :(
for (Entity p : pq.asIterable()) {
log.info("_loop " + " - " + KeyFactory.keyToString(p.getKey())
+ " -- " + p.getProperty("podcast_title") + " - "
+ p.getProperty("podcast_url"));
}
}
}
任何想法我做错了,为什么我试图从rest方法添加的实体没有被添加到数据存储中。
我知道在这两种情况下(从jsp帖子或其他服务),当我到达internalAddPodcast()时,所有方法参数都正确进入。
答案 0 :(得分:2)
High Replication数据存储最终是一致的。这意味着大多数查询都不能保证反映刚刚对数据存储区所做的更改 - 包括返回刚刚插入的记录。详细了解此问题以及如何进行强一致性查询here。
答案 1 :(得分:1)