KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > agent > GCEngine


1 /*
2  * Copyright (C) 2004 - 2005 ScalAgent Distributed Technologies
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA.
18  */

19 package fr.dyade.aaa.agent;
20
21 import java.io.IOException JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import org.objectweb.util.monolog.api.BasicLevel;
27 import org.objectweb.util.monolog.api.Logger;
28
29 import fr.dyade.aaa.util.*;
30
31 /**
32  * Implementation of Engine that used Group-Commit in order to improve
33  * performance.
34  */

35 final class GCEngine extends Engine {
36
37   int loop = 0;
38   int NbMaxLoop = 50;
39
40   GCEngine() throws Exception JavaDoc {
41     super();
42
43     NbMaxLoop = Integer.getInteger("NbMaxLoop", NbMaxLoop).intValue();
44     needToBeCommited = false;
45   }
46
47   /**
48    * Commit the agent reaction in case of rigth termination:<ul>
49    * <li>suppress the processed notification from message queue,
50    * then deletes it ;
51    * <li>push all new notifications in qin and qout, and saves them ;
52    * <li>saves the agent state ;
53    * <li>then commit the transaction to validate all changes.
54    * </ul>
55    */

56   void commit() throws Exception JavaDoc {
57     if (logmon.isLoggable(BasicLevel.DEBUG)) {
58       logmon.log(BasicLevel.DEBUG, getName() + ", commit reaction");
59     }
60     loop += 1;
61
62     AgentServer.getTransaction().begin();
63     // Suppress the processed notification from message queue ..
64
qin.pop();
65     // .. then deletes it ..
66
msg.delete();
67     // .. and frees it.
68
msg.free();
69     // Dispatch local messages
70
for (int i=0; i<mq.size(); ) {
71       Message m = (Message) mq.elementAt(i);
72       if (logmon.isLoggable(BasicLevel.DEBUG)) {
73         logmon.log(BasicLevel.DEBUG, getName() + ", dispatch: " + m);
74       }
75       if (m.to.getTo() == AgentServer.getServerId()) {
76         // !AF! Need to be synchronized in order to avoid interaction
77
// !AF! in stamp handling.
78
post(m);
79         mq.removeElementAt(i);
80       } else {
81         i += 1;
82       }
83     }
84     // !AF! It's dangerous to call validate outside of a transaction,
85
// !AF! we really need to enclose this code in a begin/release
86
validate();
87
88     // Saves the agent state then commit the transaction.
89
if (agent != null) agent.save();
90
91     if (needToBeCommited || (qin.size() == 0) || (loop > NbMaxLoop)) {
92       if (logmon.isLoggable(BasicLevel.INFO)) {
93         logmon.log(BasicLevel.INFO, getName() + ", commit: " + loop);
94       }
95       loop = 0;
96
97       // Post all notifications temporary keeped in mq in the rigth consumers,
98
// then saves changes.
99
dispatch();
100       AgentServer.getTransaction().commit();
101       // The transaction has commited, then validate all messages.
102
Channel.validate();
103     }
104     AgentServer.getTransaction().release();
105   }
106 }
107
Popular Tags