KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.walend.somnifugi.example;
2
3 import javax.swing.SwingUtilities JavaDoc;
4
5 import javax.jms.TopicConnection JavaDoc;
6 import javax.jms.TopicPublisher JavaDoc;
7 import javax.jms.Topic JavaDoc;
8 import javax.jms.TopicSession JavaDoc;
9 import javax.jms.Session JavaDoc;
10 import javax.jms.Message JavaDoc;
11 import javax.jms.JMSException JavaDoc;
12 import javax.jms.TopicSubscriber JavaDoc;
13
14 import net.walend.somnifugi.SomniJNDIBypass;
15
16 /**
17 EventTopicExample shows how to use a JMS Topic to respond to events generated in the UI thread in a background thread. When the user touches the search button, the UI thread publishes an event to a Topic. A JMS MessageListener takes the event from the topic, performs the search, and reports the results.
18 */

19
20 public class EventTopicExample
21     extends SearchUI
22 {
23     public static final String JavaDoc TOPICNAME = "EventTopicExample";
24
25     private TopicConnection JavaDoc connection;
26
27     private TopicPublisher JavaDoc publisher;
28     private TopicSession JavaDoc session;
29
30     public EventTopicExample()
31     {
32         super();
33         setUpPublisher();
34         setUpListener();
35     }
36
37     private void setUpPublisher()
38     {
39         try
40         {
41             connection = SomniJNDIBypass.IT.getTopicConnectionFactory().createTopicConnection();
42             
43             connection.start();
44
45             //session and publisher created on UI thread.
46
SwingUtilities.invokeLater(new Runnable JavaDoc()
47                 {
48                     public void run()
49                     {
50                         try
51                         {
52                             Topic JavaDoc topic = SomniJNDIBypass.IT.getTopic(TOPICNAME);
53                             session = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
54                             publisher = session.createPublisher(topic);
55                         }
56                         catch(JMSException JavaDoc jmse)
57                         {
58                             handleThrowable(jmse);
59                         }
60                     }
61                 });
62         }
63         catch(JMSException JavaDoc jmse)
64         {
65             handleThrowable(jmse);
66         }
67     }
68
69     private void setUpListener()
70     {
71         try
72         {
73             LookupMessageListener listener = new LookupMessageListener(this);
74             TopicSession JavaDoc session = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
75             Topic JavaDoc topic = SomniJNDIBypass.IT.getTopic(TOPICNAME);
76             TopicSubscriber JavaDoc subscriber = session.createSubscriber(topic);
77             subscriber.setMessageListener(listener);
78         }
79         catch(JMSException JavaDoc jmse)
80         {
81             handleThrowable(jmse);
82         }
83     }
84
85     /**
86 Called from the UI thread.
87     */

88     protected void searchButtonActionPerformed()
89     {
90         try
91         {
92             //create a new event
93
LookupRequest request = new LookupRequest(searchTF.getText());
94             
95             //and place it in the Topic
96
Message JavaDoc message = session.createObjectMessage(request);
97             publisher.publish(message);
98         }
99         catch(JMSException JavaDoc jmse)
100         {
101             handleThrowable(jmse);
102         }
103     }
104     
105     public static void main(String JavaDoc[] args)
106     {
107         EventTopicExample example = new EventTopicExample();
108
109         example.showFrame();
110     }
111     
112 }
113
Popular Tags