1 7 package org.jboss.jms.client.p2p; 8 9 import javax.jms.Destination ; 10 import javax.jms.JMSException ; 11 import javax.jms.Message ; 12 import javax.jms.MessageListener ; 13 14 import org.jboss.jms.MessageImpl; 15 import org.jboss.jms.client.ConsumerDelegate; 16 17 24 public class P2PConsumerDelegate 25 implements ConsumerDelegate 26 { 27 29 31 private P2PSessionDelegate session = null; 32 private MessageListener messageListener = null; 33 private Destination destination = null; 34 boolean noLocal; 35 private boolean waiting = false; 36 private Message lastReceivedMessage = null; 37 38 40 42 public P2PConsumerDelegate(P2PSessionDelegate session, Destination destination, String selector, boolean noLocal) 43 throws JMSException 44 { 45 this.session = session; 46 this.destination = destination; 47 this.noLocal = noLocal; 48 } 49 50 52 54 public void close() throws JMSException 55 { 56 } 57 58 public void closing() throws JMSException 59 { 60 } 61 62 public Message receive(long timeout) throws JMSException 63 { 64 Message message = this.lastReceivedMessage; 65 if (message == null && timeout != -1) 66 { 67 this.waiting = true; 68 synchronized (this) 69 { 70 try 71 { 72 this.wait(timeout); 73 } 74 catch (InterruptedException exception){} 75 } 76 message = this.lastReceivedMessage; 77 this.lastReceivedMessage = null; 78 this.waiting = false; 79 } 80 return message; 81 } 82 83 public void setMessageListener(MessageListener listener) throws JMSException 84 { 85 this.messageListener = listener; 86 } 87 88 90 92 boolean deliver(MessageImpl message) 93 { 94 try 95 { 96 if (this.noLocal && message.isLocal()) 97 { 98 return false; 99 } 100 if (message.getJMSDestination() != null) 101 { 102 if (message.getJMSDestination().equals(this.destination)) 103 { 104 if (this.messageListener != null) 105 { 106 this.messageListener.onMessage((Message )message.clone()); 107 return true; 108 } 109 else 110 { 111 if (this.waiting) 112 { 113 this.lastReceivedMessage = (MessageImpl)message.clone(); 114 synchronized(this) 115 { 116 this.notify(); 117 } 118 return true; 119 } 120 } 121 } 122 } 123 return false; 124 } 125 catch (Exception e){} 126 return false; 127 } 128 129 131 133 } 134 | Popular Tags |