KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > perf > InactiveDurableTopicTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
4  * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
5  * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
11  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
12  * specific language governing permissions and limitations under the License.
13  */

14 package org.apache.activemq.perf;
15
16 import java.io.File JavaDoc;
17 import java.net.URI JavaDoc;
18 import javax.jms.Connection JavaDoc;
19 import javax.jms.JMSException JavaDoc;
20 import javax.jms.MapMessage JavaDoc;
21 import javax.jms.Message JavaDoc;
22 import javax.jms.MessageProducer JavaDoc;
23 import javax.jms.Session JavaDoc;
24 import javax.jms.Topic JavaDoc;
25 import javax.jms.TopicSubscriber JavaDoc;
26 import junit.framework.AssertionFailedError;
27 import junit.framework.TestCase;
28 import org.apache.activemq.ActiveMQConnectionFactory;
29 import org.apache.activemq.broker.BrokerFactory;
30 import org.apache.activemq.broker.BrokerService;
31 import org.apache.activemq.store.journal.JournalPersistenceAdapterFactory;
32 import org.apache.activemq.store.kahadaptor.KahaPersistenceAdapter;
33 /**
34  * @version $Revision: 515059 $
35  */

36 public class InactiveDurableTopicTest extends TestCase{
37     private static final int MESSAGE_COUNT = 100000;
38     private static final String JavaDoc DEFAULT_PASSWORD="";
39     private static final String JavaDoc USERNAME="testuser";
40     private static final String JavaDoc CLIENTID="mytestclient";
41     private static final String JavaDoc TOPIC_NAME="testevent";
42     private static final String JavaDoc SUBID="subscription1";
43     private static final int deliveryMode=javax.jms.DeliveryMode.PERSISTENT;
44     private static final int deliveryPriority=javax.jms.Message.DEFAULT_PRIORITY;
45     private Connection JavaDoc connection=null;
46     private MessageProducer JavaDoc publisher=null;
47     private TopicSubscriber JavaDoc subscriber=null;
48     private Topic JavaDoc topic=null;
49     private Session JavaDoc session=null;
50     ActiveMQConnectionFactory connectionFactory=null;
51     BrokerService broker;
52
53     protected void setUp() throws Exception JavaDoc{
54         super.setUp();
55         broker=new BrokerService();
56         
57         broker.setPersistenceAdapter(new KahaPersistenceAdapter());
58         /*
59         JournalPersistenceAdapterFactory factory = new JournalPersistenceAdapterFactory();
60         factory.setDataDirectoryFile(broker.getDataDirectory());
61         factory.setTaskRunnerFactory(broker.getTaskRunnerFactory());
62         factory.setUseJournal(false);
63         broker.setPersistenceFactory(factory);
64         */

65         broker.addConnector(ActiveMQConnectionFactory.DEFAULT_BROKER_URL);
66         broker.start();
67         connectionFactory=new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_BROKER_URL);
68         /*
69          * Doesn't matter if you enable or disable these, so just leaving them out for this test case
70          * connectionFactory.setAlwaysSessionAsync(true); connectionFactory.setAsyncDispatch(true);
71          */

72         connectionFactory.setUseAsyncSend(true);
73     }
74
75     protected void tearDown() throws Exception JavaDoc{
76         super.tearDown();
77         broker.stop();
78     }
79
80     public void test1CreateSubscription() throws Exception JavaDoc{
81         try{
82             /*
83              * Step 1 - Establish a connection with a client id and create a durable subscription
84              */

85             connection=connectionFactory.createConnection(USERNAME,DEFAULT_PASSWORD);
86             assertNotNull(connection);
87             connection.setClientID(CLIENTID);
88             connection.start();
89             session=connection.createSession(false,javax.jms.Session.CLIENT_ACKNOWLEDGE);
90             assertNotNull(session);
91             topic=session.createTopic(TOPIC_NAME);
92             assertNotNull(topic);
93             subscriber=session.createDurableSubscriber(topic,SUBID,"",false);
94             assertNotNull(subscriber);
95             subscriber.close();
96             session.close();
97             connection.close();
98         }catch(JMSException JavaDoc ex){
99             try{
100                 connection.close();
101             }catch(Exception JavaDoc ignore){}
102             throw new AssertionFailedError("Create Subscription caught: "+ex);
103         }
104     }
105
106     public void test2ProducerTestCase(){
107         /*
108          * Step 2 - Establish a connection without a client id and create a producer and start pumping messages. We will
109          * get hung
110          */

111         try{
112             connection=connectionFactory.createConnection(USERNAME,DEFAULT_PASSWORD);
113             assertNotNull(connection);
114             session=connection.createSession(false,javax.jms.Session.CLIENT_ACKNOWLEDGE);
115             assertNotNull(session);
116             topic=session.createTopic(TOPIC_NAME);
117             assertNotNull(topic);
118             publisher=session.createProducer(topic);
119             assertNotNull(publisher);
120             MapMessage JavaDoc msg=session.createMapMessage();
121             assertNotNull(msg);
122             msg.setString("key1","value1");
123             int loop;
124             for(loop=0;loop<MESSAGE_COUNT;loop++){
125                 msg.setInt("key2",loop);
126                 publisher.send(msg,deliveryMode,deliveryPriority,Message.DEFAULT_TIME_TO_LIVE);
127                 if (loop%500==0){
128                     System.out.println("Sent " + loop + " messages");
129                 }
130             }
131             this.assertEquals(loop,MESSAGE_COUNT);
132             publisher.close();
133             session.close();
134             connection.stop();
135             connection.stop();
136         }catch(JMSException JavaDoc ex){
137             try{
138                 connection.close();
139             }catch(Exception JavaDoc ignore){}
140             throw new AssertionFailedError("Create Subscription caught: "+ex);
141         }
142     }
143     public void test3CreateSubscription() throws Exception JavaDoc{
144         try{
145             /*
146              * Step 1 - Establish a connection with a client id and create a durable subscription
147              */

148             connection=connectionFactory.createConnection(USERNAME,DEFAULT_PASSWORD);
149             assertNotNull(connection);
150             connection.setClientID(CLIENTID);
151             connection.start();
152             session=connection.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
153             assertNotNull(session);
154             topic=session.createTopic(TOPIC_NAME);
155             assertNotNull(topic);
156             subscriber=session.createDurableSubscriber(topic,SUBID,"",false);
157             assertNotNull(subscriber);
158             int loop;
159             for(loop=0;loop<MESSAGE_COUNT;loop++){
160                 Message JavaDoc msg = subscriber.receive();
161                 if (loop%500==0){
162                     System.out.println("Received " + loop + " messages");
163                 }
164             }
165             this.assertEquals(loop,MESSAGE_COUNT);
166             subscriber.close();
167             session.close();
168             connection.close();
169         }catch(JMSException JavaDoc ex){
170             try{
171                 connection.close();
172             }catch(Exception JavaDoc ignore){}
173             throw new AssertionFailedError("Create Subscription caught: "+ex);
174         }
175     }
176 }
177
Popular Tags