KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > client > p2p > P2PConsumerDelegate


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jms.client.p2p;
8
9 import javax.jms.Destination JavaDoc;
10 import javax.jms.JMSException JavaDoc;
11 import javax.jms.Message JavaDoc;
12 import javax.jms.MessageListener JavaDoc;
13
14 import org.jboss.jms.MessageImpl;
15 import org.jboss.jms.client.ConsumerDelegate;
16
17 /**
18  * The p2p consumer
19  *
20  * @author <a HREF="mailto:nathan@jboss.org">Nathan Phelps</a>
21  * @author <a HREF="mailto:adrian@jboss.org>Adrian Brock</a>
22  * @version $Revision: 1.1 $
23  */

24 public class P2PConsumerDelegate
25    implements ConsumerDelegate
26 {
27    // Constants -----------------------------------------------------
28

29    // Attributes ----------------------------------------------------
30

31    private P2PSessionDelegate session = null;
32    private MessageListener JavaDoc messageListener = null;
33    private Destination JavaDoc destination = null;
34    boolean noLocal;
35    private boolean waiting = false;
36    private Message JavaDoc lastReceivedMessage = null;
37
38    // Static --------------------------------------------------------
39

40    // Constructors --------------------------------------------------
41

42    public P2PConsumerDelegate(P2PSessionDelegate session, Destination JavaDoc destination, String JavaDoc selector, boolean noLocal)
43       throws JMSException JavaDoc
44    {
45       this.session = session;
46       this.destination = destination;
47       this.noLocal = noLocal;
48    }
49
50    // Public --------------------------------------------------------
51

52    // ConsumerDelegate implementation --------------------------------
53

54     public void close() throws JMSException JavaDoc
55     {
56     }
57
58     public void closing() throws JMSException JavaDoc
59     {
60     }
61
62    public Message JavaDoc receive(long timeout) throws JMSException JavaDoc
63    {
64       Message JavaDoc 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 JavaDoc 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 JavaDoc listener) throws JMSException JavaDoc
84    {
85       this.messageListener = listener;
86    }
87
88    // Protected ------------------------------------------------------
89

90    // Package Private ------------------------------------------------
91

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 JavaDoc)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 JavaDoc e){}
126        return false;
127    }
128
129    // Private --------------------------------------------------------
130

131    // Inner Classes --------------------------------------------------
132

133 }
134
Popular Tags