本文整理汇总了Java中org.apache.rocketmq.common.message.Message类的典型用法代码示例。如果您正苦于以下问题:Java Message类的具体用法?Java Message怎么用?Java Message使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Message类属于org.apache.rocketmq.common.message包,在下文中一共展示了Message类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendOneWayMessage
import org.apache.rocketmq.common.message.Message; //导入依赖的package包/类
/**
* 发送消息【one way】
* @param message
* @return
*/
public String sendOneWayMessage(String message){
DefaultMQProducer producer=new DefaultMQProducer(RocketMQConfiguration.ROCKETMQ_GROUP+"123456");
producer.setNamesrvAddr(RocketMQConfiguration.ROCKETMQ_NAMESRV);
producer.setVipChannelEnabled(false);
try {
producer.start();
producer.sendOneway(new Message(RocketMQConfiguration.ROCKETMQ_TOPIC, message != null ? message.getBytes() : null));
return null;
} catch (MQClientException|InterruptedException| RemotingException e) {
e.printStackTrace();
System.exit(1);
} finally {
producer.shutdown();
}
return null;
}
示例2: sendAsyncMessage
import org.apache.rocketmq.common.message.Message; //导入依赖的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());
}
});
}
示例3: testSendEnglishUserProp
import org.apache.rocketmq.common.message.Message; //导入依赖的package包/类
/**
* @since version3.4.6
*/
@Test
public void testSendEnglishUserProp() {
Message msg = MessageFactory.getRandomMessage(topic);
String msgKey = "jueyinKey";
String msgValue = "jueyinValue";
msg.putUserProperty(msgKey, msgValue);
RMQNormalConsumer consumer = getConsumer(nsAddr, topic, "*", new RMQNormalListner());
producer.send(msg, null);
assertThat(producer.getAllMsgBody().size()).isEqualTo(1);
consumer.getListner().waitForMessageConsume(producer.getAllMsgBody(), consumeTime);
Message sendMsg = (Message) producer.getFirstMsg();
Message recvMsg = (Message) consumer.getListner().getFirstMsg();
assertThat(recvMsg.getUserProperty(msgKey)).isEqualTo(sendMsg.getUserProperty(msgKey));
}
示例4: testSendChinaUserProp
import org.apache.rocketmq.common.message.Message; //导入依赖的package包/类
/**
* @since version3.4.6
*/
@Test
public void testSendChinaUserProp() {
Message msg = MessageFactory.getRandomMessage(topic);
String msgKey = "jueyinKey";
String msgValue = "jueyinzhi";
msg.putUserProperty(msgKey, msgValue);
RMQNormalConsumer consumer = getConsumer(nsAddr, topic, "*", new RMQNormalListner());
producer.send(msg, null);
assertThat(producer.getAllMsgBody().size()).isEqualTo(1);
consumer.getListner().waitForMessageConsume(producer.getAllMsgBody(), consumeTime);
Message sendMsg = (Message) producer.getFirstMsg();
Message recvMsg = (Message) consumer.getListner().getFirstMsg();
assertThat(recvMsg.getUserProperty(msgKey)).isEqualTo(sendMsg.getUserProperty(msgKey));
}
示例5: sendMessageBack
import org.apache.rocketmq.common.message.Message; //导入依赖的package包/类
public boolean sendMessageBack(final MessageExt msg) {
try {
// max reconsume times exceeded then send to dead letter queue.
Message newMsg = new Message(MixAll.getRetryTopic(this.defaultMQPushConsumer.getConsumerGroup()), msg.getBody());
String originMsgId = MessageAccessor.getOriginMessageId(msg);
MessageAccessor.setOriginMessageId(newMsg, UtilAll.isBlank(originMsgId) ? msg.getMsgId() : originMsgId);
newMsg.setFlag(msg.getFlag());
MessageAccessor.setProperties(newMsg, msg.getProperties());
MessageAccessor.putProperty(newMsg, MessageConst.PROPERTY_RETRY_TOPIC, msg.getTopic());
MessageAccessor.setReconsumeTime(newMsg, String.valueOf(msg.getReconsumeTimes()));
MessageAccessor.setMaxReconsumeTimes(newMsg, String.valueOf(getMaxReconsumeTimes()));
newMsg.setDelayTimeLevel(3 + msg.getReconsumeTimes());
this.defaultMQPushConsumer.getDefaultMQPushConsumerImpl().getmQClientFactory().getDefaultMQProducer().send(newMsg);
return true;
} catch (Exception e) {
log.error("sendMessageBack exception, group: " + this.consumerGroup + " msg: " + msg.toString(), e);
}
return false;
}
示例6: asyncSend
import org.apache.rocketmq.common.message.Message; //导入依赖的package包/类
public void asyncSend(int msgSize) {
this.msgSize = msgSize;
for (int i = 0; i < msgSize; i++) {
Message msg = new Message(topic, RandomUtil.getStringByUUID().getBytes());
this.asyncSend(msg);
}
}
示例7: receiveMessage
import org.apache.rocketmq.common.message.Message; //导入依赖的package包/类
/**
* 接收消息
*/
public void receiveMessage() {
//获取消费者
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(RocketMQConfiguration.ROCKETMQ_GROUP);
//设置namesrv地址【可以通过环境变量、运行参数来设置】
consumer.setNamesrvAddr(RocketMQConfiguration.ROCKETMQ_NAMESRV);
consumer.setVipChannelEnabled(false);
//设置消费者最大最小线程
consumer.setConsumeThreadMax(20);
consumer.setConsumeThreadMin(10);
try {
//订阅主题下的所有tag
consumer.subscribe(RocketMQConfiguration.ROCKETMQ_TOPIC, "*");
//程序第一次启动从消息队列头取数据
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
//随机获取队列中的消息,相应的是MessageListenerOrderly 按照消息发送顺序来接受消息
consumer.registerMessageListener(
new MessageListenerConcurrently() {
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext Context) {
Message msg = list.get(0);
System.out.println(msg.toString());
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
}
);
//启动消费端 线程会一直挂起 等待消息的到来
consumer.start();
} catch (MQClientException e) {
e.printStackTrace();
}
}
示例8: getRandomMessageListByTag
import org.apache.rocketmq.common.message.Message; //导入依赖的package包/类
public static Collection<Message> getRandomMessageListByTag(String topic, String tags, int size) {
List<Message> msgList = new ArrayList<Message>();
for (int i = 0; i < size; i++) {
msgList.add(getRandomMessageByTag(topic, tags));
}
return msgList;
}
示例9: getDelayMsg
import org.apache.rocketmq.common.message.Message; //导入依赖的package包/类
public static List<Object> getDelayMsg(String topic, int delayLevel, int msgSize) {
List<Object> msgs = new ArrayList<Object>();
for (int i = 0; i < msgSize; i++) {
Message msg = new Message(topic, RandomUtil.getStringByUUID().getBytes());
msg.setDelayTimeLevel(delayLevel);
msgs.add(msg);
}
return msgs;
}
示例10: sendMessageInTransaction
import org.apache.rocketmq.common.message.Message; //导入依赖的package包/类
@Override
public TransactionSendResult sendMessageInTransaction(final Message msg,
final LocalTransactionExecuter tranExecuter, final Object arg) throws MQClientException {
if (null == this.transactionCheckListener) {
throw new MQClientException("localTransactionBranchCheckListener is null", null);
}
return this.defaultMQProducerImpl.sendMessageInTransaction(msg, tranExecuter, arg);
}
示例11: testSend10kChinaPropMsg
import org.apache.rocketmq.common.message.Message; //导入依赖的package包/类
/**
* @since version3.4.6
*/
@Test
public void testSend10kChinaPropMsg() {
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);
}
示例12: send
import org.apache.rocketmq.common.message.Message; //导入依赖的package包/类
public void send(Message msg, MessageQueueSelector selector, Object arg, SendCallback sendCallback, long timeout)
throws MQClientException, RemotingException, InterruptedException {
try {
this.sendSelectImpl(msg, selector, arg, CommunicationMode.ASYNC, sendCallback, timeout);
} catch (MQBrokerException e) {
throw new MQClientException("unknownn exception", e);
}
}
示例13: buildMessage
import org.apache.rocketmq.common.message.Message; //导入依赖的package包/类
private static Message buildMessage(final int messageSize, final String topic) throws UnsupportedEncodingException {
Message msg = new Message();
msg.setTopic(topic);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < messageSize; i += 10) {
sb.append("hello baby");
}
msg.setBody(sb.toString().getBytes(RemotingHelper.DEFAULT_CHARSET));
return msg;
}
示例14: buildMessage
import org.apache.rocketmq.common.message.Message; //导入依赖的package包/类
private static Message buildMessage(final int messageSize) throws UnsupportedEncodingException {
Message msg = new Message();
msg.setTopic("BenchmarkTest");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < messageSize; i += 10) {
sb.append("hello baby");
}
msg.setBody(sb.toString().getBytes(RemotingHelper.DEFAULT_CHARSET));
return msg;
}
示例15: executeLocalTransactionBranch
import org.apache.rocketmq.common.message.Message; //导入依赖的package包/类
@Override
public LocalTransactionState executeLocalTransactionBranch(final Message msg, final Object arg) {
if (ischeck) {
return LocalTransactionState.UNKNOW;
}
return LocalTransactionState.COMMIT_MESSAGE;
}