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);
}
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);
}
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();
}
}
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();
}
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);
}
}
}
}
Aggregations