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 BatchSample2 implements Runnable {
37  	private static Logger logger = LoggerFactory.getLogger(BatchSample2.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  		List<String> items = new ArrayList<String>();
54  		String item = null;
55  		for (int i = 0; i < 100; i++) {
56  			IServiceMonitor monitor = appstatus.getServiceMonitor("Dummy service", "dummy");
57  
58  			monitor.beginCall("item");
59  			item = "item" + i;
60  			monitor.endCall();
61  			stepMonitor.setCurrentItem(item);
62  			if (i % 5 == 0) {
63  				stepMonitor.reject(item, "Test the reject feature", null);
64  			} else {
65  				try {
66  					Thread.sleep(500);
67  					items.add(item);
68  					stepMonitor.message(item + " item added");
69  				} catch (InterruptedException e) {
70  					stepMonitor.reject(item, e.getMessage(), null);
71  				}
72  			}
73  			stepMonitor.worked(1);
74  
75  			monitor = appstatus.getServiceMonitor("Refs service", "getRef");
76  			monitor.beginCall();
77  			service.getRefs();
78  			if (i % 10 == 0) {
79  				monitor.error("Test erreur reporting");}
80  			monitor.endCall();
81  			
82  			service.getRefsAOP();
83  		}
84  		stepMonitor.done();
85  		return items;
86  	}
87  
88  	/**
89  	 * Write the list content to the console.
90  	 * 
91  	 * @param items
92  	 *            items
93  	 * @param stepMonitor
94  	 *            step monitor
95  	 */
96  	private void step2(List<String> items, IBatchProgressMonitor stepMonitor) {
97  		for (String item : items) {
98  IServiceMonitor sm =	appstatus.getServiceMonitor("Console Write", "Console");
99  			stepMonitor.message("Writing item : " + item);
100 			try {
101 				sm.beginCall(null);
102 				Thread.sleep(100);
103 			} catch (InterruptedException e) {
104 				sm.failure("", e);
105 				e.printStackTrace();
106 			} finally{
107 				sm.endCall();
108 			}
109 			stepMonitor.worked(1);
110 		}
111 		stepMonitor.done();
112 	}
113 
114 	public void run() {
115 
116 		// retrieve the job monitor
117 		IBatchProgressMonitor jobMonitor = appstatus.getBatchProgressMonitor("Sample job", "sample", UUID.randomUUID()
118 				.toString());
119 
120 		jobMonitor.setLogger(logger);
121 		// start the job
122 
123 		// call step 1 (process 100 items)
124 		List<String> items = step1(jobMonitor.createSubTask(1));
125 
126 		// call step 2
127 		step2(items, jobMonitor.createSubTask(1));
128 
129 		// end the job
130 		if (System.currentTimeMillis() % 4 == 0) {
131 			jobMonitor.fail("Just to test failure");
132 
133 		} else
134 			jobMonitor.done();
135 	}
136 
137 	private AppStatus appstatus;
138 
139 	public void setAppstatus(AppStatus appstatus) {
140 		this.appstatus = appstatus;
141 	}
142 }