001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.junit;
018
019import java.net.URI;
020import javax.jms.BytesMessage;
021import javax.jms.JMSException;
022import javax.jms.MapMessage;
023import javax.jms.Message;
024import javax.jms.MessageConsumer;
025import javax.jms.ObjectMessage;
026import javax.jms.TextMessage;
027
028import org.apache.activemq.ActiveMQConnectionFactory;
029
030public abstract class AbstractActiveMQConsumerResource extends AbstractActiveMQClientResource {
031    MessageConsumer consumer;
032    long defaultReceiveTimout = 50;
033
034    public AbstractActiveMQConsumerResource(String destinationName, ActiveMQConnectionFactory connectionFactory) {
035        super(destinationName, connectionFactory);
036    }
037
038    public AbstractActiveMQConsumerResource(String destinationName, URI brokerURI) {
039        super(destinationName, brokerURI);
040    }
041
042    public AbstractActiveMQConsumerResource(String destinationName, EmbeddedActiveMQBroker embeddedActiveMQBroker) {
043        super(destinationName, embeddedActiveMQBroker);
044    }
045
046    public AbstractActiveMQConsumerResource(String destinationName, URI brokerURI, String userName, String password) {
047        super(destinationName, brokerURI, userName, password);
048    }
049
050    public long getDefaultReceiveTimout() {
051        return defaultReceiveTimout;
052    }
053
054    public void setDefaultReceiveTimout(long defaultReceiveTimout) {
055        this.defaultReceiveTimout = defaultReceiveTimout;
056    }
057
058    @Override
059    protected void createClient() throws JMSException {
060        consumer = session.createConsumer(destination);
061    }
062
063    public BytesMessage receiveBytesMessage() throws JMSException {
064        return (BytesMessage) this.receiveMessage();
065    }
066
067    public TextMessage receiveTextMessage() throws JMSException {
068        return (TextMessage) this.receiveMessage();
069    }
070
071    public MapMessage receiveMapMessage() throws JMSException {
072        return (MapMessage) this.receiveMessage();
073    }
074
075    public ObjectMessage receiveObjectMessage() throws JMSException {
076        return (ObjectMessage) this.receiveMessage();
077    }
078
079    public BytesMessage receiveBytesMessage(long timeout) throws JMSException {
080        return (BytesMessage) this.receiveMessage(timeout);
081    }
082
083    public TextMessage receiveTextMessage(long timeout) throws JMSException {
084        return (TextMessage) this.receiveMessage(timeout);
085    }
086
087    public MapMessage receiveMapMessage(long timeout) throws JMSException {
088        return (MapMessage) this.receiveMessage(timeout);
089    }
090
091    public ObjectMessage receiveObjectMessage(long timeout) throws JMSException {
092        return (ObjectMessage) this.receiveMessage(timeout);
093    }
094
095    public Message receiveMessage() throws JMSException {
096        return receiveMessage(defaultReceiveTimout);
097    }
098
099    /**
100     * Receive a message with the given timeout
101     *
102     * @param timeout
103     * @return
104     * @throws JMSException
105     */
106    public Message receiveMessage(long timeout) throws JMSException {
107        Message message = null;
108        if (timeout > 0) {
109            message = consumer.receive(timeout);
110        } else if (timeout == 0) {
111            message = consumer.receiveNoWait();
112        } else {
113            message = consumer.receive();
114        }
115
116        return message;
117    }
118}