org.apache.rocketmq.client.consumer.MessageSelector Java Examples

The following examples show how to use org.apache.rocketmq.client.consumer.MessageSelector. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultMQPushConsumerImpl.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public void subscribe(final String topic, final MessageSelector messageSelector) throws MQClientException {
    try {
        if (messageSelector == null) {
            subscribe(topic, SubscriptionData.SUB_ALL);
            return;
        }

        SubscriptionData subscriptionData = FilterAPI.build(topic,
            messageSelector.getExpression(), messageSelector.getExpressionType());

        this.rebalanceImpl.getSubscriptionInner().put(topic, subscriptionData);
        if (this.mQClientFactory != null) {
            this.mQClientFactory.sendHeartbeatToAllBrokerWithLock();
        }
    } catch (Exception e) {
        throw new MQClientException("subscription exception", e);
    }
}
 
Example #2
Source File: SqlFilterIT.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
@Test
public void testFilterConsumer() throws Exception {
    int msgSize = 16;

    String group = initConsumerGroup();
    MessageSelector selector = MessageSelector.bySql("(TAGS is not null and TAGS in ('TagA', 'TagB'))");
    RMQSqlConsumer consumer = ConsumerFactory.getRMQSqlConsumer(nsAddr, group, topic, selector, new RMQNormalListner(group + "_1"));
    Thread.sleep(3000);
    producer.send("TagA", msgSize);
    producer.send("TagB", msgSize);
    producer.send("TagC", msgSize);
    Assert.assertEquals("Not all sent succeeded", msgSize * 3, producer.getAllUndupMsgBody().size());
    consumer.getListner().waitForMessageConsume(msgSize * 2, consumeTime);
    assertThat(producer.getAllMsgBody())
        .containsAllIn(VerifyUtils.getFilterdMessage(producer.getAllMsgBody(),
        consumer.getListner().getAllMsgBody()));

    assertThat(consumer.getListner().getAllMsgBody().size()).isEqualTo(msgSize * 2);
}
 
Example #3
Source File: DefaultMQPushConsumerImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
public void subscribe(final String topic, final MessageSelector messageSelector) throws MQClientException {
    try {
        if (messageSelector == null) {
            subscribe(topic, SubscriptionData.SUB_ALL);
            return;
        }

        SubscriptionData subscriptionData = FilterAPI.build(topic,
            messageSelector.getExpression(), messageSelector.getExpressionType());

        this.rebalanceImpl.getSubscriptionInner().put(topic, subscriptionData);
        if (this.mQClientFactory != null) {
            this.mQClientFactory.sendHeartbeatToAllBrokerWithLock();
        }
    } catch (Exception e) {
        throw new MQClientException("subscription exception", e);
    }
}
 
Example #4
Source File: SqlFilterIT.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Test
public void testFilterConsumer() throws Exception {
    int msgSize = 16;

    String group = initConsumerGroup();
    MessageSelector selector = MessageSelector.bySql("(TAGS is not null and TAGS in ('TagA', 'TagB'))");
    RMQSqlConsumer consumer = ConsumerFactory.getRMQSqlConsumer(nsAddr, group, topic, selector, new RMQNormalListener(group + "_1"));
    Thread.sleep(3000);
    producer.send("TagA", msgSize);
    producer.send("TagB", msgSize);
    producer.send("TagC", msgSize);
    Assert.assertEquals("Not all sent succeeded", msgSize * 3, producer.getAllUndupMsgBody().size());
    consumer.getListener().waitForMessageConsume(msgSize * 2, consumeTime);
    assertThat(producer.getAllMsgBody())
        .containsAllIn(VerifyUtils.getFilterdMessage(producer.getAllMsgBody(),
            consumer.getListener().getAllMsgBody()));

    assertThat(consumer.getListener().getAllMsgBody().size()).isEqualTo(msgSize * 2);
}
 
