001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with 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,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    //
021    // This source code implements specifications defined by the Java
022    // Community Process. In order to remain compliant with the specification
023    // DO NOT add / change / or delete method signatures!
024    //
025    
026    package javax.jms;
027    
028    /**
029     * @version $Rev: 467553 $ $Date: 2006-10-25 05:01:51 +0100 (Wed, 25 Oct 2006) $
030     */
031    public class QueueRequestor {
032        private QueueSession session;
033        private TemporaryQueue temporaryQueue;
034        private QueueSender sender;
035        private QueueReceiver receiver;
036    
037        public QueueRequestor(QueueSession session, Queue queue)
038            throws JMSException
039        {
040            super();
041    
042            if(queue == null) {
043                throw new InvalidDestinationException("Invalid queue");
044            }
045            
046            setSession(session);
047            setTemporaryQueue(session.createTemporaryQueue());
048            setSender(session.createSender(queue));
049            setReceiver(session.createReceiver(getTemporaryQueue()));
050        }
051    
052        public Message request(Message message) throws JMSException {
053            message.setJMSReplyTo(getTemporaryQueue());
054            getSender().send(message);
055            return getReceiver().receive();
056        }
057    
058        public void close() throws JMSException {
059            getSession().close();
060            getTemporaryQueue().delete();
061        }
062    
063        private void setReceiver(QueueReceiver receiver) {
064            this.receiver = receiver;
065        }
066    
067        private QueueReceiver getReceiver() {
068            return receiver;
069        }
070    
071        private void setSender(QueueSender sender) {
072            this.sender = sender;
073        }
074    
075        private QueueSender getSender() {
076            return sender;
077        }
078    
079        private void setSession(QueueSession session) {
080            this.session = session;
081        }
082    
083        private QueueSession getSession() {
084            return session;
085        }
086    
087        private void setTemporaryQueue(TemporaryQueue temporaryQueue) {
088            this.temporaryQueue = temporaryQueue;
089        }
090    
091        private TemporaryQueue getTemporaryQueue() {
092            return temporaryQueue;
093        }
094    }