use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class AbstractTestCase method consumeMessages.
protected int consumeMessages(int count, final String key, int timeout) throws MQClientException, InterruptedException {
final AtomicInteger cc = new AtomicInteger(0);
final CountDownLatch countDownLatch = new CountDownLatch(count);
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("hello");
consumer.setNamesrvAddr(nameServer);
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.subscribe(topic, "*");
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
for (MessageExt msg : msgs) {
String body = new String(msg.getBody());
if (key == null || body.contains(key)) {
countDownLatch.countDown();
cc.incrementAndGet();
continue;
}
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start();
countDownLatch.await(timeout, TimeUnit.SECONDS);
consumer.shutdown();
return cc.get();
}
use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class Consumer method main.
public static void main(String[] args) throws InterruptedException, MQClientException, IOException {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("ConsumerGroupNamecc4");
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
File classFile = new File(classLoader.getResource("MessageFilterImpl.java").getFile());
String filterCode = MixAll.file2String(classFile);
consumer.subscribe("TopicTest", "org.apache.rocketmq.example.filter.MessageFilterImpl", filterCode);
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n");
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start();
System.out.printf("Consumer Started.%n");
}
use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class SqlConsumer method main.
public static void main(String[] args) {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_4");
try {
consumer.subscribe("TopicTest", MessageSelector.bySql("(TAGS is not null and TAGS in ('TagA', 'TagB'))" + "and (a is not null and a between 0 3)"));
} catch (MQClientException e) {
e.printStackTrace();
return;
}
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n");
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
try {
consumer.start();
} catch (MQClientException e) {
e.printStackTrace();
return;
}
System.out.printf("Consumer Started.%n");
}
use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class Consumer method main.
public static void main(String[] args) throws InterruptedException, MQClientException {
CommandLine commandLine = buildCommandline(args);
if (commandLine != null) {
String group = commandLine.getOptionValue('g');
String topic = commandLine.getOptionValue('t');
String subscription = commandLine.getOptionValue('s');
final String returnFailedHalf = commandLine.getOptionValue('f');
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(group);
consumer.setInstanceName(Long.toString(System.currentTimeMillis()));
consumer.subscribe(topic, subscription);
consumer.registerMessageListener(new MessageListenerConcurrently() {
AtomicLong consumeTimes = new AtomicLong(0);
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
long currentTimes = this.consumeTimes.incrementAndGet();
System.out.printf("%-8d %s%n", currentTimes, msgs);
if (Boolean.parseBoolean(returnFailedHalf)) {
if ((currentTimes % 2) == 0) {
return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start();
System.out.printf("Consumer Started.%n");
}
}
use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class Consumer method main.
public static void main(String[] args) throws InterruptedException, MQClientException {
/*
* Instantiate with specified consumer group name.
*/
/**
* Consumer Group,非常重要的概念,后续会慢慢补充
*/
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_4");
// 指定NameServer地址,多个地址以 ; 隔开
consumer.setNamesrvAddr("127.0.0.1:9876");
/*
* Specify name server addresses.
* <p/>
*
* Alternatively, you may specify name server addresses via exporting environmental variable: NAMESRV_ADDR
* <pre>
* {@code
* consumer.setNamesrvAddr("name-server1-ip:9876;name-server2-ip:9876");
* }
* </pre>
*/
/*
* Specify where to start in case the specified consumer group is a brand new one.
*/
/**
* 设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费
* 如果非第一次启动,那么按照上次消费的位置继续消费
*/
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
/*
* Subscribe one more more topics to consume.
*/
consumer.subscribe("TopicTest", "*");
/*
* Register callback to execute on arrival of messages fetched from brokers.
*/
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n");
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
/*
* Launch the consumer instance.
*/
consumer.start();
System.out.printf("Consumer Started.%n");
}
Aggregations