content: package com.aliyun.openservices.springboot.example.normal;
  
  import com.aliyun.openservices.ons.api.MessageListener;
  import com.aliyun.openservices.ons.api.PropertyKeyConst;
  import com.aliyun.openservices.ons.api.bean.ConsumerBean;
  import com.aliyun.openservices.ons.api.bean.Subscription;
  import com.aliyun.openservices.springboot.example.config.MqConfig;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  
  import java.util.HashMap;
  import java.util.Map;
  import java.util.Properties;
  
  //项目中加上 @Configuration 注解，这样服务启动时consumer也启动了
  public class ConsumerClient {
  
      @Autowired
      private MqConfig mqConfig;
  
      @Autowired
      private DemoMessageListener messageListener;
  
      @Bean(initMethod = "start", destroyMethod = "shutdown")
      public ConsumerBean buildConsumer() {
          ConsumerBean consumerBean = new ConsumerBean();
          //配置文件
          Properties properties = mqConfig.getMqPropertie();
          properties.setProperty(PropertyKeyConst.GROUP_ID, mqConfig.getGroupId());
          //将消费者线程数固定为20个 20为默认值
          properties.setProperty(PropertyKeyConst.ConsumeThreadNums, "20");
          consumerBean.setProperties(properties);
          //订阅关系
          Map<Subscription, MessageListener> subscriptionTable = new HashMap<Subscription, MessageListener>();
          Subscription subscription = new Subscription();
          subscription.setTopic(mqConfig.getTopic());
          subscription.setExpression(mqConfig.getTag());
          subscriptionTable.put(subscription, messageListener);
          //订阅多个topic如上面设置
  
          consumerBean.setSubscriptionTable(subscriptionTable);
          return consumerBean;
      }
  
  }
  