EcEntrustInnovateHandler.java 3.14 KB
package com.cjs.site.biz.ec;

import com.cjs.site.dao.user.interact.UserAttentionDao;
import com.cjs.site.model.entrust.EcEntrustInfo;
import com.cjs.site.model.user.UserAttentionInfo;
import com.cjs.site.util.lang.JsonUtil;
import com.cjs.site.util.redis.JedisTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Service("ecEntrustInnovateHandler")
public class EcEntrustInnovateHandler extends TextWebSocketHandler
{
    public static Map<WebSocketSession, List<UserAttentionInfo>> WEBSOCKET_SESSIONS = new HashMap();

    Logger log = LogManager.getLogger();

    @Autowired
    private JedisTemplate jedisTemplate;

    @Autowired
    private UserAttentionDao userAttentionDao;

    @Autowired
    private EcEntrustInfoBiz ecEntrustInfoBiz;

    @Value("${${env}.innovate.otcCodes}")
    private String innovateOtcCodes;

    public void afterConnectionEstablished(WebSocketSession session) throws Exception { super.afterConnectionEstablished(session);
    }

    protected void handleTextMessage(WebSocketSession session, TextMessage message)
            throws Exception
    {
        super.handleTextMessage(session, message);
        try
        {
            String userId = (String)message.getPayload();

            if (StringUtils.isBlank(userId)) {
                return;
            }

            String[] codes = this.innovateOtcCodes.split(",");

            String payload = this.jedisTemplate.get("entrust:entrusting");
            if (StringUtils.isNotBlank(payload)) {
                if (payload.indexOf("[") != -1) {
                    payload = payload.substring(payload.indexOf("["));
                }
                List entrusts = (List)JsonUtil.fromJson(payload, ArrayList.class,
                        EcEntrustInfo.class);

                this.ecEntrustInfoBiz.sendToInnovate(session, entrusts, codes);
            }
        } catch (Exception e) {
            this.log.error("官网买卖,用户关注藏品连接出错。", e);
            throw e;
        }
    }

    public void afterConnectionClosed(WebSocketSession session, CloseStatus status)
            throws Exception
    {
        super.afterConnectionClosed(session, status);
        WEBSOCKET_SESSIONS.remove(session);
    }

    public void handleTransportError(WebSocketSession session, Throwable exception)
            throws Exception
    {
        super.handleTransportError(session, exception);
        this.log.error("WebScoket传输出错[" + session.getId() + "]:" + exception.getMessage());
        if (session.isOpen()) {
            session.close();
        }
        WEBSOCKET_SESSIONS.remove(session);
    }
}