tidied up synchronization

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@637028 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Davies 2008-03-14 09:53:39 +00:00
parent c57bad8711
commit bbd2e47dbd
1 changed files with 115 additions and 54 deletions

View File

@ -16,9 +16,9 @@
*/ */
package org.apache.activemq.ra; package org.apache.activemq.ra;
import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import javax.jms.JMSException; import javax.jms.JMSException;
@ -45,8 +45,8 @@ public class ServerSessionPoolImpl implements ServerSessionPool {
private final ActiveMQEndpointWorker activeMQAsfEndpointWorker; private final ActiveMQEndpointWorker activeMQAsfEndpointWorker;
private final int maxSessions; private final int maxSessions;
private List<ServerSessionImpl> idleSessions = new CopyOnWriteArrayList<ServerSessionImpl>(); private List<ServerSessionImpl> idleSessions = new ArrayList<ServerSessionImpl>();
private List<ServerSessionImpl> activeSessions = new CopyOnWriteArrayList<ServerSessionImpl>(); private List<ServerSessionImpl> activeSessions = new ArrayList<ServerSessionImpl>();
private AtomicBoolean closing = new AtomicBoolean(false); private AtomicBoolean closing = new AtomicBoolean(false);
public ServerSessionPoolImpl(ActiveMQEndpointWorker activeMQAsfEndpointWorker, int maxSessions) { public ServerSessionPoolImpl(ActiveMQEndpointWorker activeMQAsfEndpointWorker, int maxSessions) {
@ -76,7 +76,9 @@ public class ServerSessionPoolImpl implements ServerSessionPool {
} catch (UnavailableException e) { } catch (UnavailableException e) {
// The container could be limiting us on the number of endpoints // The container could be limiting us on the number of endpoints
// that are being created. // that are being created.
if (LOG.isDebugEnabled()) {
LOG.debug("Could not create an endpoint.", e); LOG.debug("Could not create an endpoint.", e);
}
session.close(); session.close();
return null; return null;
} }
@ -92,17 +94,30 @@ public class ServerSessionPoolImpl implements ServerSessionPool {
/** /**
*/ */
public ServerSession getServerSession() throws JMSException { public ServerSession getServerSession() throws JMSException {
if (LOG.isDebugEnabled()) {
LOG.debug("ServerSession requested."); LOG.debug("ServerSession requested.");
}
if (closing.get()) { if (closing.get()) {
throw new JMSException("Session Pool Shutting Down."); throw new JMSException("Session Pool Shutting Down.");
} }
ServerSessionImpl ss = null;
synchronized (idleSessions) {
if (idleSessions.size() > 0) { if (idleSessions.size() > 0) {
ServerSessionImpl ss = idleSessions.remove(idleSessions.size() - 1); ss = idleSessions.remove(idleSessions.size() - 1);
}
}
if (ss != null) {
synchronized (activeSessions) {
activeSessions.add(ss); activeSessions.add(ss);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Using idle session: " + ss); LOG.debug("Using idle session: " + ss);
}
return ss; return ss;
} else { }
synchronized (activeSessions) {
// Are we at the upper limit? // Are we at the upper limit?
if (activeSessions.size() >= maxSessions) { if (activeSessions.size() >= maxSessions) {
// then reuse the already created sessions.. // then reuse the already created sessions..
@ -110,28 +125,38 @@ public class ServerSessionPoolImpl implements ServerSessionPool {
// processing. // processing.
return getExistingServerSession(); return getExistingServerSession();
} }
ServerSessionImpl ss = createServerSessionImpl(); }
ss = createServerSessionImpl();
// We may not be able to create a session due to the container // We may not be able to create a session due to the container
// restricting us. // restricting us.
if (ss == null) { if (ss == null) {
if (activeSessions.size() == 0) { synchronized (activeSessions) {
//no idle sessions, no active sessions, and we can't create a new session.... if (activeSessions.isEmpty()) {
throw new JMSException("Endpoint factory did not allow creation of any endpoints."); throw new JMSException(
"Endpoint factory did not allow creation any endpoints.");
}
} }
return getExistingServerSession(); return getExistingServerSession();
} }
synchronized (activeSessions) {
activeSessions.add(ss); activeSessions.add(ss);
LOG.debug("Created a new session: " + ss);
return ss;
} }
if (LOG.isDebugEnabled()) {
LOG.debug("Created a new session: " + ss);
}
return ss;
} }
/** /**
* @param messageDispatch the message to dispatch * @param messageDispatch
* the message to dispatch
* @throws JMSException * @throws JMSException
*/ */
private void dispatchToSession(MessageDispatch messageDispatch) throws JMSException { private void dispatchToSession(MessageDispatch messageDispatch)
throws JMSException {
ServerSession serverSession = getServerSession(); ServerSession serverSession = getServerSession();
Session s = serverSession.getSession(); Session s = serverSession.getSession();
@ -143,33 +168,54 @@ public class ServerSessionPoolImpl implements ServerSessionPool {
} else if (s instanceof ActiveMQTopicSession) { } else if (s instanceof ActiveMQTopicSession) {
session = (ActiveMQSession) s; session = (ActiveMQSession) s;
} else { } else {
activeMQAsfEndpointWorker.connection.onAsyncException(new JMSException("Session pool provided an invalid session type: " + s.getClass())); activeMQAsfEndpointWorker.connection
.onAsyncException(new JMSException(
"Session pool provided an invalid session type: "
+ s.getClass()));
} }
session.dispatch(messageDispatch); session.dispatch(messageDispatch);
serverSession.start(); serverSession.start();
} }
/** /**
* @return * @return session
*/ */
private ServerSession getExistingServerSession() { private ServerSession getExistingServerSession() {
ServerSessionImpl ss = activeSessions.remove(0); ServerSessionImpl ss = null;
if (!activeSessions.isEmpty()) {
if (activeSessions.size() > 1) {
// round robin
ss = activeSessions.remove(0);
activeSessions.add(ss); activeSessions.add(ss);
} else {
ss = activeSessions.get(0);
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("Reusing an active session: " + ss); LOG.debug("Reusing an active session: " + ss);
}
return ss; return ss;
} }
public void returnToPool(ServerSessionImpl ss) { public void returnToPool(ServerSessionImpl ss) {
if (LOG.isDebugEnabled()) {
LOG.debug("Session returned to pool: " + ss); LOG.debug("Session returned to pool: " + ss);
}
synchronized(activeSessions) {
activeSessions.remove(ss); activeSessions.remove(ss);
}
synchronized(idleSessions) {
idleSessions.add(ss); idleSessions.add(ss);
}
synchronized (closing) { synchronized (closing) {
closing.notify(); closing.notify();
} }
} }
public void removeFromPool(ServerSessionImpl ss) { public void removeFromPool(ServerSessionImpl ss) {
synchronized(activeSessions) {
activeSessions.remove(ss); activeSessions.remove(ss);
}
try { try {
ActiveMQSession session = (ActiveMQSession)ss.getSession(); ActiveMQSession session = (ActiveMQSession)ss.getSession();
List l = session.getUnconsumedMessages(); List l = session.getUnconsumedMessages();
@ -186,13 +232,19 @@ public class ServerSessionPoolImpl implements ServerSessionPool {
} }
public void close() { public void close() {
synchronized (closing) {
closing.set(true); closing.set(true);
closeIdleSessions(); closeIdleSessions();
while (activeSessions.size() > 0) { // we may have to wait erroneously 250ms if an
LOG.debug("Active Sessions = " + activeSessions.size()); // active session is removed during our wait and we
// are not notified
while (getActiveSessionSize() > 0) {
if (LOG.isDebugEnabled()) {
LOG.debug("Active Sessions = " + getActiveSessionSize());
}
try { try {
closing.wait(1000); synchronized (closing) {
closing.wait(250);
}
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
return; return;
@ -200,13 +252,16 @@ public class ServerSessionPoolImpl implements ServerSessionPool {
closeIdleSessions(); closeIdleSessions();
} }
} }
}
private void closeIdleSessions() { private void closeIdleSessions() {
synchronized(idleSessions) {
for (Iterator<ServerSessionImpl> iter = idleSessions.iterator(); iter.hasNext();) { for (Iterator<ServerSessionImpl> iter = idleSessions.iterator(); iter.hasNext();) {
ServerSessionImpl ss = iter.next(); ServerSessionImpl ss = iter.next();
ss.close(); ss.close();
} }
idleSessions.clear();
}
} }
/** /**
@ -223,4 +278,10 @@ public class ServerSessionPoolImpl implements ServerSessionPool {
this.closing.set(closing); this.closing.set(closing);
} }
private int getActiveSessionSize() {
synchronized(activeSessions) {
return activeSessions.size();
}
}
} }