KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > events > Client


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb.events;
25
26 public class Client implements Runnable JavaDoc
27 {
28     public static int PORT = 54321;
29     public static String JavaDoc USERNAME = "admin";
30     public static String JavaDoc PASSWORD = "admin";
31     public static Thread JavaDoc LISTEN_THREAD = null;
32
33     public static org.myoodb.MyOodbDatabase THE_DB = null;
34
35     public static void main(String JavaDoc args[]) throws Exception JavaDoc
36     {
37         org.myoodb.MyOodbDatabase db = THE_DB = org.myoodb.MyOodbDatabase.open("myoodb://localhost:" + PORT, USERNAME, PASSWORD);
38
39         org.myoodb.collectable.LinkedList persons = (org.myoodb.collectable.LinkedList) db.getRoot("Persons");
40
41         if (persons == null)
42         {
43             persons = (org.myoodb.collectable.LinkedList) db.createRoot(org.myoodb.collectable.LinkedListDbImpl.class, "Persons");
44             //persons = (org.myoodb.collectable.LinkedList) db.createRoot("org.myoodb.collectable.LinkedListDbImpl", "Persons");
45
}
46
47         Person person = (Person) db.createObject(PersonDbImpl.class);
48         //Person person = (Person) db.createObject("org.myoodb.events.PersonDbImpl");
49
person.setName("John Smith");
50
51         persons.add(person);
52
53         org.myoodb.event.EventLog eventLog = (org.myoodb.event.EventLog) db.getRoot("EventLog");
54
55         if (eventLog == null)
56         {
57             eventLog = (org.myoodb.event.EventLog) db.createRoot(org.myoodb.event.EventLogDbImpl.class, "EventLog");
58             //eventLog = (org.myoodb.event.EventLog) db.createRoot("org.myoodb.event.EventLogDbImpl", "EventLog");
59
}
60
61         LISTEN_THREAD = new Thread JavaDoc(new Client());
62         LISTEN_THREAD.setDaemon(true);
63         LISTEN_THREAD.start();
64
65         for (int i = 0; i < 100; i++)
66         {
67             MessageEvent messageEvent = (MessageEvent) db.createObject(MessageEventDbImpl.class);
68
69             messageEvent.setMessage("Did you clean the basement?");
70             messageEvent.setLogStore(eventLog);
71
72             messageEvent.notify(person);
73
74             Thread.sleep(1000);
75         }
76     }
77
78     public void run()
79     {
80         try
81         {
82             org.myoodb.event.EventLog eventLog = (org.myoodb.event.EventLog) THE_DB.getRoot("EventLog");
83
84             while (LISTEN_THREAD != null)
85             {
86                 java.util.ArrayList JavaDoc events = eventLog.listenForEvents(MessageEvent.class);
87                 //java.util.ArrayList events = eventLog.listenForEvents(org.myoodb.event.Event.class); // listen for all events
88

89                 for (int i = 0; i < events.size(); i++)
90                 {
91                     MessageEvent messageEvent = (MessageEvent) events.get(i);
92
93                     Person person = (Person) THE_DB.getObject(messageEvent.getCollectableHandle());
94
95                     System.out.println("Message: " + messageEvent + " To: " + person);
96                 }
97             }
98         }
99         catch (Exception JavaDoc e)
100         {
101             e.printStackTrace();
102         }
103     }
104 }
105
Popular Tags