View Javadoc

1   /*
2    * Copyright 2010 Capgemini
3    * Licensed under the Apache License, Version 2.0 (the "License"); 
4    * you may not use this file except in compliance with the License. 
5    * You may obtain a copy of the License at 
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0 
8    * 
9    * Unless required by applicable law or agreed to in writing, software 
10   * distributed under the License is distributed on an "AS IS" BASIS, 
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
12   * See the License for the specific language governing permissions and 
13   * limitations under the License. 
14   * 
15   */
16  package net.sf.appstatus.demo.batch;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.UUID;
21  
22  import net.sf.appstatus.core.AppStatus;
23  import net.sf.appstatus.core.batch.IBatchProgressMonitor;
24  import net.sf.appstatus.core.services.IServiceMonitor;
25  
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  /**
30   * Classic sample batch, using spring injection for appstatus.
31   * 
32   * @author Guillaume Mary
33   * @author Nicolas Richeton
34   * 
35   */
36  public class BatchSample implements Runnable {
37  	private static Logger logger = LoggerFactory.getLogger(BatchSample.class);
38  
39  	private ServiceSample service;
40  
41  	public void setService(ServiceSample service) {
42  		this.service = service;
43  	}
44  
45  	/**
46  	 * Create item list.
47  	 * 
48  	 * @param stepMonitor
49  	 *            step monitor
50  	 * @return items
51  	 */
52  	private List<String> step1(IBatchProgressMonitor stepMonitor) {
53  		stepMonitor.beginTask("step1", "Create the item list", 100);
54  		List<String> items = new ArrayList<String>();
55  		String item = null;
56  		for (int i = 0; i < 100; i++) {
57  			IServiceMonitor monitor = appstatus.getServiceMonitor("Dummy service", "dummy");
58  
59  			monitor.beginCall("item");
60  			item = "item" + i;
61  			monitor.endCall();
62  			stepMonitor.setCurrentItem(item);
63  			if (i % 5 == 0) {
64  				stepMonitor.reject(item, "Test the reject feature", null);
65  			} else {
66  				try {
67  					Thread.sleep(500);
68  					items.add(item);
69  					stepMonitor.message(item + " item added");
70  				} catch (InterruptedException e) {
71  					stepMonitor.reject(item, e.getMessage(), null);
72  				}
73  			}
74  			stepMonitor.worked(1);
75  
76  			monitor = appstatus.getServiceMonitor("Refs service", "getRef");
77  			monitor.beginCall();
78  			service.getRefs();
79  			if (i % 10 == 0) {
80  				monitor.error("Test erreur reporting");}
81  			monitor.endCall();
82  			
83  			service.getRefsAOP();
84  		}
85  		stepMonitor.done();
86  		return items;
87  	}
88  
89  	/**
90  	 * Write the list content to the console.
91  	 * 
92  	 * @param items
93  	 *            items
94  	 * @param stepMonitor
95  	 *            step monitor
96  	 */
97  	private void step2(List<String> items, IBatchProgressMonitor stepMonitor) {
98  		stepMonitor.beginTask("step2", "Write the items in the console output.", items.size());
99  		for (String item : items) {
100 IServiceMonitor sm =	appstatus.getServiceMonitor("Console Write", "Console");
101 			stepMonitor.message("Writing item : " + item);
102 			try {
103 				sm.beginCall(null);
104 				Thread.sleep(100);
105 			} catch (InterruptedException e) {
106 				sm.failure("", e);
107 				e.printStackTrace();
108 			} finally{
109 				sm.endCall();
110 			}
111 			stepMonitor.worked(1);
112 		}
113 		stepMonitor.done();
114 	}
115 
116 	public void run() {
117 
118 		// retrieve the job monitor
119 		IBatchProgressMonitor jobMonitor = appstatus.getBatchProgressMonitor("Sample job", "sample", UUID.randomUUID()
120 				.toString());
121 
122 		jobMonitor.setLogger(logger);
123 		// start the job
124 		jobMonitor.beginTask("sample", "A batch sample", 2);
125 
126 		// call step 1 (process 100 items)
127 		List<String> items = step1(jobMonitor.createSubTask(1));
128 
129 		// call step 2
130 		step2(items, jobMonitor.createSubTask(1));
131 
132 		// end the job
133 		if (System.currentTimeMillis() % 4 == 0) {
134 			jobMonitor.fail("Just to test failure");
135 
136 		} else
137 			jobMonitor.done();
138 	}
139 
140 	private AppStatus appstatus;
141 
142 	public void setAppstatus(AppStatus appstatus) {
143 		this.appstatus = appstatus;
144 	}
145 }