Solved

Start & stop 'job' from jsp ? How ?

Posted on 2003-10-29
10
307 Views
Last Modified: 2010-05-19
Hello,


I want to create a Java (jsp/servlet) which handles the following.

 -> Start a job
 -> stop a job

Between start and stop, the user should not be noticing anything about the job, so the 'job' has to be running on the server and not in the browser.

With a 'job' i mean a servlet which for instance creates or updates an xml-file every 5 minutes.

I'm not sure how i can do both ... Anyone who can get me on my way ?



0
Comment
Question by:Xyleen
  • 5
  • 4
10 Comments
 
LVL 1

Expert Comment

by:deepak_a
ID: 9641137
r u planing to take any browser values ?? u cannot do that just in case u r trying to.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9641155
By 'job', I think you mean Thread or Task.

This doesn't have to be a servlet.  An ordinary Java class can manage this.

Use a java.util.Timer and java.util.TimerTask?

Here is a very simple example of how to combine a Timer and TimerTask.  All it does is display some text every 5 seconds (5000mS).  See the javadocs for java.util.Timer for more details on the different ways this can work.

MyTaskRunner.java
--------------------------

import java.util.*;

public class MyTaskRunner
{
    private void init()
    {
        MyTask myTask = new MyTask();
        Timer myTimer = new Timer();
        myTimer.scheduleAtFixedRate(myTask, 0, 5000);
    }
   
    public static void main(String[] args)
    {
        new MyTaskRunner().init();
    }
}

MyTask.java
----------------
import java.util.*;

public class MyTask extends TimerTask
{
    public void run()
    {
        System.out.println("MyTask called");
    }
}
0
 

Author Comment

by:Xyleen
ID: 9641224
jimmack,


Thanks but how can i start/stop this from within a JSP page ?
0
 

Author Comment

by:Xyleen
ID: 9641353
deepak_a,


Actually i want to do this

the user starts by clicking on a link like
1.  startFeed.jsp?ExternalParty=15&XMLtype=2
    -> the jsp file starts the thread/task on the server

2. user enters data in a form every x seconds/minutes, in the background the thread creates/updates/uploads an xml to an external party's server.

when the user wants to stop
3. stopFeed.jsp?ExternalParty=15&XMLtype=2
    -> the jsp file stops the thread/task on the server

I don't want to create the xml every time the user enters data because this is something that can happen multiple times per second (automatic feed) or per minute (manual feed). If i would start an ftp session every time the receiving server will certainly timeout or crash.
 
 
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9641389
I assume that your data (entered via the form) is stored locally until you are ready to send it and then you send a block of data every 5 minutes.

In this case, you could start the Timer within the session, when the user selects your link.

<%
MyTimer myTimer = (MyTimer)session.getAttribute("timer");
if (myTimer == null)
{
    myTimer = new MyTimer();
    myTimer.schedule...
    session.setAttribute("timer", myTimer);
}
%>

and stop it from the other page

<%
MyTimer myTimer = (MyTimer)session.getAttribute("timer");
if (myTimer != null)
{
    myTimer.cancel();
    session.removeAttribute("timer");
}
%>

The TimerTask that you create just needs access to the data that you are storing locally.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Xyleen
ID: 9641438
jimmack,


Thanks
-> this is a solution for one user using the thread but  what if more user want to stop/start the thread ?
-> in this setup, the timer stays 'ticking' while my session is active, how can i stop it when my session is inactive ?

Let's say :
 -> I start the thread
 -> my pc crashes (session is killed)
 -> a collegue wants to stop the thread .... ???

The data is by the way stored in an oracle database but that's not really important in this context.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9641484
:-)

That's one of the things that I was wondering about ;-)

You can use a session listener to stop the timer when the session is ended.

The other thing I was concerned about is how many simultaneous users (and therefore timers) do you expect.  If you have many users, your server could get seriously overloaded with timers all trying to send data at the same/similar time.

You could change the scope of the timer to the application level (instead of session).  I'm not exactly sure how to do this.  I haven't done it myself.  This is based on the idea that the scope of attributes can be at one of three levels:  Page, Session, Application.

Perhaps you could have your main servlet simply start one timer (at startup) that ticks every 5 minutes throughout the life of the application.  The TimerTask would simply need to send any outstanding data and exit (or just exit immediately if there is no data).
0
 

Author Comment

by:Xyleen
ID: 9641543
jimmack,

thanks for you contribution, i hope anyone else also has some ideas but i'll start looking for a solution on appliction level
0
 
LVL 15

Accepted Solution

by:
jimmack earned 75 total points
ID: 9641631
Hi Xyleen,

It's so easy, I missed it :-)

In my earlier example where I used session.getAttribute() and session.setAttribute() etc. Just replace "session" with "application".

"application" is a reference to the ServletContext ;-)
0
 

Author Comment

by:Xyleen
ID: 9641661
oh, ok

i'll try that
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Run the project 5 87
Fisheye tool 2 104
some methods private some are not 8 198
maven project jars 7 50
In this blog, I will share you some basic tips for content marketing and to rank your website on Google.
With the rapid rise in mobile usage, mobile devices are here to stay and have become an integral part of doing business. Here are 9 great apps for your BYOD environment.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now