Package org.codehaus.activemq

Source Code of org.codehaus.activemq.JmsTransactionTestSupport

/**
*
* 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.codehaus.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.codehaus.activemq.test.JmsResourceProvider;
import org.codehaus.activemq.test.TestSupport;

import java.util.ArrayList;

/**
* @version $Revision: 1.3 $
*/
abstract public class JmsTransactionTestSupport extends TestSupport {

    protected ConnectionFactory connectionFactory;
    protected Connection connection;
    protected Session session;
    protected MessageConsumer consumer;
    protected MessageProducer producer;

    public JmsTransactionTestSupport() {
        super();
    }

    public JmsTransactionTestSupport(String name) {
        super(name);
    }


    public void testSendRollback() throws Exception {

        Message[] outbound = new Message[]{
            session.createTextMessage("First Message"),
            session.createTextMessage("Second Message")
        };

        producer.send(outbound[0]);
        session.commit();
        producer.send(session.createTextMessage("I'm going to get rolled back."));
        session.rollback();
        producer.send(outbound[1]);
        session.commit();

        ArrayList messages = new ArrayList();
        System.out.println("About to consume message 1");
        Message message = consumer.receive(1000);
        messages.add(message);
        System.out.println("Received: " + message);

        System.out.println("About to consume message 2");
        message = consumer.receive(4000);
        messages.add(message);
        System.out.println("Received: " + message);

        session.commit();

        Message inbound[] = new Message[messages.size()];
        messages.toArray(inbound);

        assertTextMessagesEqual("Rollback did not work.", outbound, inbound);
    }

    public void testReceiveRollback() throws Exception {

        Message[] outbound = new Message[]{
            session.createTextMessage("First Message"),
            session.createTextMessage("Second Message")
        };

        // lets consume any outstanding messages from previous test runs
        while (consumer.receive(1000) != null) {
        }
        session.commit();

        producer.send(outbound[0]);
        producer.send(outbound[1]);
        session.commit();

        System.out.println("Sent 0: " + outbound[0]);
        System.out.println("Sent 1: " + outbound[1]);

        ArrayList messages = new ArrayList();
        Message message = consumer.receive(1000);
        messages.add(message);
        assertEquals(outbound[0], message);
        session.commit();

        // rollback so we can get that last message again.
        message = consumer.receive(1000);
        assertNotNull(message);
        assertEquals(outbound[1], message);
        session.rollback();

        // Consume again.. the previous message should
        // get redelivered.
        message = consumer.receive(5000);
        assertNotNull("Should have re-received the message again!", message);
        messages.add(message);
        session.commit();

        Message inbound[] = new Message[messages.size()];
        messages.toArray(inbound);

        assertTextMessagesEqual("Rollback did not work", outbound, inbound);
    }

    public void testReceiveTwoThenRollback() throws Exception {

        Message[] outbound = new Message[]{
            session.createTextMessage("First Message"),
            session.createTextMessage("Second Message")
        };

        // lets consume any outstanding messages from previous test runs
        while (consumer.receive(1000) != null) {
        }
        session.commit();

        producer.send(outbound[0]);
        producer.send(outbound[1]);
        session.commit();

        System.out.println("Sent 0: " + outbound[0]);
        System.out.println("Sent 1: " + outbound[1]);

        ArrayList messages = new ArrayList();
        Message message = consumer.receive(1000);
        assertEquals(outbound[0], message);

        message = consumer.receive(1000);
        assertNotNull(message);
        assertEquals(outbound[1], message);
        session.rollback();

        // Consume again.. the previous message should
        // get redelivered.
        message = consumer.receive(5000);
        assertNotNull("Should have re-received the first message again!", message);
        messages.add(message);
        assertEquals(outbound[0], message);

        message = consumer.receive(5000);
        assertNotNull("Should have re-received the second message again!", message);
        messages.add(message);
        assertEquals(outbound[1], message);
        session.commit();

        Message inbound[] = new Message[messages.size()];
        messages.toArray(inbound);

        assertTextMessagesEqual("Rollback did not work", outbound, inbound);
    }

    public void testSendRollbackWithPrefetchOfOne() throws Exception {
        setPrefetchToOne();
        testSendRollback();
    }

    public void testReceiveRollbackWithPrefetchOfOne() throws Exception {
        setPrefetchToOne();
        testReceiveRollback();
    }

    protected abstract JmsResourceProvider getJmsResourceProvider();

    protected void setUp() throws Exception {
        super.setUp();

        JmsResourceProvider p = getJmsResourceProvider();
        // We will be using transacted sessions.
        p.setTransacted(true);

        connectionFactory = p.createConnectionFactory();
        connection = p.createConnection(connectionFactory);
        System.out.println("Created connection: " + connection);
        session = p.createSession(connection);
        System.out.println("Created session: " + session);
        Destination destination = p.createDestination(session, getSubject() + "." + getName());
        System.out.println("Created destination: " + destination + " of type: " + destination.getClass());
        producer = p.createProducer(session, destination);
        System.out.println("Created producer: " + producer);
        consumer = p.createConsumer(session, destination);
        System.out.println("Created consumer: " + consumer);
        connection.start();
    }

    protected void tearDown() throws Exception {
        //System.out.println("Test Done.  Stats");
        //((ActiveMQConnectionFactory) connectionFactory).getFactoryStats().dump(new IndentPrinter());
        System.out.println("Closing down connection");

        session.close();
        connection.close();
        System.out.println("Connection closed.");
    }

    protected void setPrefetchToOne() {
        ActiveMQPrefetchPolicy prefetchPolicy = ((ActiveMQConnection) connection).getPrefetchPolicy();
        prefetchPolicy.setQueuePrefetch(1);
        prefetchPolicy.setTopicPrefetch(1);
        prefetchPolicy.setDurableTopicPrefetch(1);
    }
}
TOP

Related Classes of org.codehaus.activemq.JmsTransactionTestSupport

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.