Sunday, May 5, 2024
HomeJavaEventArc with CloudRun - Java Code Geeks

EventArc with CloudRun – Java Code Geeks


 Google Cloud EventArc supplies a easy method to act on occasions generated by a wide range of Google Cloud Companies.

Think about an instance.

When a Cloud Construct set off is run, I wish to be notified of this occasion –

Eventarc makes this integration easy

The internals of the way it does that is documented effectively. Based mostly on the supply, the occasion is both obtained by EventArc straight or through Cloud Audit Logs. EventArc then dispatches the occasion to the vacation spot through one other pub/sub matter that it maintains. 

These underlying particulars are effectively hidden although, in order a developer involved solely about consuming the Construct Occasions, I can deal with the payload of the occasion and ignore the mechanics of how EventArc will get the message from the supply to my service.

Pattern EventArc listener

Since I’m occupied with simply the occasions and its payload, all I’ve to do from an utility perspective is to show an HTTP endpoint responding to a POST message with the Content material being the occasion that I’m involved about. Right here is such an endpoint in Java utilizing Spring Boot because the framework:

@RestController
public class EventArcMessageController {
    ...
    
    @RequestMapping(worth = "/", technique = RequestMethod.POST)
    public Mono<ResponseEntity<JsonNode>> receiveMessage(
            @RequestBody JsonNode physique, @RequestHeader Map<String, String> headers) {
        LOGGER.data("Acquired message: {}, headers: {}", JsonUtils.writeValueAsString(physique, objectMapper), headers);
        return Mono.simply(ResponseEntity.okay(physique));
    }
}

The complete pattern is offered right here

On this particular occasion all of the endpoint is doing is to log the message and the headers accompanying the message. So long as the response code is 200, EventArc would think about the dealing with to achieve success. 

EventArc helps over 130 Google Cloud Companies, so consuming myriad occasions from a bunch of providers is simple.

EventArc Set off

As soon as I’ve the EventArc deployed as a Cloud Run service, to combine this with the Cloud Construct Occasions in EventArc, all I’ve to do is to create an EventArc set off. This may be executed utilizing the UI:

or utilizing command line:

gcloud eventarc triggers replace cloud-build-trigger 
--location=us-west1 
--destination-run-service=cloudbuild-eventarc-sample 
--destination-run-region=us-west1 
--destination-run-path="/" 
--event-filters="kind=google.cloud.audit.log.v1.written" 
--event-filters="serviceName=cloudbuild.googleapis.com" 
--event-filters="methodName=google.devtools.cloudbuild.v1.CloudBuild.CreateBuild"

and that’s it, EventArc handles all of the underlying particulars of the combination.

Conclusion

I’ve the complete java code out there right here which exhibits what a full code would seem like. EventArc makes it quite simple to combine occasions from Google Cloud Companies with customized functions.

Printed on Java Code Geeks with permission by Biju Kunjummen, accomplice at our JCG program. See the unique article right here: EventArc with CloudRun

Opinions expressed by Java Code Geeks contributors are their very own.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments