2

I have a task to do it in background after an HTTP Post Request, so can I do it like this way in Java7 EE. (They said that Java7 EE can use the JSE concurrencies). so here is my code:

@POST public String contactMe( @FormParam("name") String name, @FormParam("email") String email, @FormParam("website") String website, @FormParam("message") String message) {
    System.out.println("you have sent name " + name + "  email  " + email + "  website " + website + "   message " + message);



    /*methode Timer*/
    Timer timer = new Timer();
    MyTimerTask myTimerTask=new MyTimerTask();
    timer.schedule(myTimerTask,10000);
    //I have to do something n back ground that take 10 seconds at least


    return "<h1>DONE</h1>";
}

I am confused may be there is a better way to handle these kind of problem in the Enterprise World, because memory managing is different than desktop apps. Thanks in advance.

AndroidLover
  • 1,171
  • 1
  • 13
  • 16

2 Answers2

1

Finally, I have got the answer and I am happy with JEE7 Multithreading tools:

 @Stateless
 public class ReportBean {
 @Resource  
 private ManagedThreadFactory threadFactory;
 public void runReports() {
 Thread thread = threadFactory.newThread(new Runnable());
 thread.start();
 }

I've used this bean in my work... The Java EE7 offer a new way to make the using of JAVA SE Managed by the Web Container and not by the JVM.

  • Looks like this example has been copied from this [post](http://stackoverflow.com/questions/3212255/java-ee-specification-and-multi-threading/19404307#19404307) – Chris Ritchie Jan 27 '14 at 19:29
0

What is the purpose of your timer?

Your code receives some GET info, then waits 10 seconds and then starts performing the myTimerTask.

Edit:

upon inspecting your post a little further: it seems like you want to perform the task synchronously; if that is truly the case, just execute the timertask.

However your comment suggests that you wish to perform the task in the background, but then I don't understand why you seem to imply that you want to wait for 10 seconds.

dstibbe
  • 1,589
  • 18
  • 33
  • Thank you for the feed back you have understood me 10/10. Well my TumerTask well send some email and store in DB and create some files so ... it may takes more than 10 sec... I don't want the client to wait for all this time to get his response 200 OK . I want it synchronous . but my question is : is it Ok if I use some JSE thread tool in my JEE application , like that way or may be by using the other one -> new Thread(r).start(); Does the server manage threads that I had created implicitly . or should I use some EJB tools ? – AndroidLover Nov 22 '13 at 15:52
  • IF that's the case, just start a seperate thread as described here : http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html . However, do you not want to let the user know, whether or not their request was successful? – dstibbe Nov 22 '13 at 16:01
  • I knew that if was Asynchronous I would use @Suspended AsyncResponse asyncResponse, but I want it Synchronous way. Thanks for the link of javaSE, but are you sure that it is really advised to work with native threads in the JEE framework ? how can I check that there is no problem to work with simple threads in JEE? – AndroidLover Nov 22 '13 at 18:06