SingleConnectionFactory102.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 4k
Category:

Java Develop

Development Platform:

Java

  1. package org.springframework.jms.connection;
  2. import javax.jms.Connection;
  3. import javax.jms.ConnectionFactory;
  4. import javax.jms.JMSException;
  5. import javax.jms.QueueConnectionFactory;
  6. import javax.jms.TopicConnectionFactory;
  7. /**
  8.  * A subclass of SingleConnectionFactory that uses the JMS 1.0.2 specification,
  9.  * rather than the JMS 1.1 methods used by SingleConnectionFactory itself.
  10.  * This class can be used for JMS 1.0.2 providers, offering the same API as
  11.  * SingleConnectionFactory does for JMS 1.1 providers.
  12.  *
  13.  * <p>You need to set the pubSubDomain property accordingly, as this class
  14.  * will always create either a QueueConnection or a TopicConnection.
  15.  *
  16.  * @author Juergen Hoeller
  17.  * @since 1.1
  18.  * @see #setTargetConnectionFactory
  19.  * @see #setPubSubDomain
  20.  */
  21. public class SingleConnectionFactory102 extends SingleConnectionFactory {
  22. private boolean pubSubDomain = false;
  23. /**
  24.  * Create a new SingleConnectionFactory102 for bean-style usage.
  25.  */
  26. public SingleConnectionFactory102() {
  27. super();
  28. }
  29. /**
  30.  * Create a new SingleConnectionFactory102 that always returns a single
  31.  * Connection that it will lazily create via the given target
  32.  * ConnectionFactory.
  33.  * @param connectionFactory the target ConnectionFactory
  34.  * @param pubSubDomain whether the Publish/Subscribe domain (Topics) or
  35.  * Point-to-Point domain (Queues) should be used
  36.  */
  37. public SingleConnectionFactory102(ConnectionFactory connectionFactory, boolean pubSubDomain) {
  38. setTargetConnectionFactory(connectionFactory);
  39. this.pubSubDomain = pubSubDomain;
  40. afterPropertiesSet();
  41. }
  42. /**
  43.  * Configure the factory with knowledge of the JMS domain used.
  44.  * This tells the JMS 1.0.2 provider which class hierarchy to use
  45.  * for creating Connections. Default is Point-to-Point (Queues).
  46.  * @param pubSubDomain true for Publish/Subscribe domain (Topics),
  47.  * false for Point-to-Point domain (Queues)
  48.  */
  49. public void setPubSubDomain(boolean pubSubDomain) {
  50. this.pubSubDomain = pubSubDomain;
  51. }
  52. /**
  53.  * Return whether the Publish/Subscribe domain (Topics) is used.
  54.  * Otherwise, the Point-to-Point domain (Queues) is used.
  55.  */
  56. public boolean isPubSubDomain() {
  57. return pubSubDomain;
  58. }
  59. /**
  60.  * In addition to checking if the connection factory is set, make sure
  61.  * that the supplied connection factory is of the appropriate type for
  62.  * the specified destination type: QueueConnectionFactory for queues,
  63.  * and TopicConnectionFactory for topics.
  64.  */
  65. public void afterPropertiesSet() {
  66. super.afterPropertiesSet();
  67. // Make sure that the ConnectionFactory passed is consistent.
  68. // Some provider implementations of the ConnectionFactory interface
  69. // implement both domain interfaces under the cover, so just check if
  70. // the selected domain is consistent with the type of connection factory.
  71. if (isPubSubDomain()) {
  72. if (!(getTargetConnectionFactory() instanceof TopicConnectionFactory)) {
  73. throw new IllegalArgumentException(
  74. "Specified a Spring JMS 1.0.2 SingleConnectionFactory for topics " +
  75. "but did not supply an instance of TopicConnectionFactory");
  76. }
  77. }
  78. else {
  79. if (!(getTargetConnectionFactory() instanceof QueueConnectionFactory)) {
  80. throw new IllegalArgumentException(
  81. "Specified a Spring JMS 1.0.2 SingleConnectionFactory for queues " +
  82. "but did not supply an instance of QueueConnectionFactory");
  83. }
  84. }
  85. }
  86. /**
  87.  * This implementation overrides the superclass method to use JMS 1.0.2 API.
  88.  */
  89. protected Connection doCreateConnection() throws JMSException {
  90. if (isPubSubDomain()) {
  91. return ((TopicConnectionFactory) getTargetConnectionFactory()).createTopicConnection();
  92. }
  93. else {
  94. return ((QueueConnectionFactory) getTargetConnectionFactory()).createQueueConnection();
  95. }
  96. }
  97. }