spring集成quartz,出现内存益出
最近开发一个文档系统,要实现定时搜索数据库并且发送通知邮件。加入了quartz之后,每分钟定时执行一次,发现系执行了60次左右,系统就会报内存益出。
具体代码如下:
applicationContext.xml配置
<beanid="scheduledJob"class="org.springframework.scheduling.quartz.JobDetailBean">
<propertyname="jobClass"value="javaoa.util.ScheduledJob"/>
</bean>
<!--触发器-->
<beanid="timerTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean">
<propertyname="jobDetail">
<refbean="scheduledJob"/>
</property>
<propertyname="cronExpression"value="0****?"/><!--在每分钟的0秒时执行一次-->
<!--propertyname="cronExpression"value="000**?"/-->
</bean>
<!--触发器调度控制器-->
<beanid="TimerTaskFactory"class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<propertyname="triggers">
<list>
<refbean="timerTrigger"/>
</list>
</property>
</bean>
</beans>
packagejavaoa.util;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.Iterator;
importjava.util.List;
importjavax.mail.*;
importjavax.mail.internet.*;
importjava.util.*;
importjavaoa.entity.DepartmentManager;
importjavaoa.entity.Document;
importjavaoa.service.common.IDocumentService;
importjavaoa.service.common.IDepartmentManagerService;
importorg.quartz.Job;
importorg.quartz.JobExecutionContext;
importorg.quartz.JobExecutionException;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassScheduledJobimplementsJob{
publicvoidexecute(JobExecutionContextcontext)throwsJobExecutionException{
try{
executeTask();
}catch(Exceptione){
e.printStackTrace();
}
}
//计划任务写在这里,这里的代码会在spring中配置的定时器被触发时执行
@SuppressWarnings({"unchecked"})
publicvoidexecuteTask(){
ApplicationContextappCxt=newClassPathXmlApplicationContext(newString[]{"applicationContext.xml","applicationContext-document.xml"});
IDocumentServicedocumentService=(IDocumentService)appCxt.getBean("documentService");
IDepartmentManagerServicedepartmentManagerService=(IDepartmentManagerService)appCxt.getBean("departmentManagerService");
Calendarcalendar=Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR,30);
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
StringtimeStr=sdf.format((calendar.getTime())).toString();
System.out.println("timeStr========="+timeStr);
Stringhql;
hql="selectdistinctdocumentfromDocumentdocumentwheredocument.d_state='Y'anddocument.censor_date<='"+timeStr+"'anddocument.censor_state='N'";
List<Document>documentList=documentService.getSelectedDocument(hql);
try{
Propertiesprops=newProperties();
props.put("mail.smtp.host","www.126.com");//设置SMTP发件服务器地址
props.put("mail.smtp.auth","true");//邮件服务器是否要验证
PopupAuthenticatorauth=newPopupAuthenticator();
Sessionsession=Session.getInstance(props,auth);//获取邮件会话
MimeMessagemessage=newMimeMessage(session);//消息,相当于一张白纸
AddressaddressFrom=newInternetAddress("[email protected]");
AddressaddressCopy=newInternetAddress("[email protected]");//抄送地址
message.setFrom(addressFrom);
Iteratorit=documentList.iterator();
while(it.hasNext()){
Documentdocument=(Document)it.next();
//通过发行部门找出部门经理的邮件地址
Stringpublish_departmentId=document.getPublish_departmentId();
Stringhql1="selectdistinctdmfromDepartmentManagerdmwheredm.de_id='"+publish_departmentId+"'anddm.email!=''";
List<DepartmentManager>departmentManagerList=departmentManagerService.getAllDepartmentManager(hql1);
InternetAddress[]address=newInternetAddress[departmentManagerList.size()];
Iteratorit1=departmentManagerList.iterator();
inti=0;
while(it1.hasNext())
{
DepartmentManagerdepartmentManager=(DepartmentManager)it1.next();
Stringemail=departmentManager.getEmail();
if(email!=null&&!"".equals(email)){
address[i]=newInternetAddress(email);}
i++;
}
document.setCensor_state("Y");
documentService.saveOrUpdateDocument(document);
message.setSubject("文件重审通知");
StringBuffertheMessage=newStringBuffer();
theMessage.append("<tr>您好:</tr></br>");
theMessage.append(document.getPublish_department()+"<tr>有文件需要重审,文件名为:《"+document.getD_name()+"》。请及时按以下链接登陆进行查看。</tr></br>");
theMessage.append("<tr><ahref='http://192.168.1.55:88/JavaOA'target='_blank'>www.126.com</a><t/r>");
theMessage.append("<hr>");
theMessage.append("<tr><strong>注意:</strong></tr></br>");
theMessage.append("<tr>这是一个不受监控的电子邮件地址,请不要答复此邮件;</tr></br>");
message.setContent(theMessage.toString(),"text/html;charset=gb2312");
message.addRecipients(Message.RecipientType.TO,address);
message.addRecipient(Message.RecipientType.CC,addressCopy);
message.saveChanges();
Transporttransport=session.getTransport("smtp");
transport.connect("www.126.com","SystemMail","123");
transport.sendMessage(message,message.getAllRecipients());
session.setDebug(true);
transport.close();
}
}catch(Exceptione)
{
e.printStackTrace();
}
}
}
catalina.bat
setJAVA_OPTS=-Xms256m-Xmx512m-XX:PermSize=128M-XX:MaxPermSize=128m
异常信息:
2008-5-3016:58:44org.apache.catalina.connector.CoyoteAdapterservice
严重:Anexceptionorerroroccurredinthecontainerduringtherequestprocessing
java.lang.OutOfMemoryError:Javaheapspace
atjava.util.LinkedHashMap.createEntry(LinkedHashMap.java:424)
atjava.util.LinkedHashMap.addEntry(LinkedHashMap.java:406)
atjava.util.HashMap.put(HashMap.java:385)
atsun.util.resources.OpenListResourceBundle.loadLookup(OpenListResourceBundle.java:118)
atsun.util.resources.OpenListResourceBundle.loadLookupTablesIfNecessary(OpenListResourceBundle.java:97)
atsun.util.resources.OpenListResourceBundle.handleGetObject(OpenListResourceBundle.java:58)
atsun.util.resources.TimeZoneNamesBundle.handleGetObject(TimeZoneNamesBundle.java:59)
atjava.util.ResourceBundle.getObject(ResourceBundle.java:378)
atjava.util.ResourceBundle.getObject(ResourceBundle.java:381)
atjava.util.ResourceBundle.getStringArray(ResourceBundle.java:361)
atsun.util.TimeZoneNameUtility.retrieveDisplayNames(TimeZoneNameUtility.java:100)
atsun.util.TimeZoneNameUtility.retrieveDisplayNames(TimeZoneNameUtility.java:81)
atjava.util.TimeZone.getDisplayNames(TimeZone.java:399)
atjava.util.TimeZone.getDisplayName(TimeZone.java:350)
atjava.text.SimpleDateFormat.subFormat(SimpleDateFormat.java:1093)
atjava.text.SimpleDateFormat.format(SimpleDateFormat.java:882)
atjava.text.SimpleDateFormat.format(SimpleDateFormat.java:852)
atjava.text.DateFormat.format(DateFormat.java:316)
atorg.apache.tomcat.util.http.FastHttpDateFormat.getCurrentDate(FastHttpDateFormat.java:116)
atorg.apache.coyote.http11.Http11Processor.prepareResponse(Http11Processor.java:1566)
atorg.apache.coyote.http11.Http11Processor.action(Http11Processor.java:934)
atorg.apache.coyote.Response.action(Response.java:183)
atorg.apache.coyote.Response.sendHeaders(Response.java:379)
atorg.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:305)
atorg.apache.catalina.connector.OutputBuffer.close(OutputBuffer.java:273)
atorg.apache.catalina.connector.Response.finishResponse(Response.java:486)
atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:287)
atorg.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
atorg.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
atorg.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
atjava.lang.Thread.run(Thread.java:619)
2008-5-3016:58:44org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessorprocessChildren
严重:Exceptioninvokingperiodicoperation:
java.lang.OutOfMemoryError:Javaheapspace
atjava.io.WinNTFileSystem.canonicalize0(NativeMethod)
atjava.io.Win32FileSystem.canonicalize(Win32FileSystem.java:375)
atjava.io.File.getCanonicalPath(File.java:559)
atorg.apache.naming.resources.FileDirContext.file(FileDirContext.java:830)
atorg.apache.naming.resources.FileDirContext.getAttributes(FileDirContext.java:428)
atorg.apache.naming.resources.BaseDirContext.getAttributes(BaseDirContext.java:725)
atorg.apache.naming.resources.ProxyDirContext.getAttributes(ProxyDirContext.java:835)
atorg.apache.catalina.loader.WebappClassLoader.modified(WebappClassLoader.java:718)
atorg.apache.catalina.loader.WebappLoader.modified(WebappLoader.java:478)
atorg.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:399)
atorg.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1309)
atorg.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1601)
atorg.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1610)
atorg.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1610)
atorg.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1590)
atjava.lang.Thread.run(Thread.java:619)
org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'sessionFactory'definedinclasspathresource[applicationContext.xml]:Invocationofinitmethodfailed;nestedexceptionisjava.lang.OutOfMemoryError:Javaheapspace
Causedby:java.lang.OutOfMemoryError:Javaheapspace
atjava.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:45)
atjava.lang.StringBuffer.<init>(StringBuffer.java:79)
atorg.hibernate.loader.DefaultEntityAliases.<init>(DefaultEntityAliases.java:63)
atorg.hibernate.loader.DefaultEntityAliases.<init>(DefaultEntityAliases.java:26)
atorg.hibernate.loader.BasicLoader.postInstantiate(BasicLoader.java:42)
atorg.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:75)
atorg.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:43)
atorg.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:33)
atorg.hibernate.loader.entity.BatchingEntityLoader.createBatchingEntityLoader(BatchingEntityLoader.java:103)
atorg.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:1621)
atorg.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:1625)
atorg.hibernate.persister.entity.AbstractEntityPersister.createLoaders(AbstractEntityPersister.java:2678)
atorg.hibernate.persister.entity.AbstractEntityPersister.postInstantiate(AbstractEntityPersister.java:2671)
atorg.hibernate.persister.entity.SingleTableEntityPersister.postInstantiate(SingleTableEntityPersister.java:667)
atorg.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:275)
atorg.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)
atorg.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:807)
atorg.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:740)
atorg.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:131)
atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1062)
atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1029)
atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:420)
atorg.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141)
atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
atorg.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:287)
atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348)
atorg.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:92)
atorg.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:77)
atjavaoa.util.ScheduledJob.executeTask(ScheduledJob.java:37)
atjavaoa.util.ScheduledJob.execute(ScheduledJob.java:29)
org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'sessionFactory'definedinclasspathresource[applicationContext.xml]:Invocationofinitmethodfailed;nestedexceptionisjava.lang.OutOfMemoryError:Javaheapspace
Causedby:java.lang.OutOfMemoryError:Javaheapspace
org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'sessionFactory'definedinclasspathresource[applicationContext.xml]:Invocationofinitmethodfailed;nestedexceptionisjava.lang.OutOfMemoryError:Javaheapspace
Causedby:java.lang.OutOfMemoryError:Javaheapspace
2008-5-3017:00:47org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessorprocessChildren
严重:Exceptioninvokingperiodicoperation:
java.lang.OutOfMemoryError:Javaheapspace
org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'sessionFactory'definedinclasspathresource[applicationContext.xml]:Invocationofinitmethodfailed;nestedexceptionisjava.lang.OutOfMemoryError:Javaheapspace
Causedby:java.lang.OutOfMemoryError:Javaheapspace
Exceptioninthread"ContainerBackgroundProcessor[StandardEngine[Catalina]]"Exceptioninthread"DefaultQuartzScheduler_QuartzSchedulerThread"java.lang.OutOfMemoryError:Javaheapspace
java.lang.OutOfMemoryError:Javaheapspace
org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'sessionFactory'definedinclasspathresource[applicationContext.xml]:Invocationofinitmethodfailed;nestedexceptionisjava.lang.OutOfMemoryError:Javaheapspace
Causedby:java.lang.OutOfMemoryError:Javaheapspace