use of org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently in project rocketmq-externals by apache.
the class RocketMQConsoleTestBase method startTestMQConsumer.
protected void startTestMQConsumer() {
// test online consumer
consumer = new DefaultMQPushConsumer(TEST_CONSUMER_GROUP);
consumerConfigInfo.setBrokerNameList(Lists.newArrayList(TestConstant.TEST_BROKER_NAME));
SubscriptionGroupConfig subscriptionGroupConfig = new SubscriptionGroupConfig();
subscriptionGroupConfig.setGroupName(TEST_CONSUMER_GROUP);
subscriptionGroupConfig.setRetryQueueNums(RETRY_QUEUE_NUMS);
consumerConfigInfo.setSubscriptionGroupConfig(subscriptionGroupConfig);
consumerService.createAndUpdateSubscriptionGroupConfig(consumerConfigInfo);
consumer.setNamesrvAddr(TestConstant.NAME_SERVER_ADDRESS);
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
try {
consumer.subscribe(TEST_CONSOLE_TOPIC, "*");
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
consoleTestBaseLog.info("op=consumeMessage message={}", JsonUtil.obj2String(msgs));
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start();
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
use of org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently in project rocketmq-externals by apache.
the class ConsumerTest method main.
public static void main(String[] args) {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("g00003");
consumer.setNamesrvAddr("localhost:9876");
try {
consumer.subscribe("flink-sink2", "*");
} catch (MQClientException e) {
e.printStackTrace();
}
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
for (MessageExt msg : msgs) {
System.out.println(msg.getKeys() + ":" + new String(msg.getBody()));
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
try {
consumer.start();
} catch (MQClientException e) {
e.printStackTrace();
}
}
use of org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently in project spring-boot-starter-samples by vindell.
the class ScheduledMessageConsumer method main.
public static void main(String[] args) throws Exception {
// Instantiate message consumer
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("ExampleConsumer");
// Subscribe topics
consumer.subscribe("TestTopic", "*");
// Register message listener
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> messages, ConsumeConcurrentlyContext context) {
for (MessageExt message : messages) {
// Print approximate delay time period
System.out.println("Receive message[msgId=" + message.getMsgId() + "] " + (System.currentTimeMillis() - message.getStoreTimestamp()) + "ms later");
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
// Launch consumer
consumer.start();
}
use of org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently in project rocketmq-externals by apache.
the class TestController method list.
@RequestMapping(value = "/runTask.do", method = RequestMethod.GET)
@ResponseBody
public Object list() throws MQClientException, RemotingException, InterruptedException {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(testTopic + "Group");
consumer.setNamesrvAddr(rMQConfigure.getNamesrvAddr());
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.subscribe(testTopic, "*");
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
logger.info("receiveMessage msgSize={}", msgs.size());
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start();
final DefaultMQProducer producer = new DefaultMQProducer(testTopic + "Group");
producer.setInstanceName(String.valueOf(System.currentTimeMillis()));
producer.setNamesrvAddr(rMQConfigure.getNamesrvAddr());
producer.start();
new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while (true) {
try {
Message msg = new Message(testTopic, "TagA" + i, "KEYS" + i, ("Hello RocketMQ " + i).getBytes());
Thread.sleep(1000L);
SendResult sendResult = producer.send(msg);
logger.info("sendMessage={}", JsonUtil.obj2String(sendResult));
} catch (Exception e) {
e.printStackTrace();
try {
Thread.sleep(1000);
} catch (Exception ignore) {
}
}
}
}
}).start();
return true;
}
use of org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently in project rocketmq-externals by apache.
the class RocketMQReceiver method onStart.
@Override
public void onStart() {
Validate.notEmpty(properties, "Consumer properties can not be empty");
ordered = RocketMqUtils.getBoolean(properties, RocketMQConfig.CONSUMER_MESSAGES_ORDERLY, false);
consumer = new DefaultMQPushConsumer();
RocketMQConfig.buildConsumerConfigs(properties, (DefaultMQPushConsumer) consumer);
if (ordered) {
consumer.registerMessageListener(new MessageListenerOrderly() {
@Override
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
if (process(msgs)) {
return ConsumeOrderlyStatus.SUCCESS;
} else {
return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
}
}
});
} else {
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
if (process(msgs)) {
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
} else {
return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}
}
});
}
try {
consumer.start();
} catch (MQClientException e) {
throw new RuntimeException(e);
}
}
Aggregations