Search in sources :

Example 16 with SendResult

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);
        }
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) SendCallback(org.apache.rocketmq.client.producer.SendCallback) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 17 with SendResult

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;
    }
}
Also used : MQProducer(org.apache.rocketmq.client.producer.MQProducer) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) JmsBaseMessage(org.apache.rocketmq.jms.domain.message.JmsBaseMessage) SendResult(org.apache.rocketmq.client.producer.SendResult) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 18 with SendResult

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();
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult)

Example 19 with SendResult

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);
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) SendResult(org.apache.rocketmq.client.producer.SendResult) MessagingException(org.springframework.messaging.MessagingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 20 with SendResult

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);
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) SendResult(org.apache.rocketmq.client.producer.SendResult) MessagingException(org.springframework.messaging.MessagingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

SendResult (org.apache.rocketmq.client.producer.SendResult)88 Message (org.apache.rocketmq.common.message.Message)57 MQClientException (org.apache.rocketmq.client.exception.MQClientException)39 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)37 Test (org.junit.Test)31 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)15 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)15 SendCallback (org.apache.rocketmq.client.producer.SendCallback)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)11 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)10 SendMessageContext (org.apache.rocketmq.client.hook.SendMessageContext)8 SendMessageRequestHeader (org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader)8 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)8 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)6 TransactionSendResult (org.apache.rocketmq.client.producer.TransactionSendResult)6 RemotingConnectException (org.apache.rocketmq.remoting.exception.RemotingConnectException)6 RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)6 MessageExt (org.apache.rocketmq.common.message.MessageExt)5 BytesMessage (io.openmessaging.BytesMessage)4