使用WebSocket和MySQL时出现Nullpointer异常

时间:2016-05-07 15:59:31

标签: java mysql tomcat spring-websocket

如何在mysql数据库上插入记录...............

SocketClass

public class ChatConnection extends MessageInbound{
private static final Logger log = LoggerFactory.getLogger(ChatConnection.class);

 @Autowired private OnlineUserService onlineUserService;


private static final Map<String, ChatConnection> connections = new HashMap<String, ChatConnection>();

private final String sessionId;
private final Long senderSeq;
private final String userName;
private final Gson jsonProcessor;

public ChatConnection(String sessionId, Long senderSeq, String userName) {
    this.sessionId = sessionId;
    this.senderSeq = senderSeq;
    this.userName = userName;
    this.jsonProcessor = new Gson();
}

public String getSessionId() {
    return sessionId;
}
public Long getSenderSeq() {
    return senderSeq;
}
public String getUserName() {
    return userName;
}

@Override
protected void onOpen(@SuppressWarnings("deprecation") WsOutbound outbound) {
    onlineUserService.addOnlineUser(this.senderSeq, this.sessionId);
    sendConnectionInfo(outbound);
    sendStatusInfoToOtherUsers(new StatusInfoMessage(userName, StatusInfoMessage.STATUS.CONNECTED));
    connections.put(sessionId, this);

}

@Override
protected void onClose(int status) {
    sendStatusInfoToOtherUsers(new StatusInfoMessage(userName, StatusInfoMessage.STATUS.DISCONNECTED));
    connections.remove(sessionId);
}

@Override
protected void onBinaryMessage(ByteBuffer byteBuffer) throws IOException {
    throw new UnsupportedOperationException("Binary messages not supported");
}

@Override
protected void onTextMessage(CharBuffer charBuffer) throws IOException {
    final MessageInfoMessage message = jsonProcessor.fromJson(charBuffer.toString(), MessageInfoMessage.class);
    final ChatConnection destinationConnection = getDestinationUserConnection(message.getMessageInfo().getTo());
    if (destinationConnection != null) {
        final CharBuffer jsonMessage = CharBuffer.wrap(jsonProcessor.toJson(message));
        destinationConnection.getWsOutbound().writeTextMessage(jsonMessage);
    } else {
        log.warn("dont't connected");
    }
}



private void sendConnectionInfo(WsOutbound outbound) {
    final List<String> activeUsers = getActiveUsers();
    final ConnectionInfoMessage connectionInfoMessage = new ConnectionInfoMessage(userName, activeUsers);

    try {
        outbound.writeTextMessage(CharBuffer.wrap(jsonProcessor.toJson(connectionInfoMessage)));
    } catch (IOException e) {
        log.error("No se pudo enviar el mensaje", e);
    }
}

private List<String> getActiveUsers() {
    final List<String> activeUsers = new ArrayList<String>();
    for (ChatConnection connection : connections.values()) {
        activeUsers.add(connection.getUserName());
        System.out.println("Name" + connection.getUserName());
    }
    return activeUsers;
}

private void sendStatusInfoToOtherUsers(StatusInfoMessage message) {
    final Collection<ChatConnection> otherUsersConnections = getAllChatConnectionsExceptThis();
    for (ChatConnection connection : otherUsersConnections) {
        try {
            connection.getWsOutbound().writeTextMessage(CharBuffer.wrap(jsonProcessor.toJson(message)));
        } catch (IOException e) {
            log.error("No se pudo enviar el mensaje", e);
        }
    }
}

private Collection<ChatConnection> getAllChatConnectionsExceptThis() {
    final Collection<ChatConnection> allConnections = connections.values();
    allConnections.remove(this);
    return allConnections;
}

private ChatConnection getDestinationUserConnection(String destinationUser) {
    for (ChatConnection connection : connections.values()) {
        if (destinationUser.equals(connection.getUserName())) {
            return connection;
        }
    }
    return null;
}
}

OnlineUserService

public interface OnlineUserService {
    Long addOnlineUser(Long userSeq, String sessionId);
}

错误

 May 07, 2016 10:01:47 PM
 org.apache.coyote.AbstractProtocol$AbstractConnectionHandler process
     SEVERE: Error reading request, ignored

     java.lang.NullPointerException
        at com.finderbar.front.websocket.ChatConnection.onOpen(ChatConnection.java:58)
        at org.apache.catalina.websocket.StreamInbound.onUpgradeComplete(StreamInbound.java:251)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:660)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)        
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)       at java.lang.Thread.run(Unknown Source)

有没有人有想法?

0 个答案:

没有答案