Reputation: 657
We tried to follow the example in Java Thread Example? - the example given by @Phil.
The following code snippet below - during run - gives the error:
HTTP Error 500
access denied ("java.lang.RuntimePermission" "modifyThreadGroup")
at CreateThreads.<init>(CreateThreads.java:15)
at LbaApi.doPost(LbaApi.java:32)
The error occurs when the Constructor is called "public CreateThreads(ArrayList strList)". The CreateThreads is called from the doPost method - which is shown below. We tried many options but could not resolve the error. Any advice on this error - if we are missing something here. Thanks,
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.http.*;
@SuppressWarnings("serial")
public class LbaApi extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter writer = resp.getWriter();
String threadName = "TNAME";
String inputJson = req.getParameter("inputJson");
String outputJson = "";
ArrayList<String> strList = new ArrayList<String>();
strList.add(threadName);
strList.add(inputJson);
strList.add(outputJson);
CreateThreads myThread = new CreateThreads(strList); //Line 32
myThread.start();
resp.setContentType("text/plain");
outputJson = strList.get(2);
writer.println(outputJson);
}
}
While the CreateThreads codesegment is as follows:
import java.util.ArrayList;
public class CreateThreads extends Thread {
private ArrayList<String> strList;
private String threadName;
public CreateThreads(ArrayList<String> strList) {//Line 15
this.threadName = strList.get(0);
super.setName(this.threadName);
this.strList = strList;
}
@Override
public void run() {
if (this.threadName.equals("TNAME")){
String outputJson = "{'TableA':{'field_A1':'A_11'}}";
this.strList.add(outputJson);
}
}
}
Upvotes: 1
Views: 460
Reputation: 5227
In order to use threads within an app engine application you may spawn a thread as documented in the official docs:
Runnable
com.google.appengine.api.ThreadManager.currentRequestThreadFactory()
newRequestThread()
of above factory with your Runnable
as argumentNote that, unless you're using a backend server, your threads cannot "outlive" your current request, making threads basically only useful for cron jobs and tasks where you don't have the usual 60 seconds timeout, but 10 minutes.
I saw the URL posted by @Andreas, however i don't believe the answer over at aerospike is appropriate. The question at aerospike.com was about someone trying to run a generic Java EE app on app engine, thus the way threads are created was not compatible with the app engine environment.
Upvotes: 2