Slide 1

Slide 1 text

How to adapt MicroProfile API for generic Web applications JJUG CCC 2019 Fall #jjug_ccc #ccc_i5 HASUNUMA Kenji @khasunuma

Slide 2

Slide 2 text

What’s MicroProfile? Not only for Microservices, but also for generic Web applications HASUNUMA Kenji @khasunuma

Slide 3

Slide 3 text

MicroProfile is ... •Java API for Microservices •Based on Java EE Technology •Community Driven HASUNUMA Kenji @khasunuma

Slide 4

Slide 4 text

MicroProfile (MP) 3.2 HASUNUMA Kenji @khasunuma

Slide 5

Slide 5 text

MP 3.2 for General Purpose HASUNUMA Kenji @khasunuma

Slide 6

Slide 6 text

Why and How to use 
 MP Config API? As my experience about systems integration HASUNUMA Kenji @khasunuma

Slide 7

Slide 7 text

Application Development Different Configuration • Settings • Databases • OS/JVM patches • Machine resources • Networks etc. HASUNUMA Kenji @khasunuma

Slide 8

Slide 8 text

System Development (Ideal) HASUNUMA Kenji @khasunuma

Slide 9

Slide 9 text

System Development (Actual) HASUNUMA Kenji @khasunuma

Slide 10

Slide 10 text

Obtaining Configurations HASUNUMA Kenji @khasunuma

Slide 11

Slide 11 text

Using MP Config API HASUNUMA Kenji @khasunuma

Slide 12

Slide 12 text

Default Config Sources • System Properties (Ordinal: 400) • Environment Variables (Ordinal: 300) • microprofile-config.properties (Ordinal: 100) HASUNUMA Kenji @khasunuma

Slide 13

Slide 13 text

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 }

Slide 14

Slide 14 text

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; } }

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Why and How to use 
 MP Rest Client API? As my experience about systems integration HASUNUMA Kenji @khasunuma

Slide 17

Slide 17 text

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; } }

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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); }

Slide 20

Slide 20 text

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); } }

Slide 21

Slide 21 text

Using MP Rest Client (3/3) How to obtain the endpoint URL: • Using MP Config API; • Key: /mp-rest/url
 (e.g. UserService/mp-rest/url) • Value: the endpoint URL
 (e.g. http://cruiser:8080/app/user) HASUNUMA Kenji @khasunuma

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

HASUNUMA Kenji @khasunuma