content: package cn.abel.queue.service;
  
  import cn.abel.queue.config.JmsConfig;
  import org.apache.rocketmq.client.exception.MQClientException;
  import org.apache.rocketmq.client.producer.DefaultMQProducer;
  import org.springframework.stereotype.Component;
  import org.springframework.stereotype.Service;
  
  
  /**
   * @author yyb
   * @time 2019/8/13
   */
  
  @Service
  @Component
  public class ProducerService {
      private String producerGroup = "test_producer";
      private DefaultMQProducer producer;
  
      public ProducerService(){
          //示例生产者
          producer = new DefaultMQProducer(producerGroup);
          //不开启vip通道 开通口端口会减2
          producer.setVipChannelEnabled(false);
          //绑定name server
          producer.setNamesrvAddr(JmsConfig.NAME_SERVER);
          start();
      }
      /**
       * 对象在使用之前必须要调用一次，只能初始化一次
       */
      public void start(){
          try {
              this.producer.start();
          } catch (MQClientException e) {
              e.printStackTrace();
          }
      }
  
      public DefaultMQProducer getProducer(){
          return this.producer;
      }
      /**
       * 一般在应用上下文，使用上下文监听器，进行关闭
       */
      public void shutdown(){
          this.producer.shutdown();
      }
  }