73

Implementing Connect- and Read Timeouts with JAX-RS 2.1 Client and Java EE 8 : A...

 6 years ago
source link: http://adambien.blog/roller/abien/entry/implementing_connect_and_read_timeouts
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Implementing Connect- and Read Timeouts with JAX-RS 2.1 Client and Java EE 8

Java EE 8 with JAX-RS 2.1 comes with configuration for read and connect timeouts (in Java EE 7 you had to use vendor-specific properties):


import java.util.concurrent.TimeUnit;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class JAXRSClientTimeoutTest {

    @Rule
    public ExpectedException expected = ExpectedException.none();

    @Test
    public void connectTimeout() {
        expected.expect(ProcessingException.class);
        Client client = ClientBuilder.
                newBuilder().
                connectTimeout(1, TimeUnit.MILLISECONDS).
                build();
        //no one listens here
        client.target("http://127.0.0.1").
                request().
                get();
    }

    @Test
    public void readTimeout() {
        expected.expect(ProcessingException.class);
        Client client = ClientBuilder.
                newBuilder().
                readTimeout(1, TimeUnit.MILLISECONDS).
                build();
        //airhacks.com is too slow to answer in 1ms
        client.target("http://airhacks.com").
                request().
                get();
    }
}

See you at Java EE 8 on Java 9, at Munich Airport, Terminal 2

Posted at 08:19AM Nov 25, 2017 by Adam Bien, Comments[0]  | Views/Hits: 10378

live, virtual workshops: NEW Serverless Event-Driven Architectures with Serverless Java on AWS, December 8th, 2022 and Serverless Persistence for Serverless Java on AWS, December 15th, 2022 are open for registration. Also via: meetup.com/airhacks and airhacks.eventbrite.com

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Comments:
Post a Comment:
  • Name:
  • E-Mail:
  • URL:
  • Notify me by email of new comments
  • Remember Information?
  • Your Comment:

  • HTML Syntax: NOT allowed
  • Please answer this simple math question

    1 + 76 =


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK