rangalo
rangalo

Reputation: 5606

Best way to consume http resource from EJB or Java EE webapp

I have a java ee 7 app with jsf2, ejb3.1 , jpa etc. running in a Glassfish4 container.

At the submission of a facelet, I want to call an external http resource (most probably using apache http client). The response is going to be text response, which I will need to parse and then do some db stuff afterwards as well.

My First idea is to call it from an ejb, but as the clint may spawn threads, it is not a good idea. I have also heard about ways to restrict the httpClient in such a way that it doesn't spawn threads e.g. use HttpConnectionManager, don't use timeouts etc. But then I think I would loose some performance.

What about an asynchronous ejb method which can eventually use / trigger the httpClient ?

What will be the best way to achieve this?

What should I do to manage the connections ?

Upvotes: 5

Views: 1662

Answers (1)

Alexander Rühl
Alexander Rühl

Reputation: 6939

Why not using a JAX-RS client as in this simple example:

 Client client = ClientBuilder.newClient();
 String content = client.target("http://www.google.de")
      .request(MediaType.TEXT_HTML)
      .get(String.class);

More informations in the Java EE 7 tutorial.

Upvotes: 3

Related Questions