Search in sources :

Example 1 with RocketMQJsonDeserializer

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

the class JsonConsumer method main.

public static void main(String[] args) throws Exception {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer-group-json");
    consumer.setNamesrvAddr("localhost:9876");
    consumer.subscribe("topic-json", "*");
    // creating deserializer for message body deserializing
    RocketMQDeserializer deserializer = new RocketMQJsonDeserializer<>(User.class);
    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
            for (MessageExt messageExt : list) {
                // getting data from message.
                User user = Messages.getMessageBody(messageExt, deserializer);
                System.out.print(user.getName() + ":" + user.getAge() + "\n");
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    consumer.start();
}
Also used : ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) RocketMQJsonDeserializer(org.apache.rocketmq.serializer.json.RocketMQJsonDeserializer)

Aggregations

DefaultMQPushConsumer (org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)1 ConsumeConcurrentlyContext (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext)1 ConsumeConcurrentlyStatus (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus)1 MessageListenerConcurrently (org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently)1 MessageExt (org.apache.rocketmq.common.message.MessageExt)1 RocketMQJsonDeserializer (org.apache.rocketmq.serializer.json.RocketMQJsonDeserializer)1