1 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 ; 12 import java.awt.event.ActionListener ; 13 import java.awt.event.WindowEvent ; 14 import java.awt.event.WindowListener ; 15 import java.util.Collection ; 16 import java.util.Vector ; 17 18 19 26 public class DistributedQueueDemo extends Frame implements WindowListener , ActionListener , 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 listbox = new java.awt.List (); 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 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 key; 77 78 Vector v = h.getContents(); 79 80 for (int i = 0; i < v.size(); i++) 81 { 82 listbox.add((String )v.elementAt(i)); 83 } 84 } 85 86 public void start(String groupname, ChannelFactory factory, String 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 151 } 152 153 public void windowActivated(WindowEvent e) 154 { 155 } 156 157 public void windowClosed(WindowEvent e) 158 { 159 } 160 161 public void windowClosing(WindowEvent e) 162 { 163 System.exit(0); 164 } 165 166 public void windowDeactivated(WindowEvent e) 167 { 168 } 169 170 public void windowDeiconified(WindowEvent e) 171 { 172 } 173 174 public void windowIconified(WindowEvent e) 175 { 176 } 177 178 public void windowOpened(WindowEvent e) 179 { 180 } 181 182 public void actionPerformed(ActionEvent e) 183 { 184 String command = e.getActionCommand(); 185 186 try 187 { 188 if (command == "Add") 189 { 190 String 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 ex) 221 { 222 value_field.setText(""); 223 showMsg(ex.toString()); 224 } 225 } 226 227 public void entryAdd(Object value) 228 { 229 showAll(); 230 } 231 232 public void entryRemoved(Object key) 233 { 234 showAll(); 235 } 236 237 public void viewChange(Vector joined, Vector left) 238 { 239 System.out.println("New members: " + joined + ", left members: " + left); 240 } 241 242 public void contentsSet(Collection 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 [] args) 253 { 254 String groupname = "QueueDemo"; 255 DistributedQueueDemo client = new DistributedQueueDemo(); 256 ChannelFactory factory = new JChannelFactory(); 257 String arg; 258 String next_arg; 259 boolean trace = false; 260 boolean persist = false; 261 262 String 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 e) 294 { 295 help(); 296 297 return; 298 } 299 300 try 301 { 302 client.start(groupname, factory, props); 303 } 304 catch (Throwable t) 305 { 306 t.printStackTrace(); 307 } 308 } 309 310 static void help() 311 { 312 System.out.println("DistributedQueueDemo [-help]"); 313 } 314 } 315 | Popular Tags |