Skip to content

Commit 49c1142

Browse files
author
Arun Gupta
committed
Changing thread to manageablethreas, also added a new sample to show how default ManagedThreadFactory can be obtained using @resource without specifying the name
1 parent ca79559 commit 49c1142

7 files changed

Lines changed: 563 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-shared-configuration>
3+
<!--
4+
This file contains additional configuration written by modules in the NetBeans IDE.
5+
The configuration is intended to be shared among all the users of project and
6+
therefore it is assumed to be part of version control checkout.
7+
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
8+
-->
9+
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
10+
<!--
11+
Properties that influence various parts of the IDE, especially code formatting and the like.
12+
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
13+
That way multiple projects can share the same settings (useful for formatting rules for example).
14+
Any value defined here will override the pom.xml file value but is only applicable to the current project.
15+
-->
16+
<org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>gfv3ee6</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>
17+
</properties>
18+
</project-shared-configuration>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.concurrency</groupId>
6+
<artifactId>concurrency-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.javaee7.concurrency</groupId>
12+
<artifactId>manageablethread</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<packaging>war</packaging>
15+
<name>manageablethread</name>
16+
</project>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.concurrency.manageablethread;
41+
42+
/**
43+
* @author Arun Gupta
44+
*/
45+
public class MyTask implements Runnable {
46+
47+
private int id;
48+
49+
public MyTask(int id) {
50+
this.id = id;
51+
}
52+
53+
@Override
54+
public void run() {
55+
System.out.println("Running Task: " + id);
56+
}
57+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.concurrency.manageablethread;
41+
42+
import java.io.IOException;
43+
import java.io.PrintWriter;
44+
import java.util.logging.Level;
45+
import java.util.logging.Logger;
46+
import javax.enterprise.concurrent.ManagedThreadFactory;
47+
import javax.naming.InitialContext;
48+
import javax.naming.NamingException;
49+
import javax.servlet.ServletException;
50+
import javax.servlet.annotation.WebServlet;
51+
import javax.servlet.http.HttpServlet;
52+
import javax.servlet.http.HttpServletRequest;
53+
import javax.servlet.http.HttpServletResponse;
54+
55+
/**
56+
* @author Arun Gupta
57+
*/
58+
@WebServlet(urlPatterns = {"/TestJNDIServlet"})
59+
public class TestJNDIServlet extends HttpServlet {
60+
61+
/**
62+
* Processes requests for both HTTP
63+
* <code>GET</code> and
64+
* <code>POST</code> methods.
65+
*
66+
* @param request servlet request
67+
* @param response servlet response
68+
* @throws ServletException if a servlet-specific error occurs
69+
* @throws IOException if an I/O error occurs
70+
*/
71+
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
72+
throws ServletException, IOException {
73+
response.setContentType("text/html;charset=UTF-8");
74+
try (PrintWriter out = response.getWriter()) {
75+
out.println("<!DOCTYPE html>");
76+
out.println("<html>");
77+
out.println("<head>");
78+
out.println("<title>Servlet TestJNDIServlet</title>");
79+
out.println("</head>");
80+
out.println("<body>");
81+
out.println("<h1>Getting ManagedThreadFactory using JNDI lookup</h1>");
82+
try {
83+
InitialContext ctx = new InitialContext();
84+
85+
ManagedThreadFactory factory = (ManagedThreadFactory) ctx.lookup("java:comp/DefaultManagedThreadFactory");
86+
// ManagedExecutorService executor = (ManagedExecutorService) ctx.lookup("concurrent/myExecutor");
87+
out.println("Getting ManageableThread<br>");
88+
Thread thread = factory.newThread(new MyTask(2));
89+
out.println("Starting thread<br>");
90+
thread.start();
91+
out.println("Thread started<br><br>");
92+
} catch (NamingException ex) {
93+
Logger.getLogger(TestResourceServlet.class.getName()).log(Level.SEVERE, null, ex);
94+
}
95+
out.println("all tasks submitted<br/><br/>");
96+
out.println("Check server.log for output from the task.");
97+
out.println("</body>");
98+
out.println("</html>");
99+
}
100+
}
101+
102+
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
103+
/**
104+
* Handles the HTTP
105+
* <code>GET</code> method.
106+
*
107+
* @param request servlet request
108+
* @param response servlet response
109+
* @throws ServletException if a servlet-specific error occurs
110+
* @throws IOException if an I/O error occurs
111+
*/
112+
@Override
113+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
114+
throws ServletException, IOException {
115+
processRequest(request, response);
116+
}
117+
118+
/**
119+
* Handles the HTTP
120+
* <code>POST</code> method.
121+
*
122+
* @param request servlet request
123+
* @param response servlet response
124+
* @throws ServletException if a servlet-specific error occurs
125+
* @throws IOException if an I/O error occurs
126+
*/
127+
@Override
128+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
129+
throws ServletException, IOException {
130+
processRequest(request, response);
131+
}
132+
133+
/**
134+
* Returns a short description of the servlet.
135+
*
136+
* @return a String containing servlet description
137+
*/
138+
@Override
139+
public String getServletInfo() {
140+
return "Short description";
141+
}// </editor-fold>
142+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.concurrency.manageablethread;
41+
42+
import java.io.IOException;
43+
import java.io.PrintWriter;
44+
import javax.annotation.Resource;
45+
import javax.enterprise.concurrent.ManagedThreadFactory;
46+
import javax.servlet.ServletException;
47+
import javax.servlet.annotation.WebServlet;
48+
import javax.servlet.http.HttpServlet;
49+
import javax.servlet.http.HttpServletRequest;
50+
import javax.servlet.http.HttpServletResponse;
51+
52+
/**
53+
* @author Arun Gupta
54+
*/
55+
@WebServlet(urlPatterns = {"/TestResourceNoNameServlet"})
56+
public class TestResourceNoNameServlet extends HttpServlet {
57+
58+
@Resource
59+
ManagedThreadFactory factory;
60+
61+
62+
/**
63+
* Processes requests for both HTTP
64+
* <code>GET</code> and
65+
* <code>POST</code> methods.
66+
*
67+
* @param request servlet request
68+
* @param response servlet response
69+
* @throws ServletException if a servlet-specific error occurs
70+
* @throws IOException if an I/O error occurs
71+
*/
72+
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
73+
throws ServletException, IOException {
74+
response.setContentType("text/html;charset=UTF-8");
75+
try (PrintWriter out = response.getWriter()) {
76+
out.println("<html>");
77+
out.println("<head>");
78+
out.println("<title>Getting ManagedThreadFactory using @Resource with no name</title>");
79+
out.println("</head>");
80+
out.println("<body>");
81+
out.println("<h1>Getting ManagedThreadFactory using @Resource with no name</h1>");
82+
83+
out.println("Getting ManageableThread<br>");
84+
Thread thread = factory.newThread(new MyTask(1));
85+
out.println("Starting thread<br>");
86+
thread.start();
87+
out.println("Thread started<br><br>");
88+
out.println("Check server.log for output from the task.");
89+
out.println("</body>");
90+
out.println("</html>");
91+
}
92+
}
93+
94+
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
95+
/**
96+
* Handles the HTTP
97+
* <code>GET</code> method.
98+
*
99+
* @param request servlet request
100+
* @param response servlet response
101+
* @throws ServletException if a servlet-specific error occurs
102+
* @throws IOException if an I/O error occurs
103+
*/
104+
@Override
105+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
106+
throws ServletException, IOException {
107+
processRequest(request, response);
108+
}
109+
110+
/**
111+
* Handles the HTTP
112+
* <code>POST</code> method.
113+
*
114+
* @param request servlet request
115+
* @param response servlet response
116+
* @throws ServletException if a servlet-specific error occurs
117+
* @throws IOException if an I/O error occurs
118+
*/
119+
@Override
120+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
121+
throws ServletException, IOException {
122+
processRequest(request, response);
123+
}
124+
125+
/**
126+
* Returns a short description of the servlet.
127+
*
128+
* @return a String containing servlet description
129+
*/
130+
@Override
131+
public String getServletInfo() {
132+
return "Short description";
133+
}// </editor-fold>
134+
135+
}

0 commit comments

Comments
 (0)