content: package com.blueskykong.chapter11.config.server.rest;
  
  import com.blueskykong.chapter11.config.server.bus.CustomRemoteApplicationEvent;
  import org.springframework.beans.BeansException;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.ApplicationContextAware;
  import org.springframework.context.ApplicationEventPublisher;
  import org.springframework.context.ApplicationEventPublisherAware;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.bind.annotation.RestController;
  
  /**
   * Bus 事件 Controller
   */
  @RestController
  public class BusEventController implements ApplicationContextAware, ApplicationEventPublisherAware {
  
      private ApplicationEventPublisher eventPublisher;
  
      private ApplicationContext applicationContext;
  
  
      @PostMapping("/bus/event/custom")
      public boolean publishUserEvent(@RequestBody String content,
                                      @RequestParam(value = "destination", required = false) String destination) {
  
          String serviceInstanceId = applicationContext.getId();
          CustomRemoteApplicationEvent event = new CustomRemoteApplicationEvent(content, serviceInstanceId, destination);
          eventPublisher.publishEvent(event);
          return true;
  
      }
  
  
      @Override
      public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
          this.eventPublisher = applicationEventPublisher;
      }
  
      @Override
      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
          this.applicationContext = applicationContext;
      }
  }
  