Issue #5108 - use set based on ConcurrentHashMap and remove synchronized blocks
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
parent
d37501dce0
commit
85c4fc5335
|
@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.jsr356;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import javax.websocket.Session;
|
import javax.websocket.Session;
|
||||||
|
|
||||||
import org.eclipse.jetty.util.component.AbstractLifeCycle;
|
import org.eclipse.jetty.util.component.AbstractLifeCycle;
|
||||||
|
@ -28,38 +29,29 @@ import org.eclipse.jetty.util.component.LifeCycle;
|
||||||
|
|
||||||
public class JsrSessionTracker extends AbstractLifeCycle implements JsrSessionListener
|
public class JsrSessionTracker extends AbstractLifeCycle implements JsrSessionListener
|
||||||
{
|
{
|
||||||
private final Set<JsrSession> sessions = new HashSet<>();
|
private final Set<JsrSession> sessions = Collections.newSetFromMap(new ConcurrentHashMap<>());
|
||||||
|
|
||||||
public Set<Session> getSessions()
|
public Set<Session> getSessions()
|
||||||
{
|
{
|
||||||
synchronized (this)
|
return Collections.unmodifiableSet(new HashSet<>(sessions));
|
||||||
{
|
|
||||||
return Collections.unmodifiableSet(new HashSet<>(sessions));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSessionOpened(JsrSession session)
|
public void onSessionOpened(JsrSession session)
|
||||||
{
|
{
|
||||||
synchronized (this)
|
sessions.add(session);
|
||||||
{
|
|
||||||
sessions.add(session);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSessionClosed(JsrSession session)
|
public void onSessionClosed(JsrSession session)
|
||||||
{
|
{
|
||||||
synchronized (this)
|
sessions.remove(session);
|
||||||
{
|
|
||||||
sessions.remove(session);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doStop() throws Exception
|
protected void doStop() throws Exception
|
||||||
{
|
{
|
||||||
for (Session session : getSessions())
|
for (Session session : sessions)
|
||||||
{
|
{
|
||||||
LifeCycle.stop(session);
|
LifeCycle.stop(session);
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ import java.io.IOException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import org.eclipse.jetty.util.component.AbstractLifeCycle;
|
import org.eclipse.jetty.util.component.AbstractLifeCycle;
|
||||||
import org.eclipse.jetty.util.component.Dumpable;
|
import org.eclipse.jetty.util.component.Dumpable;
|
||||||
|
@ -29,40 +30,31 @@ import org.eclipse.jetty.util.component.LifeCycle;
|
||||||
|
|
||||||
public class SessionTracker extends AbstractLifeCycle implements WebSocketSessionListener, Dumpable
|
public class SessionTracker extends AbstractLifeCycle implements WebSocketSessionListener, Dumpable
|
||||||
{
|
{
|
||||||
private final Set<WebSocketSession> sessions = new HashSet<>();
|
private final Set<WebSocketSession> sessions = Collections.newSetFromMap(new ConcurrentHashMap<>());
|
||||||
|
|
||||||
public Set<WebSocketSession> getSessions()
|
public Set<WebSocketSession> getSessions()
|
||||||
{
|
{
|
||||||
synchronized (this)
|
return Collections.unmodifiableSet(new HashSet<>(sessions));
|
||||||
{
|
|
||||||
return Collections.unmodifiableSet(new HashSet<>(sessions));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSessionCreated(WebSocketSession session)
|
public void onSessionCreated(WebSocketSession session)
|
||||||
{
|
{
|
||||||
LifeCycle.start(session);
|
LifeCycle.start(session);
|
||||||
synchronized (this)
|
sessions.add(session);
|
||||||
{
|
|
||||||
sessions.add(session);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSessionClosed(WebSocketSession session)
|
public void onSessionClosed(WebSocketSession session)
|
||||||
{
|
{
|
||||||
synchronized (this)
|
sessions.remove(session);
|
||||||
{
|
|
||||||
sessions.remove(session);
|
|
||||||
}
|
|
||||||
LifeCycle.stop(session);
|
LifeCycle.stop(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doStop() throws Exception
|
protected void doStop() throws Exception
|
||||||
{
|
{
|
||||||
for (WebSocketSession session : getSessions())
|
for (WebSocketSession session : sessions)
|
||||||
{
|
{
|
||||||
LifeCycle.stop(session);
|
LifeCycle.stop(session);
|
||||||
}
|
}
|
||||||
|
@ -72,6 +64,6 @@ public class SessionTracker extends AbstractLifeCycle implements WebSocketSessio
|
||||||
@Override
|
@Override
|
||||||
public void dump(Appendable out, String indent) throws IOException
|
public void dump(Appendable out, String indent) throws IOException
|
||||||
{
|
{
|
||||||
Dumpable.dumpObjects(out, indent, this, getSessions());
|
Dumpable.dumpObjects(out, indent, this, sessions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue