private void clearUpTo(final int lastReceivedCommandID) { final int numberToClear = 1 + lastReceivedCommandID - firstStoredCommandID; if (numberToClear == -1) { throw HornetQClientMessageBundle.BUNDLE.invalidCommandID(lastReceivedCommandID); } int sizeToFree = 0; for (int i = 0; i < numberToClear; i++) { final Packet packet = resendCache.poll(); if (packet == null) { HornetQClientLogger.LOGGER.cannotFindPacketToClear( lastReceivedCommandID, firstStoredCommandID); firstStoredCommandID = lastReceivedCommandID + 1; return; } if (packet.getType() != PacketImpl.PACKETS_CONFIRMED) { sizeToFree += packet.getPacketSize(); } if (commandConfirmationHandler != null) { commandConfirmationHandler.commandConfirmed(packet); } } firstStoredCommandID += numberToClear; }
public void returnBlocking() { lock.lock(); try { response = new HornetQExceptionMessage(HornetQClientMessageBundle.BUNDLE.unblockingACall()); sendCondition.signal(); } finally { lock.unlock(); } }
public void close() { if (closed) { return; } if (!connection.isDestroyed() && !connection.removeChannel(id)) { throw HornetQClientMessageBundle.BUNDLE.noChannelToClose(id); } if (failingOver) { unlock(); } closed = true; }
/** * Due to networking issues or server issues the server may take longer to answer than expected.. * the client may timeout the call throwing an exception and the client could eventually retry * another call, but the server could then answer a previous command issuing a * class-cast-exception. The expectedPacket will be used to filter out undesirable packets that * would belong to previous calls. */ public Packet sendBlocking(final Packet packet, byte expectedPacket) throws HornetQException { String interceptionResult = invokeInterceptors(packet, interceptors, connection); if (interceptionResult != null) { // if we don't throw an exception here the client might not unblock throw HornetQClientMessageBundle.BUNDLE.interceptorRejectedPacket(interceptionResult); } if (closed) { throw HornetQClientMessageBundle.BUNDLE.connectionDestroyed(); } if (connection.getBlockingCallTimeout() == -1) { throw new IllegalStateException( "Cannot do a blocking call timeout on a server side connection"); } // Synchronized since can't be called concurrently by more than one thread and this can occur // E.g. blocking acknowledge() from inside a message handler at some time as other operation on // main thread synchronized (sendBlockingLock) { packet.setChannelID(id); final HornetQBuffer buffer = packet.encode(connection); lock.lock(); try { if (failingOver) { try { if (connection.getBlockingCallFailoverTimeout() < 0) { while (failingOver) { failoverCondition.await(); } } else { if (!failoverCondition.await( connection.getBlockingCallFailoverTimeout(), TimeUnit.MILLISECONDS)) { HornetQClientLogger.LOGGER.debug("timed-out waiting for failover condition"); } } } catch (InterruptedException e) { throw new HornetQInterruptedException(e); } } response = null; if (resendCache != null && packet.isRequiresConfirmations()) { resendCache.add(packet); } connection.getTransportConnection().write(buffer, false, false); long toWait = connection.getBlockingCallTimeout(); long start = System.currentTimeMillis(); while (!closed && (response == null || (response.getType() != PacketImpl.EXCEPTION && response.getType() != expectedPacket)) && toWait > 0) { try { sendCondition.await(toWait, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new HornetQInterruptedException(e); } if (response != null && response.getType() != PacketImpl.EXCEPTION && response.getType() != expectedPacket) { HornetQClientLogger.LOGGER.packetOutOfOrder(response, new Exception("trace")); } if (closed) { break; } final long now = System.currentTimeMillis(); toWait -= now - start; start = now; } if (response == null) { throw HornetQClientMessageBundle.BUNDLE.timedOutSendingPacket(packet.getType()); } if (response.getType() == PacketImpl.EXCEPTION) { final HornetQExceptionMessage mem = (HornetQExceptionMessage) response; HornetQException e = mem.getException(); e.fillInStackTrace(); throw e; } } finally { lock.unlock(); } return response; } }