1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| public class Producer { static String[] msgs = { "15103111039,创建", "15103111065,创建", "15103111039,付款", "15103117235,创建", "15103111065,付款", "15103117235,付款", "15103111065,完成", "15103111039,推送", "15103117235,完成", "15103111039,完成" }; public static void main(String[] args) throws Exception { DefaultMQProducer p = new DefaultMQProducer("producerGroup1"); p.setNamesrvAddr("127.0.0.1:9876"); p.start();
for (String s : msgs) { String[] arr = s.split(","); long orderId = Long.parseLong(arr[0]); Message msg = new Message("Topic2", "TagA", s.getBytes());
SendResult r = p.send( msg, new MessageQueueSelector() { @Override public MessageQueue select(List<MessageQueue> list, Message message, Object o ) { long orderId = (long) o; int index = (int) orderId % list.size(); return list.get(index); } }, orderId );
System.out.println(r);
}
p.shutdown(); } }
|