1 /*** 2 * 3 * Copyright 2005 LogicBlaze, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 **/ 18 package org.logicblaze.lingo.jms.impl; 19 20 import org.logicblaze.lingo.jms.JmsProducer; 21 import org.logicblaze.lingo.jms.Requestor; 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 import javax.jms.Destination; 26 import javax.jms.JMSException; 27 import javax.jms.Message; 28 import javax.jms.Session; 29 import javax.jms.DeliveryMode; 30 31 /*** 32 * A simple requestor which only supports one-way and so does not need a consumer. 33 * 34 * @version $Revision: 1.2 $ 35 */ 36 public class OneWayRequestor implements Requestor { 37 private static final Log log = LogFactory.getLog(OneWayRequestor.class); 38 39 private JmsProducer producer; 40 private Destination serverDestination; 41 private long counter; 42 private int deliveryMode = DeliveryMode.NON_PERSISTENT; 43 private int priority = 5; 44 private long timeToLive = 30000; 45 46 public OneWayRequestor(JmsProducer producer, Destination serverDestination) { 47 this.producer = producer; 48 this.serverDestination = serverDestination; 49 } 50 51 /*** 52 * The default delivery mode of request messages 53 */ 54 public int getDeliveryMode() { 55 return deliveryMode; 56 } 57 58 public void setDeliveryMode(int deliveryMode) { 59 this.deliveryMode = deliveryMode; 60 } 61 62 /*** 63 * The default priority of request messages 64 */ 65 public int getPriority() { 66 return priority; 67 } 68 69 public void setPriority(int priority) { 70 this.priority = priority; 71 } 72 73 /*** 74 * The default time to live on request messages 75 */ 76 public long getTimeToLive() { 77 return timeToLive; 78 } 79 80 /*** 81 * Sets the maximum time to live for requests 82 */ 83 public void setTimeToLive(long timeToLive) { 84 this.timeToLive = timeToLive; 85 } 86 87 public void oneWay(Destination destination, Message message) throws JMSException { 88 oneWay(destination, message, timeToLive); 89 } 90 91 public void oneWay(Destination destination, Message message, long timeToLive) throws JMSException { 92 populateHeaders(message); 93 doSend(destination, message, timeToLive); 94 } 95 96 public Session getSession() { 97 return producer.getSession(); 98 } 99 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 }