/** * Batch sending data actually */ private void flushData(List<TraceTransferBean> transBeanList) { if (transBeanList.size() == 0) { return; } // Temporary buffer StringBuilder buffer = new StringBuilder(1024); int count = 0; Set<String> keySet = new HashSet<String>(); for (TraceTransferBean bean : transBeanList) { // Keyset of message trace includes msgId of or original message keySet.addAll(bean.getTransKey()); buffer.append(bean.getTransData()); count++; // Ensure that the size of the package should not exceed the upper limit. if (buffer.length() >= traceProducer.getMaxMessageSize()) { sendTraceDataByMQ(keySet, buffer.toString()); // Clear temporary buffer after finishing buffer.delete(0, buffer.length()); keySet.clear(); count = 0; } } if (count > 0) { sendTraceDataByMQ(keySet, buffer.toString()); } transBeanList.clear(); }
/** * Validate message */ public static void checkMessage(Message msg, DefaultMQProducer defaultMQProducer) throws MQClientException { if (null == msg) { throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message is null"); } // topic Validators.checkTopic(msg.getTopic()); // body if (null == msg.getBody()) { throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body is null"); } if (0 == msg.getBody().length) { throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body length is zero"); } if (msg.getBody().length > defaultMQProducer.getMaxMessageSize()) { throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body size over max value, MAX: " + defaultMQProducer.getMaxMessageSize()); } }
/** * Validate message */ public static void checkMessage(Message msg, DefaultMQProducer defaultMQProducer) throws MQClientException { if (null == msg) { throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message is null"); } // topic Validators.checkTopic(msg.getTopic()); // body if (null == msg.getBody()) { throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body is null"); } if (0 == msg.getBody().length) { throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body length is zero"); } if (defaultMQProducer != null && msg.getBody().length > defaultMQProducer.getMaxMessageSize()) { throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body size over max value, MAX: " + defaultMQProducer.getMaxMessageSize()); } }
/** * Batch sending data actually */ private void flushData(List<TraceTransferBean> transBeanList) { if (transBeanList.size() == 0) { return; } // Temporary buffer StringBuilder buffer = new StringBuilder(1024); int count = 0; Set<String> keySet = new HashSet<String>(); for (TraceTransferBean bean : transBeanList) { // Keyset of message trace includes msgId of or original message keySet.addAll(bean.getTransKey()); buffer.append(bean.getTransData()); count++; // Ensure that the size of the package should not exceed the upper limit. if (buffer.length() >= traceProducer.getMaxMessageSize()) { sendTraceDataByMQ(keySet, buffer.toString()); // Clear temporary buffer after finishing buffer.delete(0, buffer.length()); keySet.clear(); count = 0; } } if (count > 0) { sendTraceDataByMQ(keySet, buffer.toString()); } transBeanList.clear(); }
/** * 实际批量发送数据 */ @Override public void flush() { if (transDataList.size() == 0) { return; } // 临时缓冲区 StringBuilder sb = new StringBuilder(1024); int count = 0; Set<String> keySet = new HashSet<String>(); for (OnsTraceTransferBean bean : transDataList) { keySet.addAll(bean.getTransKey()); sb.append(bean.getTransData()); count++; // 保证包的大小不要超过上限 if (count >= this.batchSize || sb.length() >= traceProducer.getMaxMessageSize()) { sendTraceDataByMQ(keySet, sb.toString()); // 发送完成,清除临时缓冲区 sb.delete(0, sb.length()); keySet.clear(); count = 0; } } if (count > 0) { sendTraceDataByMQ(keySet, sb.toString()); } this.transDataList.clear(); }
/** * Validate message */ public static void checkMessage(Message msg, DefaultMQProducer defaultMQProducer) throws MQClientException { if (null == msg) { throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message is null"); } // topic Validators.checkTopic(msg.getTopic()); // body if (null == msg.getBody()) { throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body is null"); } if (0 == msg.getBody().length) { throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body length is zero"); } if (msg.getBody().length > defaultMQProducer.getMaxMessageSize()) { throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body size over max value, MAX: " + defaultMQProducer.getMaxMessageSize()); } }
private TransactionMQProducer createTransactionMQProducer(String name, RocketMQLocalTransactionListener transactionListener, ExecutorService executorService) { Assert.notNull(producer, "Property 'producer' is required"); Assert.notNull(transactionListener, "Parameter 'transactionListener' is required"); TransactionMQProducer txProducer = new TransactionMQProducer(name); txProducer.setTransactionListener(RocketMQUtil.convert(transactionListener)); txProducer.setNamesrvAddr(producer.getNamesrvAddr()); if (executorService != null) { txProducer.setExecutorService(executorService); } txProducer.setSendMsgTimeout(producer.getSendMsgTimeout()); txProducer.setRetryTimesWhenSendFailed(producer.getRetryTimesWhenSendFailed()); txProducer.setRetryTimesWhenSendAsyncFailed(producer.getRetryTimesWhenSendAsyncFailed()); txProducer.setMaxMessageSize(producer.getMaxMessageSize()); txProducer.setCompressMsgBodyOverHowmuch(producer.getCompressMsgBodyOverHowmuch()); txProducer.setRetryAnotherBrokerWhenNotStoreOK(producer.isRetryAnotherBrokerWhenNotStoreOK()); return txProducer; } }