Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 714
rangalo Avatar asked Oct 21 '22 10:10

rangalo


1 Answers

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.

like image 71
Alexander Rühl Avatar answered Oct 23 '22 02:10

Alexander Rühl