for (int i = 0; i < 5; i++) { //Create a message instance, specifying topic, tag and message body. Message msg = new Message(topic/* Topic */, tags /* Tag */, (message + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */ ); //Call send message to deliver message to one of brokers. SendResult sendResult = producermq.send(msg); } //Shut down once the producer instance is not longer in use. System.out.println("发送完毕"); }
producermq.setRetryTimesWhenSendAsyncFailed(0); for (int i = 0; i < 100; i++) { final int index = i; //Create a message instance, specifying topic, tag and message body. Message msg = new Message(topic, tags, msgkey, message.getBytes(RemotingHelper.DEFAULT_CHARSET)); producermq.send(msg, newSendCallback() {
publicvoidonSuccess(SendResult sendResult) { System.out.printf("%-10d OK %s %n", index, sendResult.getMsgId()); }
publicvoidonException(Throwable e) { System.out.printf("%-10d Exception %s %n", index, e); e.printStackTrace(); } }); } //Shut down once the producer instance is not longer in use.
}
/** * @Description: 传输 * @Param: * @param producermq * @param topic * @param tags * @param message * @return: void * @Author: 虾 * @Date: 2019/12/16 0:38 */ publicstaticvoid OnewayProducer(DefaultMQProducer producermq,String topic,String tags,String message) throws Exception { for (int i = 0; i < 100; i++) { //Create a message instance, specifying topic, tag and message body. Message msg = new Message(topic, tags , (message + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */ ); //Call send message to deliver message to one of brokers. producermq.sendOneway(msg);
} //Shut down once the producer instance is not longer in use.