remotingConnection = backupConnection;
int lcid = channel.getLastConfirmedCommandID();
Packet request = new ReattachSessionMessage(name, lcid);
Channel channel1 = backupConnection.getChannel(1, -1);
ReattachSessionResponseMessage response = (ReattachSessionResponseMessage) channel1.sendBlocking(request, PacketImpl.REATTACH_SESSION_RESP);
if (response.isReattached())
{
if (HornetQClientLogger.LOGGER.isDebugEnabled())
{
HornetQClientLogger.LOGGER.debug("ClientSession reattached fine, replaying commands");
}
// The session was found on the server - we reattached transparently ok
channel.replayCommands(response.getLastConfirmedCommandID());
}
else
{
if (HornetQClientLogger.LOGGER.isDebugEnabled())
{
HornetQClientLogger.LOGGER.debug("ClientSession couldn't be reattached, creating a new session");
}
for (ClientConsumerInternal consumer : cloneConsumers())
{
consumer.clearAtFailover();
}
// The session wasn't found on the server - probably we're failing over onto a backup server where the
// session won't exist or the target server has been restarted - in this case the session will need to be
// recreated,
// and we'll need to recreate any consumers
// It could also be that the server hasn't been restarted, but the session is currently executing close,
// and
// that
// has already been executed on the server, that's why we can't find the session- in this case we *don't*
// want
// to recreate the session, we just want to unblock the blocking call
if (!inClose && mayAttemptToFailover)
{
Packet createRequest = new CreateSessionMessage(name,
channel.getID(),
version,
username,
password,
minLargeMessageSize,
xa,
autoCommitSends,
autoCommitAcks,
preAcknowledge,
confirmationWindowSize,
defaultAddress == null ? null
: defaultAddress.toString());
boolean retry = false;
do
{
try
{
channel1.sendBlocking(createRequest, PacketImpl.CREATESESSION_RESP);
retry = false;
}
catch (HornetQException e)
{
// the session was created while its server was starting, retry it:
if (e.getType() == HornetQExceptionType.SESSION_CREATION_REJECTED)
{
HornetQClientLogger.LOGGER.retryCreateSessionSeverStarting(name);
retry = true;
// sleep a little bit to avoid spinning too much
Thread.sleep(10);
}
else
{
throw e;
}
}
}
while (retry && !inClose);
channel.clearCommands();
for (Map.Entry<Long, ClientConsumerInternal> entry : consumers.entrySet())
{
SessionQueueQueryResponseMessage queueInfo = entry.getValue().getQueueInfo();
// We try and recreate any non durable queues, since they probably won't be there unless
// they are defined in hornetq-configuration.xml
// This allows e.g. JMS non durable subs and temporary queues to continue to be used after failover
if (!queueInfo.isDurable())
{
CreateQueueMessage createQueueRequest = new CreateQueueMessage(queueInfo.getAddress(),
queueInfo.getName(),
queueInfo.getFilterString(),
false,
queueInfo.isTemporary(),
false);
sendPacketWithoutLock(createQueueRequest);
}
SessionCreateConsumerMessage createConsumerRequest = new SessionCreateConsumerMessage(entry.getKey(),
entry.getValue()
.getQueueName(),
entry.getValue()
.getFilterString(),
entry.getValue()
.isBrowseOnly(),
false);
sendPacketWithoutLock(createConsumerRequest);
int clientWindowSize = entry.getValue().getClientWindowSize();
if (clientWindowSize != 0)
{
SessionConsumerFlowCreditMessage packet = new SessionConsumerFlowCreditMessage(entry.getKey(),
clientWindowSize);
sendPacketWithoutLock(packet);
}
else
{
// https://jira.jboss.org/browse/HORNETQ-522
SessionConsumerFlowCreditMessage packet = new SessionConsumerFlowCreditMessage(entry.getKey(),
1);
sendPacketWithoutLock(packet);
}
}
if ((!autoCommitAcks || !autoCommitSends) && workDone)
{
// this is protected by a lock, so we can guarantee nothing will sneak here
// while we do our work here
rollbackOnly = true;
}
if (currentXID != null)
{
sendPacketWithoutLock(new SessionXAAfterFailedMessage(currentXID));
rollbackOnly = true;
}
// Now start the session if it was already started
if (started)
{
for (ClientConsumerInternal consumer : cloneConsumers())
{
consumer.clearAtFailover();
consumer.start();
}
Packet packet = new PacketImpl(PacketImpl.SESS_START);
packet.setChannelID(channel.getID());
Connection conn = channel.getConnection().getTransportConnection();
HornetQBuffer buffer = packet.encode(channel.getConnection());
conn.write(buffer, false, false);
}
resetCreditManager = true;