Package org.activemq.transport.remote

Source Code of org.activemq.transport.remote.RemoteTransportTest

/**
*
* 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.transport.remote;
import java.util.ArrayList;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.Topic;

import junit.framework.TestCase;

import org.activemq.ActiveMQConnection;
import org.activemq.ActiveMQConnectionFactory;
import org.activemq.broker.BrokerContainer;
import org.activemq.broker.impl.BrokerContainerImpl;

/**
* @version $Revision: 1.1.1.1 $
*/
public class RemoteTransportTest extends TestCase{
    protected BrokerContainer remoteBroker;
    protected Connection producerConnection;
    protected Connection consumerConnection;
    protected MessageConsumer consumer;
    protected MessageProducer producer;
    protected Session producerSession;
   
   
    protected void setUp() throws Exception{
        String URL = "reliable://" + ActiveMQConnection.DEFAULT_BROKER_URL;
      

        remoteBroker = new BrokerContainerImpl("remoteBroker");
        remoteBroker.addConnector(ActiveMQConnection.DEFAULT_BROKER_URL);
        remoteBroker.start();
       
        ActiveMQConnectionFactory fac = new ActiveMQConnectionFactory("remote://" + URL + "?brokerName=receiver");
        consumerConnection = fac.createConnection();
        consumerConnection.setClientID("receiver");
        consumerConnection.start();
       
        fac = new ActiveMQConnectionFactory("remote://" + URL + "?brokerName=sender");
        producerConnection = fac.createConnection();
        producerConnection.start();
       
       
       
       
    }
   
    private void doDurableTestSetup() throws Exception{
        Session s = consumerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        Topic destination = s.createTopic(getClass().getName());
        consumer = s.createDurableSubscriber(destination, destination.toString());
      
        producerSession = producerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        producer = producerSession.createProducer(destination);
    }
   
    private void doTopicTestSetup() throws Exception{
        Session s = consumerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        Topic destination = s.createTopic(getClass().getName());
        consumer = s.createConsumer(destination);
      
        producerSession = producerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        producer = producerSession.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    }
   
    private void doQueueTestSetup() throws Exception{
        Session s = consumerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        Queue destination = s.createQueue(getClass().getName());
        consumer = s.createConsumer(destination);
      
        producerSession = producerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        producer = producerSession.createProducer(destination);
    }
   
    protected void tearDown() throws Exception{
        remoteBroker.stop();
        consumerConnection.close();
        producerConnection.close();
    }
   
   
   
    public void doTest() throws Exception {
        Thread.sleep(3000);//make sure everything is connected
        int COUNT = 10;
        ArrayList list = new ArrayList(COUNT);
        for (int i =0; i < COUNT; i++){
            Message msg = producerSession.createTextMessage("test:"+i);
            producer.send(msg);
            list.add(msg);
        }
       
        for (int i =0; i < COUNT; i++){
            Message msg = consumer.receive(1000);
            assertNotNull("recieve: " + i + " NO message received!",msg);
            Message sentMsg = (Message)list.get(i);
            assertTrue("count " + i + " unexpected message: " +msg, msg.getJMSMessageID().equals(sentMsg.getJMSMessageID()));
        }
    }
   
    public void XtestDurableSendReceive() throws Exception {
        doDurableTestSetup();
        doTest();
    }
   
    public void testTopicSendReceive() throws Exception {
        doTopicTestSetup();
        doTest();
    }
   
    public void XtestQueueSendReceive() throws Exception {
        doQueueTestSetup();
        doTest();
    }
   
   
}
TOP

Related Classes of org.activemq.transport.remote.RemoteTransportTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.