All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.activemq.tool.MemConsumer Maven / Gradle / Ivy

There is a newer version: 5.7.0
Show newest version
/**
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.activemq.tool;

import org.apache.activemq.tool.MemMessageIdList;

import javax.jms.*;

/**
 * @version $Revision: 1.3 $
 */
public class MemConsumer extends MemMessageIdList implements MessageListener {
    protected Connection connection;
    protected MessageConsumer consumer;
    protected long counter = 0;
    protected boolean isParent = false;
    protected boolean inOrder = true;


    public MemConsumer() {
        super();
    }

    public MemConsumer(ConnectionFactory fac, Destination dest, String consumerName) throws JMSException {
        connection = fac.createConnection();
        Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        if (dest instanceof Topic && consumerName != null && consumerName.length() > 0) {
            consumer = s.createDurableSubscriber((Topic) dest, consumerName);
        } else {
            consumer = s.createConsumer(dest);
        }
        consumer.setMessageListener(this);
    }

    public MemConsumer(ConnectionFactory fac, Destination dest) throws JMSException {
        this(fac, dest, null);
    }

    public void start() throws JMSException {
        connection.start();
    }

    public void stop() throws JMSException {
        connection.stop();
    }

    public void shutDown() throws JMSException {
        connection.close();
    }


    public Message receive() throws JMSException {
        return consumer.receive();
    }

    public Message receive(long wait) throws JMSException {
        return consumer.receive(wait);
    }

    static long ctr = 0;

    public void onMessage(Message msg) {
        super.onMessage(msg);

        if (isParent) {
            try {
                long ctr = msg.getLongProperty("counter");
                if (counter != ctr) {
                    inOrder = false;
                }
                counter++;

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public boolean isInOrder() {
        return inOrder;
    }


    public void setAsParent(boolean isParent) {
        this.isParent = isParent;
    }

    public boolean isParent() {
        return this.isParent;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy