ClickStreamConsumer.java 955 Bytes
package com.yoho.unions.kafka.consumer;

import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.listener.MessageListener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class ClickStreamConsumer implements MessageListener<String, String> {
	
	private static final Logger logger = LoggerFactory.getLogger(ClickStreamConsumer.class);
	
	@Autowired
	private ClickActionDispatcher dispatcher;

	@Override
	public void onMessage(ConsumerRecord<String, String> data) {
		
    	if (null == data || StringUtils.isEmpty(data.value())) {
            logger.warn("kafka onMessage no data");
            return;
        }
    	
    	JSONObject eventJson = JSON.parseObject(data.value());
    	dispatcher.dispatch(eventJson);
	}

}