我正在尝试使用Quartz
库安排作业。以下是属于计划作业的类的方法。
private void Reminder1()
{
String[] firstReminderTime = getFirstReminderTime().split(":");
Integer firstReminderHour = Integer.parseInt(firstReminderTime[0]);
Integer firstReminderMinute = Integer.parseInt(firstReminderTime[1]);
if(firstReminderHour==null || firstReminderMinute==null)
{
return;
}
JobDetail job = newJob(PJob.class).withIdentity("p1").build();
Trigger trigger = newTrigger()
.withIdentity(triggerKey("pTrigger1", "pTriggerGroup1"))
.withSchedule(dailyAtHourAndMinute(firstReminderHour, firstReminderMinute))
.startAt(new Date()).build();
try {
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException ex) {
ex.printStackTrace();
}
}
以下是工作类。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package RemindeWorker.PJob;
import RemindeWorker.Listener.ReminderCommon;
import java.util.Properties;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
/**
*
* @author Yohan
*/
public class PJob implements Job
{
private String host;
private String userName;
private String password;
private String firstReminderTime;
private String secondReminderTime;
public PJob(String host, String userName, String password, String firstReminderTime, String secondReminderTime)
{
setHost(host);
setUserName(userName);
setPassword(password);
setFirstReminderTime(firstReminderTime);
setSecondReminderTime(secondReminderTime);
}
@Override
public void execute(JobExecutionContext jec) throws JobExecutionException
{
String host=getHost();
final String user=getUserName();//change accordingly
final String password=getPassword();//change accordingly
String to="xxx@gmail.com";//change accordingly
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});
//Compose the message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Subject");
message.setText("This is simple program of sending email using JavaMail API");
//send the message
Transport.send(message);
System.out.println("message sent successfully...");
} catch (MessagingException e) {e.printStackTrace();}
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstReminderTime() {
return firstReminderTime;
}
public void setFirstReminderTime(String firstReminderTime) {
this.firstReminderTime = firstReminderTime;
}
public String getSecondReminderTime() {
return secondReminderTime;
}
public void setSecondReminderTime(String secondReminderTime) {
this.secondReminderTime = secondReminderTime;
}
}
但是我有一个问题。请注意以下
JobDetail job = newJob(PJob.class).withIdentity("p1").build();
这里我们称之为" class literal"。但是,我需要执行Job类的构造函数,因为它接受参数。将参数传递给此类是必需的。
所以我的问题是,因为在传递class literal
时,无法传递参数,我如何将它们传递给作业类?
答案 0 :(得分:1)
创建fake job
:
real job
并将此fake job
安排到石英。