Andreas
Andreas

Reputation: 303

How to set a HTTP timeout when retrieving RDF from a URI?

I am resolving RDF URIs in a Servlet using Jena:

final Model rdfModel = ModelFactory.createDefaultModel();
rdfModel.read(resource);

Is there a possibility to set Http Connect and Socket timeouts in Jena?

Or is it the only option to do handle the http connection 'manually' using Apache HttpClient?

final HttpClient httpclient = new DefaultHttpClient();
final HttpParams params = httpclient.getParams();
params.setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 1000);
params.setParameter(HttpConnectionParams.SO_TIMEOUT, 5000);
...

Upvotes: 2

Views: 271

Answers (2)

Andreas
Andreas

Reputation: 303

Here's the code based on @rob-hall's hint:

final Model rdfModel = ModelFactory.createDefaultModel();
final HttpClient httpclient = new DefaultHttpClient();
final HttpParams params = httpclient.getParams();
params.setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 1000);
params.setParameter(HttpConnectionParams.SO_TIMEOUT, 5000);
HttpOp.setDefaultHttpClient(httpclient);
rdfModel.read(resource);

Upvotes: 2

Rob Hall
Rob Hall

Reputation: 2823

From the design of the API, it appears that you should be able to use HttpOp#setDefaultHttpClient() to modify the default HttpClient utilized within Jena.

HttpOp#execHttpGet(String,String) is delegated to by LocatorURL#open(String), which is called by the StreamManager used by RDFDataMgr. Elsewhere, HttpOp#ensureClient(...) substitutes in the default client, if it is present.

Upvotes: 1

Related Questions