tweak feed fetching

This commit is contained in:
Jeremie Panzer
2013-03-27 09:37:15 +01:00
parent 3bf414c82b
commit 0d83bd19d0
6 changed files with 69 additions and 71 deletions

View File

@@ -1,14 +1,6 @@
package com.commafeed.backend.feeds;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import javax.ejb.AccessTimeout;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.ejb.Stateless;
import javax.inject.Inject;
import org.apache.http.HttpEntity;
@@ -16,14 +8,16 @@ import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.commafeed.backend.model.Feed;
import com.sun.syndication.io.FeedException;
@Singleton
@Stateless
public class FeedFetcher {
private static Logger log = LoggerFactory.getLogger(FeedFetcher.class);
@@ -31,15 +25,15 @@ public class FeedFetcher {
@Inject
FeedParser parser;
@Asynchronous
@Lock(LockType.READ)
@AccessTimeout(value = 15, unit = TimeUnit.SECONDS)
public Future<Feed> fetch(String feedUrl) {
public Feed fetch(String feedUrl) throws FeedException {
log.debug("Fetching feed {}", feedUrl);
Feed feed = null;
HttpClient httpclient = new DefaultHttpClient();
HttpProtocolParams.setContentCharset(httpclient.getParams(), "UTF-8");
HttpConnectionParams
.setConnectionTimeout(httpclient.getParams(), 15000);
HttpConnectionParams.setSoTimeout(httpclient.getParams(), 15000);
try {
HttpGet httpget = new HttpGet(feedUrl);
@@ -48,11 +42,11 @@ public class FeedFetcher {
String content = EntityUtils.toString(entity, "UTF-8");
feed = parser.parse(feedUrl, content);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
throw new FeedException(e.getMessage(), e);
} finally {
httpclient.getConnectionManager().shutdown();
}
return new AsyncResult<Feed>(feed);
return feed;
}
}