PresaleHandler.java 2.51 KB
package com.dili.titan.handler;

import com.dili.titan.domain.RedisKeyConstant;
import com.dili.titan.domain.gq.Presale;
import com.dili.titan.domain.log.LogHelper;
import com.dili.titan.domain.log.LogTypeEnum;
import com.dili.titan.mq.sender.TopicProducer;
import com.dili.titan.redis.JedisClient;
import com.dili.titan.service.impl.PresaleService;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * <B>Description</B> 供应信息 <br />
 * <B>Copyright</B> Copyright (c) 2014 www.diligrp.com All rights reserved. <br />
 * 本软件源代码版权归地利集团,未经许可不得任意复制与传播.<br />
 * <B>Company</B> 地利集团
 * @createTime 2014年12月23日 下午4:42:36
 * @author ywd
 */
@Component
public class PresaleHandler implements BaseHandler {

    private ObjectMapper mapper = new ObjectMapper().configure(
            DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

     @Resource
     private PresaleService presaleService;

    @Resource(name = "jedisClient")
    private JedisClient jedisClient;
    
    @Resource(name = "topicProducer")
    private TopicProducer topicProducer;
    
    @Override
    public void handle(String message) throws Exception {
        try {
            JsonNode node = mapper.readTree(message);
            int operType = node.get("oper").asInt();
            Long pid = node.get("pid").asLong();
            put(pid);
            topicProducer.sendMQPresale(pid,operType);
        } catch (Exception e) {
            LogHelper.error(LogTypeEnum.PRESALE, e, e.getMessage());
            throw e;
        }
    }
    
    /**
     * this method is 根据供应ID存储供应
     * @createTime 2014年12月22日 下午5:14:24
     * @author ywd
     */
    private void put(Long pid)throws Exception{
        try {
        	Presale presale= presaleService.findByKey(pid);
        	if(presale == null){
                LogHelper.error(LogTypeEnum.PRESALE ,
                        "can't find Presale by pid={}", pid);
                return;
        	}
        	String json = mapper.writeValueAsString(presale);
            jedisClient.hput(RedisKeyConstant.PRESALE,presale.getId().toString(), json,JedisClient.expireTime);
        } catch (Exception e) {
            LogHelper.error(LogTypeEnum.PRESALE, e,
                    "load putPresale error!pid={}", pid);
            throw e;
        }
    }
}