Skip to content

Commit

Permalink
Merge pull request #81 from oracle-quickstart/events-to-micronaut-4
Browse files Browse the repository at this point in the history
Events to micronaut version 4.3.4
  • Loading branch information
ChaimaaeROUAI authored Jun 7, 2024
2 parents 5596d15 + 1bab23e commit feb2dce
Show file tree
Hide file tree
Showing 20 changed files with 671 additions and 688 deletions.
167 changes: 94 additions & 73 deletions src/events/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,73 +1,94 @@
plugins {
id("com.github.johnrengelman.shadow") version "7.1.2"
id("io.micronaut.application") version "3.6.2"
}

group = "micronaut.mushop"

repositories {
mavenCentral()
}

micronaut {
runtime("netty")
testRuntime("junit5")
processing {
incremental(true)
annotations("events.*")
}
}

dependencies {
annotationProcessor("io.micronaut.openapi:micronaut-openapi")

implementation("io.micronaut:micronaut-http-client")
implementation("io.micronaut:micronaut-runtime")
implementation("io.micronaut:micronaut-validation")

// Metrics
implementation("io.micronaut:micronaut-management")
implementation("io.micronaut.micrometer:micronaut-micrometer-core")
implementation("io.micronaut.micrometer:micronaut-micrometer-registry-prometheus")

// Tracing
implementation("io.micronaut.tracing:micronaut-tracing-core")
implementation("io.micronaut.tracing:micronaut-tracing-zipkin")

// Streaming
implementation("io.micronaut.kafka:micronaut-kafka")

implementation("io.swagger.core.v3:swagger-annotations")

runtimeOnly("ch.qos.logback:logback-classic")

testImplementation(project(":tck"))
testImplementation("org.testcontainers:junit-jupiter")
}

application {
mainClass.set("events.Application")
}

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

if (javaBaseImage == "graalvm") {
dockerfile {
baseImage = "phx.ocir.io/oraclelabs/micronaut-showcase/mushop/base/graalvm-ce:java11-21.1.0"
}
}

dockerBuild {
images = ["phx.ocir.io/oraclelabs/micronaut-showcase/mushop/$project.parent.name-$project.name-${javaBaseImage}:$project.version"]
}

dockerBuildNative {
images = ["phx.ocir.io/oraclelabs/micronaut-showcase/mushop/${project.parent.name}-${project.name}-native:$project.version"]
}

dockerfileNative {
instruction "RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.11/main' >> /etc/apk/repositories && apk update && apk add 'zlib<1.2.12'"
}
plugins {
id("com.github.johnrengelman.shadow") version "8.1.1"
id("io.micronaut.application") version "4.3.4"
id("io.micronaut.aot") version "4.3.4"
id("io.micronaut.test-resources") version "4.3.4"
}

group = "micronaut.mushop"

repositories {
mavenCentral()
}

micronaut {
runtime("netty")
testRuntime("junit5")
processing {
incremental(true)
annotations("events.*")
}
aot {
optimizeServiceLoading = false
convertYamlToJava = false
precomputeOperations = true
cacheEnvironment = true
optimizeClassLoading = true
deduceEnvironment = true
optimizeNetty = true
}
}

dependencies {
annotationProcessor("io.micronaut.openapi:micronaut-openapi")
annotationProcessor("io.micronaut:micronaut-http-validation")
annotationProcessor("io.micronaut.tracing:micronaut-tracing-opentelemetry-annotation")
annotationProcessor("io.micronaut.serde:micronaut-serde-processor")
annotationProcessor("io.micronaut.validation:micronaut-validation-processor")
annotationProcessor("io.micronaut.micrometer:micronaut-micrometer-annotation")

implementation("io.micronaut:micronaut-http-client")
implementation("io.micronaut.serde:micronaut-serde-jackson")
implementation("io.micronaut.validation:micronaut-validation")
implementation("io.micronaut:micronaut-retry")
implementation("jakarta.validation:jakarta.validation-api")
// Metrics
implementation("io.micronaut:micronaut-management")
implementation("io.micronaut.micrometer:micronaut-micrometer-core")
implementation("io.micronaut.micrometer:micronaut-micrometer-registry-prometheus")

// Tracing
implementation("io.micronaut.tracing:micronaut-tracing-core")
implementation("io.micronaut.tracing:micronaut-tracing-opentelemetry-http")
implementation("io.opentelemetry:opentelemetry-exporter-zipkin")

// Streaming
implementation("io.micronaut.kafka:micronaut-kafka")

implementation("io.swagger.core.v3:swagger-annotations")

runtimeOnly("ch.qos.logback:logback-classic")

compileOnly("io.micronaut.openapi:micronaut-openapi-annotations")

runtimeOnly("org.yaml:snakeyaml")
testImplementation(project(":tck"))
testImplementation("io.micronaut:micronaut-http-client")
}

