Example #1
0
  @Override
  public SubscribeHandle start(List<Subscriber> subscribers) {
    CompositeSubscribeHandle handle = new CompositeSubscribeHandle();

    for (Subscriber s : subscribers) {
      List<Topic> topics = m_metaService.listTopicsByPattern(s.getTopicPattern());

      if (topics == null || topics.isEmpty()) {
        throw new IllegalArgumentException(
            String.format("Can not find any topics matching pattern %s", s.getTopicPattern()));
      }

      log.info(
          "Found topics({}) matching pattern({}), groupId={}.",
          CollectionUtil.collect(
              topics,
              new Transformer() {

                @Override
                public Object transform(Object topic) {
                  return ((Topic) topic).getName();
                }
              }),
          s.getTopicPattern(),
          s.getGroupId());

      for (Topic topic : topics) {
        ConsumerContext context =
            new ConsumerContext(
                topic, s.getGroupId(), s.getConsumer(), s.getMessageClass(), s.getConsumerType());

        // FIXME validate all, if fail in any validator, exit
        if (validate(topic, context)) {
          try {
            String endpointType = m_metaService.findEndpointTypeByTopic(topic.getName());
            ConsumerBootstrap consumerBootstrap =
                m_consumerManager.findConsumerBootStrap(endpointType);
            handle.addSubscribeHandle(consumerBootstrap.start(context));

          } catch (RuntimeException e) {
            log.error(
                "Failed to start consumer for topic {}(consumer: groupId={}, sessionId={})",
                topic.getName(),
                context.getGroupId(),
                context.getSessionId(),
                e);
            throw e;
          }
        }
      }
    }

    return handle;
  }