Cloud-ready Micro Java EE 8
Ondro Mihályi | Payara Engineer
| @omihalyi
| @Payara_Fish
Who am I?
• Payara Engineer
• Java EE developer and lecturer
• Conference speaker
• Czech JUG leader
• Member of JCP and Eclipse foundation
• @omihalyi
What’s this all about?
• How lightweight is Java EE, really?
• What’s in Java EE 8 today?
• What could be in Java EE EE4J ??? in the future?
A little (recent) history...
• 12 June 2013: Java EE 7 Released
•Docker was 3 months old
•WildFly was still JBoss AS (and no Swarm)
•Before Spring Boot
•WebSphere Liberty Profile recently released
A little (recent) history...
• 21 September 2017: Java EE 8 Released, 4 years later
•Docker is now the dominant container format
•WildFly have fully product-ised WildFly Swarm
•Spring Boot have been making JARs not WARs for a few years
•IBM released Open Liberty
•Payara entered the fray and released Payara Micro
•Eclipse MicroProfile is established and progressing
What’s in Java EE 8?
• Upgrades
•JAX-RS 2.1
•Servlet 4.0
•Bean Validation 2.0
•JSF 2.3
•CDI 2.0
•JSON-P 1.1
• New
•Java EE Security
•JSON-B
What is Payara Micro?
• Payara Micro is…
•Micro! (~60MB disk size, ~30 MB RAM)
•Dynamically scalable
•Fully embeddable (if you want…)
•Web Profile “plus”
•On Maven Central
<dependency>
<groupId>fish.payara.api</groupId>
<ArtifactId>payara-api</artifactId>
<version>5.0.0-Alpha3</version>
<type>jar</type>
</dependency>
What is Payara Micro?
• Key APIs
• Servlets, JSTL, EL, JSP
• WebSockets
• JSF
• JAX-RS
• EJB Lite
• JTA
• JPA
• JCA (pluggable JMS)
• Bean Validation
• CDI
• Interceptors
• JBatch
• Concurrency
• JCache
• Config
• SOAP, remote EJB
Demo
Demo Scenario
• Stock! Micro Stock!
• Publish StockTicker events
• Listen on different endpoints
Demo Scenario
StockTickerStockTickerStockTicker
Produces Events
StockWeb
Consumes Events
Websocket Push
Demo 1
Creating the Stock Ticker
• What do we need?
• Stock object
• Stock Ticker to set the stock price
• Publisher for CDI event bus
Demo 2
Creating the Stock Web listener
• What do we need?
• Websocket endpoint
• Class to manage Websocket sessions
• Class to listen for Stock events on the CDI event bus
• Both these responsibilities can be in the same class
Integrating Java EE 8 Features
Demo
Making our MicroService generic
• What do we need to change?
• Non-Payara services can’t listen on the CDI event bus
• Solution: REST Server Sent Events (from JAX-RS 2.1)
• External services may not have access to a Stock object (or interface)
• Solution: Serialise our Stock object as JSON using JSON-B 1.0
private JsonObject toJson() {
JsonObjectBuilder objectBuilder = Json.createObjectBuilder()
.add("symbol", symbol)
.add("description", description)
.add("price", price);
return objectBuilder.build();
}
@Override
public String toString() {
return this.toJson().toString();
}
Demo 3 JSON-P manual toJson() method
@JsonbNillable
public class Stock implements Serializable {
private String symbol;
@JsonbTransient
private String description;
@JsonbProperty("RandomPrice")
private double price;
@Override
public String toString() {
JsonbConfig nillableConfig = new JsonbConfig().withNullValues(true);
return JsonbBuilder.create(nillableConfig).toJson(this);
}
}
private void observe() {
source.register((sseEvent)
-> {
this.stock = JsonbBuilder.create().fromJson(sseEvent.readData(), Stock.class);
});
source.open();
}
Demo 3 JSON-B serialisation and deserialisation
Learn more:
http://json-b.net
private void reactiveClient() {
CompletionStage<Response> cs1 = ClientBuilder.newClient()
.target("http://localhost:8080/jax-rs-2-1/books")
.request()
.rx()
.get();
CompletionStage<Response> cs2 = ClientBuilder.newClient()
.target("http://localhost:8080/jax-rs-2-1/magazines")
.request()
.rx()
.get();
cs1.thenCombine(cs2, (r1, r2) ->
r1.readEntity(String.class) + r2.readEntity(String.class))
.thenAccept(System.out::println);
}
Source: https://www.ibm.com/developerworks/library/j-whats-new-in-javaee-8/index.html
Author: Alex Theedom
Demo 4 JAX-RS reactive client API
Demo 4 SSE server endpoint in JAX-RS
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
public void eventOutput(@Context SseEventSink eventSink){
// send a single event
eventSink.send(sse.newEvent(stockTicker.getStock().toString()));
// registers the requester as a consumer of events
sseBroadcaster.register(eventSink);
}
@POST
@Path("broadcast")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void broadcast(@FormParam("event") String event){
// broadcasts the event to all registered consumers
sseBroadcaster.broadcast(sse.newEvent(event));
}
Demo 4
StockTickerStockTickerStockTicker
StockWeb
JAX-RS SSE
Client
Javascript SSE Client
The Future of Java EE…?
Demo 5
Integrating MicroProfile 1.1
<dependency>
<groupId>
org.eclipse.microprofile
</groupId>
<artifactId>
Microprofile-bom
</artifactId>
<version>1.1.0</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
@Inject
@ConfigProperty(name = "stockticker.url")
private String stockTickerURL;
@Inject
@ConfigProperty(name = "stockticker.url")
private Instance<String> tickerURLConfig;
...
String url = tickerURLConfig.get()
Demo 5
Integrating MicroProfile
// Set system property
java -jar -Dstockticker.url=http://localhost:8081/StockTicker-1.0-SNAPSHOT/rest/sse StockWeb/target/StockWeb-
1.0-SNAPSHOT-microbundle.jar
// Use asadmin to set property in Hazelcast cluster
asadmin> set-config-property
--propertyName=stockticker.url
--propertyValue=http://localhost:8080/StockTicker-1.0-SNAPSHOT/rest/sse
--source=cluster
// Use asadmin to set property for specific instances in Hazelcast cluster
asadmin> send-asadmin-command
--targets=‘*‘
--command=set-config-property
--
--propertyName=stockticker.url
--propertyValue=http://localhost:8080/StockTicker-1.0-SNAPSHOT/rest/sse
--source=config
--sourceName=sever-config
Recap
•Java EE is incredibly adaptable to changing needs
• Recent change has been driven by vendors’ implementations
• Eclipse MicroProfile offers interesting ideas for the future
Thank you!

