1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-16 17:22:06 +01:00

SMACK-382 Prevent memory leak in AdHocCommandManager by only creating the Thread if it's actually needed

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@13582 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Florian Schmaus 2013-03-22 18:15:21 +00:00 committed by flow
parent bd70a95f8c
commit 6aa195eb88

View file

@ -343,46 +343,7 @@ public class AdHocCommandManager {
PacketFilter filter = new PacketTypeFilter(AdHocCommandData.class); PacketFilter filter = new PacketTypeFilter(AdHocCommandData.class);
connection.addPacketListener(listener, filter); connection.addPacketListener(listener, filter);
// Create a thread to reap sessions. But, we'll only start it later when commands are sessionsSweeper = null;
// actually registered.
sessionsSweeper = new Thread(new Runnable() {
public void run() {
while (true) {
for (String sessionId : executingCommands.keySet()) {
LocalCommand command = executingCommands.get(sessionId);
// Since the command could be removed in the meanwhile
// of getting the key and getting the value - by a
// processed packet. We must check if it still in the
// map.
if (command != null) {
long creationStamp = command.getCreationDate();
// Check if the Session data has expired (default is
// 10 minutes)
// To remove it from the session list it waits for
// the double of the of time out time. This is to
// let
// the requester know why his execution request is
// not accepted. If the session is removed just
// after the time out, then whe the user request to
// continue the execution he will recieved an
// invalid session error and not a time out error.
if (System.currentTimeMillis() - creationStamp > SESSION_TIMEOUT * 1000 * 2) {
// Remove the expired session
executingCommands.remove(sessionId);
}
}
}
try {
Thread.sleep(1000);
}
catch (InterruptedException ie) {
// Ignore.
}
}
}
});
sessionsSweeper.setDaemon(true);
} }
/** /**
@ -486,7 +447,45 @@ public class AdHocCommandManager {
response.setStatus(Status.executing); response.setStatus(Status.executing);
executingCommands.put(sessionId, command); executingCommands.put(sessionId, command);
// See if the session reaping thread is started. If not, start it. // See if the session reaping thread is started. If not, start it.
if (!sessionsSweeper.isAlive()) { if (sessionsSweeper == null) {
sessionsSweeper = new Thread(new Runnable() {
public void run() {
while (true) {
for (String sessionId : executingCommands.keySet()) {
LocalCommand command = executingCommands.get(sessionId);
// Since the command could be removed in the meanwhile
// of getting the key and getting the value - by a
// processed packet. We must check if it still in the
// map.
if (command != null) {
long creationStamp = command.getCreationDate();
// Check if the Session data has expired (default is
// 10 minutes)
// To remove it from the session list it waits for
// the double of the of time out time. This is to
// let
// the requester know why his execution request is
// not accepted. If the session is removed just
// after the time out, then whe the user request to
// continue the execution he will recieved an
// invalid session error and not a time out error.
if (System.currentTimeMillis() - creationStamp > SESSION_TIMEOUT * 1000 * 2) {
// Remove the expired session
executingCommands.remove(sessionId);
}
}
}
try {
Thread.sleep(1000);
}
catch (InterruptedException ie) {
// Ignore.
}
}
}
});
sessionsSweeper.setDaemon(true);
sessionsSweeper.start(); sessionsSweeper.start();
} }
} }