<bean id="threadPoolManager" class="com.companyname.dhm.core.bss.service.productoffering.thread.ThreadPoolManager"
init-method="init">//指定Bean的初始化方法
<property name="corePoolSize">
<value>${core_pool_size}</value>
</property>
<property name="maximumPoolSize">
<value>${max_inum_pool_size}</value>
</property>
<property name="keepAliveTime">
<value>${keep_alive_time}</value>
</property>
</bean>
<bean id="autoAttachDataFactory" class="com.companyname.dhm.core.bss.service.productoffering.thread.AutoAttachDataFactory"
depends-on="threadPoolManager" init-method="init">//指定Bean的初始化方法
<property name="threadPoolManager">
<ref bean="threadPoolManager" />
</property>
<property name="prodOfferingDao">
<ref bean="IProductOfferingDao" />
</property>
<property name="autoAttachService">
<ref bean="IAutoAttachService" />
</property>
<!--
<property name="autoAttachNum">
<value>${auto_attach_num}</value>
</property>
-->
</bean>
applicationContextJob.xml
<!-- 媒资自动挂靠定时器 -->
<bean id="AutoBatchAttachJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref local="AutoBatchAttachJob" />
</property>
<!-- 整点执行(秒 分 小时 日期 月 星期 年)为空表示全部 :表达式表示每天凌晨1点执行任务-->
<property name="cronExpression" value="0 0/5 * * * ?"/>
</bean>
<!-- 定时媒资自动挂靠定时器 -->
<bean id="AutoBatchAttachJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 并发为true ,不想并发设false;这样会一个接一个走-->
<property name="concurrent" value="false"/>
<property name="targetObject">
<ref bean="autoAttachDataFactory"/>
</property>
<property name="targetMethod">
<value>autoPAttachMessage</value>
</property>
</bean>
package com.companyname.dhm.core.bss.service.productoffering.thread;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
/**
* 线程池管理类
* <p>Copyright: Copyright (c) 2009 <p>
* <p>Company: companyname</p>
* @author
* @since
*/
public class ThreadPoolManager
{
private static final Logger log = Logger.getLogger(ThreadPoolManager.class);
/**
* 线程池对象
*/
private ThreadPoolExecutor threadPool;
// 线程池中所保存的线程数,包括空闲线程
private int corePoolSize;
// 线程池中允许的最大线程数
private int maximumPoolSize;
// 当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间
private int keepAliveTime;
/**
* 初始化线程池
*/
public void init()
{
//建立线程池对象
threadPool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>());
log.debug("The threadPool count mix thread:" + maximumPoolSize);
}
/**
* 执行方法
* @param task
*/
public void execute(java.lang.Runnable task)
{
threadPool.execute(task);
}
public int getCorePoolSize()
{
return corePoolSize;
}
public void setCorePoolSize(int corePoolSize)
{
this.corePoolSize = corePoolSize;
}
public int getMaximumPoolSize()
{
return maximumPoolSize;
}
public void setMaximumPoolSize(int maximumPoolSize)
{
this.maximumPoolSize = maximumPoolSize;
}
public int getKeepAliveTime()
{
return keepAliveTime;
}
public void setKeepAliveTime(int keepAliveTime)
{
this.keepAliveTime = keepAliveTime;
}
}
system_conf.properties
#异步挂靠线程池配置
#线程池中所保存的线程数,包括空闲线程
core_pool_size = 5
#线程池中允许的最大线程数
max_inum_pool_size=100
#当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间
keep_alive_time=50000
#批量自动挂靠每批次挂靠最大数
auto_attach_num=5
package com.companyname.dhm.core.bss.service.productoffering.thread;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import com.companyname.bss.common.Constants;
import com.companyname.bss.model.domain.productOffering.AttachMessage;
import com.companyname.bss.util.BeanUtil;
import com.companyname.bss.util.Log;
import com.companyname.dhm.core.bss.service.productoffering.IAutoAttachService;
import com.companyname.dhm.core.dao.productoffering.IProductOfferingDao;
import com.companyname.dhm.core.dao.productoffering.po.PAttachMessage;
public class AutoAttachDataFactory
{
private static final Log log = Log.getLog(AutoAttachDataFactory.class);
private IAutoAttachService autoAttachService;
private IProductOfferingDao prodOfferingDao;
private ThreadPoolManager threadPoolManager;
//挂靠消息队列
public final static Queue<AttachMessage> messageQueue = new LinkedBlockingQueue<AttachMessage>();
/**
* 初始化工厂
*/
public void init()
{
//项目启动时,把状态为"挂靠中的"改为"待挂靠"
prodOfferingDao.updateAttachMessageInit();
AutoAttachThread thread = new AutoAttachThread(autoAttachService);
threadPoolManager.execute(thread);
}
/**
* 定时生产消息队列,构建线程
*/
public void autoPAttachMessage()
{
log.info("autoAttachTask method start!");
try
{
//得到待挂靠消息列表
List<PAttachMessage> list = prodOfferingDao.queryAttachMessageForAttach();
if(list!=null)
{
for(PAttachMessage pAttachMessage : list)
{
AttachMessage attachMessage = new AttachMessage();
BeanUtil.copyProperties(attachMessage, pAttachMessage);
//加到公共队列中,并更新状态为同步中
addToQueue(pAttachMessage);
}
}
}
catch (InterruptedException e)
{
log.error("autoAttachTask error!");
e.printStackTrace();
}
log.info("autoAttachTask method end!");
}
//构建队列
private void addToQueue(PAttachMessage pAttachMessage) throws InterruptedException
{
synchronized(this)
{
AttachMessage message = new AttachMessage();
BeanUtil.copyProperties(message, pAttachMessage);
messageQueue.add(message);
log.debug("一个挂靠消息从放入队列:"+message.getResourceId());
//修改挂靠信息状态:“挂靠中”
pAttachMessage.setStatus(Constants.AUTO_ATTACH_STATUS_ATTACHING);
prodOfferingDao.updateAttachMessageStatus(pAttachMessage);
}
}
public IAutoAttachService getAutoAttachService()
{
return autoAttachService;
}
public void setAutoAttachService(IAutoAttachService autoAttachService)
{
this.autoAttachService = autoAttachService;
}
public IProductOfferingDao getProdOfferingDao()
{
return prodOfferingDao;
}
public void setProdOfferingDao(IProductOfferingDao prodOfferingDao)
{
this.prodOfferingDao = prodOfferingDao;
}
public ThreadPoolManager getThreadPoolManager()
{
return threadPoolManager;
}
public void setThreadPoolManager(ThreadPoolManager threadPoolManager)
{
this.threadPoolManager = threadPoolManager;
}
}
package com.companyname.dhm.core.bss.service.productoffering.thread;
import java.util.List;
import org.apache.log4j.Logger;
import com.companyname.bss.model.domain.productOffering.AttachMessage;
import com.companyname.dhm.core.bss.service.productoffering.IAutoAttachService;
public class AutoAttachThread implements Runnable
{
private static final Logger logger = Logger.getLogger(AutoAttachThread.class);
private IAutoAttachService autoAttachService;
/**
* 通过构造方法为线程类的属性赋值
* @param asynWriteFactory
* @param asynDataConfigInfo
*/
@SuppressWarnings("unchecked")
public AutoAttachThread(IAutoAttachService autoAttachService)
{
this.autoAttachService = autoAttachService;
}
@Override
public void run()
{
//队列是否为空
boolean checkQueue = true;
AttachMessage attachMessage;
while(checkQueue)
{
//得到预定数目的挂靠id
synchronized(this)
{
attachMessage = AutoAttachDataFactory.messageQueue.poll();
}
if(attachMessage!=null)
{
logger.info("Resource Attach Start>>>>>>>>>>>>>>>>:resourceId="+attachMessage.getResourceId());
//自动挂靠
String result;
try
{
//处理挂靠逻辑,返回处理结果
result = autoAttachService.dealWithBatchAttach(attachMessage);
//根据挂靠结果,更新挂靠表
autoAttachService.dealWithAttachResult(attachMessage, result);
}
catch (Exception e)
{
//挂靠结果处理
autoAttachService.dealWithAttachResult(attachMessage, "Auto Attach Fail:Unknown Errors!");
logger.error("exception in AutoAttach when deal attach!", e);
}
logger.info("Resource Attach End++++++++++++++++++:resourceId="+attachMessage.getResourceId());
}
else
{
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
logger.error("sleep AutoAttachThread exception!", e);
}
}
}
}
}
init-method="init">//指定Bean的初始化方法
<property name="corePoolSize">
<value>${core_pool_size}</value>
</property>
<property name="maximumPoolSize">
<value>${max_inum_pool_size}</value>
</property>
<property name="keepAliveTime">
<value>${keep_alive_time}</value>
</property>
</bean>
<bean id="autoAttachDataFactory" class="com.companyname.dhm.core.bss.service.productoffering.thread.AutoAttachDataFactory"
depends-on="threadPoolManager" init-method="init">//指定Bean的初始化方法
<property name="threadPoolManager">
<ref bean="threadPoolManager" />
</property>
<property name="prodOfferingDao">
<ref bean="IProductOfferingDao" />
</property>
<property name="autoAttachService">
<ref bean="IAutoAttachService" />
</property>
<!--
<property name="autoAttachNum">
<value>${auto_attach_num}</value>
</property>
-->
</bean>
applicationContextJob.xml
<!-- 媒资自动挂靠定时器 -->
<bean id="AutoBatchAttachJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref local="AutoBatchAttachJob" />
</property>
<!-- 整点执行(秒 分 小时 日期 月 星期 年)为空表示全部 :表达式表示每天凌晨1点执行任务-->
<property name="cronExpression" value="0 0/5 * * * ?"/>
</bean>
<!-- 定时媒资自动挂靠定时器 -->
<bean id="AutoBatchAttachJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 并发为true ,不想并发设false;这样会一个接一个走-->
<property name="concurrent" value="false"/>
<property name="targetObject">
<ref bean="autoAttachDataFactory"/>
</property>
<property name="targetMethod">
<value>autoPAttachMessage</value>
</property>
</bean>
package com.companyname.dhm.core.bss.service.productoffering.thread;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
/**
* 线程池管理类
* <p>Copyright: Copyright (c) 2009 <p>
* <p>Company: companyname</p>
* @author
* @since
*/
public class ThreadPoolManager
{
private static final Logger log = Logger.getLogger(ThreadPoolManager.class);
/**
* 线程池对象
*/
private ThreadPoolExecutor threadPool;
// 线程池中所保存的线程数,包括空闲线程
private int corePoolSize;
// 线程池中允许的最大线程数
private int maximumPoolSize;
// 当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间
private int keepAliveTime;
/**
* 初始化线程池
*/
public void init()
{
//建立线程池对象
threadPool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>());
log.debug("The threadPool count mix thread:" + maximumPoolSize);
}
/**
* 执行方法
* @param task
*/
public void execute(java.lang.Runnable task)
{
threadPool.execute(task);
}
public int getCorePoolSize()
{
return corePoolSize;
}
public void setCorePoolSize(int corePoolSize)
{
this.corePoolSize = corePoolSize;
}
public int getMaximumPoolSize()
{
return maximumPoolSize;
}
public void setMaximumPoolSize(int maximumPoolSize)
{
this.maximumPoolSize = maximumPoolSize;
}
public int getKeepAliveTime()
{
return keepAliveTime;
}
public void setKeepAliveTime(int keepAliveTime)
{
this.keepAliveTime = keepAliveTime;
}
}
system_conf.properties
#异步挂靠线程池配置
#线程池中所保存的线程数,包括空闲线程
core_pool_size = 5
#线程池中允许的最大线程数
max_inum_pool_size=100
#当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间
keep_alive_time=50000
#批量自动挂靠每批次挂靠最大数
auto_attach_num=5
package com.companyname.dhm.core.bss.service.productoffering.thread;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import com.companyname.bss.common.Constants;
import com.companyname.bss.model.domain.productOffering.AttachMessage;
import com.companyname.bss.util.BeanUtil;
import com.companyname.bss.util.Log;
import com.companyname.dhm.core.bss.service.productoffering.IAutoAttachService;
import com.companyname.dhm.core.dao.productoffering.IProductOfferingDao;
import com.companyname.dhm.core.dao.productoffering.po.PAttachMessage;
public class AutoAttachDataFactory
{
private static final Log log = Log.getLog(AutoAttachDataFactory.class);
private IAutoAttachService autoAttachService;
private IProductOfferingDao prodOfferingDao;
private ThreadPoolManager threadPoolManager;
//挂靠消息队列
public final static Queue<AttachMessage> messageQueue = new LinkedBlockingQueue<AttachMessage>();
/**
* 初始化工厂
*/
public void init()
{
//项目启动时,把状态为"挂靠中的"改为"待挂靠"
prodOfferingDao.updateAttachMessageInit();
AutoAttachThread thread = new AutoAttachThread(autoAttachService);
threadPoolManager.execute(thread);
}
/**
* 定时生产消息队列,构建线程
*/
public void autoPAttachMessage()
{
log.info("autoAttachTask method start!");
try
{
//得到待挂靠消息列表
List<PAttachMessage> list = prodOfferingDao.queryAttachMessageForAttach();
if(list!=null)
{
for(PAttachMessage pAttachMessage : list)
{
AttachMessage attachMessage = new AttachMessage();
BeanUtil.copyProperties(attachMessage, pAttachMessage);
//加到公共队列中,并更新状态为同步中
addToQueue(pAttachMessage);
}
}
}
catch (InterruptedException e)
{
log.error("autoAttachTask error!");
e.printStackTrace();
}
log.info("autoAttachTask method end!");
}
//构建队列
private void addToQueue(PAttachMessage pAttachMessage) throws InterruptedException
{
synchronized(this)
{
AttachMessage message = new AttachMessage();
BeanUtil.copyProperties(message, pAttachMessage);
messageQueue.add(message);
log.debug("一个挂靠消息从放入队列:"+message.getResourceId());
//修改挂靠信息状态:“挂靠中”
pAttachMessage.setStatus(Constants.AUTO_ATTACH_STATUS_ATTACHING);
prodOfferingDao.updateAttachMessageStatus(pAttachMessage);
}
}
public IAutoAttachService getAutoAttachService()
{
return autoAttachService;
}
public void setAutoAttachService(IAutoAttachService autoAttachService)
{
this.autoAttachService = autoAttachService;
}
public IProductOfferingDao getProdOfferingDao()
{
return prodOfferingDao;
}
public void setProdOfferingDao(IProductOfferingDao prodOfferingDao)
{
this.prodOfferingDao = prodOfferingDao;
}
public ThreadPoolManager getThreadPoolManager()
{
return threadPoolManager;
}
public void setThreadPoolManager(ThreadPoolManager threadPoolManager)
{
this.threadPoolManager = threadPoolManager;
}
}
package com.companyname.dhm.core.bss.service.productoffering.thread;
import java.util.List;
import org.apache.log4j.Logger;
import com.companyname.bss.model.domain.productOffering.AttachMessage;
import com.companyname.dhm.core.bss.service.productoffering.IAutoAttachService;
public class AutoAttachThread implements Runnable
{
private static final Logger logger = Logger.getLogger(AutoAttachThread.class);
private IAutoAttachService autoAttachService;
/**
* 通过构造方法为线程类的属性赋值
* @param asynWriteFactory
* @param asynDataConfigInfo
*/
@SuppressWarnings("unchecked")
public AutoAttachThread(IAutoAttachService autoAttachService)
{
this.autoAttachService = autoAttachService;
}
@Override
public void run()
{
//队列是否为空
boolean checkQueue = true;
AttachMessage attachMessage;
while(checkQueue)
{
//得到预定数目的挂靠id
synchronized(this)
{
attachMessage = AutoAttachDataFactory.messageQueue.poll();
}
if(attachMessage!=null)
{
logger.info("Resource Attach Start>>>>>>>>>>>>>>>>:resourceId="+attachMessage.getResourceId());
//自动挂靠
String result;
try
{
//处理挂靠逻辑,返回处理结果
result = autoAttachService.dealWithBatchAttach(attachMessage);
//根据挂靠结果,更新挂靠表
autoAttachService.dealWithAttachResult(attachMessage, result);
}
catch (Exception e)
{
//挂靠结果处理
autoAttachService.dealWithAttachResult(attachMessage, "Auto Attach Fail:Unknown Errors!");
logger.error("exception in AutoAttach when deal attach!", e);
}
logger.info("Resource Attach End++++++++++++++++++:resourceId="+attachMessage.getResourceId());
}
else
{
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
logger.error("sleep AutoAttachThread exception!", e);
}
}
}
}
}