[技术分享]java并行调度框架封装及示例
来源:互联网 发布:国际电视直播软件 编辑:程序博客网 时间:2024/06/02 07:04
参考资料: 阿里巴巴开源项目 CobarClient 源码实现。
分享作者:闫建忠
分享时间:2014年5月7日
---------------------------------------------------------------------------------------
并行调度封装类设计: BXexample.java
package org.hdht.business.ordermanager.quartzjob;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.concurrent.BlockingQueue;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.LinkedBlockingQueue;import java.util.concurrent.ThreadFactory;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;import org.apache.commons.lang.exception.ExceptionUtils;import org.springframework.dao.ConcurrencyFailureException;public class BXexample { private static ExecutorService createCustomExecutorService(int poolSize, final String method) { int coreSize = Runtime.getRuntime().availableProcessors();//返回系统CUP数量 if (poolSize < coreSize) { coreSize = poolSize; } ThreadFactory tf = new ThreadFactory() { public Thread newThread(Runnable r) { Thread t = new Thread(r, "thread created at BXexample method [" + method + "]"); t.setDaemon(true); return t; } }; BlockingQueue<Runnable> queueToUse = new LinkedBlockingQueue<Runnable>(); final ThreadPoolExecutor executor = new ThreadPoolExecutor(coreSize, poolSize, 60, TimeUnit.SECONDS, queueToUse, tf, new ThreadPoolExecutor.CallerRunsPolicy()); return executor; }public static <T> List<T> getSubListPage(List<T> list, int skip,int pageSize) {if (list == null || list.isEmpty()) {return null;}int startIndex = skip;int endIndex = skip + pageSize;if (startIndex > endIndex || startIndex > list.size()) {return null;}if (endIndex > list.size()) {endIndex = list.size();}return list.subList(startIndex, endIndex);}public static void BXfunction(Collection<?> paramCollection,final ExectueCallBack ecb){//构建执行器ExecutorService executor = createCustomExecutorService(Runtime.getRuntime().availableProcessors(), "batchExecuteProjection");try {//监视器final CountDownLatch latch = new CountDownLatch(paramCollection.size());final StringBuffer exceptionStaktrace = new StringBuffer();Iterator<?> iter = paramCollection.iterator();while (iter.hasNext()) {final Object entity = iter.next();Runnable task = new Runnable() {public void run() {try {ecb.doExectue(entity);} catch (Throwable t) {exceptionStaktrace.append(ExceptionUtils.getFullStackTrace(t));} finally {latch.countDown();}}};executor.execute(task);//并行调度}try {latch.await();//监视器等待所有线程执行完毕} catch (InterruptedException e) {//调度异常throw new ConcurrencyFailureException("unexpected interruption when re-arranging parameter collection into sub-collections ",e);}if (exceptionStaktrace.length() > 0) {//业务异常throw new ConcurrencyFailureException("unpected exception when re-arranging parameter collection, check previous log for details.\n"+ exceptionStaktrace);}} finally {executor.shutdown();//执行器关闭}}}
回调接口类设计:ExectueCallBack.java
package org.hdht.business.ordermanager.quartzjob;public interface ExectueCallBack {void doExectue(Object executor) throws Exception;}
示例(hello 示例)
public static void main(String[] args) {List<String> paramCollection = new ArrayList<String>();paramCollection.add("9");paramCollection.add("2");paramCollection.add("18");paramCollection.add("7");paramCollection.add("6");paramCollection.add("1");paramCollection.add("3");paramCollection.add("4"); paramCollection.add("14");paramCollection.add("13");int freesize = 3;//当前处理能力for(int i=0;i<paramCollection.size();i=i+freesize){List<String> tl = BXexample.getSubListPage(paramCollection, i, freesize);BXexample.BXfunction(tl,new ExectueCallBack() { public void doExectue(Object executor) throws Exception { int k = Integer.parseInt((String)executor); for(int i=0;i<k*10000000;i++){ //执行循环 } System.out.println(k+":hello world"); } });}}
示例(实际业务应用示例)
/** * 并行调度相关处理 * * 按卫星*日期 ,将待处理的任务分解为 卫星+日期 粒度的子任务 添加到paramMapList列表中 */List<Map<String, Object>> paramMapList = new ArrayList<Map<String, Object>>();for (Iterator<OrderParamSatellite> iterator = paramSatellites.iterator(); iterator.hasNext();) {OrderParamSatellite paramSatellite = iterator.next();paramMapList.addAll(this.getParamMapList(paramSatellite));}//根据集群最大处理能力,分页处理任务列表,作为list截取的步长int fsize = HostServerQueue.getInstance().freeSize();for(int i=0;i<paramMapList.size();i=i+fsize){List<Map<String, Object>> tl = BXexample.getSubListPage(paramMapList, i, fsize);//并行调度BXexample.BXfunction(tl,new ExectueCallBack(){ public void doExectue(Object executor) throws Exception { ExecuteOrderBTask((Map<String, Object>)executor); } });//动态查找空闲节点数量,即集群最大处理能力fsize = HostServerQueue.getInstance().freeSize();}
0 0
- [技术分享]java并行调度框架封装及示例
- java并行调度框架封装及示例
- java 并行框架 并行编程
- java 调度封装
- java计划任务调度框架quartz结合spring实现调度的配置实例代码分享
- java计划任务调度框架quartz结合spring实现调度的配置实例代码分享
- java计划任务调度框架quartz结合spring实现调度的配置实例代码分享
- java jodd框架介绍及使用示例
- Tiny并行计算框架之复杂示例
- Tiny并行计算框架之复杂示例
- 技术框架分享
- rabbitmq可靠确认模式的java封装及示例
- rabbitmq可靠确认模式的java封装及示例
- RabbitMQ可靠确认模式的java封装及示例
- JAVA线程池管理及分布式HADOOP调度框架搭建
- JAVA线程池管理及分布式HADOOP调度框架搭建
- Java线程池管理及分布式Hadoop调度框架搭建
- Java线程池管理及分布式Hadoop调度框架搭建
- iPhone6等高端机斗得热闹,低端机也没闲着
- How to do logging in C# with log4net
- xlrd部分类方法说明
- 前端书籍推荐
- RCP EditorPart 调用doSave的方法
- [技术分享]java并行调度框架封装及示例
- win2003server 最基本的安全设置
- 在Flex中判断是否在组件之外单击的技巧
- Enable Accelerated Compositing on the WebKit Android NDK port && Texture Mapper
- 在 Mac 中安装 MySQLdb (Python mysql )
- URL encoding
- org.apache.catalina.connector.Request.parseParameters(Request.java:2446) NullPointerException异常处理
- python获取微信公共平台消息列表和用户头像
- Animatepacker for cocos2d-x 3.0 解析