局部顺序消息小例子

局部顺序消息:

对于相同的orderId,要放在相同的queueId中,mqs.get(index)选择具体的队列。

运行结果:  例如orderId为1都放在第一个队列中。默认有4个队列


orderId:0 ,queueId: 0

orderId:1 ,queueId: 1

orderId:2 ,queueId: 2

orderId:3 ,queueId: 3

orderId:4 ,queueId: 0

orderId:0 ,queueId: 0

orderId:1 ,queueId: 1

orderId:2 ,queueId: 2

orderId:3 ,queueId: 3

orderId:4 ,queueId: 0

orderId:0 ,queueId: 0

orderId:1 ,queueId: 1

orderId:2 ,queueId: 2

orderId:3 ,queueId: 3

orderId:4 ,queueId: 0


for (int i =0; i <20; i++) {

final int orderId = i %5;

//  final  String tag=tags[i % tags.length];

    Message msg =

new Message("TopicTestjjj","","KEY" + i,

("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));

SendResult sendResult = producer.send(msg,new MessageQueueSelector() {

@Override

        public MessageQueue select(List mqs, Message msg, Object arg) {

        Integer id = (Integer) arg;

        int index = id % mqs.size();

        System.out.println("orderId:" + orderId +" ,queueId: " +         mqs.get(index).getQueueId());

        return mqs.get(index);

}

}, orderId);

//  System.out.printf("%s%n", sendResult);

}

你可能感兴趣的:(局部顺序消息小例子)