KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > demos > DistributedQueueDemo


1 // $Id: DistributedQueueDemo.java,v 1.6 2004/09/23 16:29:35 belaban Exp $
2
package org.jgroups.demos;
3
4 import org.jgroups.ChannelException;
5 import org.jgroups.ChannelFactory;
6 import org.jgroups.JChannelFactory;
7 import org.jgroups.blocks.DistributedQueue;
8
9 import javax.swing.*;
10 import java.awt.*;
11 import java.awt.event.ActionEvent JavaDoc;
12 import java.awt.event.ActionListener JavaDoc;
13 import java.awt.event.WindowEvent JavaDoc;
14 import java.awt.event.WindowListener JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Vector JavaDoc;
17
18
19 /**
20  * Uses the DistributedQueue building block. The latter subclasses org.jgroups.util.Queue and overrides
21  * the methods that modify the queue (e.g. add()). Those methods are multicast to the group, whereas
22  * read-only methods such as peek() use the local copy. A DistributedQueue is created given the name
23  * of a group; all queues with the same name find each other and form a group.
24  * @author Romuald du Song
25  */

26 public class DistributedQueueDemo extends Frame implements WindowListener JavaDoc, ActionListener JavaDoc,
27                                                            DistributedQueue.Notification
28 {
29     DistributedQueue h = null;
30     final JButton add = new JButton("Add");
31     final JButton quit = new JButton("Quit");
32     final JButton get_all = new JButton("All");
33     final JButton remove = new JButton("Remove");
34     final JLabel value = new JLabel("Value");
35     final JLabel err_msg = new JLabel("Error");
36     final JTextField value_field = new JTextField();
37     final java.awt.List JavaDoc listbox = new java.awt.List JavaDoc();
38     final Font default_font = new Font("Helvetica", Font.PLAIN, 12);
39
40     public DistributedQueueDemo()
41     {
42         super();
43         addWindowListener(this);
44     }
45
46     private void showMsg(String JavaDoc msg)
47     {
48         err_msg.setText(msg);
49         err_msg.setVisible(true);
50     }
51
52     private void clearMsg()
53     {
54         err_msg.setVisible(false);
55     }
56
57     private void removeItem()
58     {
59         h.remove();
60     }
61
62     private void showAll()
63     {
64         if (listbox.getItemCount() > 0)
65         {
66             listbox.removeAll();
67         }
68
69         if (h.size() == 0)
70         {
71             return;
72         }
73
74         clearMsg();
75
76         String JavaDoc key;
77
78         Vector JavaDoc v = h.getContents();
79
80         for (int i = 0; i < v.size(); i++)
81         {
82             listbox.add((String JavaDoc)v.elementAt(i));
83         }
84     }
85
86     public void start(String JavaDoc groupname, ChannelFactory factory, String JavaDoc props)
87                throws ChannelException
88     {
89         h = new DistributedQueue(groupname, factory, props, 10000);
90         h.addNotifier(this);
91
92         setLayout(null);
93         setSize(400, 300);
94         setFont(default_font);
95
96         value.setBounds(new Rectangle(10, 60, 60, 30));
97         value_field.setBounds(new Rectangle(100, 60, 100, 30));
98         listbox.setBounds(new Rectangle(210, 30, 150, 160));
99         err_msg.setBounds(new Rectangle(10, 200, 350, 30));
100         err_msg.setFont(new Font("Helvetica", Font.ITALIC, 12));
101         err_msg.setForeground(Color.red);
102         err_msg.setVisible(false);
103         add.setBounds(new Rectangle(60, 250, 60, 30));
104         quit.setBounds(new Rectangle(130, 250, 70, 30));
105         get_all.setBounds(new Rectangle(210, 250, 60, 30));
106         remove.setBounds(new Rectangle(280, 250, 90, 30));
107
108         add.addActionListener(this);
109         quit.addActionListener(this);
110         get_all.addActionListener(this);
111         remove.addActionListener(this);
112
113         add(value);
114         add(value_field);
115         add(err_msg);
116         add(add);
117         add(quit);
118         add(get_all);
119         add(remove);
120         add(listbox);
121         setTitle("DistributedQueue Demo");
122         showAll();
123         pack();
124         setVisible(true);
125
126         /*
127                  new Thread() {
128                      public void run() {
129                          System.out.println("-- sleeping");
130                          Util.sleep(10000);
131                          for(int i=0; i < 10; i++) {
132                              System.out.println("-- add()");
133                              h.add("Bela#" + i);
134                          }
135
136                          while(true) {
137                              Util.sleep(500);
138                              try
139                             {
140                                 System.out.println(h.remove());
141                             }
142                             catch (QueueClosedException e)
143                             {
144                                 e.printStackTrace();
145                             }
146                          }
147
148                      }
149                  }.start();
150         */

151     }
152
153     public void windowActivated(WindowEvent JavaDoc e)
154     {
155     }
156
157     public void windowClosed(WindowEvent JavaDoc e)
158     {
159     }
160
161     public void windowClosing(WindowEvent JavaDoc e)
162     {
163         System.exit(0);
164     }
165
166     public void windowDeactivated(WindowEvent JavaDoc e)
167     {
168     }
169
170     public void windowDeiconified(WindowEvent JavaDoc e)
171     {
172     }
173
174     public void windowIconified(WindowEvent JavaDoc e)
175     {
176     }
177
178     public void windowOpened(WindowEvent JavaDoc e)
179     {
180     }
181
182     public void actionPerformed(ActionEvent JavaDoc e)
183     {
184         String JavaDoc command = e.getActionCommand();
185
186         try
187         {
188             if (command == "Add")
189             {
190                 String JavaDoc value_name = value_field.getText();
191
192                 if ((value_name == null) || (value_name.length() == 0))
193                 {
194                     showMsg("Value is empty !");
195
196                     return;
197                 }
198
199                 showMsg("Adding value " + value_name + ':');
200                 h.add(value_name);
201             }
202             else if (command == "All")
203             {
204                 showAll();
205             }
206             else if (command == "Quit")
207             {
208                 setVisible(false);
209                 System.exit(0);
210             }
211             else if (command == "Remove")
212             {
213                 removeItem();
214             }
215             else
216             {
217                 System.out.println("Unknown action");
218             }
219         }
220         catch (Exception JavaDoc ex)
221         {
222             value_field.setText("");
223             showMsg(ex.toString());
224         }
225     }
226
227     public void entryAdd(Object JavaDoc value)
228     {
229         showAll();
230     }
231
232     public void entryRemoved(Object JavaDoc key)
233     {
234         showAll();
235     }
236
237     public void viewChange(Vector JavaDoc joined, Vector JavaDoc left)
238     {
239         System.out.println("New members: " + joined + ", left members: " + left);
240     }
241
242     public void contentsSet(Collection JavaDoc new_entries)
243     {
244         System.out.println("Contents Set:" + new_entries);
245     }
246
247     public void contentsCleared()
248     {
249         System.out.println("Contents cleared()");
250     }
251
252     public static void main(String JavaDoc[] args)
253     {
254         String JavaDoc groupname = "QueueDemo";
255         DistributedQueueDemo client = new DistributedQueueDemo();
256         ChannelFactory factory = new JChannelFactory();
257         String JavaDoc arg;
258         String JavaDoc next_arg;
259         boolean trace = false;
260         boolean persist = false;
261
262         String JavaDoc props =
263             "UDP(mcast_addr=228.8.8.8;mcast_port=45566;ip_ttl=32;" +
264             "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):" + "PING(timeout=2000;num_initial_members=3):" +
265             "MERGE2(min_interval=5000;max_interval=10000):" + "FD_SOCK:" + "VERIFY_SUSPECT(timeout=1500):" +
266             "UNICAST(timeout=5000):" + "FRAG(frag_size=8192;down_thread=false;up_thread=false):" +
267             "TOTAL_TOKEN(block_sending=50;unblock_sending=10):" +
268             "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" + "shun=false;print_local_addr=true):" +
269             "STATE_TRANSFER:" + "QUEUE";
270
271         try
272         {
273             for (int i = 0; i < args.length; i++)
274             {
275                 arg = args[i];
276
277                 if ("-trace".equals(arg))
278                 {
279                     trace = true;
280                     continue;
281                 }
282
283                 if ("-groupname".equals(args[i]))
284                 {
285                     groupname = args[++i];
286                     continue;
287                 }
288
289                 help();
290                 return;
291             }
292         }
293         catch (Exception JavaDoc e)
294         {
295             help();
296
297             return;
298         }
299
300         try
301         {
302             client.start(groupname, factory, props);
303         }
304         catch (Throwable JavaDoc t)
305         {
306             t.printStackTrace();
307         }
308     }
309
310     static void help()
311     {
312         System.out.println("DistributedQueueDemo [-help]");
313     }
314 }
315
Popular Tags