Server Side Function

Hi all,

i want to create a function that execute every 4 hours on server side. i did one but i think it is working only when client enter the my application, and naturally it is working according to client number. For example, one client entered the application at 12:00 then loop function begins working and other client entered the application at 12:10, the function begins again.

This function should work on server side and also work according to server time.

is there any solution for this?

The jobs need to be completely independent of your users, as I understand. If such is the case, you could look at using a library like Quartz (see also
this list
). You will need to add your startup code to a ServletContextListener so it starts when your application server is started.

thanks for response Jean,

right, it is completely independent of users. i will look at your suggestions, and also share the results. Thanks again…

Why not just launch a thread to run your background tasks? It can wake up periodically (every minute or 15 minutes or whatever) and check if the current time is good or not, or you can set the timeout to match the amount of time (4 hours) to pause until it works again.

yes, i am already doing that loop fuction with thread. But the problem is that thread works on client side. i realize that the thread began when user entered the application, also it begins again when the other user enters the app.

i hope i could explain it :smiley:

Not sure what you mean that the thread runs in the client. The only client code in Vaadin is with a specific widget that’s compiled into javascript via GWT.

Just use a simple context listener and start the thread there and you can stop it there, too. It’s only called when your app starts and stops and is not tied to a client.

public class ContextListener
    implements ServletContextListener 
	public void contextInitialized(ServletContextEvent sce)

	public void contextDestroyed(ServletContextEvent sce)

firstly thank you David, i do not know anything about server cide code or whatever :slight_smile:

i am using this code. i mean, this thread which is in automaticGetResult function. Besides automaticGetResult is called in init, i guess this makes a client problem. automaticGetResult function will be executed for every entered user, right?

i want a function that is executed once - in my example it is automaticGetResult- by server, independent from clients.

with this way, David, if your ContextListener class will be answer for my problem, can you help me how and where i have to use this class?

public void init() {



	public void automaticGetResult(){
		Thread t = new Thread() {
		    public void run() {
		        while(true) {
		            try {
		            	        Date date = new Date();   
					Calendar calendar = Calendar.getInstance(); 
		    			System.out.println(" Print Time : "+ calendar.get(Calendar.MINUTE) + ":"+ calendar.get(Calendar.SECOND) );

		            } catch (InterruptedException ie) {

Well, you really should get up to speed on Java servlets. Vaadin relies on it. Some depends on which version you are using.

With Servlets 3.0 supported by Tomcat 7, just creating that ServletContextListener class in your code will cause Tomcat (or any Servlet 3 container) to call your contextInitialized method on startup, which can just call automaticGetResult();

The annotation makes the association that this class is a context listener to be called by the container as it starts/stops. If you are on an older Tomcat/Servlet, you’ll need to add web.xml entry that defines the context listener.

Of course your thread leaves much to be desired because it doesn’t appear to have a way to stop it. Typically, you’ll want to create a flag like ‘boolean continueThread’ set to true and use that instead of ‘while (true)’ you are then ‘while( continueThread )’ and then provide a method to set continueThread to false so the next time it wakes up it will stop itself.

Then in the contextDestroyed() method of the context listener, you’d tell the thread to stop.

Consider a pattern like this, perhaps instead:

To start your background thread (called directly or indirectly from the contextInitialized() method):

        try  {
            backgrounder = new Backgrounder(); 

            Thread thread = new Thread( backgrounder );
        } catch( Exception e ) {
         // report it

Then stop it in the contextDestroyed() method:

        if ( backgrounder != null )  {
            synchronized(backgrounder)  {
                backgrounder = null;

And your backgroud thread something like:

public class Backgrounder implements java.lang.Runnable {
    private boolean 	shouldContinue  = true;
    public Backgrounder()   {

    public void run()  {
        while ( shouldContinue ) {
            try  {
                synchronized(this)  {
                    wait( waitTimeMsec );
                if ( shouldContinue )
                	doBackgroundTasks(); // call a method or do whatever you want to do when you wake up
            }  catch( java.lang.InterruptedException e )  {
    public void stop()  {
        shouldContinue = false;