- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的任务是创建一个应用程序,该应用程序将每 (n) 分钟向选定的收件人发送一封电子邮件。它所在的应用程序的结构方式是通过回调 <classname>.main(args)
来重置自身。每当需要的时候。我的问题是,当我调用<classname>.emailSending
时,应用程序立即向每个用户发送 2 封电子邮件。应用程序确实需要在运行时发送电子邮件,但只需要向每个收件人发送一封电子邮件。
有人有什么建议吗?
package database_administration;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Date;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
class EmailSending extends TimerTask
{
public static FileInputStream propFile;
static Connection conn = null;
static Statement query = null;
static String path;
static Statement stmnt;
public void run()
{
try
{
Date date = new Date();
SimpleDateFormat mailDate = new SimpleDateFormat();
mailDate = new SimpleDateFormat("dd-MM-yy HH:mm:ss");
String mail = mailDate.format(date);
propFile = new FileInputStream("config.ini");
Properties config = new Properties(System.getProperties());
config.load(propFile);
String host = config.getProperty("host");
String port = config.getProperty("port");
path = config.getProperty("path");
String DB_URL = config.getProperty("DB_URL");
String USER = config.getProperty("USER");
String PASS = config.getProperty("PASS");
path = config.getProperty("path");
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sender = config.getProperty("sender");
Properties toRecipients = System.getProperties();
Session current = Session.getDefaultInstance(toRecipients);
toRecipients.setProperty("mail.smtp.host", host);
toRecipients.setProperty("mail.smtp.port", port);
MimeMessage message = new MimeMessage(current);
message.setFrom(new InternetAddress(sender));
String[] recipients = config.getProperty("EmailList").split(";");
for(int i=0;i<recipients.length;i++)
{
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients[i].trim()));
message.setSubject("Results of Audit Trail "+mail);
message.setText(messageBody().toString());
Transport.send(message);
}
}
catch (MessagingException me)
{
System.out.println(me.getMessage());
}
catch (FileNotFoundException fnf)
{
System.out.println(fnf.getMessage());
}
catch (IOException ioe)
{
System.out.println(ioe.getMessage());
}
catch (SQLException sqle)
{
System.out.println(sqle.getMessage());
}
catch (ClassNotFoundException cnf)
{
System.out.println(cnf.getMessage());
}
}
public static void emailSend(int control) throws IOException
{
Timer timer = new Timer();
timer.schedule(new EmailSending(), 0, control*60000);
}
private static StringBuilder messageBody() throws SQLException
{
stmnt = conn.createStatement();
String SQL = "Select Action from Java_Test_AuditTrail";
ResultSet rs1 = stmnt.executeQuery(SQL);
rs1.last();
int rowNumb = rs1.getRow();
int list = 0;
int delete = 0;
int update = 0;
int load = 0;
int upload = 0;
int display = 0;
int add = 0;
rs1.beforeFirst();
rs1.next();
int seeker=1;
while(rs1.next()&&seeker<=rowNumb)
{
String actExecuted = rs1.getString("Action");
if(actExecuted.equals("LIST"))
{
list++;
}
if(actExecuted.equals("DELETE"))
{
delete++;
}
if(actExecuted.equals("UPDATE"))
{
update++;
}
if(actExecuted.equals("RE-LOAD"))
{
load++;
}
if(actExecuted.equals("UPLOAD"))
{
upload++;
}
if(actExecuted.equals("DISPLAY AUDIT"))
{
display++;
}
if(actExecuted.equals("USER_CREATED"))
{
add++;
}
}
StringBuilder builder = new StringBuilder();
builder.append("Since Creation of the database, there have been: ["+list+"] List requests executed"+"\n");
builder.append("\n");
builder.append("Since Creation of the database, there have been: ["+delete+"] Delete requests executed"+"\n");
builder.append("\n");
builder.append("Since Creation of the database, there have been: ["+update+"] Update requests executed"+"\n");
builder.append("\n");
builder.append("Since Creation of the database, there have been: ["+load+"] Re-load requests executed"+"\n");
builder.append("\n");
builder.append("Since Creation of the database, there have been: ["+upload+"] Upload requests executed"+"\n");
builder.append("\n");
builder.append("Since Creation of the database, there have been: ["+display+"] Audit-Display requests executed"+"\n");
builder.append("\n");
builder.append("Since Creation of the database, there have been: ["+add+"] User-Creation requests executed"+"\n");
return builder;
}
}
最佳答案
您应该查看Executors.newSingleThreadScheduledExecutor
( http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html#newSingleThreadScheduledExecutor() )。这旨在完全满足您的需求。
ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
scheduledExecutor.schedule(new Runnable() {
@Override
public void run() {
/*
send email
*/
}
}, n, TimeUnit.MINUTES);
可运行程序将每 n 分钟执行一次。如果您想停止系统,只需向 scheduledExecutor
发送 shutdown
命令即可。
关于java - 安排 TimerTask 运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28967202/
我正在用 Java 编写应用程序,但遇到以下问题:一段时间后,显示了一个 inputDialog(JOptionPane)。因为间隔很短,即 10 秒,如果我延迟与第一个对话的交互,那么第二个将在我之
基本上我想做的是制作一个计时器,它在 x 秒后运行一个特定的 TimerTask,但是 TimerTask 可以重新安排计时器在 y 秒后执行任务。下面是示例,它在我尝试在 TimerTask 运行中
在下面的代码中,注释掉的部分会导致异常,该异常向我们解释了 timerTask2 已经被调度或取消,但是没有什么可以阻止 if 被执行器在下面的代码中重用和运行注释掉了部分。怎么解释呢? Timer
我编写了一个连接到服务器并向他发送 ping 命令的应用程序,服务器使用 pong 命令进行应答。 我想实现连接超时机制。我认为它将如下: 客户端发送 ping 并使用 timertask 和 del
我遇到以下问题。每隔 2 秒,程序就会进入 if 语句。在这个 if 语句中,我想要一个计时器,它会在 15 秒后给我一条消息。计时器应延迟 1 秒运行。但是当我用计时器“等待”时,if 语句将再执行
我正在开发一个 ODL 应用程序,并从我的主函数中安排了一个任务 ( MonitorLinksTask ),以便监视拓扑的所有链接中的延迟。 我想为每个链接发送一个数据包,并在 Controller
尝试使用 ScheduledExecutorService 来执行 TimerTask。安排一个延迟 10 秒的任务,并调用 task.cancel。但任务仍在运行,不确定会发生什么,并且取消方法似乎
无论如何,我正在尝试制作类似聊天程序的东西,有人告诉我使用此代码来检查新消息,同时允许用户提交消息: timer.schedule(new TimerTask() { @Override pu
看看这段代码: public Reminder() { a[0]=1000; a[1]=3000; a[2]=1000; a[3]=5000; timer = n
我有以下设计: 有一个任务扩展了TimerTask,并且计划每分钟运行一次。此任务将尝试从中央队列(作为单个消费者)获取项目并将其表示形式写入文件中。 此外,还有多个生产者不时将项目放入中央队列。 我
我在打印特定数组的每个值(在一定时间内)时遇到问题。例如,我有一个包含值的数组:“Value1”、“Value2”、“Value3”。我想输出“Value1”,5秒后“Value2”,5秒后“Valu
我对这个标题感到抱歉,但我不确定还有什么方法可以描述这个问题。我有一个 SingletonBean,其中有一个扩展 TimerTask 的类。要点是,当应用程序启动时,它开始循环运行一组计划的后台任务
我有一个程序,它启动一个计时器,然后在停止之前每秒重复一次代码。发生的情况是它不会停止来自 main 的执行。我假设它只是一个与之一起运行的线程,但通过查看帖子,我仍然不确定如何暂时停止它。 fina
我正在尝试使用每秒更新标签的时间(因此它显示倒计时)但它只似乎“滴答作响”一次,我无法弄清楚我做错了什么! public class Puzzle extends UiApplication {
我设置了 TimerTask UpdateTask,但它只在我启动程序时触发一次。为什么不继续触发? 这里的一些方法在其他类中,如果您需要它们,请随时告诉我。 import java.awt.Grap
我又碰壁了。让我的键输入工作后,我已经绞尽脑汁几个小时,我想创建一个暂停功能,这样如果再次按下同一个键,timertask 将停止运行(即游戏暂停) JPanel component = (JPane
我有一个 android 应用程序,我必须在 20-30 秒的非常短的时间间隔内执行一些任务。这是一项要求,它在某种程度上是一个“监控”应用程序,不会用于商业目的,因此无需担心电池和数据使用。 我正在
这是我的计时器类,该类旨在不断更新 View 中的计时器。但是,当我运行该应用程序时,第一条 toast 消息显示在屏幕上,但从未到达第二条消息(永远不会执行 timerTask 的“运行”方法)。我
我在我的android代码中添加了一个TimerTask: private class MyTimerTask extends TimerTask { @Override public
我有一个名为 timer 的任务: timer.schedule(new task1(), 1000*minutes); 任务: class task1 extends TimerTask {
我是一名优秀的程序员,十分优秀!