application {
mainClass.set("events.Application")
}

java {
sourceCompatibility = JavaVersion.toVersion("21")
targetCompatibility = JavaVersion.toVersion("21")
}

if (javaBaseImage == "graalvm") {
dockerfile {
baseImage = "ghcr.io/graalvm/native-image-community:21"
}
}

dockerBuild {
images = ["phx.ocir.io/oraclelabs/micronaut-showcase/mushop/$project.parent.name-$project.name-${javaBaseImage}:$project.version"]
}

dockerBuildNative {
images = ["phx.ocir.io/oraclelabs/micronaut-showcase/mushop/${project.parent.name}-${project.name}-native:$project.version"]
}

dockerfileNative {
instruction "RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.11/main' >> /etc/apk/repositories && apk update && apk add 'zlib<1.2.12'"
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
package events.controllers;

import events.model.Event;
import events.model.EventsReceived;
import events.service.EventService;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Post;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;

@Controller("/events")
class EventController {

private final EventService eventService;

EventController(EventService eventService) {
this.eventService = eventService;
}

/**
* Submit events endpoint.
*
* @param source The source of the event
* @param track The event to track
* @param events The events to post
* @return Whether the events were received or not
*/
@Post(processes = MediaType.APPLICATION_JSON)
EventsReceived postEvents(
@NotBlank String source,
@NotBlank String track,
@Min(1) Event...events) {
return eventService.postEvents(source, track, events);
}
}
package events.controllers;

import events.model.Event;
import events.model.EventsReceived;
import events.service.EventService;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Post;
import jakarta.validation.constraints.NotBlank;

@Controller("/events")
class EventController {

private final EventService eventService;

EventController(EventService eventService) {
this.eventService = eventService;
}

/**
* Submit events endpoint.
*
* @param source The source of the event
* @param track The event to track
* @param events The events to post
* @return Whether the events were received or not
*/
@Post(processes = MediaType.APPLICATION_JSON)
EventsReceived postEvents(
@NotBlank String source,
@NotBlank String track,
Event...events) {
return eventService.postEvents(source, track, events);
}
}
45 changes: 15 additions & 30 deletions src/events/app/src/main/java/events/model/Event.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
package events.model;

import io.micronaut.core.annotation.Introspected;

import java.util.Collections;
import java.util.Map;
import java.util.Objects;

/**
* The event to track.
*/
@Introspected
public class Event {

private final String type;
private final Map<String, String> detail;

public Event(String type, Map<String, String> detail) {
this.type = Objects.requireNonNull(type, "Type cannot be null");
this.detail = detail != null ? detail : Collections.emptyMap();
}

public String getType() {
return type;
}

public Map<String, String> getDetail() {
return detail;
}
}
package events.model;

import io.micronaut.serde.annotation.Serdeable;


import java.util.Map;


/**
* The event to track.
*/
@Serdeable
public record Event(String type,Map<String, String> detail){
}

83 changes: 35 additions & 48 deletions src/events/app/src/main/java/events/model/EventRecord.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,35 @@
package events.model;

import io.micronaut.core.annotation.Creator;
import io.micronaut.core.annotation.Introspected;

import java.time.Instant;
import java.util.Map;
import java.util.Objects;

/**
* Event record.
*/
@Introspected
public class EventRecord extends Event {

private final String source;
private final String track;
private final Instant time;

public EventRecord(String source,
String track,
Event event) {
this(source, track, Objects.requireNonNull(event, "Event cannot be null").getType(), event.getDetail());
}

@Creator
public EventRecord(String source,
String track,
String type,
Map<String, String> detail) {
super(type, detail);
this.source = source;
this.track = track;
time = Instant.now();
}

public String getSource() {
return source;
}

public String getTrack() {
return track;
}

public Instant getTime() {
return time;
}
}
package events.model;

import io.micronaut.core.annotation.Creator;
import io.micronaut.serde.annotation.Serdeable;

import java.time.Instant;
import java.util.Map;
import java.util.Objects;

/**
* Event record.
*/


@Serdeable
public record EventRecord(String source, String track, Instant time, Event event) {

@Creator
public EventRecord(String source, String track, Event event) {
this(source, track, Instant.now(), Objects.requireNonNull(event, "Event cannot be null"));
}


public EventRecord(String source, String track, String type, Map<String, String> detail) {
this(source, track, Instant.now(), new Event(type, detail));
}

public String type() {
return event.type();
}

public Map<String, String> detail() {
return event.detail();
}
}
Loading

0 comments on commit feb2dce

Please sign in to comment.