1 16 17 package org.springframework.jms.listener.serversession; 18 19 import javax.jms.JMSException ; 20 import javax.jms.ServerSession ; 21 import javax.jms.Session ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import org.springframework.core.task.TaskExecutor; 27 import org.springframework.jms.support.JmsUtils; 28 import org.springframework.scheduling.timer.TimerTaskExecutor; 29 30 44 public abstract class AbstractPoolingServerSessionFactory implements ServerSessionFactory { 45 46 protected final Log logger = LogFactory.getLog(getClass()); 47 48 private TaskExecutor taskExecutor; 49 50 private int maxSize; 51 52 53 62 public void setTaskExecutor(TaskExecutor taskExecutor) { 63 this.taskExecutor = taskExecutor; 64 } 65 66 69 protected TaskExecutor getTaskExecutor() { 70 return this.taskExecutor; 71 } 72 73 76 public void setMaxSize(int maxSize) { 77 this.maxSize = maxSize; 78 } 79 80 83 public int getMaxSize() { 84 return this.maxSize; 85 } 86 87 88 96 protected final ServerSession createServerSession(ListenerSessionManager sessionManager) throws JMSException { 97 return new PoolableServerSession(sessionManager); 98 } 99 100 105 protected final void destroyServerSession(ServerSession serverSession) { 106 if (serverSession != null) { 107 ((PoolableServerSession) serverSession).close(); 108 } 109 } 110 111 112 120 protected abstract void serverSessionFinished( 121 ServerSession serverSession, ListenerSessionManager sessionManager); 122 123 124 131 private class PoolableServerSession implements ServerSession { 132 133 private final ListenerSessionManager sessionManager; 134 135 private final Session session; 136 137 private TaskExecutor taskExecutor; 138 139 private TimerTaskExecutor internalExecutor; 140 141 public PoolableServerSession(final ListenerSessionManager sessionManager) throws JMSException { 142 this.sessionManager = sessionManager; 143 this.session = sessionManager.createListenerSession(); 144 this.taskExecutor = getTaskExecutor(); 145 if (this.taskExecutor == null) { 146 this.internalExecutor = new TimerTaskExecutor(); 147 this.internalExecutor.afterPropertiesSet(); 148 this.taskExecutor = this.internalExecutor; 149 } 150 } 151 152 public Session getSession() { 153 return this.session; 154 } 155 156 public void start() { 157 this.taskExecutor.execute(new Runnable () { 158 public void run() { 159 try { 160 sessionManager.executeListenerSession(session); 161 } 162 finally { 163 serverSessionFinished(PoolableServerSession.this, sessionManager); 164 } 165 } 166 }); 167 } 168 169 public void close() { 170 if (this.internalExecutor != null) { 171 this.internalExecutor.destroy(); 172 } 173 JmsUtils.closeSession(this.session); 174 } 175 } 176 177 } 178 | Popular Tags |