最近在做项目,目前已经到了测试的阶段了,我用quartz做了一个任务调度,每隔3秒钟(测试的时候周期比较短),会扫描数据库,如果发现满足条件的结果,就会发起一个流程。
但是当项目运行4个小时左右,jboss就会报内存泄露异常:java.lang.OutOfMemoryError: Java heap space
以下是我的代码片段,以及配置文件,请大家看看
Quartz的job配置
[code="java"]<?xml version="1.0" encoding="UTF-8"?>
deployAds
group1
cn.itstudio.ad_server.quartz.DeployAdsJob
deployAds
group1
deployAds
group1
0/3 * * ? * *
[/code]
DeployAdsJob.java
[code="java"]public class DeployAdsJob implements Job {
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
System.out.println("-------------广告上架");
Session session = null;
Transaction tx = null;
try {
session = HibernateSessionFactory.getSession();
Date current = new Date();
// 获取当前日的前一天
Date compareDate = DateUtil.getPrevDate(current);
String sql = "from PlayList p where p.playBegin<='"
+ DateUtil.dateFormate(current, "yyyy-MM-dd")
+ "' and p.playEnd > '"
+ DateUtil.dateFormate(compareDate, "yyyy-MM-dd")
+ "' and p.other1 ='false'";
//System.out.println("发布广告-------"+sql);
PlayListDao playListDao = new PlayListDao();
List<PlayList> list = playListDao.findByCondition(session, sql,
null);
JbpmUtil jbpmUtil = new JbpmUtil();
//开启事务
tx = session.beginTransaction();
for (PlayList playList : list) {
// 发布广告上架流程实例
jbpmUtil.startNewProcessInstance(playList.getTerminal()
.getTerminalCode(), playList.getAds().getAdsId()
.toString(), JbpmUtil.DEPLOY_ADS);
System.out.println("广告:" + playList.getAds().getLabel()
+ " 需要在终端机:"
+ playList.getTerminal().getTerminalCode() + " 上架");
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
} finally {
HibernateSessionFactory.closeSession();
}
}
}[/code]
JbpmUtil中的startNewProcessInstance方法
[code="java"]public boolean startNewProcessInstance(String terminalCode, String adId,
String processDefinitionKey) throws Exception {
ProcessEngine pe = null;
try {
pe = Configuration.getProcessEngine();
Session session = HibernateSessionFactory.getSession();
pe.setHibernateSession(session);
ExecutionService es = pe.getExecutionService();
Map context = new HashMap();
context.put("terminalCode", terminalCode);
context.put("adId", adId);
ProcessKeyMapDao processKeyMapDao = new ProcessKeyMapDao();
// 构建流程id
String processInstanceId = processKeyMapDao.getProcessIdBySDKey(
session, processDefinitionKey + "." + terminalCode + "."
+ adId);
if (processInstanceId.equals("")) {
processInstanceId = processDefinitionKey + "." + terminalCode
+ "." + adId;
}
Query query = session
.createQuery("from ExecutionImpl e where e.id like '%"
+ processInstanceId + "%'");
if (query.list().size() != 0) {
System.out.println("该processInstance已经启动过了,无法重复启动");
return false;
}
// 如果该流程为收集终端机状态的流程,如果该广告正在下架,则不允许收集状态
if (processDefinitionKey.equals("CollectTerminalState")) {
String deployProcessId = "CollectTerminalState" + "."
+ terminalCode + "." + adId;
query = session
.createQuery("from ExecutionImpl e where e.id like '%"
+ deployProcessId + "%'");
if (query.list().size() != 0) {
System.out.println("广告:" + adId + " 正在下架,不允许收集状态");
return false;
}
}
// es.startProcessInstanceById(pd.getId(), context, terminalCode +
// "."
// + adId);
es.startProcessInstanceByKey(processDefinitionKey, context,
terminalCode + "." + adId);
System.out.println("成功创建processInstance,流程Id为"
+ processDefinitionKey + "." + terminalCode + "." + adId);
return true;
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (pe != null) {
pe.close();
}
}
}[/code]
有一点不是很清楚,processEngine需不需要关闭,已经我的问题出在哪,谢谢大家