Cloud-ready Micro Java EE 8

  • 1.
    Cloud-ready Micro JavaEE 8 Ondro Mihályi | Payara Engineer | @omihalyi | @Payara_Fish
  • 2.
    Who am I? •Payara Engineer • Java EE developer and lecturer • Conference speaker • Czech JUG leader • Member of JCP and Eclipse foundation • @omihalyi
  • 3.
    What’s this allabout? • How lightweight is Java EE, really? • What’s in Java EE 8 today? • What could be in Java EE EE4J ??? in the future?
  • 4.
    A little (recent)history... • 12 June 2013: Java EE 7 Released •Docker was 3 months old •WildFly was still JBoss AS (and no Swarm) •Before Spring Boot •WebSphere Liberty Profile recently released
  • 5.
    A little (recent)history... • 21 September 2017: Java EE 8 Released, 4 years later •Docker is now the dominant container format •WildFly have fully product-ised WildFly Swarm •Spring Boot have been making JARs not WARs for a few years •IBM released Open Liberty •Payara entered the fray and released Payara Micro •Eclipse MicroProfile is established and progressing
  • 6.
    What’s in JavaEE 8? • Upgrades •JAX-RS 2.1 •Servlet 4.0 •Bean Validation 2.0 •JSF 2.3 •CDI 2.0 •JSON-P 1.1 • New •Java EE Security •JSON-B
  • 7.
    What is PayaraMicro? • Payara Micro is… •Micro! (~60MB disk size, ~30 MB RAM) •Dynamically scalable •Fully embeddable (if you want…) •Web Profile “plus” •On Maven Central <dependency> <groupId>fish.payara.api</groupId> <ArtifactId>payara-api</artifactId> <version>5.0.0-Alpha3</version> <type>jar</type> </dependency>
  • 8.
    What is PayaraMicro? • Key APIs • Servlets, JSTL, EL, JSP • WebSockets • JSF • JAX-RS • EJB Lite • JTA • JPA • JCA (pluggable JMS) • Bean Validation • CDI • Interceptors • JBatch • Concurrency • JCache • Config • SOAP, remote EJB
  • 9.
  • 10.
    Demo Scenario • Stock!Micro Stock! • Publish StockTicker events • Listen on different endpoints
  • 11.
  • 12.
    Demo 1 Creating theStock Ticker • What do we need? • Stock object • Stock Ticker to set the stock price • Publisher for CDI event bus
  • 13.
    Demo 2 Creating theStock Web listener • What do we need? • Websocket endpoint • Class to manage Websocket sessions • Class to listen for Stock events on the CDI event bus • Both these responsibilities can be in the same class
  • 14.
  • 15.
    Demo Making our MicroServicegeneric • What do we need to change? • Non-Payara services can’t listen on the CDI event bus • Solution: REST Server Sent Events (from JAX-RS 2.1) • External services may not have access to a Stock object (or interface) • Solution: Serialise our Stock object as JSON using JSON-B 1.0
  • 16.
    private JsonObject toJson(){ JsonObjectBuilder objectBuilder = Json.createObjectBuilder() .add("symbol", symbol) .add("description", description) .add("price", price); return objectBuilder.build(); } @Override public String toString() { return this.toJson().toString(); } Demo 3 JSON-P manual toJson() method
  • 17.
    @JsonbNillable public class Stockimplements Serializable { private String symbol; @JsonbTransient private String description; @JsonbProperty("RandomPrice") private double price; @Override public String toString() { JsonbConfig nillableConfig = new JsonbConfig().withNullValues(true); return JsonbBuilder.create(nillableConfig).toJson(this); } } private void observe() { source.register((sseEvent) -> { this.stock = JsonbBuilder.create().fromJson(sseEvent.readData(), Stock.class); }); source.open(); } Demo 3 JSON-B serialisation and deserialisation Learn more: http://json-b.net
  • 18.
    private void reactiveClient(){ CompletionStage<Response> cs1 = ClientBuilder.newClient() .target("http://localhost:8080/jax-rs-2-1/books") .request() .rx() .get(); CompletionStage<Response> cs2 = ClientBuilder.newClient() .target("http://localhost:8080/jax-rs-2-1/magazines") .request() .rx() .get(); cs1.thenCombine(cs2, (r1, r2) -> r1.readEntity(String.class) + r2.readEntity(String.class)) .thenAccept(System.out::println); } Source: https://www.ibm.com/developerworks/library/j-whats-new-in-javaee-8/index.html Author: Alex Theedom Demo 4 JAX-RS reactive client API
  • 19.
    Demo 4 SSEserver endpoint in JAX-RS @GET @Produces(MediaType.SERVER_SENT_EVENTS) public void eventOutput(@Context SseEventSink eventSink){ // send a single event eventSink.send(sse.newEvent(stockTicker.getStock().toString())); // registers the requester as a consumer of events sseBroadcaster.register(eventSink); } @POST @Path("broadcast") @Consumes(MediaType.MULTIPART_FORM_DATA) public void broadcast(@FormParam("event") String event){ // broadcasts the event to all registered consumers sseBroadcaster.broadcast(sse.newEvent(event)); }
  • 20.
  • 21.
    The Future ofJava EE…?
  • 22.
    Demo 5 Integrating MicroProfile1.1 <dependency> <groupId> org.eclipse.microprofile </groupId> <artifactId> Microprofile-bom </artifactId> <version>1.1.0</version> <type>pom</type> <scope>provided</scope> </dependency> @Inject @ConfigProperty(name = "stockticker.url") private String stockTickerURL; @Inject @ConfigProperty(name = "stockticker.url") private Instance<String> tickerURLConfig; ... String url = tickerURLConfig.get()
  • 23.
    Demo 5 Integrating MicroProfile //Set system property java -jar -Dstockticker.url=http://localhost:8081/StockTicker-1.0-SNAPSHOT/rest/sse StockWeb/target/StockWeb- 1.0-SNAPSHOT-microbundle.jar // Use asadmin to set property in Hazelcast cluster asadmin> set-config-property --propertyName=stockticker.url --propertyValue=http://localhost:8080/StockTicker-1.0-SNAPSHOT/rest/sse --source=cluster // Use asadmin to set property for specific instances in Hazelcast cluster asadmin> send-asadmin-command --targets=‘*‘ --command=set-config-property -- --propertyName=stockticker.url --propertyValue=http://localhost:8080/StockTicker-1.0-SNAPSHOT/rest/sse --source=config --sourceName=sever-config
  • 24.
    Recap •Java EE isincredibly adaptable to changing needs • Recent change has been driven by vendors’ implementations • Eclipse MicroProfile offers interesting ideas for the future
  • 25.