JmsTopicSendReceiveTest xref

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq;
    19
    20  import org.codehaus.activemq.util.IndentPrinter;
    21
    22  import javax.jms.Connection;
    23  import javax.jms.DeliveryMode;
    24  import javax.jms.JMSException;
    25  import javax.jms.MessageConsumer;
    26  import javax.jms.Session;
    27  import javax.jms.Topic;
    28
    29  /***
    30   * @version $Revision: 1.15 $
    31   */
    32  public class JmsTopicSendReceiveTest extends JmsSendReceiveTestSupport {
    33      protected Connection connection;
    34
    35      protected void setUp() throws Exception {
    36          super.setUp();
    37
    38          connectionFactory = createConnectionFactory();
    39          connection = createConnection();
    40          if (durable) {
    41              connection.setClientID(getClass().getName());
    42          }
    43
    44          System.out.println("Created connection: " + connection);
    45
    46          session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    47
    48          System.out.println("Created session: " + session);
    49          producer = session.createProducer(null);
    50          producer.setDeliveryMode(deliveryMode);
    51
    52          System.out.println("Created producer: " + producer + " delivery mode = " +
    53                  (deliveryMode == DeliveryMode.PERSISTENT ? "PERSISTENT" : "NON_PERSISTENT"));
    54
    55          if (topic) {
    56              consumerDestination = session.createTopic(getConsumerSubject());
    57              producerDestination = session.createTopic(getProducerSubject());
    58          }
    59          else {
    60              consumerDestination = session.createQueue(getConsumerSubject());
    61              producerDestination = session.createQueue(getProducerSubject());
    62          }
    63
    64          System.out.println("Created  consumer destination: " + consumerDestination + " of type: " + consumerDestination.getClass());
    65          System.out.println("Created  producer destination: " + producerDestination + " of type: " + producerDestination.getClass());
    66          consumer = createConsumer();
    67          consumer.setMessageListener(this);
    68          connection.start();
    69
    70          System.out.println("Created connection: " + connection);
    71      }
    72
    73      protected MessageConsumer createConsumer() throws JMSException {
    74          if (durable) {
    75              System.out.println("Creating durable consumer");
    76              return session.createDurableSubscriber((Topic) consumerDestination, getName());
    77          }
    78          return session.createConsumer(consumerDestination);
    79      }
    80
    81      protected void tearDown() throws Exception {
    82          System.out.println("Dumping stats...");
    83          connectionFactory.getFactoryStats().dump(new IndentPrinter());
    84
    85          System.out.println("Closing down connection");
    86
    87          /*** TODO we should be able to shut down properly */
    88          session.close();
    89          connection.close();
    90      }
    91
    92  }