KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > JmsCreateConsumerInOnMessageTest


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

18 package org.apache.activemq;
19
20 import javax.jms.Connection JavaDoc;
21 import javax.jms.Message JavaDoc;
22 import javax.jms.MessageConsumer JavaDoc;
23 import javax.jms.MessageListener JavaDoc;
24 import javax.jms.MessageProducer JavaDoc;
25 import javax.jms.Session JavaDoc;
26 import javax.jms.Topic JavaDoc;
27
28 import org.apache.activemq.TestSupport;
29
30 /**
31  * @version $Revision: 1.2 $
32  */

33 public class JmsCreateConsumerInOnMessageTest extends TestSupport implements MessageListener JavaDoc {
34
35     private Connection JavaDoc connection;
36     private Session JavaDoc publisherSession;
37     private Session JavaDoc consumerSession;
38     private MessageConsumer JavaDoc consumer;
39     private MessageConsumer JavaDoc testConsumer;
40     private MessageProducer JavaDoc producer;
41     private Topic JavaDoc topic;
42     private Object JavaDoc lock = new Object JavaDoc();
43
44     /*
45      * @see junit.framework.TestCase#setUp()
46      */

47     protected void setUp() throws Exception JavaDoc {
48         super.setUp();
49         super.topic = true;
50         connection = createConnection();
51         connection.setClientID("connection:" + getSubject());
52         publisherSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
53         consumerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
54         topic = (Topic JavaDoc)super.createDestination("Test.Topic");
55         consumer = consumerSession.createConsumer(topic);
56         consumer.setMessageListener(this);
57         producer = publisherSession.createProducer(topic);
58         connection.start();
59     }
60     
61     /*
62      * @see junit.framework.TestCase#tearDown()
63      */

64     protected void tearDown() throws Exception JavaDoc {
65         super.tearDown();
66         connection.close();
67     }
68
69     
70     /**
71      * Tests if a consumer can be created asynchronusly
72      *
73      * @throws Exception
74      */

75     public void testCreateConsumer() throws Exception JavaDoc{
76         Message msg = super.createMessage();
77         producer.send(msg);
78         if (testConsumer == null){
79             synchronized(lock){
80                 lock.wait(3000);
81             }
82         }
83         assertTrue(testConsumer != null);
84     }
85
86     /**
87      * Use the asynchronous subscription mechanism
88      *
89      * @param message
90      */

91     public void onMessage(Message message) {
92         try {
93             testConsumer = consumerSession.createConsumer(topic);
94             MessageProducer JavaDoc anotherProducer = consumerSession.createProducer(topic);
95             synchronized(lock){
96                 lock.notify();
97             }
98         }catch (Exception JavaDoc ex){
99             ex.printStackTrace();
100             assertTrue(false);
101         }
102     }
103 }
104
Popular Tags