gpt4 book ai didi

java - 安排 TimerTask 运行一次

转载 作者:行者123 更新时间:2023-12-01 18:16:58 28 4
gpt4 key购买 nike

我的任务是创建一个应用程序,该应用程序将每 (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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com