本文整理汇总了Java中org.apache.rocketmq.client.producer.SendResult类的典型用法代码示例。如果您正苦于以下问题:Java SendResult类的具体用法?Java SendResult怎么用?Java SendResult使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SendResult类属于org.apache.rocketmq.client.producer包,在下文中一共展示了SendResult类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSendMessageSync_Success
import org.apache.rocketmq.client.producer.SendResult; //导入依赖的package包/类
@Test
public void testSendMessageSync_Success() throws InterruptedException, RemotingException, MQBrokerException {
doAnswer(new Answer() {
@Override public Object answer(InvocationOnMock mock) throws Throwable {
RemotingCommand request = mock.getArgument(1);
return createSuccessResponse(request);
}
}).when(remotingClient).invokeSync(anyString(), any(RemotingCommand.class), anyLong());
SendMessageRequestHeader requestHeader = createSendMessageRequestHeader();
SendResult sendResult = mqClientAPI.sendMessage(brokerAddr, brokerName, msg, requestHeader,
3 * 1000, CommunicationMode.SYNC, new SendMessageContext(), defaultMQProducerImpl);
assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
assertThat(sendResult.getOffsetMsgId()).isEqualTo("123");
assertThat(sendResult.getQueueOffset()).isEqualTo(123L);
assertThat(sendResult.getMessageQueue().getQueueId()).isEqualTo(1);
}
示例2: sendMessage
import org.apache.rocketmq.client.producer.SendResult; //导入依赖的package包/类
/**
* 发送消息【同步】
* @param message
* @return
*/
public String sendMessage(String message){
DefaultMQProducer producer=new DefaultMQProducer(RocketMQConfiguration.ROCKETMQ_GROUP);
producer.setNamesrvAddr(RocketMQConfiguration.ROCKETMQ_NAMESRV);
producer.setVipChannelEnabled(false);
try {
producer.start();
for (int i = 0; i < 100; i++) {
SendResult sendResult = producer.send(new Message(RocketMQConfiguration.ROCKETMQ_TOPIC, message!=null?message.getBytes():null));
System.out.println(sendResult);
}
return null;
} catch (MQClientException|InterruptedException| RemotingException|MQBrokerException e) {
e.printStackTrace();
System.exit(1);
}finally {
producer.shutdown();
}
return null;
}
示例3: sendFilterMessage
import org.apache.rocketmq.client.producer.SendResult; //导入依赖的package包/类
/**
* 发送消息
* @return
*/
public String sendFilterMessage(){
DefaultMQProducer producer=new DefaultMQProducer(RocketMQConfiguration.ROCKETMQ_GROUP);
producer.setNamesrvAddr(RocketMQConfiguration.ROCKETMQ_NAMESRV);
producer.setVipChannelEnabled(false);
try {
producer.start();
for (int i = 0; i <10 ; i++) {
Message message1 = new Message(RocketMQConfiguration.ROCKETMQ_TOPIC,"filter",null, ("lovecws"+i).getBytes());
message1.putUserProperty("a",String.valueOf(i));
SendResult sendResult = producer.send(message1);
System.out.println(sendResult);
}
return null;
} catch (MQClientException |InterruptedException| RemotingException |MQBrokerException e) {
e.printStackTrace();
System.exit(1);
}finally {
producer.shutdown();
}
return null;
}
示例4: sendAsyncMessage
import org.apache.rocketmq.client.producer.SendResult; //导入依赖的package包/类
/**
* 发送异步消息
* @throws InterruptedException
* @throws RemotingException
* @throws MQClientException
* @throws MQBrokerException
*/
@Benchmark
@BenchmarkMode(Mode.Throughput)
public void sendAsyncMessage() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
producer.send(new Message(RocketMQConfiguration.ROCKETMQ_TOPIC,message), new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
//System.out.println("receive message"+sendResult);
}
@Override
public void onException(Throwable throwable) {
//System.out.println("receive message error"+throwable.getLocalizedMessage());
}
});
}
示例5: syncSend
import org.apache.rocketmq.client.producer.SendResult; //导入依赖的package包/类
/**
* 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);
}
}
示例6: syncSendOrderly
import org.apache.rocketmq.client.producer.SendResult; //导入依赖的package包/类
/**
* 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);
}
}
示例7: main
import org.apache.rocketmq.client.producer.SendResult; //导入依赖的package包/类
public static void main(String[] args) throws MQClientException, InterruptedException {
DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
producer.start();
for (int i = 0; i < 1; i++)
try {
{
Message msg = new Message("TopicTest1",
"TagA",
"key113",
"Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
SendResult sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult);
QueryResult queryMessage =
producer.queryMessage("TopicTest1", "key113", 10, 0, System.currentTimeMillis());
for (MessageExt m : queryMessage.getMessageList()) {
System.out.printf("%s%n", m);
}
}
} catch (Exception e) {
e.printStackTrace();
}
producer.shutdown();
}
示例8: main
import org.apache.rocketmq.client.producer.SendResult; //导入依赖的package包/类
public static void main(String[] args) throws MQClientException, InterruptedException {
DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
producer.start();
for (int i = 0; i < 10000000; i++)
try {
{
Message msg = new Message("TopicTest",
"TagA",
"OrderID188",
"Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
SendResult sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult);
}
} catch (Exception e) {
e.printStackTrace();
}
producer.shutdown();
}
示例9: main
import org.apache.rocketmq.client.producer.SendResult; //导入依赖的package包/类
public static void main(String[] args) throws MQClientException, InterruptedException {
DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
producer.start();
try {
for (int i = 0; i < 6000000; i++) {
Message msg = new Message("TopicFilter7",
"TagA",
"OrderID001",
"Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
msg.putUserProperty("SequenceId", String.valueOf(i));
SendResult sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult);
}
} catch (Exception e) {
e.printStackTrace();
}
producer.shutdown();
}
示例10: sendMsg
import org.apache.rocketmq.client.producer.SendResult; //导入依赖的package包/类
private void sendMsg(final DefaultMQAdminExt defaultMQAdminExt, final DefaultMQProducer defaultMQProducer,
final String msgId) throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
try {
MessageExt msg = defaultMQAdminExt.viewMessage(msgId);
if (msg != null) {
// resend msg by id
System.out.printf("prepare resend msg. originalMsgId=" + msgId);
SendResult result = defaultMQProducer.send(msg);
System.out.printf("%s", result);
} else {
System.out.printf("no message. msgId=" + msgId);
}
} catch (Exception e) {
e.printStackTrace();
}
}
示例11: execute
import org.apache.rocketmq.client.producer.SendResult; //导入依赖的package包/类
@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=" + result);
}
} catch (Exception e) {
throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
} finally {
producer.shutdown();
}
}
示例12: testBatchSend_ViewMessage
import org.apache.rocketmq.client.producer.SendResult; //导入依赖的package包/类
@Test
public void testBatchSend_ViewMessage() throws Exception {
List<Message> messageList = new ArrayList<>();
int batchNum = 100;
for (int i = 0; i < batchNum; i++) {
messageList.add(new Message(topic, RandomUtils.getStringByUUID().getBytes()));
}
DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
SendResult sendResult = producer.send(messageList);
Assert.assertEquals(SendStatus.SEND_OK, sendResult.getSendStatus());
String[] offsetIds = sendResult.getOffsetMsgId().split(",");
String[] msgIds = sendResult.getMsgId().split(",");
Assert.assertEquals(messageList.size(), offsetIds.length);
Assert.assertEquals(messageList.size(), msgIds.length);
Thread.sleep(2000);
for (int i = 0; i < 3; i++) {
producer.viewMessage(offsetIds[random.nextInt(batchNum)]);
}
for (int i = 0; i < 3; i++) {
producer.viewMessage(topic, msgIds[random.nextInt(batchNum)]);
}
}
示例13: sendAsync
import org.apache.rocketmq.client.producer.SendResult; //导入依赖的package包/类
private static void sendAsync() throws MqWapperException, MqBussinessException
{
producer.sendMsg("topic20170118", "id3", "UU", new SendCallback() {
@Override
public void onSuccess(SendResult sendResult)
{
System.out.println("success");
}
@Override
public void onException(Throwable e)
{
System.out.println("failed");
}
});
}
示例14: sendAsyncUseConfig
import org.apache.rocketmq.client.producer.SendResult; //导入依赖的package包/类
private static void sendAsyncUseConfig() throws MqWapperException, MqBussinessException
{
EasyMQMessageConfig config = new EasyMQMessageConfig("topic20170118", "id2", "onlyU");
config.setTransferMode(ProducerTransferMode.ASYNC);
config.setCallback(new SendCallback() {
@Override
public void onSuccess(SendResult sendResult)
{
System.out.println("success");
}
@Override
public void onException(Throwable e)
{
System.out.println("failed");
}
});
producer.sendMsg(config);
}
示例15: syncSendWithDelay
import org.apache.rocketmq.client.producer.SendResult; //导入依赖的package包/类
/**
* 同步发送消息
* @param topic topic
* @param tag tag
* @param msgObj 消息体
* @param delayTimeLevel 默认延迟等级 : 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h, 传入1代表1s, 2代表5s, 以此类推
* @throws MQException 消息异常
*/
public void syncSendWithDelay(String topic, String tag, Object msgObj, int delayTimeLevel) throws MQException {
if(null == msgObj) {
return;
}
Message delayedMsg = genMessage(topic, tag, msgObj);
try {
if(delayTimeLevel > 0 && delayTimeLevel <= DELAY_ARRAY.length) {
delayedMsg.setDelayTimeLevel(delayTimeLevel);
}
SendResult sendResult = producer.send(delayedMsg);
log.info("sync send rocketmq message with delay, messageId : {}, default delay interval: {}", sendResult.getMsgId(), DELAY_ARRAY[delayTimeLevel-1]);
this.doAfterSyncSend(delayedMsg, sendResult);
} catch (Exception e) {
log.error("消息发送失败,topic : {}, msgObj {}", delayedMsg.getTopic(), msgObj);
throw new MQException("消息发送失败,topic :" + topic + ",e:" + e.getMessage());
}
}