Example #5
Source File: SqlFilterConsumer.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name");

        // Don't forget to set enablePropertyFilter=true in broker
        consumer.subscribe("SqlFilterTest",
            MessageSelector.bySql("(TAGS is not null and TAGS in ('TagA', 'TagB'))" +
                "and (a is not null and a between 0 and 3)"));

        consumer.registerMessageListener(new MessageListenerConcurrently() {

            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
                ConsumeConcurrentlyContext context) {
                System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        consumer.start();
        System.out.printf("Consumer Started.%n");
    }
 
Example #6
Source File: DefaultMQPushConsumerImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public void subscribe(final String topic, final MessageSelector messageSelector) throws MQClientException {
    try {
        if (messageSelector == null) {
            subscribe(topic, SubscriptionData.SUB_ALL);
            return;
        }

        SubscriptionData subscriptionData = FilterAPI.build(topic,
            messageSelector.getExpression(), messageSelector.getExpressionType());

        this.rebalanceImpl.getSubscriptionInner().put(topic, subscriptionData);
        if (this.mQClientFactory != null) {
            this.mQClientFactory.sendHeartbeatToAllBrokerWithLock();
        }
    } catch (Exception e) {
        throw new MQClientException("subscription exception", e);
    }
}
 
Example #7
Source File: DefaultMQPushConsumerImpl.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public void subscribe(final String topic, final MessageSelector messageSelector) throws MQClientException {
    try {
        if (messageSelector == null) {
            subscribe(topic, SubscriptionData.SUB_ALL);
            return;
        }

        SubscriptionData subscriptionData = FilterAPI.build(topic,
            messageSelector.getExpression(), messageSelector.getExpressionType());

        this.rebalanceImpl.getSubscriptionInner().put(topic, subscriptionData);
        if (this.mQClientFactory != null) {
            this.mQClientFactory.sendHeartbeatToAllBrokerWithLock();
        }
    } catch (Exception e) {
        throw new MQClientException("subscription exception", e);
    }
}
 
Example #8
Source File: SqlFilterIT.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
@Test
public void testFilterConsumer() throws Exception {
    int msgSize = 16;

    String group = initConsumerGroup();
    MessageSelector selector = MessageSelector.bySql("(TAGS is not null and TAGS in ('TagA', 'TagB'))");
    RMQSqlConsumer consumer = ConsumerFactory.getRMQSqlConsumer(nsAddr, group, topic, selector, new RMQNormalListener(group + "_1"));
    Thread.sleep(3000);
    producer.send("TagA", msgSize);
    producer.send("TagB", msgSize);
    producer.send("TagC", msgSize);
    Assert.assertEquals("Not all sent succeeded", msgSize * 3, producer.getAllUndupMsgBody().size());
    consumer.getListener().waitForMessageConsume(msgSize * 2, consumeTime);
    assertThat(producer.getAllMsgBody())
        .containsAllIn(VerifyUtils.getFilterdMessage(producer.getAllMsgBody(),
            consumer.getListener().getAllMsgBody()));

    assertThat(consumer.getListener().getAllMsgBody().size()).isEqualTo(msgSize * 2);
}
 
Example #9
Source File: DefaultMQPushConsumerImpl.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
/**
 * subsicribe
 * @param topic topic
 * @param messageSelector 消息选择器
 * @throws MQClientException ;
 */
public void subscribe(final String topic, final MessageSelector messageSelector) throws MQClientException {
    try {
        if (messageSelector == null) {
            subscribe(topic, SubscriptionData.SUB_ALL);
            return;
        }

        SubscriptionData subscriptionData = FilterAPI.build(topic,
            messageSelector.getExpression(), messageSelector.getExpressionType());

        this.rebalanceImpl.getSubscriptionInner().put(topic, subscriptionData);
        if (this.mQClientFactory != null) {
            this.mQClientFactory.sendHeartbeatToAllBrokerWithLock();
        }
    } catch (Exception e) {
        throw new MQClientException("subscription exception", e);
    }
}
 
Example #10
Source File: SqlFilterIT.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
@Test
public void testFilterConsumer() throws Exception {
    int msgSize = 16;

    String group = initConsumerGroup();
    MessageSelector selector = MessageSelector.bySql("(TAGS is not null and TAGS in ('TagA', 'TagB'))");
    RMQSqlConsumer consumer = ConsumerFactory.getRMQSqlConsumer(nsAddr, group, topic, selector, new RMQNormalListener(group + "_1"));
    Thread.sleep(3000);
    producer.send("TagA", msgSize);
    producer.send("TagB", msgSize);
    producer.send("TagC", msgSize);
    Assert.assertEquals("Not all sent succeeded", msgSize * 3, producer.getAllUndupMsgBody().size());
    consumer.getListener().waitForMessageConsume(msgSize * 2, consumeTime);
    assertThat(producer.getAllMsgBody())
        .containsAllIn(VerifyUtils.getFilterdMessage(producer.getAllMsgBody(),
            consumer.getListener().getAllMsgBody()));

    assertThat(consumer.getListener().getAllMsgBody().size()).isEqualTo(msgSize * 2);
}
 
Example #11
Source File: DefaultLitePullConsumerImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public synchronized void subscribe(String topic, MessageSelector messageSelector) throws MQClientException {
    try {
        if (topic == null || topic.equals("")) {
            throw new IllegalArgumentException("Topic can not be null or empty.");
        }
        setSubscriptionType(SubscriptionType.SUBSCRIBE);
        if (messageSelector == null) {
            subscribe(topic, SubscriptionData.SUB_ALL);
            return;
        }
        SubscriptionData subscriptionData = FilterAPI.build(topic,
            messageSelector.getExpression(), messageSelector.getExpressionType());
        this.rebalanceImpl.getSubscriptionInner().put(topic, subscriptionData);
        this.defaultLitePullConsumer.setMessageQueueListener(new MessageQueueListenerImpl());
        assignedMessageQueue.setRebalanceImpl(this.rebalanceImpl);
        if (serviceState == ServiceState.RUNNING) {
            this.mQClientFactory.sendHeartbeatToAllBrokerWithLock();
            updateTopicSubscribeInfoWhenSubscriptionChanged();
        }
    } catch (Exception e) {
        throw new MQClientException("subscribe exception", e);
    }
}
 
Example #12
Source File: DefaultMQPushConsumerImpl.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
public void subscribe(final String topic, final MessageSelector messageSelector) throws MQClientException {
    try {
        if (messageSelector == null) {
            subscribe(topic, SubscriptionData.SUB_ALL);
            return;
        }

        SubscriptionData subscriptionData = FilterAPI.build(topic,
            messageSelector.getExpression(), messageSelector.getExpressionType());

        this.rebalanceImpl.getSubscriptionInner().put(topic, subscriptionData);
        if (this.mQClientFactory != null) {
            this.mQClientFactory.sendHeartbeatToAllBrokerWithLock();
        }
    } catch (Exception e) {
        throw new MQClientException("subscription exception", e);
    }
}
 
Example #13
Source File: SqlFilterIT.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Test
public void testFilterConsumer() throws Exception {
    int msgSize = 16;

    String group = initConsumerGroup();
    MessageSelector selector = MessageSelector.bySql("(TAGS is not null and TAGS in ('TagA', 'TagB'))");
    RMQSqlConsumer consumer = ConsumerFactory.getRMQSqlConsumer(nsAddr, group, topic, selector, new RMQNormalListener(group + "_1"));
    Thread.sleep(3000);
    producer.send("TagA", msgSize);
    producer.send("TagB", msgSize);
    producer.send("TagC", msgSize);
    Assert.assertEquals("Not all sent succeeded", msgSize * 3, producer.getAllUndupMsgBody().size());
    consumer.getListener().waitForMessageConsume(msgSize * 2, consumeTime);
    assertThat(producer.getAllMsgBody())
        .containsAllIn(VerifyUtils.getFilterdMessage(producer.getAllMsgBody(),
            consumer.getListener().getAllMsgBody()));

    assertThat(consumer.getListener().getAllMsgBody().size()).isEqualTo(msgSize * 2);
}
 
Example #14
Source File: SqlFilterIT.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Test
public void testFilterConsumer() throws Exception {
    int msgSize = 16;

    String group = initConsumerGroup();
    MessageSelector selector = MessageSelector.bySql("(TAGS is not null and TAGS in ('TagA', 'TagB'))");
    RMQSqlConsumer consumer = ConsumerFactory.getRMQSqlConsumer(nsAddr, group, topic, selector, new RMQNormalListener(group + "_1"));
    Thread.sleep(3000);
    producer.send("TagA", msgSize);
    producer.send("TagB", msgSize);
    producer.send("TagC", msgSize);
    Assert.assertEquals("Not all sent succeeded", msgSize * 3, producer.getAllUndupMsgBody().size());
    consumer.getListener().waitForMessageConsume(msgSize * 2, consumeTime);
    assertThat(producer.getAllMsgBody())
        .containsAllIn(VerifyUtils.getFilterdMessage(producer.getAllMsgBody(),
            consumer.getListener().getAllMsgBody()));

    assertThat(consumer.getListener().getAllMsgBody().size()).isEqualTo(msgSize * 2);
}
 
Example #15
Source File: DefaultMQPullConsumerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void pull(MessageQueue mq, MessageSelector messageSelector, long offset, int maxNums,
    PullCallback pullCallback,
    long timeout)
    throws MQClientException, RemotingException, InterruptedException {
    SubscriptionData subscriptionData = getSubscriptionData(mq, messageSelector);
    this.pullAsyncImpl(mq, subscriptionData, offset, maxNums, pullCallback, false, timeout);
}
 
Example #16
Source File: DefaultMQPullConsumerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private SubscriptionData getSubscriptionData(MessageQueue mq, MessageSelector messageSelector)
    throws MQClientException {

    if (null == mq) {
        throw new MQClientException("mq is null", null);
    }

    try {
        return FilterAPI.build(mq.getTopic(),
            messageSelector.getExpression(), messageSelector.getExpressionType());
    } catch (Exception e) {
        throw new MQClientException("parse subscription error", e);
    }
}
 
Example #17
Source File: ConsumerFactory.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public static RMQSqlConsumer getRMQSqlConsumer(String nsAddr, String consumerGroup,
    String topic, MessageSelector selector,
    AbstractListener listner) {
    RMQSqlConsumer consumer = new RMQSqlConsumer(nsAddr, topic, selector,
        consumerGroup, listner);
    consumer.create();
    consumer.start();
    return consumer;
}
 
Example #18
Source File: SqlFilterConsumer.java    From blog with MIT License 5 votes vote down vote up
public static void main(String[] args) throws MQClientException {
  /** TODO: 创建消息消费者 */
  DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("Sql_Filter_Consumer");
  /** TODO: 设置 nameserver 地址 */
  consumer.setNamesrvAddr("127.0.0.1:9876");

  /**
   * TODO: 订阅主题, MessageSelect.bySql() 指定消息中的属性过滤,MessageSelector.byTag() 指定消息中的 TAG
   *
   * <p>SQL 过滤需要在 broker 配置文件中设置 enablePropertyFilter = true
   */
  consumer.subscribe(
      "SqlFilterTest", MessageSelector.bySql("(TAGS in ('TagA')) and (index > 3)"));
  consumer.registerMessageListener(
      new MessageListenerConcurrently() {
        @Override
        public ConsumeConcurrentlyStatus consumeMessage(
            List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
          msgs.forEach(o -> System.out.println(new String(o.getBody())));
          return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
      });

  /** TODO: 连接 nameserver */
  consumer.start();
  System.out.println("Consumer 已开启============");
}
 
Example #19
Source File: ConsumerFactory.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public static RMQSqlConsumer getRMQSqlConsumer(String nsAddr, String consumerGroup,
    String topic, MessageSelector selector,
    AbstractListener listner) {
    RMQSqlConsumer consumer = new RMQSqlConsumer(nsAddr, topic, selector,
        consumerGroup, listner);
    consumer.create();
    consumer.start();
    return consumer;
}
 
Example #20
Source File: ConsumerFactory.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public static RMQSqlConsumer getRMQSqlConsumer(String nsAddr, String consumerGroup,
    String topic, MessageSelector selector,
    AbstractListener listner) {
    RMQSqlConsumer consumer = new RMQSqlConsumer(nsAddr, topic, selector,
        consumerGroup, listner);
    consumer.create();
    consumer.start();
    return consumer;
}
 
Example #21
Source File: ConsumerFactory.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public static RMQSqlConsumer getRMQSqlConsumer(String nsAddr, String consumerGroup,
    String topic, MessageSelector selector,
    AbstractListener listner) {
    RMQSqlConsumer consumer = new RMQSqlConsumer(nsAddr, topic, selector,
        consumerGroup, listner);
    consumer.create();
    consumer.start();
    return consumer;
}
 
Example #22
Source File: ConsumerFactory.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
public static RMQSqlConsumer getRMQSqlConsumer(String nsAddr, String consumerGroup,
    String topic, MessageSelector selector,
    AbstractListener listner) {
    RMQSqlConsumer consumer = new RMQSqlConsumer(nsAddr, topic, selector,
        consumerGroup, listner);
    consumer.create();
    consumer.start();
    return consumer;
}
 
Example #23
Source File: ConsumerFactory.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
public static RMQSqlConsumer getRMQSqlConsumer(String nsAddr, String consumerGroup,
    String topic, MessageSelector selector,
    AbstractListener listner) {
    RMQSqlConsumer consumer = new RMQSqlConsumer(nsAddr, topic, selector,
        consumerGroup, listner);
    consumer.create();
    consumer.start();
    return consumer;
}
 
Example #24
Source File: RMQSqlConsumer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
public RMQSqlConsumer(String nsAddr, String topic, MessageSelector selector,
    String consumerGroup, AbstractListener listener) {
    super(nsAddr, topic, "*", consumerGroup, listener);
    this.selector = selector;
}
 
Example #25
Source File: DefaultRocketMQListenerContainer.java    From spring-boot-starter-rocketmq with Apache License 2.0 4 votes vote down vote up
private void initRocketMQPushConsumer() throws MQClientException {

        Assert.notNull(rocketMQListener, "Property 'rocketMQListener' is required");
        Assert.notNull(consumerGroup, "Property 'consumerGroup' is required");
        Assert.notNull(nameServer, "Property 'nameServer' is required");
        Assert.notNull(topic, "Property 'topic' is required");

        consumer = new DefaultMQPushConsumer(consumerGroup);
        consumer.setNamesrvAddr(nameServer);
        consumer.setConsumeThreadMax(consumeThreadMax);
        if (consumeThreadMax < consumer.getConsumeThreadMin()) {
            consumer.setConsumeThreadMin(consumeThreadMax);
        }
        consumer.setPullThresholdForTopic(pullThresholdForTopic);
        consumer.setPullThresholdSizeForTopic(pullThresholdSizeForTopic);

        consumer.setMessageModel(messageModel);

        switch (selectorType) {
            case TAG:
                consumer.subscribe(topic, selectorExpress);
                break;
            case SQL92:
                consumer.subscribe(topic, MessageSelector.bySql(selectorExpress));
                break;
            default:
                throw new IllegalArgumentException("Property 'selectorType' was wrong.");
        }

        switch (consumeMode) {
            case ORDERLY:
                consumer.setMessageListener(new DefaultMessageListenerOrderly());
                break;
            case CONCURRENTLY:
                consumer.setMessageListener(new DefaultMessageListenerConcurrently());
                break;
            default:
                throw new IllegalArgumentException("Property 'consumeMode' was wrong.");
        }

        // provide an entryway to custom setting RocketMQ consumer
        if (rocketMQListener instanceof RocketMQPushConsumerLifecycleListener) {
            ((RocketMQPushConsumerLifecycleListener) rocketMQListener).prepareStart(consumer);
        }

    }
 
Example #26
Source File: RMQSqlConsumer.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
public RMQSqlConsumer(String nsAddr, String topic, MessageSelector selector,
    String consumerGroup, AbstractListener listener) {
    super(nsAddr, topic, "*", consumerGroup, listener);
    this.selector = selector;
}
 
Example #27
Source File: DefaultMQPullConsumerImpl.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public PullResult pull(MessageQueue mq, MessageSelector messageSelector, long offset, int maxNums)
    throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
    return pull(mq, messageSelector, offset, maxNums, this.defaultMQPullConsumer.getConsumerPullTimeoutMillis());
}
 
Example #28
Source File: DefaultMQPullConsumerImpl.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public PullResult pull(MessageQueue mq, MessageSelector messageSelector, long offset, int maxNums, long timeout)
    throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
    SubscriptionData subscriptionData = getSubscriptionData(mq, messageSelector);
    return this.pullSyncImpl(mq, subscriptionData, offset, maxNums, false, timeout);
}
 
Example #29
Source File: RMQSqlConsumer.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
public RMQSqlConsumer(String nsAddr, String topic, MessageSelector selector,
    String consumerGroup, AbstractListener listener) {
    super(nsAddr, topic, "*", consumerGroup, listener);
    this.selector = selector;
}
 
Example #30
Source File: DefaultMQPullConsumerImpl.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public void pull(MessageQueue mq, MessageSelector messageSelector, long offset, int maxNums,
    PullCallback pullCallback)
    throws MQClientException, RemotingException, InterruptedException {
    pull(mq, messageSelector, offset, maxNums, pullCallback, this.defaultMQPullConsumer.getConsumerPullTimeoutMillis());
}