2012-03-22 36 views
0

自从我使用Java特别是异常以来,它已经有一段时间了。我正在将ektorp couchdb集成到我正在处理的内容中。不过,我遇到内容消耗异常。ektorp couchdb IllegalStateException内容消耗

有问题的程序使用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(doc)或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 lib以及当前?

回答

0

CouchDbConnector是线程安全的,因此您不需要为每个操作重新创建它。

我从来没有遇到过你的问题,你的用例很简单,保存基本文档应该没有问题。

验证httpclient-4.1.1或更高版本是否在类路径中。

+0

感谢您的评论。我会仔细检查下班回家的时间,但我非常肯定它的httpclient-4.1.4 plus核心和其他公共软件包。 – IainS 2012-03-23 09:38:31