001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.activemq.tool;
019
020 import org.apache.activemq.tool.MemMessageIdList;
021
022 import javax.jms.*;
023
024 /**
025 * @version $Revision: 1.3 $
026 */
027 public class MemConsumer extends MemMessageIdList implements MessageListener {
028 protected Connection connection;
029 protected MessageConsumer consumer;
030 protected long counter = 0;
031 protected boolean isParent = false;
032 protected boolean inOrder = true;
033
034
035 public MemConsumer() {
036 super();
037 }
038
039 public MemConsumer(ConnectionFactory fac, Destination dest, String consumerName) throws JMSException {
040 connection = fac.createConnection();
041 Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
042 if (dest instanceof Topic && consumerName != null && consumerName.length() > 0) {
043 consumer = s.createDurableSubscriber((Topic) dest, consumerName);
044 } else {
045 consumer = s.createConsumer(dest);
046 }
047 consumer.setMessageListener(this);
048 }
049
050 public MemConsumer(ConnectionFactory fac, Destination dest) throws JMSException {
051 this(fac, dest, null);
052 }
053
054 public void start() throws JMSException {
055 connection.start();
056 }
057
058 public void stop() throws JMSException {
059 connection.stop();
060 }
061
062 public void shutDown() throws JMSException {
063 connection.close();
064 }
065
066
067 public Message receive() throws JMSException {
068 return consumer.receive();
069 }
070
071 public Message receive(long wait) throws JMSException {
072 return consumer.receive(wait);
073 }
074
075 static long ctr = 0;
076
077 public void onMessage(Message msg) {
078 super.onMessage(msg);
079
080 if (isParent) {
081 try {
082 long ctr = msg.getLongProperty("counter");
083 if (counter != ctr) {
084 inOrder = false;
085 }
086 counter++;
087
088 } catch (Exception e) {
089 e.printStackTrace();
090 }
091 }
092 }
093
094
095 public boolean isInOrder() {
096 return inOrder;
097 }
098
099
100 public void setAsParent(boolean isParent) {
101 this.isParent = isParent;
102 }
103
104 public boolean isParent() {
105 return this.isParent;
106 }
107
108
109 }