我已经有一段时间没有使用Java了,尤其是异常。我正在将ektorp couchdb集成添加到我正在开发的项目中。然而,我遇到了内容消费异常。
这个程序使用twitter4j,我正在获取状态并将它们写入到couchdb实例中。
public void putTweet(Status status)
{
Map<String, Object> newTweetDoc = new HashMap<String, Object>();
String docname = status.getUser().getName() + " "
+ status.getCreatedAt().toString();
newTweetDoc.put("_id", docname);
newTweetDoc.put("User", status.getUser().getName());
newTweetDoc.put("Contents", status.getText());
newTweetDoc.put("Created", status.getCreatedAt().toString());
newTweetDoc.put("RetweetCount", status.getRetweetCount());
UserMentionEntity[] mentions = status.getUserMentionEntities();
Map<String, HashMap<String, String>> formattedMentions = formatMentions(mentions);
newTweetDoc.put("Mentions", formattedMentions);
db.addToBulkBuffer(newTweetDoc);
}起初,我也尝试了db.create(newTweetDoc)。每次尝试时都需要重新创建couchdbConnector吗?
db为全局CouchDbConnector: public CouchDbConnector db = null;
public CouchTwitter()
{
//create the db connection etc
}导致错误的是db.create(文档)或flushBulkBuffer。下面是堆栈跟踪:
Exception in thread "main" java.lang.IllegalStateException: Content has been consumed
at org.apache.http.entity.BasicHttpEntity.getContent(BasicHttpEntity.java:84)
at org.apache.http.conn.BasicManagedEntity.getContent(BasicManagedEntity.java:88)
at org.ektorp.http.StdHttpResponse.releaseConnection(StdHttpResponse.java:82)
at org.ektorp.http.RestTemplate.handleResponse(RestTemplate.java:111)
at org.ektorp.http.RestTemplate.post(RestTemplate.java:66)
at org.ektorp.impl.StdCouchDbConnector.executeBulk(StdCouchDbConnector.java:638)
at org.ektorp.impl.StdCouchDbConnector.executeBulk(StdCouchDbConnector.java:596)
at org.ektorp.impl.StdCouchDbConnector.flushBulkBuffer(StdCouchDbConnector.java:617)我在上面看到了两个独立的实体类都调用了.getContent(),我最近一直在玩弄我的被引用的库,有没有可能它调用了一个旧的apache Http库以及当前的?
发布于 2012-03-23 16:35:07
CouchDbConnector是线程安全的,所以你不需要为每个操作重新创建它。
我从来没有遇到过你的问题,你的用例非常简单,保存一个基本的文档应该不会有任何问题。
验证httpclient-4.1.1或更高版本是否在类路径中。
https://stackoverflow.com/questions/9831628
复制相似问题