use of org.apache.rocketmq.client.producer.SendResult in project rocketmq-externals by apache.
the class RocketMQSink method invoke.
@Override
public void invoke(IN input, Context context) throws Exception {
Message msg = prepareMessage(input);
if (batchFlushOnCheckpoint) {
batchList.add(msg);
return;
}
if (async) {
// async sending
try {
producer.send(msg, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
LOG.debug("Async send message success! result: {}", sendResult);
}
@Override
public void onException(Throwable throwable) {
if (throwable != null) {
LOG.error("Async send message failure!", throwable);
}
}
});
} catch (Exception e) {
LOG.error("Async send message failure!", e);
}
} else {
// sync sending, will return a SendResult
try {
SendResult result = producer.send(msg);
LOG.debug("Sync send message result: {}", result);
} catch (Exception e) {
LOG.error("Sync send message failure!", e);
}
}
}
use of org.apache.rocketmq.client.producer.SendResult in project rocketmq-externals by apache.
the class JmsBaseMessageProducer method send.
/**
* Send the message to the defined Destination success---return normally. Exception---throw out JMSException.
*
* @param destination see <CODE>Destination</CODE>
* @param message the message to be sent.
* @throws javax.jms.JMSException
*/
@Override
public void send(Destination destination, javax.jms.Message message) throws JMSException {
JmsBaseMessage jmsMsg = (JmsBaseMessage) message;
initJMSHeaders(jmsMsg, destination);
try {
if (context == null) {
throw new IllegalStateException("Context should be inited");
}
org.apache.rocketmq.common.message.Message rocketmqMsg = MessageConverter.convert2RMQMessage(jmsMsg);
MQProducer producer = producerMap.get(context.getProducerId());
if (producer == null) {
throw new Exception("producer is null ");
}
SendResult sendResult = producer.send(rocketmqMsg);
if (sendResult != null && sendResult.getSendStatus() == SendStatus.SEND_OK) {
jmsMsg.setHeader(JmsBaseConstant.JMS_MESSAGE_ID, "ID:" + sendResult.getMsgId());
} else {
throw new Exception("SendResult is " + (sendResult == null ? "null" : sendResult.toString()));
}
} catch (Exception e) {
logger.error("Send rocketmq message failure !", e);
// if fail to send the message, throw out JMSException
JMSException jmsException = new JMSException("Send rocketmq message failure!");
jmsException.setLinkedException(e);
throw jmsException;
}
}
use of org.apache.rocketmq.client.producer.SendResult in project rocketmq-externals by apache.
the class RocketMQProducer method push.
public long push(String json) throws Exception {
LOGGER.debug(json);
Message message = new Message(config.mqTopic, json.getBytes("UTF-8"));
SendResult sendResult = producer.send(message);
return sendResult.getQueueOffset();
}
use of org.apache.rocketmq.client.producer.SendResult in project rocketmq-externals by apache.
the class RocketMQTemplate method syncSendOrderly.
/**
* Same to {@link #syncSendOrderly(String, Message, String)} with send timeout specified in addition.
*
* @param destination formats: `topicName:tags`
* @param message {@link org.springframework.messaging.Message}
* @param hashKey use this key to select queue. for example: orderId, productId ...
* @param timeout send timeout with millis
* @return {@link SendResult}
*/
public SendResult syncSendOrderly(String destination, Message<?> message, String hashKey, long timeout) {
if (Objects.isNull(message) || Objects.isNull(message.getPayload())) {
log.info("syncSendOrderly failed. destination:{}, message is null ", destination);
throw new IllegalArgumentException("`message` and `message.payload` cannot be null");
}
try {
long now = System.currentTimeMillis();
org.apache.rocketmq.common.message.Message rocketMsg = convertToRocketMsg(destination, message);
SendResult sendResult = producer.send(rocketMsg, messageQueueSelector, hashKey, timeout);
long costTime = System.currentTimeMillis() - now;
log.debug("send message cost: {} ms, msgId:{}", costTime, sendResult.getMsgId());
return sendResult;
} catch (Exception e) {
log.info("syncSendOrderly failed. destination:{}, message:{} ", destination, message);
throw new MessagingException(e.getMessage(), e);
}
}
use of org.apache.rocketmq.client.producer.SendResult in project rocketmq-externals by apache.
the class RocketMQTemplate method syncSend.
/**
* Same to {@link #syncSend(String, Message)} with send timeout specified in addition.
*
* @param destination formats: `topicName:tags`
* @param message {@link org.springframework.messaging.Message}
* @param timeout send timeout with millis
* @return {@link SendResult}
*/
public SendResult syncSend(String destination, Message<?> message, long timeout) {
if (Objects.isNull(message) || Objects.isNull(message.getPayload())) {
log.info("syncSend failed. destination:{}, message is null ", destination);
throw new IllegalArgumentException("`message` and `message.payload` cannot be null");
}
try {
long now = System.currentTimeMillis();
org.apache.rocketmq.common.message.Message rocketMsg = convertToRocketMsg(destination, message);
SendResult sendResult = producer.send(rocketMsg, timeout);
long costTime = System.currentTimeMillis() - now;
log.debug("send message cost: {} ms, msgId:{}", costTime, sendResult.getMsgId());
return sendResult;
} catch (Exception e) {
log.info("syncSend failed. destination:{}, message:{} ", destination, message);
throw new MessagingException(e.getMessage(), e);
}
}
Aggregations