All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.edgar615.util.spring.eventbus.SpringEventProducer Maven / Gradle / Ivy

package com.github.edgar615.util.spring.eventbus;

import com.github.edgar615.util.event.Event;
import com.github.edgar615.util.eventbus.EventProducer;
import com.github.edgar615.util.eventbus.Helper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by Edgar on 2017/10/24.
 *
 * @author Edgar  Date 2017/10/24
 */
public class SpringEventProducer implements EventProducer, ApplicationEventPublisherAware {

  private static final Logger LOGGER = LoggerFactory.getLogger(SpringEventProducer.class);

  private ApplicationEventPublisher publisher;

  @Override
  public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
    this.publisher = applicationEventPublisher;
  }

  @Override
  public void send(Event event) {
    try {
      publisher.publishEvent(EventAdapter.create(event));
      LOGGER.info("[{}] [MS] [LOCAL] [OK] [{}] [{}] [{}]", event.head().id(),
              event.head().to(),
              Helper.toHeadString(event),
              Helper.toActionString(event));
    } catch (Exception e) {
      LOGGER.warn("[{}] [MS] [LOCAL] [FAILED] [{}] [{}] [{}]", event.head().id(),
              event.head().to(),
              Helper.toHeadString(event),
              Helper.toActionString(event));
    }

  }

  @Override
  public void close() {

  }

  @Override
  public Map metrics() {
    return null;
  }

  @Override
  public long waitForSend() {
    return 0;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy