Search in sources :

Example 1 with SendCallback

use of org.apache.rocketmq.client.producer.SendCallback 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 2 with SendCallback

use of org.apache.rocketmq.client.producer.SendCallback in project spring-boot-starter-samples by vindell.

the class SimpleAsyncProducer method main.

public static void main(String[] args) throws Exception {
    // Instantiate with a producer group name.
    DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup");
    // Launch the instance.
    producer.start();
    producer.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("TopicTest", "TagA", "OrderID188", "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
        producer.send(msg, new SendCallback() {

            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.printf("%-10d OK %s %n", index, sendResult.getMsgId());
            }

            @Override
            public void onException(Throwable e) {
                System.out.printf("%-10d Exception %s %n", index, e);
                e.printStackTrace();
            }
        });
    }
    // Shut down once the producer instance is not longer in use.
    producer.shutdown();
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) SendCallback(org.apache.rocketmq.client.producer.SendCallback)

Aggregations

SendCallback (org.apache.rocketmq.client.producer.SendCallback)2 SendResult (org.apache.rocketmq.client.producer.SendResult)2 Message (org.apache.rocketmq.common.message.Message)2 MQClientException (org.apache.rocketmq.client.exception.MQClientException)1 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)1