001    /** 
002     * 
003     * Copyright 2005 LogicBlaze, Inc.
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * 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     * 
017     **/
018    package org.logicblaze.lingo.jms.impl;
019    
020    import org.logicblaze.lingo.jms.JmsProducer;
021    import org.logicblaze.lingo.jms.Requestor;
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    
025    import javax.jms.Destination;
026    import javax.jms.JMSException;
027    import javax.jms.Message;
028    import javax.jms.Session;
029    import javax.jms.DeliveryMode;
030    
031    /**
032     * A simple requestor which only supports one-way and so does not need a consumer.
033     *
034     * @version $Revision: 1.2 $
035     */
036    public class OneWayRequestor implements Requestor {
037        private static final Log log = LogFactory.getLog(OneWayRequestor.class);
038    
039        private JmsProducer producer;
040        private Destination serverDestination;
041        private long counter;
042        private int deliveryMode = DeliveryMode.NON_PERSISTENT;
043        private int priority = 5;
044        private long timeToLive = 30000;
045    
046        public OneWayRequestor(JmsProducer producer, Destination serverDestination) {
047            this.producer = producer;
048            this.serverDestination = serverDestination;
049        }
050    
051        /**
052         * The default delivery mode of request messages
053         */
054        public int getDeliveryMode() {
055            return deliveryMode;
056        }
057    
058        public void setDeliveryMode(int deliveryMode) {
059            this.deliveryMode = deliveryMode;
060        }
061    
062        /**
063         * The default priority of request messages
064         */
065        public int getPriority() {
066            return priority;
067        }
068    
069        public void setPriority(int priority) {
070            this.priority = priority;
071        }
072    
073        /**
074         * The default time to live on request messages
075         */
076        public long getTimeToLive() {
077            return timeToLive;
078        }
079    
080        /**
081         * Sets the maximum time to live for requests
082         */ 
083        public void setTimeToLive(long timeToLive) {
084            this.timeToLive = timeToLive;
085        }
086    
087        public void oneWay(Destination destination, Message message) throws JMSException {
088            oneWay(destination, message, timeToLive);
089        }
090    
091        public void oneWay(Destination destination, Message message, long timeToLive) throws JMSException {
092            populateHeaders(message);
093            doSend(destination, message, timeToLive);
094        }
095    
096        public Session getSession() {
097            return producer.getSession();
098        }
099    
100        public void close() throws JMSException {
101            producer.close();
102        }
103    
104        public Message receive(long timeout) throws JMSException {
105            throw new JMSException("receive(timeToLive) not implemented for OneWayRequestor");
106        }
107    
108        public Message request(Destination destination, Message message) throws JMSException {
109            throw new JMSException("request() not implemented for OneWayRequestor");
110        }
111    
112        public Message request(Destination destination, Message message, long timeout) throws JMSException {
113            throw new JMSException("request() not implemented for OneWayRequestor");
114        }
115    
116        protected void populateHeaders(Message message) throws JMSException {
117        }
118    
119        protected void doSend(Destination destination, Message message, long timeToLive) throws JMSException {
120            if (destination == null) {
121                destination = serverDestination;
122            }
123            if (log.isDebugEnabled()) {
124                log.debug("Sending message to: " + destination + " message: " + message);
125            }
126            producer.getMessageProducer().send(destination, message, deliveryMode, priority, timeToLive);
127        }
128    
129        /**
130         * Creates a new correlation ID. Note that because the correlationID is used
131         * on a per-temporary destination basis, it does not need to be unique across
132         * more than one destination. So a simple counter will suffice.
133         *
134         * @return
135         */
136        public String createCorrelationID() {
137            return Long.toString(nextCounter());
138        }
139    
140        protected synchronized long nextCounter() {
141            return ++counter;
142        }
143    }