KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > examples > DateService


1 /*
2    DateService - Web service that returns the local date
3 */

4
5 package Jt.examples;
6 import java.util.*;
7 import java.lang.reflect.*;
8 import java.beans.*;
9 import java.io.*;
10 import Jt.*;
11
12
13 public class DateService extends JtObject {
14
15   
16
17   public DateService() {
18   }
19
20
21
22
23
24   // Process object messages
25

26   public Object JavaDoc processMessage (Object JavaDoc event) {
27
28    String JavaDoc msgid = null;
29    JtMessage e = (JtMessage) event;
30    Object JavaDoc content;
31    String JavaDoc dt;
32
33      if (e == null)
34     return null;
35
36      msgid = (String JavaDoc) e.getMsgId ();
37
38      if (msgid == null)
39     return null;
40
41      content = e.getMsgContent();
42
43      // Message1
44

45      if (msgid.equals ("JtGET_DATE")) {
46
47         dt = "" + new Date ();
48              
49         return (dt);
50      }
51
52
53      if (msgid.equals ("JtGET_TIME")) {
54              
55         return (new Long JavaDoc ((new Date ()).getTime ()));
56      }
57
58           
59      handleError ("DateService.processMessage: invalid message id:" + msgid);
60      return (null);
61
62   }
63
64
65   // Test program
66

67   public static void main(String JavaDoc[] args) {
68
69     JtObject main = new JtObject ();
70     JtMessage msg1, msg2;
71     Integer JavaDoc count;
72
73     //main.setObjTrace (1);
74
//main.setLogFile ("log.txt");
75

76     msg1 = new JtMessage ();
77     msg2 = new JtMessage ();
78
79
80     msg1.setMsgId ("JtGET_DATE");
81     //msg2.setMsgId ("JtMessage2");
82

83
84     // Create template object
85

86     main.createObject ("Jt.examples.DateService", "template");
87
88
89
90     // Send JtMessage1
91

92     System.out.println ((String JavaDoc) main.sendMessage ("template", msg1));
93
94
95     // Send JtMessage2
96

97     main.sendMessage ("template", msg2);
98
99     main.removeObject ("template");
100         
101
102   }
103
104 }
105
106
107
Popular Tags