Search in sources :

Example 1 with RocketMQJsonSerializer

use of org.apache.rocketmq.serializer.json.RocketMQJsonSerializer in project rocketmq-externals by apache.

the class JsonProducer method main.

public static void main(String[] args) throws Exception {
    DefaultMQProducer producer = new DefaultMQProducer("producer-group-json");
    producer.setNamesrvAddr("localhost:9876");
    producer.start();
    // creating serializer for message body serializing
    RocketMQSerializer serializer = new RocketMQJsonSerializer<User>();
    for (int i = 0; i < 100; i++) {
        User user = new User();
        user.setName("tom");
        user.setAge(i);
        // creating message from user data.
        Message message = Messages.newMessage("topic-json", user, serializer);
        SendResult result = producer.send(message);
        System.out.print(result.getSendStatus() + " " + i + "\n");
        Thread.sleep(1000);
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) RocketMQJsonSerializer(org.apache.rocketmq.serializer.json.RocketMQJsonSerializer) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer)

Aggregations

DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)1 SendResult (org.apache.rocketmq.client.producer.SendResult)1 Message (org.apache.rocketmq.common.message.Message)1 RocketMQJsonSerializer (org.apache.rocketmq.serializer.json.RocketMQJsonSerializer)1