Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-24-2012, 03:51 PM   PM User | #1
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
[SOLVED] function not executing with scheduleAtFixedRate

Hello, I'm trying execute a function repeatedly using scheduleAtFixedRate, however it's not working. my code;

PHP Code:
    public static void KeepAlive(){
        
          
TimerTask tasknew = new TimerAction();
          
Timer t = new Timer(true);
          
          
// scheduling the task at fixed rate
          
t.scheduleAtFixedRate(tasknew0100); 
          
          
System.out.println("execute KeepAlive");
    }
    
    static class 
TimerAction extends TimerTask {
        public static 
void main(String[] argsthrows SQLException{
            
System.out.println("execute TimerAction");

            
Connect conn Connect.getInstance();
            
Statement st conn.createStatement();
            
st.executeQuery("SELECT 1");
        }

        @
Override
        
public void run() {
            
// TODO Auto-generated method stub
            
        
}
    } 
"execute KeepAlive" comes up when I execute the KeepAlive() function, but not "execute TimerAction".

any ideas what i'm doing wrong here?
angst is offline   Reply With Quote
Old 10-24-2012, 04:38 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Execute timer action will never come up. That's in the main method of a nested class which is not run; you need to put it into the run method.
Fou-Lu is offline   Reply With Quote
Old 10-24-2012, 04:44 PM   PM User | #3
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
Thanks! yes thats what I figured out with a bit more testing.
angst is offline   Reply With Quote
Old 10-24-2012, 04:45 PM   PM User | #4
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
PHP Code:
    static class TimerAction extends TimerTask {
        public static 
void KeepAlive() throws SQLException{
            
System.out.println("execute TimerAction");

            
Connect conn Connect.getInstance();
            
Statement st conn.createStatement();
            
st.executeQuery("SELECT 1");
        }

        @
Override
        
public void run() {
            
// TODO Auto-generated method stub
            
try {
                
KeepAlive();
            } catch (
SQLException e) {
                
// TODO Auto-generated catch block
                
e.printStackTrace();
            }
        }
    } 
angst is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:30 PM.


Advertisement
Log in to turn off these ads.