KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > engine > clock > ClockListener


1 package org.objectweb.petals.engine.clock;
2
3 import java.text.DateFormat JavaDoc;
4 import java.text.SimpleDateFormat JavaDoc;
5 import java.util.Date JavaDoc;
6 import java.util.logging.Level JavaDoc;
7 import java.util.logging.Logger JavaDoc;
8
9 import javax.jbi.messaging.DeliveryChannel;
10 import javax.jbi.messaging.ExchangeStatus;
11 import javax.jbi.messaging.MessageExchange;
12 import javax.jbi.messaging.MessagingException;
13 import javax.jbi.messaging.NormalizedMessage;
14 import javax.xml.namespace.QName JavaDoc;
15
16 import org.objectweb.petals.shared.xmlmanipulation.SourceHelper;
17
18 public class ClockListener implements Runnable JavaDoc {
19
20     private DeliveryChannel channel;
21     
22     private MessageExchange messageExchange;
23     
24     private boolean running;
25     
26     private Logger JavaDoc logger;
27     
28     public ClockListener(DeliveryChannel channel, Logger JavaDoc logger) {
29         super();
30         this.channel = channel;
31         this.logger = logger;
32     }
33
34     public void run() {
35         
36         running=true;
37         
38         mainLoop: while(running){
39             try
40             {
41                 messageExchange = channel.accept();
42                 
43                 process(messageExchange);
44     
45             }
46             catch (MessagingException e) {
47                 logger.log(Level.SEVERE,e.getMessage(),e);
48                 continue mainLoop;
49             }
50         }
51
52     }
53
54     private void process(MessageExchange msg)
55     {
56         logger.log(Level.INFO,"");
57         try
58         {
59             if(new QName JavaDoc("time").equals(msg.getOperation()))
60             {
61                 logger.log(Level.INFO,"The Clock service is called to return current time");
62             
63                 DateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("MM/dd/yyyy HH:mm:ss");
64                 String JavaDoc en = formatter.format(new Date JavaDoc(System.currentTimeMillis()));
65                 
66                 
67                 logger.log(Level.INFO,"The Clock answer : "+en);
68                 
69                 NormalizedMessage nm = msg.createMessage();
70                 
71                 nm.setContent(SourceHelper.createSource(en));
72                 msg.setMessage(nm,"OUT");
73                 msg.setStatus(ExchangeStatus.DONE);
74             }
75             else
76             {
77                 Exception JavaDoc e = new Exception JavaDoc("only 'time' operation is recognized");
78                 msg.setError(e);
79                 msg.setStatus(ExchangeStatus.ERROR);
80                 logger.log(Level.WARNING,e.getMessage(),e);
81             }
82             // Send the message (response and/or status)
83
channel.send(msg);
84         }
85         catch (Throwable JavaDoc e)
86         {
87             logger.log(Level.SEVERE,e.getMessage(),e);
88         }
89     }
90     
91     public void stopProcessing(){
92         this.running = false;
93     }
94     
95 }
96
Popular Tags