我正在尝试使用cron job每天执行一次工作。我正在使用自动缩放。使用cron job我每天都会发送一次电子邮件。我有两个条件:
每当我每天设置一次时间,例如every day 18:00
,然后cron作业成功运行,但它只执行3,4行CronJob实现类。
在记录器中,我只得到4行执行如下: -
每当我将时间设置为every 2 minutes
或every 5 minutes
,然后cron作业成功运行并且cron作业实现类成功执行意味着它成功发送电子邮件。
为什么第一个条件不发送电子邮件?
当时可能有应用程序闲置,这就是为什么它没有执行?
有任何帮助吗? Cron.xml: -
<?xml version="1.0" encoding="UTF-8"?>
<cron>
<url>/slick_erp/cronCustomerService</url>
<description>Implemented for due services of customer.</description>
<schedule>every day 11:30</schedule>
</cron>
</cronentries>
Web.xml中: -
<servlet>
<servlet-name>CustomerServiceCronJobImpl</servlet-name>
<servlet-class>com.slicktechnologies.server.cronjobimpl.CustomerServiceCronJobImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CustomerServiceCronJobImpl</servlet-name>
<url-pattern>/slick_erp/cronCustomerService</url-pattern>
</servlet-mapping>
我的cronjob实现类: -
public class CustomerServiceCronJobImpl extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -6268357776825855510L;
private SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");
private SimpleDateFormat fmt1 = new SimpleDateFormat("dd/MM/yyyy");
Logger logger = Logger.getLogger("NameOfYourLogger");
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
servicecontactlist();
}
private void servicecontactlist() {
fmt.setTimeZone(TimeZone.getTimeZone("IST"));
fmt1.setTimeZone(TimeZone.getTimeZone("IST"));
Email cronEmail = new Email();
Date today=DateUtility.getDateWithTimeZone("IST", new Date());
/**
* Adding 1 day extra to date
*/
logger.log(Level.SEVERE,"Date Before Adding One Day"+today);
DateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy");
Calendar cal=Calendar.getInstance();
cal.setTime(today);
cal.add(Calendar.DATE, 0);
Date dateForFilter=null;
try {
dateForFilter=dateFormat.parse(dateFormat.format(cal.getTime()));
cal.set(Calendar.HOUR_OF_DAY,23);
cal.set(Calendar.MINUTE,59);
cal.set(Calendar.SECOND,59);
cal.set(Calendar.MILLISECOND,999);
dateForFilter=cal.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
logger.log(Level.SEVERE,"Date After Adding One Date"+dateForFilter);
/*************************************End*********************************/
try{
logger.log(Level.SEVERE,"In service ContactList");
logger.log(Level.SEVERE,"Date After Adding One Date=="+dateForFilter);
logger.log(Level.SEVERE," 1 ");
/********************************Adding status in the list ***********************/
logger.log(Level.SEVERE," 2 ");
ArrayList<String> obj = new ArrayList<String>();
obj.add("Scheduled");
obj.add("Pending");
obj.add("Rescheduled");
logger.log(Level.SEVERE," 3 ");
/******************************Converting todayDate to String format*****************/
logger.log(Level.SEVERE," 4 ");
Date todaysDate = new Date();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
logger.log(Level.SEVERE," 5 ");
String todayDateString = df.format(todaysDate);
logger.log(Level.SEVERE," 6 ");
System.out.println("String in dd/MM/yyyy format is: " + todayDateString);
logger.log(Level.SEVERE," 7 ");
/********************************Adding Companies in the list ***********************/
logger.log(Level.SEVERE," 8 ");
List<Company> compEntity = ofy().load().type(Company.class).list();
logger.log(Level.SEVERE," 9 ");
if(compEntity.size()>0){
logger.log(Level.SEVERE,"If compEntity size > 0");
logger.log(Level.SEVERE,"Size of compEntity"+compEntity.size());
for(int i=0;i<compEntity.size();i++){
Company c=compEntity.get(i);
logger.log(Level.SEVERE,"In the for loop");
logger.log(Level.SEVERE,"Company Name="+c);
logger.log(Level.SEVERE,"The value of i is:" +i);
logger.log(Level.SEVERE,"Date After Adding One Date"+dateForFilter);
/********************************Checking prosname & prosconfig for each company ***********************/
ProcessName prosName = ofy().load().type(ProcessName.class).filter("processName","CronJob").filter("status",true).filter("companyId", compEntity.get(i).getCompanyId()).first().now();
ProcessConfiguration prosconfig = ofy().load().type(ProcessConfiguration.class).filter("processList.processName", "CronJob").filter("processList.processType", "ServiceDailyMail").filter("processList.status",true).filter("companyId", compEntity.get(i).getCompanyId()).first().now();
logger.log(Level.SEVERE,"after 3 filters for ProcessConfigurations");
System.out.println("after 3 filters for ProcessConfigurations");
if(prosName!=null){
logger.log(Level.SEVERE,"In the prossName");
if(prosconfig!=null){
logger.log(Level.SEVERE,"In the ProsConfifg !=null ");
/********************************Reading services from Service entity ***********************/
List<Service> serEntity = ofy().load().type(Service.class).filter("companyId",compEntity.get(i).getCompanyId()).filter("serviceDate <=",dateForFilter).filter("status IN",obj).list();
logger.log(Level.SEVERE,"Today date Is:"+dateForFilter);
logger.log(Level.SEVERE,"service entity size:"+serEntity.size());
// email id is added to emailList
ArrayList<String> toEmailList=new ArrayList<String>();
toEmailList.add(compEntity.get(i).getPocEmail());
String mailTitl = "Services Due As On Date";
if(serEntity.size()>0){
ArrayList<String> tbl_header = new ArrayList<String>();
tbl_header.add("Branch");
tbl_header.add("Customer Id");
tbl_header.add("Customer Name");
tbl_header.add("Customer Contact No");
tbl_header.add("Crontact Id");
tbl_header.add("Service Id");
tbl_header.add("Service Date");
tbl_header.add("Service Engineer");
tbl_header.add("Product Name");
tbl_header.add("Status");
tbl_header.add("Ageing");
/********************************Sorting table with Branch & Service Date ***********************/
Comparator<Service> serviceDateComparator2 = new Comparator<Service>() {
public int compare(Service s1, Service s2) {
Date date1 = s1.getServiceDate();
Date date2 = s2.getServiceDate();
//ascending order
return date1.compareTo(date2);
}
};
Collections.sort(serEntity, serviceDateComparator2);
Comparator<Service> serviceDateComparator = new Comparator<Service>() {
public int compare(Service s1, Service s2) {
String branch1 = s1.getBranch();
String branch2 = s2.getBranch();
//ascending order
return branch1.compareTo(branch2);
}
};
Collections.sort(serEntity, serviceDateComparator);
/********************************Getting serviceEntity data and adding in the tbl1 List ***********************/
ArrayList<String> tbl1=new ArrayList<String>();
for(int j=0;j<serEntity.size();j++){
tbl1.add(serEntity.get(j).getBranch()+"");
tbl1.add(serEntity.get(j).getPersonInfo().getCount()+"");
tbl1.add(serEntity.get(j).getPersonInfo().getFullName()+"");
tbl1.add(serEntity.get(j).getPersonInfo().getCellNumber()+"");
tbl1.add(serEntity.get(j).getContractCount()+"");
tbl1.add(serEntity.get(j).getCount()+"");
tbl1.add(fmt.format(serEntity.get(j).getServiceDate()) +"");
/**************** for getting ageing for each service******/
String StringServiceDate = fmt1.format(serEntity.get(j).getServiceDate());
// String StringServiceDate = df.format(serviceDate);
Date d1 = null;
Date d2 = null;
d1 = df.parse(StringServiceDate);
d2 = df.parse(todayDateString);
long diff = d2.getTime() - d1.getTime();
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.println("Ageing:"+diffDays);
logger.log(Level.SEVERE,"Ageing:"+diffDays);
tbl1.add(serEntity.get(j).getEmployee()+"");
tbl1.add(serEntity.get(j).getProduct().getProductName()+"");
tbl1.add(serEntity.get(j).getStatus()+"");
tbl1.add(diffDays +"");
}
logger.log(Level.SEVERE,"In the ProsConfig !=null 3 ");
/********************************Calling cronsendEmail Method ***********************/
// Email e=new Email();
try {
System.out.println("Before send mail method");
logger.log(Level.SEVERE,"Before send method call to send mail ");
cronEmail.cronSendEmail(toEmailList, "Services Due As On Date"+" "+ todayDateString, mailTitl +" "+ todayDateString, c, tbl_header, tbl1, null, null, null, null);
logger.log(Level.SEVERE,"After send mail method ");
//+" "+
} catch (IOException e1) {
logger.log(Level.SEVERE,"In the catch block ");
e1.printStackTrace();
}
}
else{ // else block for if(serviceEntity.size()>0){
System.out.println("Sorry no services found for this company");
logger.log(Level.SEVERE,"Sorry no services found for this company");
//
mailTitl = "No Services Found Due";
cronEmail.cronSendEmail(toEmailList, "Services Due As On Date"+" "+ todayDateString, mailTitl , c, null, null, null, null, null, null);
}
}
else{ //else block for prosconfig
logger.log(Level.SEVERE,"ProcessConfiguration is null");
System.out.println("ProcessConfiguration is null");
}
}else{ //else block for pross!=null
logger.log(Level.SEVERE,"Cron job status is Inactive");
System.out.println("Cron job status is Inactive");
}
} //end of for loop
}
else{ //else block for if(compEntity.size()>0)
logger.log(Level.SEVERE,"No Company found from cron job completed");
System.out.println("No Company found from cron job completed");
}
}catch(Exception e2){
e2.printStackTrace();
}
}
}