Search in sources :

Example 11 with Message

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();
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer)

Example 12 with Message

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();
}
Also used : Message(org.apache.rocketmq.common.message.Message) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 13 with Message

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();
}
Also used : Message(org.apache.rocketmq.common.message.Message) ArrayList(java.util.ArrayList) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer)

Example 14 with Message

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();
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer)

Example 15 with Message

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;
}
Also used : Message(org.apache.rocketmq.common.message.Message)

Aggregations

Message (org.apache.rocketmq.common.message.Message)29 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)13 SendResult (org.apache.rocketmq.client.producer.SendResult)12 MQClientException (org.apache.rocketmq.client.exception.MQClientException)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 Channel (org.apache.flume.Channel)2 Event (org.apache.flume.Event)2 EventDeliveryException (org.apache.flume.EventDeliveryException)2 Transaction (org.apache.flume.Transaction)2 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)2 SendCallback (org.apache.rocketmq.client.producer.SendCallback)2 MessageExt (org.apache.rocketmq.common.message.MessageExt)2 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)2 Test (org.junit.Test)2 BusinessException (com.paascloud.base.exception.BusinessException)1 MqMessage (com.paascloud.core.mq.MqMessage)1 TpcBizException (com.paascloud.provider.exceptions.TpcBizException)1 List (java.util.List)1