Search in sources :

Example 1 with TypeReference

use of com.azure.core.util.serializer.TypeReference in project java-design-patterns by iluwatar.

the class MessageHandlerUtility method readFromPersistantStorage.

/**
 * Read message from blob storage.
 * @param messageReference represents MessageReference
 * @param logger represents Logger
 * @return Message
 */
public Message<T> readFromPersistantStorage(MessageReference messageReference, Logger logger) {
    Message<T> message = null;
    try {
        // Get container name from message reference
        String containerName = messageReference.getDataLocation();
        // Get blob name from message reference
        String blobName = messageReference.getDataFileName();
        // Get container client
        BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
        // Get a reference to a blob
        BlobClient blobClient = containerClient.getBlobClient(blobName);
        // download the blob
        message = blobClient.downloadContent().toObject(new TypeReference<Message<T>>() {
        });
    } catch (Exception e) {
        logger.info(e.getMessage());
    }
    return message;
}
Also used : BlobContainerClient(com.azure.storage.blob.BlobContainerClient) BlobClient(com.azure.storage.blob.BlobClient) TypeReference(com.azure.core.util.serializer.TypeReference)

Example 2 with TypeReference

use of com.azure.core.util.serializer.TypeReference in project java-design-patterns by iluwatar.

the class UsageCostProcessorFunction method run.

/**
 * Azure function which gets triggered when event grid event send event to it.
 * After receiving event, it read input file from blob storage, calculate call cost details.
 * It creates new message with cost details and drop message to blob storage.
 * @param request represents HttpRequestMessage
 * @param context represents ExecutionContext
 * @return HttpResponseMessage
 */
@FunctionName("UsageCostProcessorFunction")
public HttpResponseMessage run(@HttpTrigger(name = "req", methods = { HttpMethod.GET, HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, final ExecutionContext context) {
    try {
        var eventGridEvents = EventGridEvent.fromString(request.getBody().get());
        for (var 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")) {
                // Get message header and reference
                var messageReference = eventGridEvent.getData().toObject(MessageReference.class);
                // Read message from persistent storage
                var message = this.messageHandlerUtilityForUsageDetail.readFromPersistantStorage(messageReference, context.getLogger());
                // Get Data and generate cost details
                List<UsageDetail> usageDetailsList = BinaryData.fromObject(message.getMessageBody().getData()).toObject(new TypeReference<>() {
                });
                var usageCostDetailsList = calculateUsageCostDetails(usageDetailsList);
                // Create message body
                var newMessageBody = new MessageBody<UsageCostDetail>();
                newMessageBody.setData(usageCostDetailsList);
                // Create message header
                var newMessageReference = new MessageReference("callusageapp", eventGridEvent.getId() + "/output.json");
                var newMessageHeader = new MessageHeader();
                newMessageHeader.setId(eventGridEvent.getId());
                newMessageHeader.setSubject("UsageCostProcessor");
                newMessageHeader.setTopic("");
                newMessageHeader.setEventType("UsageCostDetail");
                newMessageHeader.setEventTime(OffsetDateTime.now().toString());
                newMessageHeader.setData(newMessageReference);
                newMessageHeader.setDataVersion("v1.0");
                // Create entire message
                var newMessage = new Message<UsageCostDetail>();
                newMessage.setMessageHeader(newMessageHeader);
                newMessage.setMessageBody(newMessageBody);
                // Drop data to persistent storage
                this.messageHandlerUtilityForUsageCostDetail.dropToPersistantStorage(newMessage, context.getLogger());
                context.getLogger().info("Message is dropped successfully");
                return request.createResponseBuilder(HttpStatus.OK).body("Message is dropped successfully").build();
            }
        }
    } catch (Exception e) {
        context.getLogger().warning(e.getMessage());
    }
    return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body(null).build();
}
Also used : SubscriptionValidationResponse(com.azure.messaging.eventgrid.systemevents.SubscriptionValidationResponse) SubscriptionValidationEventData(com.azure.messaging.eventgrid.systemevents.SubscriptionValidationEventData) ArrayList(java.util.ArrayList) List(java.util.List) TypeReference(com.azure.core.util.serializer.TypeReference) MessageHeader(com.iluwatar.claimcheckpattern.domain.MessageHeader) MessageReference(com.iluwatar.claimcheckpattern.domain.MessageReference) UsageCostDetail(com.iluwatar.claimcheckpattern.domain.UsageCostDetail) FunctionName(com.microsoft.azure.functions.annotation.FunctionName)

Aggregations

TypeReference (com.azure.core.util.serializer.TypeReference)2 SubscriptionValidationEventData (com.azure.messaging.eventgrid.systemevents.SubscriptionValidationEventData)1 SubscriptionValidationResponse (com.azure.messaging.eventgrid.systemevents.SubscriptionValidationResponse)1 BlobClient (com.azure.storage.blob.BlobClient)1 BlobContainerClient (com.azure.storage.blob.BlobContainerClient)1 MessageHeader (com.iluwatar.claimcheckpattern.domain.MessageHeader)1 MessageReference (com.iluwatar.claimcheckpattern.domain.MessageReference)1 UsageCostDetail (com.iluwatar.claimcheckpattern.domain.UsageCostDetail)1 FunctionName (com.microsoft.azure.functions.annotation.FunctionName)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1