io.curly.advisor.integration.event.CreatedReviewHandler.java Source code

Java tutorial

Introduction

Here is the source code for io.curly.advisor.integration.event.CreatedReviewHandler.java

Source

/*
 *        Copyright 2015 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package io.curly.advisor.integration.event;

import io.curly.advisor.integration.service.EventEmitter;
import io.curly.advisor.model.Review;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * @author Joao Pedro Evangelista
 */
@Slf4j
@Component
public class CreatedReviewHandler implements ApplicationListener<CreatedReview> {

    private final EventEmitter<Review> reviewEventEmitter;

    @Autowired
    public CreatedReviewHandler(EventEmitter<Review> reviewEventEmitter) {
        this.reviewEventEmitter = reviewEventEmitter;
    }

    @Override
    public void onApplicationEvent(CreatedReview event) {
        final Object source = event.getSource();

        if (log.isDebugEnabled()) {
            log.debug("Received application event with source {} at {}", source,
                    LocalDateTime.ofInstant(Instant.ofEpochMilli(event.getTimestamp()), ZoneId.systemDefault()));
        }
        if (source instanceof Review) {
            final Review review = (Review) source;
            reviewEventEmitter.emmit(review);
        } else {
            log.warn("Application event source is not instance of review cannot emmit it!");
        }

    }
}