KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > walend > somnifugi > example > LookupMessageListener


1 package net.walend.somnifugi.example;
2
3 import java.util.Collection JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 import javax.jms.MessageListener JavaDoc;
8 import javax.jms.Message JavaDoc;
9 import javax.jms.ObjectMessage JavaDoc;
10 import javax.jms.JMSException JavaDoc;
11
12 /**
13 A MessageListener that listens for LookupRequest and reports results.
14 */

15
16 public class LookupMessageListener
17     implements MessageListener JavaDoc
18 {
19     private LookupListener listener;
20
21     public LookupMessageListener(LookupListener listener)
22     {
23         this.listener = listener;
24     }
25
26     public void onMessage(Message JavaDoc message)
27     {
28         try
29         {
30             ObjectMessage JavaDoc om = (ObjectMessage JavaDoc)message;
31
32             LookupRequest request = (LookupRequest)om.getObject();
33             
34             fireLookupStarted(request);
35             //mimic the server call delay...
36
Thread.sleep(1000);
37
38             //imagine we got this from a server
39
String JavaDoc[] resultArray = new String JavaDoc[]{"Book one", "Book two", "Book three"};
40             LookupResults results = new LookupResults(request,resultArray);
41             
42             fireLookupCompleted(results);
43         }
44         catch(JMSException JavaDoc jmse)
45         {
46             throw new RuntimeException JavaDoc(jmse);
47         }
48         catch (InterruptedException JavaDoc e)
49         {
50             Thread.currentThread().interrupt();
51         }
52     }
53
54     private void fireLookupStarted(LookupRequest request)
55     {
56         listener.lookupStarted(request);
57     }
58
59     private void fireLookupCompleted(LookupResults results)
60     {
61         listener.lookupCompleted(results);
62     }
63
64 }
65
Popular Tags