use of org.apache.rocketmq.common.message.Message in project ignite by apache.
the class RocketMQStreamerTest method produceData.
/**
* Adds data to RocketMQ.
*
* @throws Exception If fails.
*/
private void produceData() throws Exception {
initTopic(TOPIC_NAME, TEST_IP + ":" + NAME_SERVER_PORT);
DefaultMQProducer producer = new DefaultMQProducer("testProducerGrp");
producer.setNamesrvAddr(TEST_IP + ":" + NAME_SERVER_PORT);
try {
producer.start();
for (int i = 0; i < EVT_NUM; i++) producer.send(new Message(TOPIC_NAME, "", String.valueOf(i).getBytes("UTF-8")));
} catch (Exception e) {
throw new Exception(e);
} finally {
producer.shutdown();
}
}
use of org.apache.rocketmq.common.message.Message in project rocketmq-externals by apache.
the class RocketMQConsoleTestBase method sendTestTopicMessage.
protected SendResult sendTestTopicMessage() throws InterruptedException {
if (producer == null) {
startTestMQProducer();
}
Message message = new Message(TEST_CONSOLE_TOPIC, TEST_TOPIC_MESSAGE_BODY.getBytes());
message.setKeys(Lists.<String>newArrayList(TEST_TOPIC_MESSAGE_KEY));
for (int i = 0; i < 3; i++) {
try {
return producer.send(message);
} catch (Exception ignore) {
Thread.sleep(500);
}
}
throw new RuntimeException("send message error");
}
use of org.apache.rocketmq.common.message.Message in project rocketmq-externals by apache.
the class RocketMQSink method invoke.
@Override
public void invoke(IN input, Context context) throws Exception {
Message msg = prepareMessage(input);
if (batchFlushOnCheckpoint) {
batchList.add(msg);
return;
}
if (async) {
// async sending
try {
producer.send(msg, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
LOG.debug("Async send message success! result: {}", sendResult);
}
@Override
public void onException(Throwable throwable) {
if (throwable != null) {
LOG.error("Async send message failure!", throwable);
}
}
});
} catch (Exception e) {
LOG.error("Async send message failure!", e);
}
} else {
// sync sending, will return a SendResult
try {
SendResult result = producer.send(msg);
LOG.debug("Sync send message result: {}", result);
} catch (Exception e) {
LOG.error("Sync send message failure!", e);
}
}
}
use of org.apache.rocketmq.common.message.Message in project rocketmq-externals by apache.
the class ProducerTest method main.
public static void main(String[] args) {
DefaultMQProducer producer = new DefaultMQProducer("p001");
producer.setNamesrvAddr("localhost:9876");
try {
producer.start();
} catch (MQClientException e) {
e.printStackTrace();
}
for (int i = 0; i < 10000; i++) {
Message msg = new Message("flink-source2", "", "id_" + i, ("country_X province_" + i).getBytes());
try {
producer.send(msg);
} catch (MQClientException e) {
e.printStackTrace();
} catch (RemotingException e) {
e.printStackTrace();
} catch (MQBrokerException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("send " + i);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
use of org.apache.rocketmq.common.message.Message in project rocketmq-externals by apache.
the class MessageConverter method convert2RMQMessage.
public static Message convert2RMQMessage(JmsBaseMessage jmsMsg) throws Exception {
Message rocketmqMsg = new MessageExt();
// 1. Transform message body
rocketmqMsg.setBody(MessageConverter.getContentFromJms(jmsMsg));
// 2. Transform topic and messageType
JmsBaseTopic destination = (JmsBaseTopic) jmsMsg.getHeaders().get(JmsBaseConstant.JMS_DESTINATION);
String topic = destination.getMessageTopic();
rocketmqMsg.setTopic(topic);
String messageType = destination.getMessageType();
Preconditions.checkState(!messageType.contains("||"), "'||' can not be in the destination when sending a message");
rocketmqMsg.setTags(messageType);
// 3. Transform message properties
Properties properties = initRocketMQHeaders(jmsMsg, topic, messageType);
for (String name : properties.stringPropertyNames()) {
String value = properties.getProperty(name);
if (MessageConst.PROPERTY_KEYS.equals(name)) {
rocketmqMsg.setKeys(value);
} else if (MessageConst.PROPERTY_TAGS.equals(name)) {
rocketmqMsg.setTags(value);
} else if (MessageConst.PROPERTY_DELAY_TIME_LEVEL.equals(name)) {
rocketmqMsg.setDelayTimeLevel(Integer.parseInt(value));
} else if (MessageConst.PROPERTY_WAIT_STORE_MSG_OK.equals(name)) {
rocketmqMsg.setWaitStoreMsgOK(Boolean.parseBoolean(value));
} else if (MessageConst.PROPERTY_BUYER_ID.equals(name)) {
rocketmqMsg.setBuyerId(value);
} else {
rocketmqMsg.putUserProperty(name, value);
}
}
return rocketmqMsg;
}
Aggregations