1 16 17 package org.springframework.jms.core; 18 19 import javax.jms.Connection ; 20 import javax.jms.ConnectionFactory ; 21 import javax.jms.Destination ; 22 import javax.jms.JMSException ; 23 import javax.jms.Message ; 24 import javax.jms.MessageConsumer ; 25 import javax.jms.MessageProducer ; 26 import javax.jms.Queue ; 27 import javax.jms.QueueConnection ; 28 import javax.jms.QueueConnectionFactory ; 29 import javax.jms.QueueSender ; 30 import javax.jms.QueueSession ; 31 import javax.jms.Session ; 32 import javax.jms.Topic ; 33 import javax.jms.TopicConnection ; 34 import javax.jms.TopicConnectionFactory ; 35 import javax.jms.TopicPublisher ; 36 import javax.jms.TopicSession ; 37 38 import org.springframework.jms.connection.JmsResourceHolder; 39 import org.springframework.jms.support.converter.SimpleMessageConverter102; 40 41 76 public class JmsTemplate102 extends JmsTemplate { 77 78 85 public JmsTemplate102() { 86 super(); 87 } 88 89 96 public JmsTemplate102(ConnectionFactory connectionFactory, boolean pubSubDomain) { 97 this(); 98 setConnectionFactory(connectionFactory); 99 setPubSubDomain(pubSubDomain); 100 afterPropertiesSet(); 101 } 102 103 111 protected void initDefaultStrategies() { 112 setMessageConverter(new SimpleMessageConverter102()); 113 } 114 115 121 public void afterPropertiesSet() { 122 super.afterPropertiesSet(); 123 124 if (isPubSubDomain()) { 129 if (!(getConnectionFactory() instanceof TopicConnectionFactory )) { 130 throw new IllegalArgumentException ( 131 "Specified a Spring JMS 1.0.2 template for topics " + 132 "but did not supply an instance of TopicConnectionFactory"); 133 } 134 } 135 else { 136 if (!(getConnectionFactory() instanceof QueueConnectionFactory )) { 137 throw new IllegalArgumentException ( 138 "Specified a Spring JMS 1.0.2 template for queues " + 139 "but did not supply an instance of QueueConnectionFactory"); 140 } 141 } 142 } 143 144 145 149 protected Connection getConnection(JmsResourceHolder holder) { 150 return holder.getConnection(isPubSubDomain() ? (Class ) TopicConnection .class : QueueConnection .class); 151 } 152 153 157 protected Session getSession(JmsResourceHolder holder) { 158 return holder.getSession(isPubSubDomain() ? (Class ) TopicSession .class : QueueSession .class); 159 } 160 161 164 protected Connection createConnection() throws JMSException { 165 if (isPubSubDomain()) { 166 return ((TopicConnectionFactory ) getConnectionFactory()).createTopicConnection(); 167 } 168 else { 169 return ((QueueConnectionFactory ) getConnectionFactory()).createQueueConnection(); 170 } 171 } 172 173 176 protected Session createSession(Connection con) throws JMSException { 177 if (isPubSubDomain()) { 178 return ((TopicConnection ) con).createTopicSession(isSessionTransacted(), getSessionAcknowledgeMode()); 179 } 180 else { 181 return ((QueueConnection ) con).createQueueSession(isSessionTransacted(), getSessionAcknowledgeMode()); 182 } 183 } 184 185 188 protected MessageProducer doCreateProducer(Session session, Destination destination) throws JMSException { 189 if (isPubSubDomain()) { 190 return ((TopicSession ) session).createPublisher((Topic ) destination); 191 } 192 else { 193 return ((QueueSession ) session).createSender((Queue ) destination); 194 } 195 } 196 197 200 protected MessageConsumer createConsumer(Session session, Destination destination, String messageSelector) 201 throws JMSException { 202 203 if (isPubSubDomain()) { 204 return ((TopicSession ) session).createSubscriber((Topic ) destination, messageSelector, isPubSubNoLocal()); 205 } 206 else { 207 return ((QueueSession ) session).createReceiver((Queue ) destination, messageSelector); 208 } 209 } 210 211 214 protected void doSend(MessageProducer producer, Message message) throws JMSException { 215 if (isPubSubDomain()) { 216 if (isExplicitQosEnabled()) { 217 ((TopicPublisher ) producer).publish(message, getDeliveryMode(), getPriority(), getTimeToLive()); 218 } 219 else { 220 ((TopicPublisher ) producer).publish(message); 221 } 222 } 223 else { 224 if (isExplicitQosEnabled()) { 225 ((QueueSender ) producer).send(message, getDeliveryMode(), getPriority(), getTimeToLive()); 226 } 227 else { 228 ((QueueSender ) producer).send(message); 229 } 230 } 231 } 232 233 239 protected boolean isClientAcknowledge(Session session) throws JMSException { 240 return (getSessionAcknowledgeMode() == Session.CLIENT_ACKNOWLEDGE); 241 } 242 243 } 244 | Popular Tags |