Results 1 to 5 of 5
  1. #1
    sagarece2005 is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Issue with Timer and TimerTask

    Dear all,

    Please help me. I am developing one web application . I need to insert one message daily at morning 9 and evening 7 in database. So for that i used Timer and TimerTask. For that i am using two java classes and two servlets.

    Servlet 1:

    import java.io.IOException;
    import java.util.Timer;
    import java.util.TimerTask;

    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import zig.servlet.Reminder.RemindTask;

    /**
    * Servlet implementation class TestTimer
    */
    @WebServlet("/TestTimer")
    public class TestTimer extends HttpServlet {
    private static final long serialVersionUID = 1L;
    Timer timer;
    /**
    * @see HttpServlet#HttpServlet()
    */
    public TestTimer() {
    super();
    // TODO Auto-generated constructor stub
    }

    public void init(ServletConfig config) throws ServletException {

    Reminder r=new Reminder();
    }

    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    }

    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    }

    }

    Servlet 2:


    import java.io.IOException;
    import java.util.Timer;
    import java.util.TimerTask;

    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import zig.servlet.Reminder.RemindTask;

    /**
    * Servlet implementation class TestTimer
    */
    @WebServlet("/TestTimer1")
    public class TestTimer1 extends HttpServlet {
    private static final long serialVersionUID = 1L;
    Timer timer;
    /**
    * @see HttpServlet#HttpServlet()
    */
    public TestTimer1() {
    super();
    // TODO Auto-generated constructor stub
    }

    public void init(ServletConfig config) throws ServletException {

    Reminder1 r1=new Reminder1();

    }

    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    }

    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    }

    }

    =======================
    Reminder.java
    ======================
    import java.util.Calendar;
    import java.util.Date;
    import java.util.List;
    import java.util.Timer;
    import java.util.TimerTask;

    import org.apache.log4j.Logger;

    import zig.dao.NoDataFoundException;
    import zig.orm.presentation.PSensorLog;
    import zig.svc.SensorLogSvc;
    import zig.svc.WebPageSvc;

    /**
    * Simple demo that uses java.util.Timer to schedule a task
    * to execute once 5 seconds have passed.
    */

    public class Reminder {
    Timer timer;
    private static Logger logger = Logger.getLogger(Reminder.class);
    public Reminder() {
    /*timer = new Timer();
    timer.schedule(new RemindTask(), seconds*1000);*/

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 18);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 45);
    Date time = calendar.getTime();
    System.out.println("TIME"+time);
    timer = new Timer();
    timer.schedule(new RemindTask(), time,24*60*60*1000);
    }

    class RemindTask extends TimerTask {
    public void run() {
    System.out.format("Time's up!%n");
    System.out.println("CALLED MYACTION");

    }
    }

    }

    ==============================
    Reminder1.java
    ===============================

    import java.util.Calendar;
    import java.util.Date;
    import java.util.List;
    import java.util.Timer;
    import java.util.TimerTask;

    import org.apache.log4j.Logger;

    import zig.dao.NoDataFoundException;
    import zig.orm.presentation.PSensorLog;
    import zig.svc.SensorLogSvc;
    import zig.svc.WebPageSvc;

    /**
    * Simple demo that uses java.util.Timer to schedule a task
    * to execute once 5 seconds have passed.
    */

    public class Reminder1 {
    Timer timer1;
    private static Logger logger = Logger.getLogger(Reminder1.class);
    public Reminder1() {
    /*timer = new Timer();
    timer.schedule(new RemindTask(), seconds*1000);*/

    Calendar calendar1 = Calendar.getInstance();
    calendar1.set(Calendar.HOUR_OF_DAY, 8);
    calendar1.set(Calendar.MINUTE, 59);
    calendar1.set(Calendar.SECOND, 45);
    Date time1 = calendar1.getTime();
    System.out.println("TIME1"+time1);
    timer1 = new Timer();
    //timer1.sch
    timer1.schedule(new RemindTask1(), time1,24*60*60*1000);
    }

    class RemindTask1 extends TimerTask {
    public void run() {


    System.out.println("CALLED MYACTION");

    }
    }
    }


    =======================
    web.xml
    ==========================

    <servlet>
    <display-name>TestTimer</display-name>
    <servlet-name>TestTimer</servlet-name>
    <servlet-class>zig.servlet.TestTimer</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>TestTimer</servlet-name>
    <url-pattern>/TestTimer</url-pattern>
    </servlet-mapping>

    <servlet>
    <display-name>TestTimer1</display-name>
    <servlet-name>TestTimer1</servlet-name>
    <servlet-class>zig.servlet.TestTimer1</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>TestTimer1</servlet-name>
    <url-pattern>/TestTimer1</url-pattern>
    </servlet-mapping>


    But only one timer i.e 18:59:45 is working properly. But second timer is not working . Please help me . I strucked here for last one month. Thanks in advance. Any help is highly appreciated


    Thanks and Regards
    kalasagar

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Issue with Timer and TimerTask

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    sagarece2005 is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Re: Issue with Timer and TimerTask

    I am using Mysql Database

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Issue with Timer and TimerTask

    Quote Originally Posted by sagarece2005 View Post
    I am using Mysql Database
    What does that have to do with the links I posted? Did you go through them?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    sagarece2005 is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Re: Issue with Timer and TimerTask

    Hi , Sorry i missed that while posting. I will follow the same while posting next time . Surely . Thanks .

Similar Threads

  1. Java timer issue not running correctly
    By alexandra12 in forum New To Java
    Replies: 7
    Last Post: 08-16-2012, 10:47 PM
  2. Timer Class Issue
    By CuppaCoffee in forum New To Java
    Replies: 3
    Last Post: 01-05-2012, 10:31 PM
  3. Java Game Timer Issue! Help
    By smithywill in forum Advanced Java
    Replies: 2
    Last Post: 03-11-2010, 09:09 AM
  4. Timer and TimerTask
    By AndrewM16921 in forum New To Java
    Replies: 2
    Last Post: 04-07-2009, 11:40 PM
  5. [SOLVED] Swing Timer issue
    By Doctor Cactus in forum New To Java
    Replies: 6
    Last Post: 03-03-2009, 12:25 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •