Source Code Cross Referenced for PersistedServiceJob.java in  » ERP-CRM-Financial » SourceTap-CRM » org » ofbiz » service » job » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » SourceTap CRM » org.ofbiz.service.job 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: PersistedServiceJob.java,v 1.7 2003/12/14 02:16:47 ajzeneski Exp $
003:         *
004:         * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005:         *
006:         * Permission is hereby granted, free of charge, to any person obtaining a
007:         * copy of this software and associated documentation files (the "Software"),
008:         * to deal in the Software without restriction, including without limitation
009:         * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010:         * and/or sell copies of the Software, and to permit persons to whom the
011:         * Software is furnished to do so, subject to the following conditions:
012:         *
013:         * The above copyright notice and this permission notice shall be included
014:         * in all copies or substantial portions of the Software.
015:         *
016:         * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017:         * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018:         * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019:         * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020:         * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021:         * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022:         * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023:         *
024:         */
025:        package org.ofbiz.service.job;
026:
027:        import java.io.IOException;
028:        import java.sql.Timestamp;
029:        import java.util.HashMap;
030:        import java.util.Map;
031:        import java.util.Calendar;
032:        import java.util.Date;
033:
034:        import javax.xml.parsers.ParserConfigurationException;
035:
036:        import org.ofbiz.base.util.Debug;
037:        import org.ofbiz.base.util.UtilDateTime;
038:        import org.ofbiz.base.util.UtilMisc;
039:        import org.ofbiz.base.util.UtilProperties;
040:        import org.ofbiz.entity.GenericDelegator;
041:        import org.ofbiz.entity.GenericEntityException;
042:        import org.ofbiz.entity.GenericValue;
043:        import org.ofbiz.entity.serialize.SerializeException;
044:        import org.ofbiz.entity.serialize.XmlSerializer;
045:        import org.ofbiz.service.DispatchContext;
046:        import org.ofbiz.service.GenericRequester;
047:        import org.ofbiz.service.config.ServiceConfigUtil;
048:        import org.ofbiz.service.calendar.RecurrenceInfo;
049:        import org.xml.sax.SAXException;
050:
051:        /**
052:         * Entity Service Job - Store => Schedule => Run
053:         *
054:         * @author     <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
055:         * @version    $Revision: 1.7 $
056:         * @since      2.0
057:         */
058:        public class PersistedServiceJob extends GenericServiceJob {
059:
060:            public static final String module = PersistedServiceJob.class
061:                    .getName();
062:
063:            private transient GenericDelegator delegator = null;
064:            private Timestamp storedDate = null;
065:            private long nextRecurrence = -1;
066:
067:            /**
068:             * Creates a new PersistedServiceJob
069:             * @param dctx
070:             * @param jobValue
071:             * @param req
072:             */
073:            public PersistedServiceJob(DispatchContext dctx,
074:                    GenericValue jobValue, GenericRequester req) {
075:                super (jobValue.getString("jobName"));
076:                this .delegator = dctx.getDelegator();
077:                this .requester = req;
078:                this .dctx = dctx;
079:                this .storedDate = jobValue.getTimestamp("runTime");
080:                this .runtime = storedDate.getTime();
081:
082:                // set the start time to now
083:                String instanceId = UtilProperties.getPropertyValue(
084:                        "general.properties", "unique.instanceId", "ofbiz0");
085:                jobValue.set("startDateTime", UtilDateTime.nowTimestamp());
086:                jobValue.set("runByInstanceId", instanceId);
087:                try {
088:                    jobValue.store();
089:                } catch (GenericEntityException e) {
090:                    Debug
091:                            .logError(e,
092:                                    "Unable to set the startDateTime on the current job; not running!");
093:                    runtime = -1;
094:                }
095:            }
096:
097:            /**
098:             * @see org.ofbiz.service.job.GenericServiceJob#init()
099:             */
100:            protected void init() {
101:                super .init();
102:
103:                // configure any addition recurrences
104:                GenericValue job = this .getJob();
105:                RecurrenceInfo recurrence = JobManager.getRecurrenceInfo(job);
106:
107:                try {
108:                    if (recurrence != null) {
109:                        recurrence.incrementCurrentCount();
110:                        long next = recurrence.next();
111:                        createRecurrence(job, next);
112:                    }
113:                } catch (GenericEntityException e) {
114:                    throw new RuntimeException(e.getMessage());
115:                }
116:                if (Debug.infoOn())
117:                    Debug.logInfo(this .toString() + " -- Next runtime: "
118:                            + nextRecurrence, module);
119:            }
120:
121:            private void createRecurrence(GenericValue job, long next)
122:                    throws GenericEntityException {
123:                if (Debug.verboseOn())
124:                    Debug.logVerbose("Next runtime returned: " + next, module);
125:                GenericValue newJob = new GenericValue(job);
126:                if (next > runtime) {
127:                    newJob.set("startDateTime", null);
128:                    newJob.set("runTime", new java.sql.Timestamp(next));
129:                    nextRecurrence = next;
130:                    delegator.create(newJob);
131:                    if (Debug.verboseOn())
132:                        Debug.logVerbose("Created next job entry: " + newJob,
133:                                module);
134:                }
135:            }
136:
137:            /**
138:             * @see org.ofbiz.service.job.GenericServiceJob#finish()
139:             */
140:            protected void finish() {
141:                super .finish();
142:
143:                // set the finish date
144:                GenericValue job = getJob();
145:                job.set("finishDateTime", UtilDateTime.nowTimestamp());
146:                try {
147:                    job.store();
148:                } catch (GenericEntityException e) {
149:                    Debug.logError(e, "Cannot update the datasource", module);
150:                }
151:            }
152:
153:            /**
154:             * @see org.ofbiz.service.job.GenericServiceJob#failed(Throwable)
155:             */
156:            protected void failed(Throwable t) {
157:                super .failed(t);
158:
159:                // if the job has not been re-scheduled; we need to re-schedule and run again
160:                if (nextRecurrence == -1) {
161:                    // create a recurrence
162:                    Calendar cal = Calendar.getInstance();
163:                    cal.setTime(new Date());
164:                    cal.add(Calendar.MINUTE, ServiceConfigUtil
165:                            .getFailedRetryMin());
166:                    long next = cal.getTimeInMillis();
167:                    GenericValue job = getJob();
168:                    try {
169:                        createRecurrence(job, next);
170:                    } catch (GenericEntityException gee) {
171:                        Debug.logError(gee,
172:                                "ERROR: Unable to re-schedule job to re-run : "
173:                                        + job, module);
174:                    }
175:                    Debug.log("Persisted Job Failed Re-Scheduling : " + next,
176:                            module);
177:                }
178:            }
179:
180:            /**
181:             * @see org.ofbiz.service.job.GenericServiceJob#getServiceName()
182:             */
183:            protected String getServiceName() {
184:                GenericValue jobObj = getJob();
185:                if (jobObj == null || jobObj.get("serviceName") == null)
186:                    return null;
187:                return jobObj.getString("serviceName");
188:            }
189:
190:            /**
191:             * @see org.ofbiz.service.job.GenericServiceJob#getContext()
192:             */
193:            protected Map getContext() {
194:                Map context = null;
195:                try {
196:                    GenericValue jobObj = getJob();
197:                    GenericValue contextObj = jobObj
198:                            .getRelatedOne("RuntimeData");
199:
200:                    if (contextObj != null) {
201:                        context = (Map) XmlSerializer.deserialize(contextObj
202:                                .getString("runtimeInfo"), delegator);
203:                    }
204:
205:                    if (context == null) {
206:                        context = new HashMap();
207:                    }
208:
209:                    // check the runAsUser
210:                    GenericValue runAsUser = jobObj
211:                            .getRelatedOne("RunAsUserLogin");
212:                    if (runAsUser != null) {
213:                        context.put("userLogin", runAsUser);
214:                    }
215:                } catch (GenericEntityException e) {
216:                    Debug
217:                            .logError(
218:                                    e,
219:                                    "PersistedServiceJob.getContext(): Entity Exception",
220:                                    module);
221:                } catch (SerializeException e) {
222:                    Debug
223:                            .logError(
224:                                    e,
225:                                    "PersistedServiceJob.getContext(): Serialize Exception",
226:                                    module);
227:                } catch (ParserConfigurationException e) {
228:                    Debug
229:                            .logError(
230:                                    e,
231:                                    "PersistedServiceJob.getContext(): Parse Exception",
232:                                    module);
233:                } catch (SAXException e) {
234:                    Debug.logError(e,
235:                            "PersistedServiceJob.getContext(): SAXException",
236:                            module);
237:                } catch (IOException e) {
238:                    Debug.logError(e,
239:                            "PersistedServiceJob.getContext(): IOException",
240:                            module);
241:                }
242:                if (context == null)
243:                    Debug.logError("Job context is null", module);
244:                return context;
245:            }
246:
247:            // gets the job value object
248:            private GenericValue getJob() {
249:                try {
250:                    Map fields = UtilMisc.toMap("jobName", getJobName(),
251:                            "runTime", storedDate);
252:                    GenericValue jobObj = delegator.findByPrimaryKey(
253:                            "JobSandbox", fields);
254:
255:                    if (jobObj == null)
256:                        Debug.logError("Job came back null from datasource",
257:                                module);
258:                    return jobObj;
259:                } catch (GenericEntityException e) {
260:                    Debug.logError(e, "Cannot get job definition from entity",
261:                            module);
262:                    e.printStackTrace();
263:                }
264:                return null;
265:            }
266:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.