KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > scheduler > AtomConsumer


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oña, 107 1º2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.scheduler;
34
35 import java.util.Date JavaDoc;
36
37 import java.sql.SQLException JavaDoc;
38 import java.sql.Connection JavaDoc;
39 import java.sql.PreparedStatement JavaDoc;
40 import java.sql.Timestamp JavaDoc;
41
42 import com.knowgate.jdc.JDCConnection;
43 import com.knowgate.dataobjs.DB;
44 import com.knowgate.debug.DebugFile;
45
46 /**
47  * Atom queue consumer
48  * @author Sergio Montoro Ten
49  * @version 1.0
50  */

51 public class AtomConsumer {
52
53   private AtomQueue oQueue;
54   private JDCConnection oConn;
55   private PreparedStatement JavaDoc oStmt;
56
57   // ----------------------------------------------------------
58

59   /**
60    * <p>Create Atom Queue Consumer</p>
61    * @param oConnection
62    * @param oAtomQueue
63    * @throws SQLException
64    */

65   public AtomConsumer(JDCConnection oConnection, AtomQueue oAtomQueue) throws SQLException JavaDoc {
66     oQueue = oAtomQueue;
67     oConn = oConnection;
68
69     if (DebugFile.trace) {
70       DebugFile.writeln("Connection.prepareStatement (UPDATE " + DB.k_job_atoms + " SET " + DB.id_status + "=" + String.valueOf(Atom.STATUS_RUNNING) + ", " + DB.dt_execution + "=? WHERE " + DB.gu_job + "=? AND " + DB.pg_atom + "=?)");
71     }
72
73     // deja preparada la sentencia de actulización de estado del átomo para mejor velocidad
74
oStmt = oConn.prepareStatement("UPDATE " + DB.k_job_atoms + " SET " + DB.id_status + "=" + String.valueOf(Atom.STATUS_RUNNING) + ", " + DB.dt_execution + "=? WHERE " + DB.gu_job + "=? AND " + DB.pg_atom + "=?");
75
76     try { oStmt.setQueryTimeout(20); } catch (SQLException JavaDoc sqle) { }
77   }
78
79   // ----------------------------------------------------------
80

81   public void close() {
82     if (null!=oStmt)
83       try { oStmt.close(); } catch (SQLException JavaDoc sqle) { }
84     oStmt = null;
85   }
86
87   // ----------------------------------------------------------
88

89   /**
90    * <p>Get next Atom and remove it from queue</p>
91    * @return Atom object instance
92    * @throws SQLException
93    */

94
95   public synchronized Atom next() throws SQLException JavaDoc {
96
97     if (DebugFile.trace) {
98       DebugFile.writeln("Begin AtomConsumer.next()");
99       DebugFile.incIdent();
100     }
101
102     Atom oAtm = oQueue.pop();
103
104     if (oAtm!=null) {
105
106       if (DebugFile.trace) DebugFile.writeln("PreparedStatement.setTimestamp(1, new Timestamp(new Date().getTime()))");
107
108       oStmt.setTimestamp(1, new Timestamp JavaDoc(new Date JavaDoc().getTime()));
109
110       if (DebugFile.trace) DebugFile.writeln("PreparedStatement.setString(2, " + oAtm.getStringNull(DB.gu_job,"null") + ")");
111
112       // Actualizar el estado en la base de datos a Finished
113
oStmt.setString(2, oAtm.getString(DB.gu_job));
114
115       if (DebugFile.trace) DebugFile.writeln("PreparedStatement.setInt(3, " + String.valueOf(oAtm.getInt(DB.pg_atom)) + ")");
116
117       oStmt.setInt(3, oAtm.getInt(DB.pg_atom));
118
119       if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeUpdate()");
120
121       oStmt.executeUpdate();
122     }
123
124     if (DebugFile.trace) {
125       DebugFile.decIdent();
126       DebugFile.writeln("End AtomConsumer.next()");
127     }
128
129     return oAtm;
130   } // next()
131

132   // ----------------------------------------------------------
133

134   public JDCConnection getConnection() {
135     return oConn;
136   }
137
138 } // AtomConsumer
Popular Tags