use of org.apache.rocketmq.common.message.MessageExt in project rocketmq-externals by apache.
the class BinlogPositionManager method initPositionFromMqTail.
private void initPositionFromMqTail() throws Exception {
DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("BINLOG_CONSUMER_GROUP");
consumer.setNamesrvAddr(config.mqNamesrvAddr);
consumer.setMessageModel(MessageModel.valueOf("BROADCASTING"));
consumer.start();
Set<MessageQueue> queues = consumer.fetchSubscribeMessageQueues(config.mqTopic);
MessageQueue queue = queues.iterator().next();
if (queue != null) {
Long offset = consumer.maxOffset(queue);
if (offset > 0)
offset--;
PullResult pullResult = consumer.pull(queue, "*", offset, 100);
if (pullResult.getPullStatus() == PullStatus.FOUND) {
MessageExt msg = pullResult.getMsgFoundList().get(0);
String json = new String(msg.getBody(), "UTF-8");
JSONObject js = JSON.parseObject(json);
binlogFilename = (String) js.get("binlogFilename");
nextPosition = js.getLong("nextPosition");
}
}
}
use of org.apache.rocketmq.common.message.MessageExt 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.common.message.MessageExt 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);
}
}
use of org.apache.rocketmq.common.message.MessageExt in project rocketmq-externals by apache.
the class RocketMqUtilsTest method testGetOffsets.
@Test
public void testGetOffsets() throws MQBrokerException, MQClientException, InterruptedException, UnsupportedEncodingException {
Map<String, String> optionParams = new HashMap<>();
optionParams.put(RocketMQConfig.NAME_SERVER_ADDR, NAME_SERVER);
SparkConf sparkConf = new SparkConf().setAppName("JavaCustomReceiver").setMaster("local[*]");
JavaStreamingContext sc = new JavaStreamingContext(sparkConf, new Duration(1000));
List<String> topics = new ArrayList<>();
topics.add(TOPIC_DEFAULT);
LocationStrategy locationStrategy = LocationStrategy.PreferConsistent();
JavaInputDStream<MessageExt> dStream = RocketMqUtils.createJavaMQPullStream(sc, UUID.randomUUID().toString(), topics, ConsumerStrategy.earliest(), false, false, false, locationStrategy, optionParams);
// hold a reference to the current offset ranges, so it can be used downstream
final AtomicReference<Map<TopicQueueId, OffsetRange[]>> offsetRanges = new AtomicReference<>();
final Set<MessageExt> result = Collections.synchronizedSet(new HashSet<MessageExt>());
dStream.transform(new Function<JavaRDD<MessageExt>, JavaRDD<MessageExt>>() {
@Override
public JavaRDD<MessageExt> call(JavaRDD<MessageExt> v1) throws Exception {
Map<TopicQueueId, OffsetRange[]> offsets = ((HasOffsetRanges) v1.rdd()).offsetRanges();
offsetRanges.set(offsets);
return v1;
}
}).foreachRDD(new VoidFunction<JavaRDD<MessageExt>>() {
@Override
public void call(JavaRDD<MessageExt> messageExtJavaRDD) throws Exception {
result.addAll(messageExtJavaRDD.collect());
}
});
sc.start();
long startTime = System.currentTimeMillis();
boolean matches = false;
while (!matches && System.currentTimeMillis() - startTime < 20000) {
matches = MESSAGE_NUM == result.size();
Thread.sleep(50);
}
sc.stop();
}
use of org.apache.rocketmq.common.message.MessageExt in project paascloud-master by paascloud.
the class UacPushMessageListener method consumeMessage.
/**
* Consume message consume concurrently status.
*
* @param messageExtList the message ext list
* @param consumeConcurrentlyContext the consume concurrently context
*
* @return the consume concurrently status
*/
@Override
@MqConsumerStore
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> messageExtList, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
MessageExt msg = messageExtList.get(0);
String body = new String(msg.getBody());
String topicName = msg.getTopic();
String tags = msg.getTags();
String keys = msg.getKeys();
try {
MqMessage.checkMessage(body, topicName, tags, keys);
String mqKV = redisService.getKey(keys);
if (PublicUtil.isNotEmpty(mqKV)) {
log.error("MQ消费Topic={},tag={},key={}, 重复消费", topicName, tags, keys);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
if (AliyunMqTopicConstants.MqTopicEnum.TPC_TOPIC.getTopic().equals(topicName)) {
mqMessageService.deleteMessageTopic(body, tags);
} else {
log.info("OPC订单信息消 topicName={} 不存在", topicName);
}
} catch (IllegalArgumentException ex) {
log.error("校验MQ message 失败 ex={}", ex.getMessage(), ex);
} catch (Exception e) {
log.error("处理MQ message 失败 topicName={}, keys={}, ex={}", topicName, keys, e.getMessage(), e);
return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}
redisService.setKey(keys, keys, 10, TimeUnit.DAYS);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
Aggregations