Joke Collection Website - Blessing messages - How to realize the automatic sending function of short messages with Java

How to realize the automatic sending function of short messages with Java

Method 1:

Configure the listener in1.web.xml.

& lt Audience & gt

& lt listener class & gt

cn . com . JX life . SHS . web . action . cs inquiry . submission . my listener

& lt/listener-class & gt;

& lt/listener & gt;

Create a listener

Import java.util.timer;

Import javax.servlet.servletcontextevent;

Import javax.servlet.servletcontextlistener;

The public class MyListener implements ServletContextListener {

Private timer timer = null

@ Overlay

public void context initialized(ServletContextEvent SCE){

Timer = new timer (true);

//Set the task plan, start and interval time.

timer.schedule(new MyTask(),0,3 * 60 * 1000);

//3 minutes

//timer.schedule(new MyTask(),0,3 * 60 * 1000);

///Perform this task after kloc-0/second, with an interval of 2 seconds each time.

//timer.schedule(new MyTask(), 1000,2000);

}

@ Overlay

public void context destroyed(ServletContextEvent SCE){

timer . cancel();

}

}

3. Create the started class MyTask.

Import java.util.date;

Import java.util.timertask;

The public class MyTask extends TimerTask {

@ Overlay

Public invalid operation () {

system . out . println(" call at "+(new Date()));

}

}

Method 2:

Scheduling framework of routine work under Quartz and spring.

1。 From macho me . quartz;

Public class CustomJob 1 {

public void onTimeWork() {

System.out.println ("data backup task starts");

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

System.out.println ("data backup task completed");

}

}

4。 The rest of the work is done in the spring xml file.

& lt! -planned bean->;

& ltbean id = " job 1 " class = " com . macho me . quartz . custom job 1 " >& lt/bean & gt;

& lt! -define the task object->

& ltbean id="jobtask 1 "

class = " org . spring framework . scheduling . quartz . methodinvokingjobdetailfactorybean " & gt;

& lt! -planned bean->;

& ltproperty name="targetObject " >

& ltref bean="job 1" />

& lt/property & gt;

& lt! -scheduling method in scheduling bean->;

& ltproperty name="targetMethod " >

& lt value & gtonTimeWork & lt/value & gt;;

& lt/property & gt;

& lt/bean & gt;

& lt! -Define the trigger object->;

& ltbean id="trigger 1 "

class = " org . spring framework . scheduling . quartz . crontriggerbean " & gt;

& lt! -Task object->

& ltproperty name="jobDetail " >

& ltref bean="jobtask 1" />

& lt/property & gt;

& lt! -cron expression->

& ltproperty name="cronExpression " >

& lt value & gt10,15,20,25,30,35,40,45,50,55 * * *? & lt/value & gt;

& lt/property & gt;

& lt/bean & gt;

& lt! -Plan target->

& lt! -If lazy-init='false', the scheduler will be executed when the container is started->

& ltbean id = " scheduler " lazy-init = " false "

class = " org . spring framework . scheduling . quartz . schedulerfactorybean " & gt;

& lt! -trigger object->

& ltproperty name = " triggers " & gt

& lt list & gt

& ltref bean="trigger 1" />

& lt/list & gt;

& lt/property & gt;

& lt/bean & gt;

5。 Deploy the spring project to TOMCAT or JBOSS server.

Deployment project

Restart TOMCAT server

6。 Look at the results:

Cron expression reference: Quartz's Cron expression