@Test public void getErrorAfterConnectionReset() throws Exception { class TestConnection extends Connection { public TestConnection() { super("localhost", 6379); } @Override protected Connection sendCommand(ProtocolCommand cmd, byte[]... args) { return super.sendCommand(cmd, args); } } TestConnection conn = new TestConnection(); try { conn.sendCommand(Command.HMSET, new byte[1024 * 1024 + 1][0]); fail("Should throw exception"); } catch (JedisConnectionException jce) { assertEquals("ERR Protocol error: invalid multibulk length", jce.getMessage()); } }
protected Connection sendCommand(final ProtocolCommand cmd, final String... args) { final byte[][] bargs = new byte[args.length][]; for (int i = 0; i < args.length; i++) { bargs[i] = SafeEncoder.encode(args[i]); } return sendCommand(cmd, bargs); }
protected Connection sendCommand(final ProtocolCommand cmd, final byte[]... args) { try { //统计开始 UsefulDataModel costModel = UsefulDataModel.getCostModel(threadLocal); costModel.setCommand(cmd.toString().toLowerCase()); costModel.setStartTime(System.currentTimeMillis()); connect(); Protocol.sendCommand(outputStream, cmd, args); return this; } catch (JedisConnectionException ex) { UsefulDataCollector.collectException(ex, getHostPort(), System.currentTimeMillis()); /* * When client send request which formed by invalid protocol, Redis send back error message * before close connection. We try to read it to provide reason of failure. */ try { String errorMessage = Protocol.readErrorLineIfPossible(inputStream); if (errorMessage != null && errorMessage.length() > 0) { ex = new JedisConnectionException(errorMessage, ex.getCause()); } } catch (Exception e) { /* * Catch any IOException or JedisConnectionException occurred from InputStream#read and just * ignore. This approach is safe because reading error message is optional and connection * will eventually be closed. */ } // Any other exceptions related to connection? broken = true; throw ex; } }
protected Connection sendCommand(final ProtocolCommand cmd, final byte[]... args) { try { connect(); Protocol.sendCommand(outputStream, cmd, args); return this; } catch (JedisConnectionException ex) { /* * When client send request which formed by invalid protocol, Redis send back error message * before close connection. We try to read it to provide reason of failure. */ try { String errorMessage = Protocol.readErrorLineIfPossible(inputStream); if (errorMessage != null && errorMessage.length() > 0) { ex = new JedisConnectionException(errorMessage, ex.getCause()); } } catch (Exception e) { /* * Catch any IOException or JedisConnectionException occurred from InputStream#read and just * ignore. This approach is safe because reading error message is optional and connection * will eventually be closed. */ } // Any other exceptions related to connection? broken = true; throw ex; } }
@Override public ProtocolCommand getCreateCommand() { return Command.CREATE; }
@Override public ProtocolCommand getAddCommand() { return Command.ADD; }
@Override public ProtocolCommand getAddHashCommand() { return Command.ADDHASH; }
@Override public ProtocolCommand getDelCommand() { return Command.DEL; }
@Override public ProtocolCommand getInfoCommand() { return Command.INFO; }
@Override public ProtocolCommand getDropCommand() { return Command.DROP; }
@Override public ProtocolCommand getSearchCommand() { return Command.SEARCH; }
@Override public ProtocolCommand getOptimizeCommand() { return Command.OPTIMIZE; }
@Override public ProtocolCommand getCreateCommand() { return ClusterCommand.CREATE; }
@Override public ProtocolCommand getAddCommand() { return ClusterCommand.ADD; }
@Override public ProtocolCommand getAddHashCommand() { return ClusterCommand.ADDHASH; }
@Override public ProtocolCommand getDelCommand() { return ClusterCommand.DEL; }
@Override public ProtocolCommand getInfoCommand() { return ClusterCommand.INFO; }
@Override public ProtocolCommand getDropCommand() { return ClusterCommand.DROP; }
@Override public ProtocolCommand getSearchCommand() { return ClusterCommand.SEARCH; }
@Override public ProtocolCommand getOptimizeCommand() { return ClusterCommand.OPTIMIZE; }
protected Connection sendCommand(final ProtocolCommand cmd) { return sendCommand(cmd, EMPTY_ARGS); }
public static void sendCommand(final RedisOutputStream os, final ProtocolCommand command, final byte[]... args) { sendCommand(os, command.getRaw(), args); }
ProtocolCommand getCreateCommand();
ProtocolCommand getAddCommand();
ProtocolCommand getAddHashCommand();
ProtocolCommand getDelCommand();
ProtocolCommand getInfoCommand();
ProtocolCommand getDropCommand();
ProtocolCommand getSearchCommand();
ProtocolCommand getOptimizeCommand();