Source code: org/activemq/JmsCreateConsumerInOnMessageTest.java
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.activemq;
19
20 import javax.jms.Connection;
21 import javax.jms.Message;
22 import javax.jms.MessageConsumer;
23 import javax.jms.MessageListener;
24 import javax.jms.MessageProducer;
25 import javax.jms.Session;
26 import javax.jms.Topic;
27
28 import org.activemq.test.TestSupport;
29
30 /**
31 * @version $Revision: 1.1.1.1 $
32 */
33 public class JmsCreateConsumerInOnMessageTest extends TestSupport implements MessageListener {
34
35 private Connection connection;
36 private Session publisherSession;
37 private Session consumerSession;
38 private MessageConsumer consumer;
39 private MessageConsumer testConsumer;
40 private MessageProducer producer;
41 private Topic topic;
42 private Object lock = new Object();
43
44
45 /**
46 * @throws Exception
47 */
48 public void testCreateConsumer() throws Exception{
49 Message msg = super.createMessage();
50 producer.send(msg);
51 if (testConsumer == null){
52 synchronized(lock){
53 lock.wait(3000);
54 }
55 }
56 assertTrue(testConsumer != null);
57 }
58
59 /**
60 * Use the asynchronous subscription mechanism
61 * @param message
62 */
63 public void onMessage(Message message) {
64 try {
65 testConsumer = consumerSession.createConsumer(topic);
66 MessageProducer anotherProducer = consumerSession.createProducer(topic);
67 synchronized(lock){
68 lock.notify();
69 }
70 }catch (Exception ex){
71 ex.printStackTrace();
72 assertTrue(false);
73 }
74 }
75
76
77
78
79 protected void setUp() throws Exception {
80 super.setUp();
81 super.topic = true;
82 connection = createConnection();
83 connection.setClientID("connection:" + getSubject());
84 publisherSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
85 consumerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
86 topic = (Topic)super.createDestination("Test.Topic");
87 consumer = consumerSession.createConsumer(topic);
88 consumer.setMessageListener(this);
89 producer = publisherSession.createProducer(topic);
90 connection.start();
91 }
92
93 protected void tearDown() throws Exception {
94 super.tearDown();
95 connection.close();
96 }
97 }