Search in sources :

Example 1 with EventGridEvent

use of com.azure.messaging.eventgrid.EventGridEvent in project java-design-patterns by iluwatar.

the class UsageDetailPublisherFunction method run.

/**
 * Azure function which create message, drop it in persistent storage
 * and publish the event to Event Grid topic.
 * @param request represents HttpRequestMessage
 * @param context represents ExecutionContext
 * @return HttpResponseMessage
 */
@FunctionName("UsageDetailPublisherFunction")
public HttpResponseMessage run(@HttpTrigger(name = "req", methods = { HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, final ExecutionContext context) {
    try {
        var eventGridEvents = EventGridEvent.fromString(request.getBody().get());
        for (EventGridEvent eventGridEvent : eventGridEvents) {
            // Handle system events
            if (eventGridEvent.getEventType().equals("Microsoft.EventGrid.SubscriptionValidationEvent")) {
                SubscriptionValidationEventData subscriptionValidationEventData = eventGridEvent.getData().toObject(SubscriptionValidationEventData.class);
                // Handle the subscription validation event
                var responseData = new SubscriptionValidationResponse();
                responseData.setValidationResponse(subscriptionValidationEventData.getValidationCode());
                return request.createResponseBuilder(HttpStatus.OK).body(responseData).build();
            } else if (eventGridEvent.getEventType().equals("UsageDetail")) {
                // Create message body
                var messageBody = new MessageBody<UsageDetail>();
                var usageDetailsList = new ArrayList<UsageDetail>();
                var random = new Random();
                for (int i = 0; i < 51; i++) {
                    var usageDetail = new UsageDetail();
                    usageDetail.setUserId("userId" + i);
                    usageDetail.setData(random.nextInt(500));
                    usageDetail.setDuration(random.nextInt(500));
                    usageDetailsList.add(usageDetail);
                }
                messageBody.setData(usageDetailsList);
                // Create message header
                var messageHeader = new MessageHeader();
                messageHeader.setId(UUID.randomUUID().toString());
                messageHeader.setSubject("UsageDetailPublisher");
                messageHeader.setTopic("usagecostprocessorfunction-topic");
                messageHeader.setEventType("UsageDetail");
                messageHeader.setEventTime(OffsetDateTime.now().toString());
                var messageReference = new MessageReference("callusageapp", messageHeader.getId() + "/input.json");
                messageHeader.setData(messageReference);
                messageHeader.setDataVersion("v1.0");
                // Create entire message
                var message = new Message<UsageDetail>();
                message.setMessageHeader(messageHeader);
                message.setMessageBody(messageBody);
                // Drop data to persistent storage
                this.messageHandlerUtility.dropToPersistantStorage(message, context.getLogger());
                // Publish event to event grid topic
                eventHandlerUtility.publishEvent(messageHeader, context.getLogger());
                context.getLogger().info("Message is dropped and event is published successfully");
                return request.createResponseBuilder(HttpStatus.OK).body(message).build();
            }
        }
    } catch (Exception e) {
        context.getLogger().warning(e.getMessage());
    }
    return request.createResponseBuilder(HttpStatus.OK).body(null).build();
}
Also used : SubscriptionValidationResponse(com.azure.messaging.eventgrid.systemevents.SubscriptionValidationResponse) Random(java.util.Random) SubscriptionValidationEventData(com.azure.messaging.eventgrid.systemevents.SubscriptionValidationEventData) UsageDetail(com.iluwatar.claimcheckpattern.domain.UsageDetail) MessageHeader(com.iluwatar.claimcheckpattern.domain.MessageHeader) MessageReference(com.iluwatar.claimcheckpattern.domain.MessageReference) EventGridEvent(com.azure.messaging.eventgrid.EventGridEvent) FunctionName(com.microsoft.azure.functions.annotation.FunctionName)

Example 2 with EventGridEvent

use of com.azure.messaging.eventgrid.EventGridEvent in project DataSpaceConnector by eclipse-dataspaceconnector.

the class AzureEventGridPublisher method sendEvent.

private Mono<Void> sendEvent(String what, String where, Object payload) {
    BinaryData data = BinaryData.fromObject(payload);
    var evt = new EventGridEvent(what, where, data, "0.1");
    return client.sendEvent(evt);
}
Also used : BinaryData(com.azure.core.util.BinaryData) EventGridEvent(com.azure.messaging.eventgrid.EventGridEvent)

Aggregations

EventGridEvent (com.azure.messaging.eventgrid.EventGridEvent)2 BinaryData (com.azure.core.util.BinaryData)1 SubscriptionValidationEventData (com.azure.messaging.eventgrid.systemevents.SubscriptionValidationEventData)1 SubscriptionValidationResponse (com.azure.messaging.eventgrid.systemevents.SubscriptionValidationResponse)1 MessageHeader (com.iluwatar.claimcheckpattern.domain.MessageHeader)1 MessageReference (com.iluwatar.claimcheckpattern.domain.MessageReference)1 UsageDetail (com.iluwatar.claimcheckpattern.domain.UsageDetail)1 FunctionName (com.microsoft.azure.functions.annotation.FunctionName)1 Random (java.util.Random)1