1 16 17 package org.springframework.jms.connection; 18 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.LinkedList ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import javax.jms.Connection ; 26 import javax.jms.ConnectionFactory ; 27 import javax.jms.JMSException ; 28 import javax.jms.Session ; 29 import javax.jms.TransactionInProgressException ; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 import org.springframework.transaction.support.ResourceHolderSupport; 35 import org.springframework.util.Assert; 36 import org.springframework.util.CollectionUtils; 37 38 50 public class JmsResourceHolder extends ResourceHolderSupport { 51 52 private static final Log logger = LogFactory.getLog(JmsResourceHolder.class); 53 54 private boolean frozen; 55 56 private final ConnectionFactory connectionFactory; 57 58 private final List connections = new LinkedList (); 59 60 private final List sessions = new LinkedList (); 61 62 private final Map sessionsPerConnection = new HashMap (); 63 64 65 70 public JmsResourceHolder() { 71 this(null); 72 } 73 74 79 public JmsResourceHolder(ConnectionFactory connectionFactory) { 80 this.connectionFactory = connectionFactory; 81 this.frozen = false; 82 } 83 84 89 public JmsResourceHolder(Connection connection, Session session) { 90 this(null, connection, session); 91 } 92 93 100 public JmsResourceHolder(ConnectionFactory connectionFactory, Connection connection, Session session) { 101 this.connectionFactory = connectionFactory; 102 addConnection(connection); 103 addSession(session, connection); 104 this.frozen = true; 105 } 106 107 108 public final boolean isFrozen() { 109 return this.frozen; 110 } 111 112 public final void addConnection(Connection connection) { 113 Assert.isTrue(!this.frozen, "Cannot add Connection because JmsResourceHolder is frozen"); 114 Assert.notNull(connection, "Connection must not be null"); 115 if (!this.connections.contains(connection)) { 116 this.connections.add(connection); 117 } 118 } 119 120 public final void addSession(Session session) { 121 addSession(session, null); 122 } 123 124 public final void addSession(Session session, Connection connection) { 125 Assert.isTrue(!this.frozen, "Cannot add Session because JmsResourceHolder is frozen"); 126 Assert.notNull(session, "Session must not be null"); 127 if (!this.sessions.contains(session)) { 128 this.sessions.add(session); 129 if (connection != null) { 130 List sessions = (List ) this.sessionsPerConnection.get(connection); 131 if (sessions == null) { 132 sessions = new LinkedList (); 133 this.sessionsPerConnection.put(connection, sessions); 134 } 135 sessions.add(session); 136 } 137 } 138 } 139 140 public boolean containsSession(Session session) { 141 return this.sessions.contains(session); 142 } 143 144 145 public Connection getConnection() { 146 return (!this.connections.isEmpty() ? (Connection ) this.connections.get(0) : null); 147 } 148 149 public Connection getConnection(Class connectionType) { 150 return (Connection ) CollectionUtils.findValueOfType(this.connections, connectionType); 151 } 152 153 public Session getSession() { 154 return (!this.sessions.isEmpty() ? (Session ) this.sessions.get(0) : null); 155 } 156 157 public Session getSession(Class sessionType) { 158 return getSession(sessionType, null); 159 } 160 161 public Session getSession(Class sessionType, Connection connection) { 162 List sessions = this.sessions; 163 if (connection != null) { 164 sessions = (List ) this.sessionsPerConnection.get(connection); 165 } 166 return (Session ) CollectionUtils.findValueOfType(sessions, sessionType); 167 } 168 169 170 public void commitAll() throws JMSException { 171 for (Iterator it = this.sessions.iterator(); it.hasNext();) { 172 try { 173 ((Session ) it.next()).commit(); 174 } 175 catch (TransactionInProgressException ex) { 176 } 178 catch (javax.jms.IllegalStateException ex) { 179 } 181 } 182 } 183 184 public void closeAll() { 185 for (Iterator it = this.sessions.iterator(); it.hasNext();) { 186 try { 187 ((Session ) it.next()).close(); 188 } 189 catch (Throwable ex) { 190 logger.debug("Could not close synchronized JMS Session after transaction", ex); 191 } 192 } 193 for (Iterator it = this.connections.iterator(); it.hasNext();) { 194 Connection con = (Connection ) it.next(); 195 ConnectionFactoryUtils.releaseConnection(con, this.connectionFactory, true); 196 } 197 } 198 199 } 200 | Popular Tags |