KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.walend.somnifugi.example;
2
3 import javax.swing.SwingUtilities JavaDoc;
4
5 import javax.jms.QueueConnection JavaDoc;
6 import javax.jms.QueueSender JavaDoc;
7 import javax.jms.Queue JavaDoc;
8 import javax.jms.QueueSession JavaDoc;
9 import javax.jms.Session JavaDoc;
10 import javax.jms.Message JavaDoc;
11 import javax.jms.JMSException JavaDoc;
12 import javax.jms.QueueReceiver JavaDoc;
13
14 import net.walend.somnifugi.SomniJNDIBypass;
15
16 /**
17 CommandQueueExample shows how to use a JMS Queue to transfer commands generated in the UI thread to a background thread. When the user touches the search button, the UI thread places a command to perform the lookup in a Queue. A JMS MessageListener takes the command from the queue, performs the search, and reports the results.
18 */

19
20 public class CommandQueueExample
21     extends SearchUI
22 {
23     public static final String JavaDoc QUEUENAME = "CommandQueueExample";
24
25     private QueueConnection JavaDoc connection;
26
27     private QueueSender JavaDoc sender;
28     private QueueSession JavaDoc session;
29
30     public CommandQueueExample()
31     {
32         super();
33         setUpSender();
34         setUpListener();
35     }
36
37     private void setUpSender()
38     {
39         try
40         {
41             connection = SomniJNDIBypass.IT.getQueueConnectionFactory().createQueueConnection();
42             
43             connection.start();
44
45             //session and sender created on UI thread.
46
SwingUtilities.invokeLater(new Runnable JavaDoc()
47                 {
48                     public void run()
49                     {
50                         try
51                         {
52                             Queue JavaDoc queue = SomniJNDIBypass.IT.getQueue(QUEUENAME);
53                             session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
54                             sender = session.createSender(queue);
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             QueueSession JavaDoc session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
75             Queue JavaDoc queue = SomniJNDIBypass.IT.getQueue(QUEUENAME);
76             QueueReceiver JavaDoc receiver = session.createReceiver(queue);
77             receiver.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 command
93
LookupRequest request = new LookupRequest(searchTF.getText());
94             
95             //and place it in the Queue
96
Message JavaDoc message = session.createObjectMessage(request);
97             sender.send(message);
98         }
99         catch(JMSException JavaDoc jmse)
100         {
101             handleThrowable(jmse);
102         }
103     }
104     
105     public static void main(String JavaDoc[] args)
106     {
107         CommandQueueExample example = new CommandQueueExample();
108
109         example.showFrame();
110     }
111     
112 }
113
Popular Tags