Search in sources :

Example 11 with MessageQueue

use of org.apache.rocketmq.common.message.MessageQueue in project spring-boot-starter-samples by vindell.

the class OrderedProducer method main.

public static void main(String[] args) throws Exception {
    // Instantiate with a producer group name.
    MQProducer producer = new DefaultMQProducer("example_group_name");
    // Launch the instance.
    producer.start();
    String[] tags = new String[] { "TagA", "TagB", "TagC", "TagD", "TagE" };
    for (int i = 0; i < 100; i++) {
        int orderId = i % 10;
        // Create a message instance, specifying topic, tag and message body.
        Message msg = new Message("TopicTestjjj", tags[i % tags.length], "KEY" + i, ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
        SendResult sendResult = producer.send(msg, new MessageQueueSelector() {

            @Override
            public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                Integer id = (Integer) arg;
                int index = id % mqs.size();
                return mqs.get(index);
            }
        }, orderId);
        System.out.printf("%s%n", sendResult);
    }
    // server shutdown
    producer.shutdown();
}
Also used : Message(org.apache.rocketmq.common.message.Message) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MQProducer(org.apache.rocketmq.client.producer.MQProducer) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MessageQueueSelector(org.apache.rocketmq.client.producer.MessageQueueSelector) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) SendResult(org.apache.rocketmq.client.producer.SendResult)

Aggregations

MessageQueue (org.apache.rocketmq.common.message.MessageQueue)11 PullResult (org.apache.rocketmq.client.consumer.PullResult)7 MessageExt (org.apache.rocketmq.common.message.MessageExt)7 MQClientException (org.apache.rocketmq.client.exception.MQClientException)5 DefaultMQPullConsumer (org.apache.rocketmq.client.consumer.DefaultMQPullConsumer)4 Date (java.util.Date)3 List (java.util.List)3 Event (org.apache.flume.Event)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Context (org.apache.flume.Context)2 Sink (org.apache.flume.Sink)2 Transaction (org.apache.flume.Transaction)2 MemoryChannel (org.apache.flume.channel.MemoryChannel)2 MultiMQAdminCmdMethod (org.apache.rocketmq.console.aspect.admin.annotation.MultiMQAdminCmdMethod)2 JSONObject (com.alibaba.fastjson.JSONObject)1 Function (com.google.common.base.Function)1 Predicate (com.google.common.base.Predicate)1