/**
*
* Copyright 2004 Protique Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.activemq;
import java.util.Iterator;
import javax.jms.*;
import junit.framework.TestCase;
import org.activemq.*;
import org.activemq.advisories.ConnectionAdvisor;
import org.activemq.message.*;
/**
* @version $Revision: 1.1.1.1 $
*/
public class JmsTopicRequestorTest extends TestCase implements MessageListener{
protected static final int MESSAGE_COUNT = 10;
protected Connection serverConnection;
protected TopicConnection requestorConnection;
protected MessageProducer serverProducer;
protected Topic destination;
protected void setUp() throws Exception {
ActiveMQConnectionFactory fac1 = new ActiveMQConnectionFactory("peer://" + getClass().getName() + "?brokerName=server");
serverConnection = fac1.createConnection();
serverConnection.setClientID("server");
serverConnection.start();
ActiveMQConnectionFactory fac2 = new ActiveMQConnectionFactory("peer://" + getClass().getName() + "?brokerName=requestor");
requestorConnection = fac2.createTopicConnection();
requestorConnection.setClientID("requestor");
String destinationName = getClass().getName();
destination = new ActiveMQTopic(destinationName);
requestorConnection.start();
Session s = serverConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
MessageConsumer mc = s.createConsumer(destination);
mc.setMessageListener(this);
serverProducer = s.createProducer(null);
}
protected void tearDown() throws Exception{
serverConnection.close();
requestorConnection.close();
}
public void onMessage(Message msg){
try {
Destination replyTo = msg.getJMSReplyTo();
serverProducer.send(replyTo,msg);
System.out.println("Server sent reply ...: " + msg);
}catch(JMSException jmsEx){
jmsEx.printStackTrace();
}
}
public void testRequests() throws Exception{
try {
TopicSession s = requestorConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
TopicRequestor requestor = new TopicRequestor(s,destination);
ConnectionAdvisor ca = new ConnectionAdvisor(requestorConnection);
ca.start();
//
// A peer:// when connected will have 1 + (2 * number of connections) connections
// As we have 2 peers - total connections == 6!!
ca.waitForActiveConnections(6, 30000);
for (Iterator i = ca.getConnections().iterator(); i.hasNext();){
System.out.println(i.next());
}
for (int i =0; i < MESSAGE_COUNT; i++){
Message msg = s.createTextMessage("test:" + i);
Message receipt = requestor.request(msg);
System.out.println("Got reply: " + receipt);
assertNotNull("receipt is null!!",receipt);
}
}catch(Throwable e){
e.printStackTrace();
}
}
}