use of org.apache.rocketmq.common.message.Message in project spring-boot-starter-samples by vindell.
the class SimpleSyncProducer method main.
public static void main(String[] args) throws Exception {
// Instantiate with a producer group name.
DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
// Launch the instance.
producer.start();
for (int i = 0; i < 100; i++) {
// Create a message instance, specifying topic, tag and message body.
Message msg = new Message("TopicTest", /* Topic */
"TagA", /* Tag */
("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
// Call send message to deliver message to one of brokers.
SendResult sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult);
}
// Shut down once the producer instance is not longer in use.
producer.shutdown();
}
use of org.apache.rocketmq.common.message.Message in project spring-boot-starter-samples by vindell.
the class ProducerController method sendTransactionMsg.
@RequestMapping(value = "/sendTransactionMsg/{id}/{arg1}", method = RequestMethod.GET)
public String sendTransactionMsg(@PathVariable String id, @PathVariable String arg1) {
SendResult sendResult = null;
try {
// 构造消息
Message msg = new // topic
Message(// topic
"test", // tag
"TagA", // key
"OrderID11111", // body
id.getBytes());
// 发送事务消息,LocalTransactionExecute的executeLocalTransactionBranch方法中执行本地逻辑
sendResult = sendMessageInTransaction(msg, (Message msg1, Object arg) -> {
int value = Integer.valueOf(arg.toString());
System.out.println("执行本地事务(结合自身的业务逻辑)。。。完成");
if (value == 0) {
throw new RuntimeException("Could not find db");
} else if ((value % 5) == 0) {
return LocalTransactionState.ROLLBACK_MESSAGE;
} else if ((value % 4) == 0) {
return LocalTransactionState.COMMIT_MESSAGE;
}
return LocalTransactionState.ROLLBACK_MESSAGE;
}, arg1);
System.out.println(sendResult);
} catch (Exception e) {
e.printStackTrace();
}
return sendResult.toString();
}
use of org.apache.rocketmq.common.message.Message in project spring-boot-starter-samples by vindell.
the class BatchProducer method main.
public static void main(String[] args) throws Exception {
// Instantiate with a producer group name.
DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup");
producer.setRetryTimesWhenSendAsyncFailed(0);
// Launch the instance.
producer.start();
String topic = "BatchTest";
List<Message> messages = new ArrayList<Message>();
messages.add(new Message(topic, "TagA", "OrderID001", "Hello world 0".getBytes()));
messages.add(new Message(topic, "TagA", "OrderID002", "Hello world 1".getBytes()));
messages.add(new Message(topic, "TagA", "OrderID003", "Hello world 2".getBytes()));
try {
producer.send(messages);
} catch (Exception e) {
e.printStackTrace();
// handle the error
}
// then you could split the large list into small ones:
ListSplitter splitter = new ListSplitter(messages);
while (splitter.hasNext()) {
try {
List<Message> listItem = splitter.next();
producer.send(listItem);
} catch (Exception e) {
e.printStackTrace();
// handle the error
}
}
// Shut down once the producer instance is not longer in use.
producer.shutdown();
}
use of org.apache.rocketmq.common.message.Message in project rocketmq-externals by apache.
the class TopicServiceImpl method sendTopicMessageRequest.
@Override
public SendResult sendTopicMessageRequest(SendTopicMessageRequest sendTopicMessageRequest) {
DefaultMQProducer producer = new DefaultMQProducer(MixAll.SELF_TEST_PRODUCER_GROUP);
producer.setInstanceName(String.valueOf(System.currentTimeMillis()));
producer.setNamesrvAddr(rMQConfigure.getNamesrvAddr());
try {
producer.start();
Message msg = new Message(sendTopicMessageRequest.getTopic(), sendTopicMessageRequest.getTag(), sendTopicMessageRequest.getKey(), sendTopicMessageRequest.getMessageBody().getBytes());
return producer.send(msg);
} catch (Exception e) {
throw Throwables.propagate(e);
} finally {
producer.shutdown();
}
}
use of org.apache.rocketmq.common.message.Message in project rocketmq-externals by apache.
the class RocketMQSink method prepareMessage.
// Mapping: from storm tuple -> rocketmq Message
private Message prepareMessage(IN input) {
String topic = topicSelector.getTopic(input);
String tag = topicSelector.getTag(input) != null ? topicSelector.getTag(input) : "";
byte[] k = serializationSchema.serializeKey(input);
String key = k != null ? new String(k, StandardCharsets.UTF_8) : "";
byte[] value = serializationSchema.serializeValue(input);
Validate.notNull(topic, "the message topic is null");
Validate.notNull(value, "the message body is null");
Message msg = new Message(topic, tag, key, value);
return msg;
}
Aggregations