本文整理汇总了Java中com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyContext类的典型用法代码示例。如果您正苦于以下问题:Java ConsumeOrderlyContext类的具体用法?Java ConsumeOrderlyContext怎么用?Java ConsumeOrderlyContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ConsumeOrderlyContext类属于com.alibaba.rocketmq.client.consumer.listener包,在下文中一共展示了ConsumeOrderlyContext类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: main
import com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyContext; //导入依赖的package包/类
public static void main(String[] args) throws MQClientException {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_3");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.subscribe("TopicTest", "TagA || TagC || TagD");
consumer.registerMessageListener(new MessageListenerOrderly() {
AtomicLong consumeTimes = new AtomicLong(0);
@Override
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
context.setAutoCommit(false);
System.out.println(Thread.currentThread().getName() + " Receive New Messages: " + msgs);
this.consumeTimes.incrementAndGet();
if ((this.consumeTimes.get() % 2) == 0) {
return ConsumeOrderlyStatus.SUCCESS;
}
else if ((this.consumeTimes.get() % 3) == 0) {
return ConsumeOrderlyStatus.ROLLBACK;
}
else if ((this.consumeTimes.get() % 4) == 0) {
return ConsumeOrderlyStatus.COMMIT;
}
else if ((this.consumeTimes.get() % 5) == 0) {
context.setSuspendCurrentQueueTimeMillis(3000);
return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
}
return ConsumeOrderlyStatus.SUCCESS;
}
});
consumer.start();
System.out.println("Consumer Started.");
}
开发者ID:y123456yz,项目名称:reading-and-annotate-rocketmq-3.4.6,代码行数:39,代码来源:Consumer.java
示例2: testOrderedConsumer
import com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyContext; //导入依赖的package包/类
@Before
public void testOrderedConsumer() throws Exception {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("example_group_name");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.subscribe("TopicTest", "TagA || TagC || TagD");
consumer.registerMessageListener(new MessageListenerOrderly() {
AtomicLong consumeTimes = new AtomicLong(0);
@SuppressWarnings("deprecation")
@Override
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
context.setAutoCommit(false);
System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n");
this.consumeTimes.incrementAndGet();
if ((this.consumeTimes.get() % 2) == 0) {
return ConsumeOrderlyStatus.SUCCESS;
} else if ((this.consumeTimes.get() % 3) == 0) {
return ConsumeOrderlyStatus.ROLLBACK;
} else if ((this.consumeTimes.get() % 4) == 0) {
return ConsumeOrderlyStatus.COMMIT;
} else if ((this.consumeTimes.get() % 5) == 0) {
context.setSuspendCurrentQueueTimeMillis(3000);
return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
}
return ConsumeOrderlyStatus.SUCCESS;
}
});
consumer.start();
System.out.printf("Consumer Started.%n");
}
开发者ID:dzh,项目名称:coca,代码行数:38,代码来源:TestOrderedProducer.java
示例3: getRollBackMessageListener
import com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyContext; //导入依赖的package包/类
private static MessageListenerOrderly getRollBackMessageListener() {
return new MessageListenerOrderly() {
AtomicLong consumeTimes = new AtomicLong(0);
@Override
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
System.out.println("MESSAGE BODY========" + new String(msgs.get(0).getBody()));
context.setAutoCommit(false);
this.consumeTimes.incrementAndGet();
if ((this.consumeTimes.get() % 1) == 0) {
System.out.println("SUCCESS========Receive New Messages: " + msgs);
return ConsumeOrderlyStatus.SUCCESS;
}
else if ((this.consumeTimes.get() % 2) == 0) {
System.out.println("ROLLBACK========Receive New Messages: " + msgs);
return ConsumeOrderlyStatus.ROLLBACK;
}
else if ((this.consumeTimes.get() % 3) == 0) {
System.out.println("COMMIT========Receive New Messages: " + msgs);
return ConsumeOrderlyStatus.COMMIT;
}
else if ((this.consumeTimes.get() % 5) == 0) {
System.out.println("SUSPEND_CURRENT_QUEUE_A_MOMENT========Receive New Messages: "
+ msgs.get(0));
context.setSuspendCurrentQueueTimeMillis(3000);
return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
}
return ConsumeOrderlyStatus.SUCCESS;
}
};
}
开发者ID:diwayou,项目名称:rocketmq-all-trans,代码行数:34,代码来源:PushConsumerTest.java
示例4: getAutoCommitMessageListener
import com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyContext; //导入依赖的package包/类
private static MessageListenerOrderly getAutoCommitMessageListener() {
return new MessageListenerOrderly() {
AtomicLong consumeTimes = new AtomicLong(0);
@Override
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
System.out.println("MESSAGE BODY========" + new String(msgs.get(0).getBody()));
this.consumeTimes.incrementAndGet();
if ((this.consumeTimes.get() % 1) == 0) {
System.out.println("SUCCESS========Receive New Messages: " + msgs);
return ConsumeOrderlyStatus.SUCCESS;
}
else if ((this.consumeTimes.get() % 2) == 0) {
System.out.println("ROLLBACK========Receive New Messages: " + msgs);
return ConsumeOrderlyStatus.ROLLBACK;
}
else if ((this.consumeTimes.get() % 5) == 0) {
System.out.println("SUSPEND_CURRENT_QUEUE_A_MOMENT========Receive New Messages: "
+ msgs.get(0));
context.setSuspendCurrentQueueTimeMillis(3000);
return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
}
return ConsumeOrderlyStatus.SUCCESS;
}
};
}
开发者ID:diwayou,项目名称:rocketmq-all-trans,代码行数:29,代码来源:PushConsumerTest.java
示例5: createMessageListener
import com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyContext; //导入依赖的package包/类
private static MessageListener createMessageListener() {
return new MessageListenerOrderly() {
@Override
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
for (MessageExt msg : msgs) {
System.err.println("topic:" + msg.getTopic() + ", msgId:" + msg.getMsgId() + ", body:"
+ new String(msg.getBody()));
}
return ConsumeOrderlyStatus.SUCCESS;
}
};
}
开发者ID:diwayou,项目名称:rocketmq-all-trans,代码行数:14,代码来源:UnitOrderTest.java
示例6: consumeMessage
import com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyContext; //导入依赖的package包/类
@Override
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs,
ConsumeOrderlyContext context) {
MessageExt msg = msgs.get(0);
String jsonString = new String(msg.getBody());
try {
UpdateBody body = seriaziler.deserializeAsObject(
jsonString, UpdateBody.class);
if(body == null){
return ConsumeOrderlyStatus.SUCCESS;
}
logger.info("receive a synchronous JVM caching from "
+ body.getClientId());
if(ClientIdGenerator.getClientId().equals(body.getClientId())){
logger.info("Ignore it, because he was himself a sender");
}
updateManager.update(body.getCommands());
} catch (SerializationException e) {
logger.error("serialized message error", e);
if(!logger.isDebugEnabled()){
logger.debug("message data: " + jsonString);
}
}
return ConsumeOrderlyStatus.SUCCESS;
}
开发者ID:leopardoooo,项目名称:easyooo-framework,代码行数:29,代码来源:JvmCacheMessageListenerOrderly.java
示例7: main
import com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyContext; //导入依赖的package包/类
public static void main(String[] args) throws MQClientException {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_3");
consumer.subscribe("TopicTest", "TagA || TagC || TagD");
consumer.registerMessageListener(new MessageListenerOrderly() {
AtomicLong consumeTimes = new AtomicLong(0);
@Override
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
context.setAutoCommit(false);
System.out.println(Thread.currentThread().getName() + " Receive New Messages: " + msgs);
this.consumeTimes.incrementAndGet();
if ((this.consumeTimes.get() % 2) == 0) {
return ConsumeOrderlyStatus.SUCCESS;
}
else if ((this.consumeTimes.get() % 3) == 0) {
return ConsumeOrderlyStatus.ROLLBACK;
}
else if ((this.consumeTimes.get() % 4) == 0) {
return ConsumeOrderlyStatus.COMMIT;
}
else if ((this.consumeTimes.get() % 5) == 0) {
context.setSuspendCurrentQueueTimeMillis(3000);
return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
}
return ConsumeOrderlyStatus.SUCCESS;
}
});
consumer.start();
System.out.println("Consumer Started.");
}
开发者ID:brucechan0921,项目名称:RocketMQ-3.0.8,代码行数:37,代码来源:Consumer.java
示例8: processConsumeResult
import com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyContext; //导入依赖的package包/类
public boolean processConsumeResult(//
final List<MessageExt> msgs, //
final ConsumeOrderlyStatus status, //
final ConsumeOrderlyContext context, //
final ConsumeRequest consumeRequest//
) {
boolean continueConsume = true;
long commitOffset = -1L;
if (context.isAutoCommit()) {
switch (status) {
case COMMIT:
case ROLLBACK:
log.warn(
"the message queue consume result is illegal, we think you want to ack these message {}",
consumeRequest.getMessageQueue());
case SUCCESS:
commitOffset = consumeRequest.getProcessQueue().commit();
this.getConsumerStatsManager().incConsumeOKTPS(consumerGroup,
consumeRequest.getMessageQueue().getTopic(), msgs.size());
break;
case SUSPEND_CURRENT_QUEUE_A_MOMENT:
consumeRequest.getProcessQueue().makeMessageToCosumeAgain(msgs);
this.submitConsumeRequestLater(//
consumeRequest.getProcessQueue(), //
consumeRequest.getMessageQueue(), //
context.getSuspendCurrentQueueTimeMillis());
continueConsume = false;
this.getConsumerStatsManager().incConsumeFailedTPS(consumerGroup,
consumeRequest.getMessageQueue().getTopic(), msgs.size());
break;
default:
break;
}
}
else {
switch (status) {
case SUCCESS:
this.getConsumerStatsManager().incConsumeOKTPS(consumerGroup,
consumeRequest.getMessageQueue().getTopic(), msgs.size());
break;
case COMMIT:
commitOffset = consumeRequest.getProcessQueue().commit();
break;
case ROLLBACK:
consumeRequest.getProcessQueue().rollback();
this.submitConsumeRequestLater(//
consumeRequest.getProcessQueue(), //
consumeRequest.getMessageQueue(), //
context.getSuspendCurrentQueueTimeMillis());
continueConsume = false;
break;
case SUSPEND_CURRENT_QUEUE_A_MOMENT:
consumeRequest.getProcessQueue().makeMessageToCosumeAgain(msgs);
this.submitConsumeRequestLater(//
consumeRequest.getProcessQueue(), //
consumeRequest.getMessageQueue(), //
context.getSuspendCurrentQueueTimeMillis());
continueConsume = false;
this.getConsumerStatsManager().incConsumeFailedTPS(consumerGroup,
consumeRequest.getMessageQueue().getTopic(), msgs.size());
break;
default:
break;
}
}
if (commitOffset >= 0) {
this.defaultMQPushConsumerImpl.getOffsetStore().updateOffset(consumeRequest.getMessageQueue(),
commitOffset, false);
}
return continueConsume;
}
开发者ID:y123456yz,项目名称:reading-and-annotate-rocketmq-3.4.6,代码行数:75,代码来源:ConsumeMessageOrderlyService.java
示例9: consumeMessageDirectly
import com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyContext; //导入依赖的package包/类
@Override
public ConsumeMessageDirectlyResult consumeMessageDirectly(MessageExt msg, String brokerName) {
ConsumeMessageDirectlyResult result = new ConsumeMessageDirectlyResult();
result.setOrder(true);
List<MessageExt> msgs = new ArrayList<MessageExt>();
msgs.add(msg);
MessageQueue mq = new MessageQueue();
mq.setBrokerName(brokerName);
mq.setTopic(msg.getTopic());
mq.setQueueId(msg.getQueueId());
ConsumeOrderlyContext context = new ConsumeOrderlyContext(mq);
final long beginTime = System.currentTimeMillis();
log.info("consumeMessageDirectly receive new messge: {}", msg);
try {
ConsumeOrderlyStatus status = this.messageListener.consumeMessage(msgs, context);
if (status != null) {
switch (status) {
case COMMIT:
result.setConsumeResult(CMResult.CR_COMMIT);
break;
case ROLLBACK:
result.setConsumeResult(CMResult.CR_ROLLBACK);
break;
case SUCCESS:
result.setConsumeResult(CMResult.CR_SUCCESS);
break;
case SUSPEND_CURRENT_QUEUE_A_MOMENT:
result.setConsumeResult(CMResult.CR_LATER);
break;
default:
break;
}
}
else {
result.setConsumeResult(CMResult.CR_RETURN_NULL);
}
}
catch (Throwable e) {
result.setConsumeResult(CMResult.CR_THROW_EXCEPTION);
result.setRemark(RemotingHelper.exceptionSimpleDesc(e));
log.warn(String.format("consumeMessageDirectly exception: %s Group: %s Msgs: %s MQ: %s",//
RemotingHelper.exceptionSimpleDesc(e),//
ConsumeMessageOrderlyService.this.consumerGroup,//
msgs,//
mq), e);
}
result.setAutoCommit(context.isAutoCommit());
result.setSpentTimeMills(System.currentTimeMillis() - beginTime);
log.info("consumeMessageDirectly Result: {}", result);
return result;
}
开发者ID:y123456yz,项目名称:reading-and-annotate-rocketmq-3.4.6,代码行数:61,代码来源:ConsumeMessageOrderlyService.java
示例10: processConsumeResult
import com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyContext; //导入依赖的package包/类
public boolean processConsumeResult(//
final List<MessageExt> msgs, //
final ConsumeOrderlyStatus status, //
final ConsumeOrderlyContext context, //
final ConsumeRequest consumeRequest//
) {
boolean continueConsume = true;
long commitOffset = -1L;
// 非事务方式,自动提交
if (context.isAutoCommit()) {
switch (status) {
case COMMIT:
case ROLLBACK:
log.warn(
"the message queue consume result is illegal, we think you want to ack these message {}",
consumeRequest.getMessageQueue());
case SUCCESS:
commitOffset = consumeRequest.getProcessQueue().commit();
// 统计信息
this.getConsumerStat().getConsumeMsgOKTotal().addAndGet(msgs.size());
break;
case SUSPEND_CURRENT_QUEUE_A_MOMENT:
consumeRequest.getProcessQueue().makeMessageToCosumeAgain(msgs);
this.submitConsumeRequestLater(//
consumeRequest.getProcessQueue(), //
consumeRequest.getMessageQueue(), //
context.getSuspendCurrentQueueTimeMillis());
continueConsume = false;
// 统计信息
this.getConsumerStat().getConsumeMsgFailedTotal().addAndGet(msgs.size());
break;
default:
break;
}
}
// 事务方式,由用户来控制提交回滚
else {
switch (status) {
case SUCCESS:
// 统计信息
this.getConsumerStat().getConsumeMsgOKTotal().addAndGet(msgs.size());
break;
case COMMIT:
commitOffset = consumeRequest.getProcessQueue().commit();
// 统计信息
this.getConsumerStat().getConsumeMsgOKTotal().addAndGet(msgs.size());
break;
case ROLLBACK:
// 如果Rollback后,最好suspend一会儿再消费,防止应用无限Rollback下去
consumeRequest.getProcessQueue().rollback();
this.submitConsumeRequestLater(//
consumeRequest.getProcessQueue(), //
consumeRequest.getMessageQueue(), //
context.getSuspendCurrentQueueTimeMillis());
continueConsume = false;
// 统计信息
this.getConsumerStat().getConsumeMsgFailedTotal().addAndGet(msgs.size());
break;
case SUSPEND_CURRENT_QUEUE_A_MOMENT:
consumeRequest.getProcessQueue().makeMessageToCosumeAgain(msgs);
this.submitConsumeRequestLater(//
consumeRequest.getProcessQueue(), //
consumeRequest.getMessageQueue(), //
context.getSuspendCurrentQueueTimeMillis());
continueConsume = false;
// 统计信息
this.getConsumerStat().getConsumeMsgFailedTotal().addAndGet(msgs.size());
break;
default:
break;
}
}
if (commitOffset >= 0) {
this.defaultMQPushConsumerImpl.getOffsetStore().updateOffset(consumeRequest.getMessageQueue(),
commitOffset, false);
}
return continueConsume;
}
开发者ID:diwayou,项目名称:rocketmq-all-trans,代码行数:82,代码来源:ConsumeMessageOrderlyService.java
示例11: main
import com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyContext; //导入依赖的package包/类
public static void main(String[] args) throws MQClientException {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_3");
/**
* 设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费<br>
* 如果非第一次启动,那么按照上次消费的位置继续消费
*/
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.subscribe("TopicTest", "TagA || TagC || TagD");
consumer.registerMessageListener(new MessageListenerOrderly() {
AtomicLong consumeTimes = new AtomicLong(0);
@Override
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
context.setAutoCommit(false);
System.out.println(Thread.currentThread().getName() + " Receive New Messages: " + msgs);
this.consumeTimes.incrementAndGet();
if ((this.consumeTimes.get() % 2) == 0) {
return ConsumeOrderlyStatus.SUCCESS;
}
else if ((this.consumeTimes.get() % 3) == 0) {
return ConsumeOrderlyStatus.ROLLBACK;
}
else if ((this.consumeTimes.get() % 4) == 0) {
return ConsumeOrderlyStatus.COMMIT;
}
else if ((this.consumeTimes.get() % 5) == 0) {
context.setSuspendCurrentQueueTimeMillis(3000);
return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
}
return ConsumeOrderlyStatus.SUCCESS;
}
});
consumer.start();
System.out.println("Consumer Started.");
}
开发者ID:diwayou,项目名称:rocketmq-all-trans,代码行数:42,代码来源:Consumer.java
注:本文中的com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyContext类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论