前言:最近公司项目有个功能,在A模块中填写表单,B模块中进行审批,审批结果要通知回A系统进行展示,并且审批的流程是有顺序的,所以决定选用rocketMq进行实现。
代码实现:
一、生产者实现(demo形式)
try {
DefaultMQProducer producer = new DefaultMQProducer("approve_group_name");
producer.setNamesrvAddr("106.12.174.109:9876");
producer.start();
for (int i = 0; i < 5; i++) {
int approveId = i;
for(int j = 0 ; j <= 5 ; j ++){
Message msg =
new Message("ApproveTopic", "approve_"+approveId, "key" + approveId,
("approve_"+approveId+" step " + j).getBytes(RemotingHelper.DEFAULT_CHARSET));
SendResult approveSendResult = producer.send(msg, (List mqs,Message message ,Object arg)->{
Integer id = (Integer) arg;
int index = id % mqs.size();
return mqs.get(index); },
approveId);
System.out.printf("%s%n", approveSendResult);
}
}
producer.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
二、消费者实现
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("approve_group_name");
consumer.setNamesrvAddr("106.12.174.109:9876");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
consumer.subscribe("ApproveTopic", "*");
consumer.registerMessageListener(new MessageListenerOrderly() {
@Override
public ConsumeOrderlyStatus consumeMessage(List msgs, ConsumeOrderlyContext context) {
context.setAutoCommit(true);
for(MessageExt msg:msgs){
System.out.println("收到审批结果 "+new String(msg.getBody()));
}
return ConsumeOrderlyStatus.SUCCESS;
}
});
三、效果演示
1、生产者
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF65110000, offsetMsgId=6A0CAE6D00002A9F000000000003EBC8, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=0], queueOffset=12]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF65560001, offsetMsgId=6A0CAE6D00002A9F000000000003ECA3, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=0], queueOffset=13]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF65760002, offsetMsgId=6A0CAE6D00002A9F000000000003ED7E, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=0], queueOffset=14]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF659A0003, offsetMsgId=6A0CAE6D00002A9F000000000003EE59, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=0], queueOffset=15]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF65B80004, offsetMsgId=6A0CAE6D00002A9F000000000003EF34, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=0], queueOffset=16]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF65D60005, offsetMsgId=6A0CAE6D00002A9F000000000003F00F, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=0], queueOffset=17]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF65F40006, offsetMsgId=6A0CAE6D00002A9F000000000003F0EA, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=1], queueOffset=6]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF66140007, offsetMsgId=6A0CAE6D00002A9F000000000003F1C5, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=1], queueOffset=7]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF66320008, offsetMsgId=6A0CAE6D00002A9F000000000003F2A0, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=1], queueOffset=8]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF66550009, offsetMsgId=6A0CAE6D00002A9F000000000003F37B, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=1], queueOffset=9]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF6673000A, offsetMsgId=6A0CAE6D00002A9F000000000003F456, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=1], queueOffset=10]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF6690000B, offsetMsgId=6A0CAE6D00002A9F000000000003F531, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=1], queueOffset=11]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF66B0000C, offsetMsgId=6A0CAE6D00002A9F000000000003F60C, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=2], queueOffset=6]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF66CE000D, offsetMsgId=6A0CAE6D00002A9F000000000003F6E7, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=2], queueOffset=7]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF66F0000E, offsetMsgId=6A0CAE6D00002A9F000000000003F7C2, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=2], queueOffset=8]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF6723000F, offsetMsgId=6A0CAE6D00002A9F000000000003F89D, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=2], queueOffset=9]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF675C0010, offsetMsgId=6A0CAE6D00002A9F000000000003F978, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=2], queueOffset=10]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF677B0011, offsetMsgId=6A0CAE6D00002A9F000000000003FA53, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=2], queueOffset=11]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF67980012, offsetMsgId=6A0CAE6D00002A9F000000000003FB2E, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=3], queueOffset=6]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF67B50013, offsetMsgId=6A0CAE6D00002A9F000000000003FC09, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=3], queueOffset=7]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF67D00014, offsetMsgId=6A0CAE6D00002A9F000000000003FCE4, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=3], queueOffset=8]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF67EC0015, offsetMsgId=6A0CAE6D00002A9F000000000003FDBF, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=3], queueOffset=9]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF680E0016, offsetMsgId=6A0CAE6D00002A9F000000000003FE9A, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=3], queueOffset=10]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF682B0017, offsetMsgId=6A0CAE6D00002A9F000000000003FF75, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=3], queueOffset=11]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF68460018, offsetMsgId=6A0CAE6D00002A9F0000000000040050, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=0], queueOffset=18]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF68630019, offsetMsgId=6A0CAE6D00002A9F000000000004012B, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=0], queueOffset=19]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF687F001A, offsetMsgId=6A0CAE6D00002A9F0000000000040206, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=0], queueOffset=20]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF689A001B, offsetMsgId=6A0CAE6D00002A9F00000000000402E1, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=0], queueOffset=21]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF68B7001C, offsetMsgId=6A0CAE6D00002A9F00000000000403BC, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=0], queueOffset=22]
SendResult [sendStatus=SEND_OK, msgId=AC1018481C8C18B4AAC217CF68D4001D, offsetMsgId=6A0CAE6D00002A9F0000000000040497, messageQueue=MessageQueue [topic=ApproveTopic, brokerName=broker-a, queueId=0], queueOffset=23]
2、消费者
Consumer Started.
收到审批结果 approve_1 step 0
收到审批结果 approve_1 step 1
收到审批结果 approve_1 step 2
收到审批结果 approve_1 step 3
收到审批结果 approve_1 step 4
收到审批结果 approve_1 step 5
收到审批结果 approve_2 step 0
收到审批结果 approve_2 step 1
收到审批结果 approve_2 step 2
收到审批结果 approve_2 step 3
收到审批结果 approve_2 step 4
收到审批结果 approve_2 step 5
收到审批结果 approve_0 step 0
收到审批结果 approve_0 step 1
收到审批结果 approve_0 step 2
收到审批结果 approve_0 step 3
收到审批结果 approve_0 step 4
收到审批结果 approve_0 step 5
收到审批结果 approve_4 step 0
收到审批结果 approve_4 step 1
收到审批结果 approve_4 step 2
收到审批结果 approve_4 step 3
收到审批结果 approve_4 step 4
收到审批结果 approve_4 step 5
收到审批结果 approve_3 step 0
收到审批结果 approve_3 step 1
收到审批结果 approve_3 step 2
收到审批结果 approve_3 step 3
收到审批结果 approve_3 step 4
收到审批结果 approve_3 step 5
四、依赖jar包
org.apache.rocketmq
rocketmq-spring-boot-starter
2.1.1