Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to adapt MicroProfile API for generic Web applications

How to adapt MicroProfile API for generic Web applications

HASUNUMA Kenji

November 23, 2019
Tweet

More Decks by HASUNUMA Kenji

Other Decks in Programming

Transcript

  1. How to adapt MicroProfile API for generic Web applications JJUG

    CCC 2019 Fall #jjug_ccc #ccc_i5 HASUNUMA Kenji @khasunuma
  2. MicroProfile is ... •Java API for Microservices •Based on Java

    EE Technology •Community Driven HASUNUMA Kenji @khasunuma
  3. Why and How to use 
 MP Config API? As

    my experience about systems integration HASUNUMA Kenji @khasunuma
  4. Application Development Different Configuration • Settings • Databases • OS/JVM

    patches • Machine resources • Networks etc. HASUNUMA Kenji @khasunuma
  5. Default Config Sources • System Properties (Ordinal: 400) • Environment

    Variables (Ordinal: 300) • microprofile-config.properties (Ordinal: 100) HASUNUMA Kenji @khasunuma
  6. e.g. Inject a Config Property HASUNUMA Kenji @khasunuma @Path(“Hello”) @RequestScoped

    public class HelloResource { @Inject private Config config; @Inject @ConfigProperty(name = “service.url”) private String url; // snip; continue the next }
  7. e.g. Using a Config Property HASUNUMA Kenji @khasunuma @Path(“Hello”) @RequestScoped

    public class HelloResource { // snip; url is assigned by Config API (prev) @GET public String sayHello() { Client client = ClientBulder.newClient(); String userName = client.target(url)
 .queryParam(“id”, 63)
 .request(“text/plain”).get(String.class); return “Hello, ” + username; } }
  8. Wrap Up: MP Config API • Divides configurations from code

    so that applications can run any environments • Provides the same way to obtain the settings • Very versatile API – suitable for most kind of applications; see also JSR 382 (Withdrawn) HASUNUMA Kenji @khasunuma
  9. Why and How to use 
 MP Rest Client API?

    As my experience about systems integration HASUNUMA Kenji @khasunuma
  10. Using JAX-RS Client HASUNUMA Kenji @khasunuma @Path(“hello”) @RequestScoped public class

    HelloResource { // url is assigned by anyway @GET public String sayHello() { Client client = ClientBulder.newClient(); String userName = client.target(url)
 .queryParam(“id”, 63)
 .request(“text/plain”).get(String.class); return “Hello, ” + username; } }
  11. Why needs MP Rest Client? • JAX-RS Client is very

    flexible, but... • Not Type-safe • Often complex (especially Async) • Type-safe and simple API is required HASUNUMA Kenji @khasunuma
  12. Using MP Rest Client (1/3) HASUNUMA Kenji @khasunuma /* REST

    Interface */ @RegisterRestClient @ApplicationScoped public interface UserService { // method to call API // (works such as a proxy) @GET @Produces(“text/plain”) String getName(@QueryParam(“id”) int id); }
  13. Using MP Rest Client (2/3) HASUNUMA Kenji @khasunuma @Path(“hello”) @RequestScoped

    public class HelloResource { // Inject the REST Interface @Inject @RestClient
 private UserService userService; @GET public String sayHello() { return “Hello, ” + userSerivce.getName(63); } }
  14. Using MP Rest Client (3/3) How to obtain the endpoint

    URL: • Using MP Config API; • Key: <ClassName>/mp-rest/url
 (e.g. UserService/mp-rest/url) • Value: the endpoint URL
 (e.g. http://cruiser:8080/app/user) HASUNUMA Kenji @khasunuma
  15. Wrap Up: MP Rest Client API • Rich wrapper of

    JAX-RS Client • Not alternative way, but like a proxy • Handles HTTP request/response such as invoking Java methods (Type-safe) • Integration with CDI, MP Config, etc. HASUNUMA Kenji @khasunuma