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.io.IOException;
19  import java.util.concurrent.ExecutorService;
20  import java.util.concurrent.Executors;
21  
22  import javax.servlet.ServletException;
23  import javax.servlet.ServletOutputStream;
24  import javax.servlet.http.HttpServlet;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.springframework.web.context.support.WebApplicationContextUtils;
29  
30  /**
31   * Sample service call servlet, used to generate some service usage.
32   * 
33   * @author Guillaume Mary
34   * 
35   */
36  public class LaunchClassicBatchSampleServlet extends HttpServlet {
37  
38  	private static final String ENCODING = "UTF-8";
39  
40  	/**
41  	 * Generated serial version UID.
42  	 */
43  	private static final long serialVersionUID = -8830877354343317996L;
44  
45  	/*
46  	 * (non-Javadoc)
47  	 * 
48  	 * @see
49  	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
50  	 * , javax.servlet.http.HttpServletResponse)
51  	 */
52  	@Override
53  	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
54  			throws ServletException, IOException {
55  
56  		// launch the batch
57  		ExecutorService executorService = Executors.newCachedThreadPool();
58  
59  		BatchSample batch = (BatchSample) WebApplicationContextUtils
60  				.getRequiredWebApplicationContext(getServletContext()).getBean(
61  						"batch");
62  		executorService.execute(batch);
63  		
64  		BatchSample2 batch2 = (BatchSample2) WebApplicationContextUtils
65  				.getRequiredWebApplicationContext(getServletContext()).getBean(
66  						"batch2");
67  		executorService.execute(batch2);
68  
69  		ServletOutputStream os = resp.getOutputStream();
70  
71  		os.write("<html><head".getBytes(ENCODING));
72  		os.write("<body>".getBytes(ENCODING));
73  		os.write("<h1>Ok</h1>".getBytes(ENCODING));
74  		os.write("</body></html>".getBytes(ENCODING));
75  	}
76  }