Search in sources :

Example 36 with SendResult

use of org.apache.rocketmq.client.producer.SendResult in project rocketmq by apache.

the class MessageExceptionIT method testSendMsgWithUserProperty.

@Test
public void testSendMsgWithUserProperty() {
    Message msg = MessageFactory.getRandomMessage(topic);
    msg.putUserProperty("key", RandomUtils.getCheseWord(10 * 1024));
    SendResult sendResult = null;
    try {
        sendResult = producer.send(msg);
    } catch (Exception e) {
    }
    assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) Test(org.junit.Test)

Example 37 with SendResult

use of org.apache.rocketmq.client.producer.SendResult in project rocketmq by apache.

the class MessageExceptionIT method testSendLess128kMsg.

@Test
public void testSendLess128kMsg() {
    Message msg = new Message(topic, RandomUtils.getStringWithNumber(128 * 1024).getBytes());
    SendResult sendResult = null;
    try {
        sendResult = producer.send(msg);
    } catch (Exception e) {
    }
    assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) Test(org.junit.Test)

Example 38 with SendResult

use of org.apache.rocketmq.client.producer.SendResult in project rocketmq by apache.

the class SendMsgStatusCommand method execute.

@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    final DefaultMQProducer producer = new DefaultMQProducer("PID_SMSC", rpcHook);
    producer.setInstanceName("PID_SMSC_" + System.currentTimeMillis());
    try {
        producer.start();
        String brokerName = commandLine.getOptionValue('b').trim();
        int messageSize = commandLine.hasOption('s') ? Integer.parseInt(commandLine.getOptionValue('s')) : 128;
        int count = commandLine.hasOption('c') ? Integer.parseInt(commandLine.getOptionValue('c')) : 50;
        producer.send(buildMessage(brokerName, 16));
        for (int i = 0; i < count; i++) {
            long begin = System.currentTimeMillis();
            SendResult result = producer.send(buildMessage(brokerName, messageSize));
            System.out.printf("rt:" + (System.currentTimeMillis() - begin) + "ms, SendResult=%s", result);
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        producer.shutdown();
    }
}
Also used : SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) SendResult(org.apache.rocketmq.client.producer.SendResult) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 39 with SendResult

use of org.apache.rocketmq.client.producer.SendResult in project java-example by 1479005017.

the class HelloSender method main.

public static void main(String[] args) throws Exception {
    /**
     * 初始化
     */
    DefaultMQProducer producer = new DefaultMQProducer();
    producer.setNamesrvAddr(namesrvAddr);
    producer.setProducerGroup(producerGroup);
    /**
     * 启动
     */
    producer.start();
    /**
     * 发送消息
     */
    Message msg = new Message("hello", "hello-test", "Hello RocketMQ".getBytes(Charset.forName("UTF-8")));
    SendResult sendResult = producer.send(msg);
    System.out.printf("%s Send Message: %s, and Result: %s %n", Thread.currentThread().getName(), msg, sendResult);
    /**
     * 关闭
     */
    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)

Example 40 with SendResult

use of org.apache.rocketmq.client.producer.SendResult in project storm by apache.

the class RocketMqBolt method execute.

@Override
public void execute(Tuple input) {
    if (!batch && TupleUtils.isTick(input)) {
        return;
    }
    String topic = selector.getTopic(input);
    if (topic == null) {
        LOG.warn("skipping Message due to topic selector returned null.");
        collector.ack(input);
        return;
    }
    if (batch) {
        // batch sync sending
        try {
            if (batchHelper.shouldHandle(input)) {
                batchHelper.addBatch(input);
                messages.add(prepareMessage(input));
            }
            if (batchHelper.shouldFlush()) {
                producer.send(messages);
                batchHelper.ack();
                messages.clear();
            }
        } catch (Exception e) {
            LOG.error("Batch send messages failure!", e);
            batchHelper.fail(e);
            messages.clear();
        }
    } else {
        if (async) {
            // async sending
            try {
                producer.send(prepareMessage(input), new SendCallback() {

                    @Override
                    public void onSuccess(SendResult sendResult) {
                        collector.ack(input);
                    }

                    @Override
                    public void onException(Throwable throwable) {
                        if (throwable != null) {
                            LOG.error("Async send messages failure!", throwable);
                            collector.reportError(throwable);
                            collector.fail(input);
                        }
                    }
                });
            } catch (Exception e) {
                LOG.error("Async send messages failure!", e);
                collector.reportError(e);
                collector.fail(input);
            }
        } else {
            // sync sending, will return a SendResult
            try {
                producer.send(prepareMessage(input));
                collector.ack(input);
            } catch (Exception e) {
                LOG.error("Sync send messages failure!", e);
                collector.reportError(e);
                collector.fail(input);
            }
        }
    }
}
Also used : SendResult(org.apache.rocketmq.client.producer.SendResult) MQClientException(org.apache.rocketmq.client.exception.MQClientException) SendCallback(org.apache.rocketmq.client.producer.SendCallback)

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