From 044e365bf44a2de99b61f5da21f9729e34e4f93f Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期六, 18 十一月 2023 09:52:14 +0800 Subject: [PATCH] 修改查询农户sql语句 --- pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/ThreadPool.java | 73 ++ pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/queue/Node.java | 8 pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/queue/NodeObj.java | 4 pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/schedulerTask/Test.java | 36 + pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/queue/Queue.java | 186 ++++++ pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/schedulerTask/SchedulerTaskSupport.java | 464 +++++++++++++++++ pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/Test.java | 157 +++++ pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/schedulerTask/SchedulerTaskFactory.java | 85 +++ pipIrr-platform/pipIrr-global/src/main/resources/mapper/BaClientMapper.xml | 48 + pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/ThreadPoolImp.java | 442 ++++++++++++++++ pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/schedulerTask/TaskJob.java | 16 pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/TreadPoolFactory.java | 79 ++ 12 files changed, 1,578 insertions(+), 20 deletions(-) diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/queue/Node.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/queue/Node.java new file mode 100644 index 0000000..f6b36b4 --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/queue/Node.java @@ -0,0 +1,8 @@ +package com.dy.common.queue; + + +public class Node { + public Node pre ; + public Node next ; + public NodeObj obj ; +} diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/queue/NodeObj.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/queue/NodeObj.java new file mode 100644 index 0000000..c39f3eb --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/queue/NodeObj.java @@ -0,0 +1,4 @@ +package com.dy.common.queue; + +public interface NodeObj { +} diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/queue/Queue.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/queue/Queue.java new file mode 100644 index 0000000..cfa77d5 --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/queue/Queue.java @@ -0,0 +1,186 @@ +package com.dy.common.queue; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * 闃熷垪锛屽厛杩涘厛鍑� + */ +public class Queue { + @SuppressWarnings("unfinal") + private final Logger log = LogManager.getLogger(Queue.class.getName()) ; + + private final Object synObj ; + + private String queueName ; + + private final Node head ; + private final Node tail ; + private int size ; + + //鏈�澶ч檺鍒讹紝瓒呰繃杩欎釜闄愬埗锛岄槦鍒楀皢鎷掔粷push璋冪敤锛屽苟鎶涘嚭寮傚父 + private int maxSize ; + //璀﹀憡闄愬埗锛岃秴杩囪繖涓檺鍒讹紝闃熷垪鎺ユ敹push鍚庯紝灏嗚褰晈arn鏃ュ織 + private int warnSize ;// + + + public Queue(String queueName){ + synObj = new Object() ; + this.queueName = queueName ; + if(this.queueName == null){ + this.queueName = Queue.class.getName() ; + } + this.head = new Node() ; + this.tail = new Node() ; + this.head.next = this.tail ; + this.tail.pre = this.head ; + this.size = 0 ; + + this.warnSize = 1000000 ; + this.maxSize = Integer.MAX_VALUE ; + } + + public void setLimit(int warnSize, int maxSize){ + this.warnSize = warnSize ; + this.maxSize = maxSize ; + if(this.warnSize <= 0){ + this.warnSize = 1 ; + } + if(this.warnSize == Integer.MAX_VALUE){ + this.warnSize = Integer.MAX_VALUE - 1000 ; + } + + if(this.maxSize <= 0){ + this.maxSize = 1 ; + } + } + /** + * 姝ゆ柟娉曞湪澶氱嚎绋嬩腑鎵ц锛屽悓姝ユ柟寮忥紝闃叉鍏朵粬绾跨▼鎿嶄綔鍏朵粬鏂规硶鏃朵笉鍚屾 + * 鍦ㄩ槦鍒楀熬閮ㄥ帇鍏ヤ竴鑺傜偣 + * 鍦ㄥ绾跨▼涓簲鐢紝鍔犲悓姝� + * @param obj 鍏ュ垪鐨勫璞� + */ + @SuppressWarnings("unused") + public void pushHead(NodeObj obj)throws Exception{ + synchronized(synObj){ + if(obj == null){ + return ; + } + if(this.size >= this.maxSize){ + throw new Exception("闃熷垪锛�" + queueName + "锛夌紦瀛樿妭鐐规暟" + this.size + " 瓒呭嚭鏈�澶ч檺鍒�(" + this.maxSize + ")锛屾嫆缁濇帴鏀舵柊鏁版嵁銆�"); + } + Node node = new Node(); + node.obj = obj ; + node.next = this.head.next ; + node.pre = this.head ; + this.head.next.pre = node ; + this.head.next = node ; + this.size ++ ; + if(this.size >= this.warnSize){ + log.warn("闃熷垪锛�" + queueName + "锛夌紦瀛樿妭鐐规暟 " + this.size + " 瓒呴檺(" + this.warnSize + ")璀﹀憡銆�"); + } + } + } + + /** + * 姝ゆ柟娉曞湪澶氱嚎绋嬩腑鎵ц锛屽悓姝ユ柟寮忥紝闃叉鍏朵粬绾跨▼鎿嶄綔鍏朵粬鏂规硶鏃朵笉鍚屾 + * 鍦ㄩ槦鍒楀熬閮ㄥ帇鍏ヤ竴鑺傜偣 + * 鍦ㄥ绾跨▼涓簲鐢紝鍔犲悓姝� + * @param obj 鍏ュ垪鐨勫璞� + */ + public void pushTail(NodeObj obj)throws Exception{ + synchronized(synObj){ + if(obj == null){ + return ; + } + if(this.size >= this.maxSize){ + throw new Exception("闃熷垪锛�" + queueName + "锛夌紦瀛樿妭鐐规暟" + this.size + " 瓒呭嚭鏈�澶ч檺鍒�(" + this.maxSize + ")锛屾嫆缁濇帴鏀舵柊鏁版嵁銆�"); + } + Node node = new Node(); + node.obj = obj ; + node.next = this.tail ; + node.pre = this.tail.pre ; + this.tail.pre.next = node ; + this.tail.pre = node ; + this.size ++ ; + if(this.size >= this.warnSize){ + log.warn("闃熷垪锛�" + queueName + "锛夌紦瀛樿妭鐐规暟 " + this.size + " 瓒呴檺(" + this.warnSize + ")璀﹀憡銆�"); + } + } + } + + /** + * 姝ゆ柟娉曞湪鍗曠嚎绋嬩腑鎵ц锛屼絾鍙兘鍙楄皟鐢╬ush鐨勫绾跨▼褰卞搷锛屾墍浠ヤ篃鐢ㄥ埌浜嗗悓姝� + * 寮瑰嚭绗竴涓妭鐐� + * 鍦ㄥ绾跨▼涓簲鐢紝鍔犲悓姝� + * @return 鍑哄垪瀵硅薄 + */ + public NodeObj pop(){ + synchronized(synObj){ + NodeObj obj = null ; + if(this.size > 0){ + Node node = this.head.next ; + if(node != this.tail){ + obj = node.obj ; + this.head.next = this.head.next.next ; + this.head.next.pre = this.head ; + node.next = null ; + node.pre = null ; + this.size -- ; + } + } + return obj ; + } + } + + /** + * 寰楀埌绗竴涓妭鐐癸紝浣嗕笉鎶婅妭鐐逛粠闃熷垪涓竻闄� + * @return 绗竴涓璞� + */ + @SuppressWarnings("unused") + public Node getFirstNode(){ + Node node = this.head.next ; + if(node != this.tail){ + return node ; + } + return null ; + } + /** + * 寰楀埌鏈�鍚庝竴涓妭鐐癸紝浣嗕笉鎶婅妭鐐逛粠闃熷垪涓竻闄� + * @return 鏈�鍚庝竴涓璞� + */ + @SuppressWarnings("unused") + public Node getLastNode(){ + Node node = this.tail.pre ; + if(node != this.head){ + return node ; + } + return null ; + } + + /** + * 娓呴櫎涓�涓妭鐐� + * @param node 鍒犻櫎涓�涓妭鐐� + */ + @SuppressWarnings("unused") + public void remove(Node node){ + synchronized(synObj){ + if(node != null && node != this.head && node != this.tail){ + node.pre.next = node.next ; + node.next.pre = node.pre ; + node.next = null ; + node.pre = null ; + this.size -- ; + } + } + } + + /** + * 寰楀埌闃熷垪涓湁鏁堣妭鐐逛釜鏁� + * @return 闃熷垪鍐呰妭鐐规暟閲� + */ + public int size(){ + return this.size ; + } + +} \ No newline at end of file diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/schedulerTask/SchedulerTaskFactory.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/schedulerTask/SchedulerTaskFactory.java new file mode 100644 index 0000000..1751d79 --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/schedulerTask/SchedulerTaskFactory.java @@ -0,0 +1,85 @@ +package com.dy.common.schedulerTask; + +import java.util.Properties; + +import org.apache.logging.log4j.*; +import org.quartz.Scheduler; +import org.quartz.SchedulerException; +import org.quartz.impl.StdSchedulerFactory; + +public class SchedulerTaskFactory { + + private static Logger log = LogManager.getLogger(SchedulerTaskFactory.class.getName()) ; + + private static Scheduler scheduler ; + + private static Properties pro ; + + static{ + pro = new Properties() ; + //浠ヤ笅灞炴�т粠quartz-all-2.1.7.jar寰楀埌锛岃�屼笖涓嬮潰鎵�鏈夐」鐩兘瑕侀厤缃� + pro.put("org.quartz.scheduler.instanceName", "DefaultQuartzScheduler"); + pro.put("org.quartz.scheduler.rmi.export", "false"); + pro.put("org.quartz.scheduler.rmi.proxy", "false"); + pro.put("org.quartz.scheduler.wrapJobExecutionInUserTransaction", "false"); + pro.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool"); + pro.put("org.quartz.threadPool.threadCount", "10"); + pro.put("org.quartz.threadPool.threadPriority", "5"); + pro.put("org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread", "true"); + pro.put("org.quartz.jobStore.misfireThreshold", "60000"); + pro.put("org.quartz.jobStore.class", "org.quartz.simpl.RAMJobStore"); + } + + /** + * 寰楀埌璋冨害鍣ㄥ敮涓�瀹炰緥 + * @return + */ + public static Scheduler getSingleScheduler(){ + if(scheduler == null){ + try { + scheduler = new StdSchedulerFactory(pro).getScheduler(); + } catch (SchedulerException e) { + log.error(e) ; + } + } + return scheduler ; + } + + + /** + * 寰楀埌璋冨害鍣ㄥ敮涓�瀹炰緥 + * @param threadPoolMaxCount + * @param threadPoolPriority + * @return + */ + public static Scheduler getSingleScheduler(Integer threadPoolMaxCount, Integer threadPoolPriority){ + if(scheduler == null){ + try { + if(threadPoolMaxCount != null && threadPoolMaxCount.intValue() >= 0){ + pro.put("org.quartz.threadPool.threadCount", "" + (threadPoolMaxCount==null?10:(threadPoolMaxCount<=0?10:threadPoolMaxCount))); + } + if(threadPoolPriority != null && threadPoolPriority.intValue() >= 0){ + pro.put("org.quartz.threadPool.threadPriority", "" + (threadPoolPriority==null?5:(threadPoolPriority<=0?5:threadPoolPriority))); + } + if(threadPoolMaxCount != null && threadPoolPriority != null){ + if(threadPoolMaxCount.intValue() < threadPoolPriority.intValue()){ + throw new SchedulerException("threadPoolMaxCount蹇呴』澶т簬绛変簬threadPoolPriority") ; + } + } + scheduler = new StdSchedulerFactory(pro).getScheduler(); + } catch (SchedulerException e) { + log.error(e) ; + } + } + return scheduler ; + } + + /** + * 鍏抽棴璋冨害鍣� + * @throws SchedulerException + */ + public static void shutdownScheduler() throws SchedulerException{ + scheduler.shutdown() ; + } + +} diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/schedulerTask/SchedulerTaskSupport.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/schedulerTask/SchedulerTaskSupport.java new file mode 100644 index 0000000..0fc205e --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/schedulerTask/SchedulerTaskSupport.java @@ -0,0 +1,464 @@ +package com.dy.common.schedulerTask; + + +import java.util.*; + +import org.quartz.*; + + +public class SchedulerTaskSupport { + + private static Integer threadPoolMaxCount; + private static Integer threadPoolPriority; + + public static void setThreadPoolPro(Integer threadPoolMaxCount, Integer threadPoolPriority){ + if(SchedulerTaskSupport.threadPoolMaxCount == null){ + SchedulerTaskSupport.threadPoolMaxCount = threadPoolMaxCount; + } + if(SchedulerTaskSupport.threadPoolPriority == null){ + SchedulerTaskSupport.threadPoolPriority = threadPoolPriority; + } + } + + /** + * 娣诲姞姣廥绉掗挓閲嶅宸ヤ綔涓�娆$殑宸ヤ綔浠诲姟锛� + * @param jobName 宸ヤ綔鍚嶇О + * @param jobGroupName 宸ヤ綔缁勫悕绉� + * @param jobClass 宸ヤ綔绫� + * @param futureSecond 寤惰繜鍒板皢鏉ユ椂闀�(鍗曚綅绉�)浠ュ紑濮嬪伐浣� + * @param intervalInSeconds 閲嶅宸ヤ綔闂撮殧鏃堕暱(绉�) + * @param repeatCount 閲嶅宸ヤ綔娆℃暟(濡傛灉灏忎簬0锛屽皢涓�鐩撮噸澶嶆墽琛屼笅鍘� , 濡傛灉鏄�0鎵ц涓�娆★紝濡傛灉鏄�1鎵ц2娆★紝渚濇绫绘帹) + * @return 宸ヤ綔浠诲姟Key + * @throws Exception + */ + @SuppressWarnings({"unchecked","rawtypes"}) + public static String addSecondlyJob( + String jobName, + String jobGroupName, + Class jobClass , + HashMap<String , Object> jobDataMap , + int futureSecond, + int intervalInSeconds , + int repeatCount) throws Exception { + SchedulerTaskSupport.checkDelayStart(futureSecond) ; + if(intervalInSeconds < 1){ + throw new Exception("閲嶅宸ヤ綔闂撮殧鏃堕暱(鍗曚綅绉�)涓嶈兘灏忎簬1绉�)!") ; + } + Scheduler sched = SchedulerTaskFactory.getSingleScheduler(threadPoolMaxCount, threadPoolPriority) ; + + Date futureDate = DateBuilder.futureDate(futureSecond, DateBuilder.IntervalUnit.SECOND) ; + Date startTime = DateBuilder.evenSecondDate(futureDate); + + JobDetail job = JobBuilder.newJob(jobClass) + .withIdentity(jobName, jobGroupName) + .build(); + SchedulerTaskSupport.setDataMap(job, jobDataMap) ; + + Trigger trigger = TriggerBuilder.newTrigger() + .withIdentity(jobName + "_trigger", jobGroupName + "_trigger") + .startAt(startTime) + .withSchedule(SimpleScheduleBuilder.simpleSchedule() + .withIntervalInSeconds(intervalInSeconds) + .withRepeatCount(repeatCount)) + .build(); + + sched.scheduleJob(job, trigger); + sched.start(); + + return job.getKey().toString() ; + } + /** + * 娣诲姞姣廥鍒嗛挓閲嶅宸ヤ綔涓�娆$殑宸ヤ綔浠诲姟锛� + * @param jobName 宸ヤ綔鍚嶇О + * @param jobGroupName 宸ヤ綔缁勫悕绉� + * @param jobClass 宸ヤ綔绫� + * @param futureSecond 寤惰繜鍒板皢鏉ユ椂闀�(鍗曚綅绉�)浠ュ紑濮嬪伐浣� + * @param intervalInMinutes 閲嶅宸ヤ綔闂撮殧鏃堕暱(鍒嗛挓) + * @param repeatCount 閲嶅宸ヤ綔娆℃暟(濡傛灉灏忎簬0锛屽皢涓�鐩撮噸澶嶆墽琛屼笅鍘� , 濡傛灉鏄�0鎵ц涓�娆★紝濡傛灉鏄�1鎵ц2娆★紝渚濇绫绘帹) + * @throws Exception + */ + @SuppressWarnings({"unchecked","rawtypes"}) + public static String addMinutelyJob( + String jobName, + String jobGroupName, + Class jobClass , + HashMap<String , Object> jobDataMap , + int futureSecond, + int intervalInMinutes , + int repeatCount) throws Exception { + SchedulerTaskSupport.checkDelayStart(futureSecond) ; + if(intervalInMinutes < 1){ + throw new Exception("閲嶅宸ヤ綔闂撮殧鏃堕暱(鍗曚綅鍒嗛挓)涓嶈兘灏忎簬1鍒嗛挓)!") ; + } + + Scheduler sched = SchedulerTaskFactory.getSingleScheduler(threadPoolMaxCount, threadPoolPriority) ; + + Date futureDate = DateBuilder.futureDate(futureSecond, DateBuilder.IntervalUnit.SECOND) ; + Date startTime = DateBuilder.evenSecondDate(futureDate); + + JobDetail job = JobBuilder.newJob(jobClass) + .withIdentity(jobName, jobGroupName) + .build(); + SchedulerTaskSupport.setDataMap(job, jobDataMap) ; + + Trigger trigger = TriggerBuilder.newTrigger() + .withIdentity(jobName + "_trigger", jobGroupName + "_trigger") + .startAt(startTime) + .withSchedule(SimpleScheduleBuilder.simpleSchedule() + .withIntervalInMinutes(intervalInMinutes) + .withRepeatCount(repeatCount)) + .build(); + + sched.scheduleJob(job, trigger); + sched.start(); + + return job.getKey().toString() ; + + } + /** + * 娣诲姞姣廥灏忔椂閲嶅宸ヤ綔涓�娆$殑宸ヤ綔浠诲姟锛� + * @param jobName 宸ヤ綔鍚嶇О + * @param jobGroupName 宸ヤ綔缁勫悕绉� + * @param jobClass 宸ヤ綔绫� + * @param futureSecond 寤惰繜鍒板皢鏉ユ椂闀�(鍗曚綅绉�)浠ュ紑濮嬪伐浣� + * @param intervalInHour 閲嶅宸ヤ綔闂撮殧鏃堕暱(灏忔椂) + * @param repeatCount 閲嶅宸ヤ綔娆℃暟(濡傛灉灏忎簬0锛屽皢涓�鐩撮噸澶嶆墽琛屼笅鍘� , 濡傛灉鏄�0鎵ц涓�娆★紝濡傛灉鏄�1鎵ц2娆★紝渚濇绫绘帹) + * @throws Exception + */ + @SuppressWarnings({"unchecked","rawtypes"}) + public static String addHourlyJob( + String jobName, + String jobGroupName, + Class jobClass , + HashMap<String , Object> jobDataMap , + int futureSecond, + int intervalInHour , + int repeatCount) throws Exception { + SchedulerTaskSupport.checkDelayStart(futureSecond) ; + if(intervalInHour < 1){ + throw new Exception("閲嶅宸ヤ綔闂撮殧鏃堕暱(鍗曚綅灏忔椂)涓嶈兘灏忎簬1灏忔椂)!") ; + } + + Scheduler sched = SchedulerTaskFactory.getSingleScheduler(threadPoolMaxCount, threadPoolPriority) ; + + Date futureDate = DateBuilder.futureDate(futureSecond, DateBuilder.IntervalUnit.SECOND) ; + Date startTime = DateBuilder.evenSecondDate(futureDate); + + JobDetail job = JobBuilder.newJob(jobClass) + .withIdentity(jobName, jobGroupName) + .build(); + SchedulerTaskSupport.setDataMap(job, jobDataMap) ; + + Trigger trigger = TriggerBuilder.newTrigger() + .withIdentity(jobName + "_trigger", jobGroupName + "_trigger") + .startAt(startTime) + .withSchedule(SimpleScheduleBuilder.simpleSchedule() + .withIntervalInHours(intervalInHour) + .withRepeatCount(repeatCount)) + .build(); + + sched.scheduleJob(job, trigger); + sched.start(); + + return job.getKey().toString() ; + } + + /** + * 娣诲姞姣忓ぉ鏌愭椂鏌愬垎閲嶅宸ヤ綔涓�娆$殑宸ヤ綔浠诲姟锛� + * @param jobName 宸ヤ綔鍚嶇О + * @param jobGroupName 宸ヤ綔缁勫悕绉� + * @param jobClass 宸ヤ綔绫� + * @param hour 鏌愭椂(0-23涔嬮棿(鍖呮嫭0涓�23)) + * @param minute 鏌愬垎(0-59涔嬮棿(鍖呮嫭0涓�59)) + * @throws Exception + */ +// 琛ㄨ揪寮� 鎰忎箟 +// "0 0 12 * * ?" 姣忓ぉ涓崍12鐐硅Е鍙� +// "0 15 10 ? * *" 姣忓ぉ涓婂崍10:15瑙﹀彂 +// "0 15 10 * * ?" 姣忓ぉ涓婂崍10:15瑙﹀彂 +// "0 15 10 * * ? *" 姣忓ぉ涓婂崍10:15瑙﹀彂 +// "0 15 10 * * ? 2005" 2005骞寸殑姣忓ぉ涓婂崍10:15瑙﹀彂 +// "0 * 14 * * ?" 鍦ㄦ瘡澶╀笅鍗�2鐐瑰埌涓嬪崍2:59鏈熼棿鐨勬瘡1鍒嗛挓瑙﹀彂 +// "0 0/5 14 * * ?" 鍦ㄦ瘡澶╀笅鍗�2鐐瑰埌涓嬪崍2:55鏈熼棿鐨勬瘡5鍒嗛挓瑙﹀彂 +// "0 0/5 14,18 * * ?" 鍦ㄦ瘡澶╀笅鍗�2鐐瑰埌2:55鏈熼棿鍜屼笅鍗�6鐐瑰埌6:55鏈熼棿鐨勬瘡5鍒嗛挓瑙﹀彂 +// "0 0-5 14 * * ?" 鍦ㄦ瘡澶╀笅鍗�2鐐瑰埌涓嬪崍2:05鏈熼棿鐨勬瘡1鍒嗛挓瑙﹀彂 +// "0 10,44 14 ? 3 WED" 姣忓勾涓夋湀鐨勬槦鏈熶笁鐨勪笅鍗�2:10鍜�2:44瑙﹀彂 +// "0 15 10 ? * MON-FRI" 鍛ㄤ竴鑷冲懆浜旂殑涓婂崍10:15瑙﹀彂 +// "0 15 10 15 * ?" 姣忔湀15鏃ヤ笂鍗�10:15瑙﹀彂 +// "0 15 10 L * ?" 姣忔湀鏈�鍚庝竴鏃ョ殑涓婂崍10:15瑙﹀彂 +// "0 15 10 ? * 6L" 姣忔湀鐨勬渶鍚庝竴涓槦鏈熶簲涓婂崍10:15瑙﹀彂 +// "0 15 10 ? * 6L 2002-2005" 2002骞磋嚦2005骞寸殑姣忔湀鐨勬渶鍚庝竴涓槦鏈熶簲涓婂崍10:15瑙﹀彂 +// "0 15 10 ? * 6#3" 姣忔湀鐨勭涓変釜鏄熸湡浜斾笂鍗�10:15瑙﹀彂 + @SuppressWarnings({"unchecked","rawtypes"}) + public static String addDailyJob( + String jobName, + String jobGroupName, + Class jobClass , + HashMap<String , Object> jobDataMap , + int hour , + int minute) throws Exception { + SchedulerTaskSupport.checkHour(hour) ; + SchedulerTaskSupport.checkMinute(minute) ; + + Scheduler sched = SchedulerTaskFactory.getSingleScheduler(threadPoolMaxCount, threadPoolPriority) ; + + JobDetail job = JobBuilder.newJob(jobClass) + .withIdentity(jobName, jobGroupName) + .build(); + SchedulerTaskSupport.setDataMap(job, jobDataMap) ; + + Trigger trigger = TriggerBuilder.newTrigger() + .withIdentity(jobName + "_trigger", jobGroupName + "_trigger") + .withSchedule(CronScheduleBuilder.cronSchedule("0 " + minute + " " + hour + " * * ? *")) + .build(); + + sched.scheduleJob(job, trigger); + sched.start(); + + return job.getKey().toString() ; + } + + /** + * 娣诲姞姣忓懆涓�銆佸懆浜屻�佸懆涓夈�佸懆鍥涖�佸懆浜斿悇閲嶅宸ヤ綔涓�娆$殑宸ヤ綔浠诲姟锛� + * @param jobName 宸ヤ綔鍚嶇О + * @param jobGroupName 宸ヤ綔缁勫悕绉� + * @param jobClass 宸ヤ綔绫� + * @param hour 鏌愭椂(0-23涔嬮棿(鍖呮嫭0涓�23)) + * @param minute 鏌愬垎(0-59涔嬮棿(鍖呮嫭0涓�59)) + * @throws Exception + */ + @SuppressWarnings({"unchecked","rawtypes"}) + public static String addWorkingDayInWeekJob( + String jobName, + String jobGroupName, + Class jobClass , + HashMap<String , Object> jobDataMap , + int hour , + int minute) throws Exception { + SchedulerTaskSupport.checkHour(hour) ; + SchedulerTaskSupport.checkMinute(minute) ; + + Scheduler sched = SchedulerTaskFactory.getSingleScheduler(threadPoolMaxCount, threadPoolPriority) ; + + JobDetail job = JobBuilder.newJob(jobClass) + .withIdentity(jobName, jobGroupName) + .build(); + SchedulerTaskSupport.setDataMap(job, jobDataMap) ; + + Trigger trigger = TriggerBuilder.newTrigger() + .withIdentity(jobName + "_trigger", jobGroupName + "_trigger") + .withSchedule(CronScheduleBuilder.cronSchedule("0 " + minute + " " + hour + " ? * MON-FRI" )) + .build(); + + sched.scheduleJob(job, trigger); + sched.start(); + + return job.getKey().toString() ; + } + /** + * 娣诲姞姣忎竴鍛ㄧ殑鏌愪竴澶╂煇鏃舵煇鍒嗛噸澶嶅伐浣滀竴娆$殑宸ヤ綔浠诲姟锛� + * @param jobName 宸ヤ綔鍚嶇О + * @param jobGroupName 宸ヤ綔缁勫悕绉� + * @param jobClass 宸ヤ綔绫� + * @param dayOfWeek 涓�鍛ㄧ殑鏌愪竴澶�(1浠h〃鍛ㄤ竴,渚濇绫绘帹)(鍙栧��1-7(鍖呮嫭1涓�7)) + * @param hour 鏌愭椂(0-23涔嬮棿(鍖呮嫭0涓�23)) + * @param minute 鏌愬垎(0-59涔嬮棿(鍖呮嫭0涓�59)) + * @throws Exception + */ + @SuppressWarnings({"unchecked","rawtypes"}) + public static String addWeeklyJob( + String jobName, + String jobGroupName, + Class jobClass , + HashMap<String , Object> jobDataMap , + int dayOfWeek, + int hour , + int minute) throws Exception { + SchedulerTaskSupport.checkHour(hour) ; + SchedulerTaskSupport.checkMinute(minute) ; + dayOfWeek = SchedulerTaskSupport.checkDayOfWeek(dayOfWeek); + + Scheduler sched = SchedulerTaskFactory.getSingleScheduler(threadPoolMaxCount, threadPoolPriority) ; + + JobDetail job = JobBuilder.newJob(jobClass) + .withIdentity(jobName, jobGroupName) + .build(); + SchedulerTaskSupport.setDataMap(job, jobDataMap) ; + + Trigger trigger = TriggerBuilder.newTrigger() + .withIdentity(jobName + "_trigger", jobGroupName + "_trigger") + .withSchedule(CronScheduleBuilder.cronSchedule("0 " + minute + " " + hour + " ? * " + dayOfWeek )) + .build(); + + sched.scheduleJob(job, trigger); + sched.start(); + + return job.getKey().toString() ; + } + /** + * 娣诲姞姣忔湀鐨勬煇涓�澶╂煇鏃舵煇鍒嗛噸澶嶅伐浣滀竴娆$殑宸ヤ綔浠诲姟锛� + * @param jobName 宸ヤ綔鍚嶇О + * @param jobGroupName 宸ヤ綔缁勫悕绉� + * @param jobClass 宸ヤ綔绫� + * @param dayOfMonth 涓�鏈堢殑鏌愪竴澶�(1-31涔嬮棿(鍖呮嫭1涓�31)) + * @param hour 鏌愭椂(0-23涔嬮棿(鍖呮嫭0涓�23)) + * @param minute 鏌愬垎(0-59涔嬮棿(鍖呮嫭0涓�59)) + * @throws Exception + */ + @SuppressWarnings({"unchecked","rawtypes"}) + public static String addMonthlyJob( + String jobName, + String jobGroupName, + Class jobClass , + HashMap<String , Object> jobDataMap , + int dayOfMonth, + int hour , + int minute) throws Exception { + SchedulerTaskSupport.checkHour(hour) ; + SchedulerTaskSupport.checkMinute(minute) ; + SchedulerTaskSupport.checkDayOfMonth(dayOfMonth); + + Scheduler sched = SchedulerTaskFactory.getSingleScheduler(threadPoolMaxCount, threadPoolPriority) ; + + JobDetail job = JobBuilder.newJob(jobClass) + .withIdentity(jobName, jobGroupName) + .build(); + SchedulerTaskSupport.setDataMap(job, jobDataMap) ; + + Trigger trigger = TriggerBuilder.newTrigger() + .withIdentity(jobName + "_trigger", jobGroupName + "_trigger") + .withSchedule(CronScheduleBuilder.cronSchedule("0 " + minute + " " + hour + " " + dayOfMonth + " * ?")) + .build(); + + sched.scheduleJob(job, trigger); + sched.start(); + + return job.getKey().toString() ; + } + /** + * 娣诲姞姣忔湀鐨勬渶鍚庝竴澶╂煇鏃舵煇鍒嗛噸澶嶅伐浣滀竴娆$殑宸ヤ綔浠诲姟锛� + * @param jobName 宸ヤ綔鍚嶇О + * @param jobGroupName 宸ヤ綔缁勫悕绉� + * @param jobClass 宸ヤ綔绫� + * @param hour 鏌愭椂(0-23涔嬮棿(鍖呮嫭0涓�23)) + * @param minute 鏌愬垎(0-59涔嬮棿(鍖呮嫭0涓�59)) + * @throws Exception + */ + @SuppressWarnings({"unchecked","rawtypes"}) + public static String addLastDateOfMonthJob( + String jobName, + String jobGroupName, + Class jobClass , + HashMap<String , Object> jobDataMap , + int hour , + int minute) throws Exception { + SchedulerTaskSupport.checkHour(hour) ; + SchedulerTaskSupport.checkMinute(minute) ; + + Scheduler sched = SchedulerTaskFactory.getSingleScheduler(threadPoolMaxCount, threadPoolPriority) ; + + JobDetail job = JobBuilder.newJob(jobClass) + .withIdentity(jobName, jobGroupName) + .build(); + SchedulerTaskSupport.setDataMap(job, jobDataMap) ; + + Trigger trigger = TriggerBuilder.newTrigger() + .withIdentity(jobName + "_trigger", jobGroupName + "_trigger") + .withSchedule(CronScheduleBuilder.cronSchedule("0 " + minute + " " + hour + " L * ?")) + .build(); + + sched.scheduleJob(job, trigger); + sched.start(); + + return job.getKey().toString() ; + } + + + /** + * 璁剧疆鍙傛暟鏁版嵁 + */ + private static void setDataMap(JobDetail job , HashMap<String , Object> jobDataMap){ + if(jobDataMap != null && jobDataMap.size() > 0){ + JobDataMap map = job.getJobDataMap(); + Set<Map.Entry<String , Object>> set = jobDataMap.entrySet() ; + Iterator<Map.Entry<String , Object>> it = set.iterator() ; + Map.Entry<String , Object> entry = null ; + String key = null ; + Object value = null ; + while(it.hasNext()){ + entry = it.next() ; + key = entry.getKey() ; + value = entry.getValue() ; + map.put(key, value); + } + } + } + + /** + * + * @param futureSecond + */ + private static void checkDelayStart(int futureSecond)throws Exception { + if(futureSecond < 0){ + throw new Exception("寤惰繜寮�濮嬪伐浣滄椂闀�(鍗曚綅绉�)涓嶈兘灏忎簬0!") ; + } + } + + /** + * + * @param hour + * @throws Exception + */ + private static void checkHour(int hour)throws Exception { + if(hour < 0 || hour >= 24){ + throw new Exception("閲嶅宸ヤ綔鐨勫皬鏃舵椂闂寸偣鍙栧�煎繀椤诲湪0-23涔嬮棿(鍖呮嫭0涓�23)!") ; + } + } + /** + * + * @param minute + * @throws Exception + */ + private static void checkMinute(int minute)throws Exception { + if(minute < 0 || minute >= 60){ + throw new Exception("閲嶅宸ヤ綔鐨勫垎閽熸椂闂寸偣鍙栧�煎繀椤诲湪0-59涔嬮棿(鍖呮嫭0涓�59)!") ; + } + } + /** + * + * @param dayOfWeek + * @throws Exception + */ + private static int checkDayOfWeek(int dayOfWeek)throws Exception { + if(dayOfWeek < 1 || dayOfWeek > 7){ + throw new Exception("閲嶅宸ヤ綔鐨勪竴鏄熸湡鐨勬煇涓�澶╁彇鍊煎繀椤诲湪1(鍛ㄤ竴)-7(鍛ㄦ棩)闂�(鍖呮嫭1涓�7)!") ; + } + int d = 0 ; + if(dayOfWeek == 1){ + d = DateBuilder.MONDAY ; + }else if(dayOfWeek == 2){ + d = DateBuilder.TUESDAY ; + }else if(dayOfWeek == 3){ + d = DateBuilder.WEDNESDAY ; + }else if(dayOfWeek == 4){ + d = DateBuilder.THURSDAY ; + }else if(dayOfWeek == 5){ + d = DateBuilder.FRIDAY ; + }else if(dayOfWeek == 6){ + d = DateBuilder.SATURDAY ; + }else if(dayOfWeek == 7){ + d = DateBuilder.SUNDAY ; + } + return d ; + } + /** + * + * @param dayOfMonth + * @throws Exception + */ + private static void checkDayOfMonth(int dayOfMonth)throws Exception { + if(dayOfMonth < 1 || dayOfMonth > 31){ + throw new Exception("閲嶅宸ヤ綔涓�鏈堟煇涓�澶╁彇鍊煎繀椤诲湪1-31涔嬮棿(鍖呮嫭1涓�31)!") ; + } + } +} diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/schedulerTask/TaskJob.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/schedulerTask/TaskJob.java new file mode 100644 index 0000000..af4694d --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/schedulerTask/TaskJob.java @@ -0,0 +1,16 @@ +package com.dy.common.schedulerTask; + +import org.quartz.Job; +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; + + +public abstract class TaskJob implements Job { + + /** + * 瀹氭椂璋冨害鐨勫伐浣�(甯﹀弬鏁�)銆� + * @throws JobExecutionException 寮傚父 + */ + public abstract void execute(JobExecutionContext ctx) throws JobExecutionException; + +} diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/schedulerTask/Test.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/schedulerTask/Test.java new file mode 100644 index 0000000..20ca355 --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/schedulerTask/Test.java @@ -0,0 +1,36 @@ +package com.dy.common.schedulerTask; + +import java.util.HashMap; + +import org.apache.logging.log4j.*; +import org.quartz.*; + +public class Test extends TaskJob{ + + private static Logger log = LogManager.getLogger(Test.class.getName()) ; +// +// public static void main(String[] args) throws Exception{ +//// SchedulerTaskSupport.addSecondlyJob("test", "testGroup", Test.class, null, 10, 1, 2) ; +//// SchedulerTaskSupport.addMinutelyJob("test", "testGroup", Test.class, null, 1, 1, -1) ; +//// SchedulerTaskSupport.addDailyJob("test", "testGroup", Test.class, null, 11, 16) ; +//// SchedulerTaskSupport.addWeeklyJob("test", "testGroup", Test.class, null, 6, 11, 34) ; +//// SchedulerTaskSupport.addWorkingDayInWeekJob("test", "testGroup", Test.class, null, 11, 41) ; +//// SchedulerTaskSupport.addMonthlyJob("test", "testGroup", Test.class, null, 25, 11, 44) ; +// +// HashMap<String , Object> jobDataMap = new HashMap<String , Object>() ; +// jobDataMap.put("123", "test123") ; +// +// SchedulerTaskSupport.addSecondlyJob("test", "testGroup", Test.class, jobDataMap, 10, 1, 2) ; +// } + + @Override + public void execute(JobExecutionContext ctx) throws JobExecutionException { + log.info("1_" + System.currentTimeMillis() ) ; + JobDataMap jobDataMap = ctx.getJobDetail().getJobDataMap() ; + if(jobDataMap != null){ + Object o = jobDataMap.get("123") ; + log.info("1_" + o ) ; + } + } + +} diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/Test.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/Test.java new file mode 100644 index 0000000..37eec13 --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/Test.java @@ -0,0 +1,157 @@ +package com.dy.common.threadPool; + + +public class Test { + +// +// public static void main(String args[])throws Exception { +//// Test t = new Test(); +//// t.testShort1(); +//// t.testShort2(); +//// t.testLong(); +// } + + /** + * 娴嬭瘯澧炲姞绾跨▼绾跨▼ + * @throws Exception + * + */ + @SuppressWarnings("unused") + private void testShort1() throws Exception { + TreadPoolFactory.initThreadPoolShort("娴嬭瘯绾跨▼缁�", 2, 1, 10, 20); + ThreadPool.Pool pool = TreadPoolFactory.getThreadPoolShort() ; + try { + int n = 1 ; + Thread.sleep(1000); + while (n < 4) { + System.out.println("鍔犲叆鐭伐浣滀换鍔�(" + n + ")"); + pool.putJob(new ThreadPool.Job() { + public void execute() { + System.out.println("!!!!!寮�濮嬬煭宸ヤ綔" + this.hashCode()); + try { + Thread.sleep(2000); + } catch (Exception e) { + ; + } + System.out.println("!!!!!缁撴潫鐭伐浣�" + this.hashCode()); + } + @Override + public void destroy(){ + } + @Override + public boolean isDestroy(){ + return false ; + } + }); + n++ ; + Thread.sleep(100); + } + } catch (Exception e) { + + } + } + /** + * 娴嬭瘯澧炲姞绾跨▼绾跨▼ + * @throws Exception + * + */ + @SuppressWarnings("unused") + private void testShort2() throws Exception { + TreadPoolFactory.initThreadPoolShort("娴嬭瘯鐭伐浣滅嚎绋嬬粍", 2, 1, 5, 6); + ThreadPool.Pool pool = TreadPoolFactory.getThreadPoolShort() ; + try { + int n = 1 ; + Thread.sleep(1000); + while (n < 2) { + System.out.println("鍔犲叆鐭伐浣滀换鍔�(" + n + ")"); + pool.putJob(new ThreadPool.Job() { + @Override + public void execute() { + Long times = 0L ; + System.out.println("!!!!!寮�濮嬬煭宸ヤ綔" + this.hashCode()); + while(true){ + if(this.isDestroy == null || !this.isDestroy.booleanValue()){ + times++ ; + System.out.println("!!!!!鎵ц鐭伐浣�" + this.hashCode() + " 娆℃暟锛�" + times); + try { + Thread.sleep(2000); + } catch (Exception e) { + ; + } +// System.out.println("!!!!!缁撴潫闀垮伐浣�" + this.hashCode()); + } + } + } + @Override + public void destroy(){ + this.isDestroy = true ; + } + @Override + public boolean isDestroy(){ + if(this.isDestroy == null){ + return false ; + } + return this.isDestroy.booleanValue() ; + } + private Boolean isDestroy; + }); + n++ ; + Thread.sleep(100); + } + } catch (Exception e) { + + } + }/** + * 娴嬭瘯澧炲姞绾跨▼绾跨▼ + * @throws Exception + * + */ + @SuppressWarnings("unused") + private void testLong() throws Exception { + TreadPoolFactory.initThreadPoolLong("娴嬭瘯闀垮伐浣滅嚎绋嬬粍", -1, 1, 5, -1); + ThreadPool.Pool pool = TreadPoolFactory.getThreadPoolLong() ; + try { + int n = 1 ; + Thread.sleep(1000); + while (n < 2) { + System.out.println("鍔犲叆闀垮伐浣滀换鍔�(" + n + ")"); + pool.putJob(new ThreadPool.Job() { + @Override + public void execute() { + Long times = 0L ; + System.out.println("!!!!!寮�濮嬮暱宸ヤ綔" + this.hashCode()); + while(true){ + if(this.isDestroy == null || !this.isDestroy.booleanValue()){ + times++ ; + System.out.println("!!!!!鎵ц闀垮伐浣�" + this.hashCode() + " 娆℃暟锛�" + times); + try { + Thread.sleep(2000); + } catch (Exception e) { + ; + } +// System.out.println("!!!!!缁撴潫闀垮伐浣�" + this.hashCode()); + } + } + } + @Override + public void destroy(){ + this.isDestroy = true ; + } + @Override + public boolean isDestroy(){ + if(this.isDestroy == null){ + return false ; + } + return this.isDestroy.booleanValue() ; + } + private Boolean isDestroy; + }); + n++ ; + Thread.sleep(100); + } + } catch (Exception e) { + + } + } + +} diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/ThreadPool.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/ThreadPool.java new file mode 100644 index 0000000..84e72eb --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/ThreadPool.java @@ -0,0 +1,73 @@ +package com.dy.common.threadPool; + + +public interface ThreadPool { + + /** + * 绾跨▼姹� + * @author Administrator + * + */ + public interface Pool{ + /** + * 鎶婃墍瑕佹墽琛岀殑宸ヤ綔瀵硅薄瀹炰緥鏀惧叆绾跨▼姹犱腑 + * @param job ThreadJob 宸ヤ綔瀵硅薄瀹炰緥 + * @throws Exception + */ + public void putJob(Job job) throws Exception ; + } + + /** + * 绾跨▼姹犲伐浣滀换鍔� + * @author Administrator + * + */ + public interface Job{ + /** + * 绾跨▼姹犲伐浣滅被鐨勫洖璋冩柟娉曘�� + * 娉ㄦ剰锛� + * 1銆佽繖涓柟娉曞唴锛屽敖閲忎笉浣跨敤try catch璇彞锛屽鏋滅‘瀹為渶瑕佺敤锛� + * 瑕佷竴瀹氱敤try catch finnaly璇彞锛岃�屼笖finnaly鍐呬笉瑕� + * 鍐嶆湁鍙兘浜х敓寮傚父銆傚惁鍒欑嚎绋嬪緱涓嶅埌缁撴潫锛屼笉鑳藉洖褰掔┖闂茬嚎绋嬫睜銆� + * 2銆佸鏋滃湪鐭嚎绋嬫睜涓紝杩欎釜鏂规硶鍐呬笉鑳芥墽琛屾案寰幆宸ヤ綔锛屼緥濡傛湁while(true)杩欐牱 + * 鐨勫伐浣滀换鍔★紝鍚﹀垯鑷宠秴鏃舵椂闂达紝绯荤粺寮哄埗鍋滄姝ゅ伐浣溿�� + * 3銆佸鏋滃湪鐭嚎绋嬫睜涓紝杩欎釜鏂规硶鍐呭彧鑳芥墽琛岀煭鏃堕棿鍗虫垚瀹屾垚鐨勫伐浣滐紝宸ヤ綔瀹屾垚鍚庡嵆閫�鍑烘湰鏂规硶浣撱�� + * @throws Exception + */ + public void execute() throws Exception ; + + /** + * 澶栭儴璋冪敤锛屽己鍒堕攢姣佸伐浣� + * 涓昏搴旂敤锛� + * 濡傛灉鍦╡xecute()鏂规硶涓湁while(true){}姝诲惊鐜紝杩欑鎯呭喌涓嬶紝澶栭儴璋冪敤鑰呮病娉曞彨绾跨▼鍋滀笅鏉ョ殑锛� + * 濡傛灉澶栭儴璋冪敤鑰呴渶瑕佸仠姝㈡宸ヤ綔锛孞ob鎺ュ彛璁捐浜哾estroy鏂规硶锛屽閮ㄨ皟鐢ㄨ�呯敤job瀵硅薄璋冩鏂规硶瀹屾垚鍋滄宸ヤ綔銆� + * 杩欐椂闇�瑕佸湪while(true){}姝诲惊鐜腑鍒ゆ柇鏄惁澶栭儴璋冪敤鑰呰姹傚仠姝㈡墽琛宩ob + * 绀轰緥浠g爜锛� + * public void execute(){ + * while(true){ + * if(this.isDestroy){ + * break; + * } + * ... ... + * } + * } + * public void destroy(){ + * this.isDestroy = true ; + * } + * + * 褰撶劧鏈墂hile(true){}姝诲惊鐜殑宸ヤ綔job涓�瀹氬湪闀跨嚎绋嬫睜涓伐浣滐紝濡傛灉鍦ㄧ煭绾跨▼姹犱腑宸ヤ綔锛岀嚎绋嬫睜鐩戞帶绾跨▼灏变細 + * 浠ュ繖纰岃秴鏃跺師鍥犳妸绾跨▼寮哄埗閿�姣侊紙绾跨▼姹犵洃鎺х嚎绋嬭皟鐢ㄥ伐浣滅嚎绋嬬殑destroy鏂规硶锛夛紝杩欓噷鐨勯攢姣佸疄闄呬笂閿�姣佷笉浜� + * 鐨勶紝鍙笉鎶婅绾跨▼娓呬簡绾跨▼姹狅紝杩欎釜绾跨▼浠嶇劧鏄椿鐫�鐨勶紝 绾跨▼閲岀殑job浠嶇劧琚墽琛岋紝鎵�浠ュ湪绾跨▼鐨刣estroy鏂规硶 + * 涓皟鐢ㄤ簡job.destroy()锛屼娇job鍋滄涓嬫潵锛岀嚎绋嬩篃鑷劧鎵ц瀹屾瘯鑰屽緱浠ュ仠姝紝浠ヤ笂鏄唬鐮侀�昏緫瀛樺湪while(true){} + * 姝诲惊鐜紝濡傛灉涓嶆槸浠g爜閫昏緫锛岃�屾槸绋嬪簭bug閫犳垚鐨勬寰幆锛屾垨绋嬪簭鎶涘嚭浜嗗紓甯镐笖鏈姄浣忓紓甯革紝杩欐椂鏃犺濡備綍涔熷仠姝� + * 涓嶄簡宸ヤ綔job锛屼篃閿�姣佷笉浜嗚繖涓嚎绋嬬殑 + */ + public void destroy() ; + + /** + * 鍒ゆ柇锛屽伐浣滄槸鍚﹁澶栭儴寮哄埗閿�姣侊紝閿�姣佸悗锛屾寔鏈夋湰job鐨勭嚎绋嬪氨鑳芥柦鏀惧洖褰� + */ + public boolean isDestroy() ; + + } +} diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/ThreadPoolImp.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/ThreadPoolImp.java new file mode 100644 index 0000000..204d758 --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/ThreadPoolImp.java @@ -0,0 +1,442 @@ +package com.dy.common.threadPool; + +import java.util.*; + +import org.apache.logging.log4j.*; + +import com.dy.common.threadPool.ThreadPool.Job; + +public class ThreadPoolImp { + + /** + * 绾跨▼姹犲疄鐜� + * @author Administrator + * + */ + public class MyThreadPool implements ThreadPool.Pool{ + + /** + * 绾跨▼姹犲敮涓�瀹炰緥 + */ + public MyThreadPool myPool ; + + /** + * 绾跨▼姹犲悕绉帮紱 + */ + private String poolName ; + /** + * 绌洪棽绾跨▼闆嗗悎 + */ + private List<MyThread> freeThreads; + + /** + * 宸ヤ綔涓殑绾跨▼闆嗗悎 + */ + private List<MyThread> busiThreads; + + /** + * 绾跨▼姹犳渶澶х嚎绋嬫暟 , 鑻axNum <= 0锛屽垯maxNum = 1 銆� + */ + private int maxNum; + + /** + * 鏈�灏忕嚎绋嬫暟锛岃嫢minNum > maxNum锛屽垯minNum = maxNum 銆� + */ + private int minNum; + + /** + * 褰撳墠绾跨▼姹犱腑鐨勭嚎绋嬫暟銆� + */ + private int currNum; + + /** + * 绌洪棽绾跨▼瓒呮椂鐨勬椂闀�(绉�)锛岃秴杩囪繖涓椂闂达紝灏辨竻闄ゅ浣欑嚎绋嬶紝 + * 浣跨┖闂茬嚎绋嬫渶澶氭槸minNum涓� + */ + private long freeTimeout ; + + /** + * 蹇欑绾跨▼瓒呮椂鐨勬椂闀�(绉�)锛岃秴杩囪繖涓椂闂达紝灏辫涓烘槸宕╂簝绾跨▼锛屽皢琚竻闄ゃ�� + */ + private long busyTimeout ; + + /** + * 鍚屾瀵硅薄 + */ + private Object synObj = new Object(); + + /** + * 鍐呴儴鍚姩鐨勭┖闂插拰蹇欑瓒呮椂绾跨▼鐨勭嚎绋� + */ + private Thread monitorThread ; + + /** + * 鏃ュ織 + */ + private Logger log = LogManager.getLogger(MonitorThread.class.getName()); + + /** + * 寰楀埌榛樿鍞竴瀹炰緥 + * @return + */ +// public MyThreadPool getDefaultInstance(){ +// if(myPool == null){ +// myPool = new MyThreadPool(null, null, null, null, null) ; +// } +// return myPool ; +// } + /** + * 寰楀埌鍞竴瀹炰緥 + * @param poolName + * @param maxNum + * @param minNum + * @param freeTimeout + * @param busyTimeout + * @return + */ +// public MyThreadPool getInstance( +// String poolName , +// Integer maxNum , +// Integer minNum , +// Long freeTimeout , +// Long busyTimeout){ +// if(myPool == null){ +// myPool = new MyThreadPool(poolName, maxNum, minNum, freeTimeout, busyTimeout) ; +// } +// return myPool ; +// } + + /** + * 绾跨▼姹犳瀯閫犳柟娉� + * @param poolName 绾跨▼姹犲拰绾跨▼鍚嶇О + * @param maxNum 绾跨▼姹犳渶澶х嚎绋嬫暟锛岃嫢涓�-1锛屼笉鍙楅檺鍒� + * @param minNum 绾跨▼姹犳渶灏忕嚎绋嬫暟锛屾垨鍒濆绾跨▼鏁� + * @param freeTimeout 绌洪棽绾跨▼瓒呮椂鏃堕暱(绉�) + * @param busyTimeout 蹇欑绾跨▼瓒呮椂鏃堕暱(绉�)锛岃嫢涓�-1锛屼笉鍙楅檺鍒� + */ + public MyThreadPool( + String poolName , + Integer maxNum , + Integer minNum , + Long freeTimeout , + Long busyTimeout) { + if(poolName == null){ + poolName="绾跨▼姹�" ; + } + this.poolName = poolName ; + + if(maxNum == null || maxNum.intValue() <= 0){ + maxNum = -1 ; + } + if(minNum == null || minNum.intValue() < 0){ + minNum = 0 ; + } + if(minNum > maxNum){ + minNum = maxNum ; + } + this.maxNum = maxNum ; + this.minNum = minNum ; + this.currNum = 0; + + if(freeTimeout == null || freeTimeout.longValue() <= 0){ + freeTimeout = 60000L ; + } + this.freeTimeout = freeTimeout ; + + if(busyTimeout == null || busyTimeout.longValue() <= 0){ + this.busyTimeout = -1L ; + }else{ + this.busyTimeout = busyTimeout ; + } + + this.busiThreads = new ArrayList<MyThread>(); + this.freeThreads = new ArrayList<MyThread>(); + + //鏈�灏忓寲绾跨▼姹� + for (int i = 0; i < this.minNum ; i++) { + MyThread t = new MyThread(this); + t.start(); + this.freeThreads.add(t); + this.currNum++; + } + + this.monitorThread = new MonitorThread(this) ; + this.monitorThread.start() ; + } + + /** + * 鎶婃墍瑕佹墽琛岀殑宸ヤ綔瀵硅薄瀹炰緥鏀惧叆绾跨▼姹犱腑 + * @param job ThreadJob 宸ヤ綔瀵硅薄瀹炰緥 + * @throws Exception + */ + @Override + public void putJob(Job job) throws Exception { + synchronized(this.synObj) { + //log.debug("宸ヤ綔浠诲姟鍒嗛厤鍒扮嚎绋嬫睜涓��") ; + MyThread t = null ; + if (this.freeThreads.size() == 0) { + //褰撳墠娌℃湁绌洪棽绾跨▼ + if (this.maxNum == -1 || this.currNum < this.maxNum) { + //褰撳墠绾跨▼鏁版湭杈惧埌鏈�澶у�� , 澧炲姞鏂扮殑绾跨▼ + t = new MyThread(this); + t.start(); + this.currNum++; + } else { + //褰撳墠绾跨▼杈惧埌鏈�澶ф暟浜嗭紝绛夊緟鍥炲綊绾跨▼ + while (freeThreads.size() == 0) { + //濡傛灉娌℃湁绌洪棽鐨勭嚎绋�,绛夊緟宸ヤ綔绾跨▼宸ヤ綔瀹屾垚鍥炴潵 + try { + //log.warn("绾跨▼姹�(" + this.poolName + ")涓嚎绋嬫暟杈惧埌涓婇檺锛屾柊宸ヤ綔浠诲姟绛夊緟閲婃斁绾跨▼!"); + synObj.wait(); + } catch (Exception e) { + log.error("'" + this.poolName + "'绾跨▼姹犱腑绾跨▼绛夊緟閲婃斁绾挎椂鍙戠敓绛夊緟寮傚父!", e); + } + t = (MyThread) freeThreads.get(0); + if (t != null) { + //璇存槑寰楀埌浜嗛噴鏀惧洖鏉ョ殑绾跨▼ + freeThreads.remove(0); + break; + }else{ + //璇存槑娌℃湁寰楀埌閲婃斁鍥炴潵鐨勭嚎绋嬶紝鍙互鍏朵粬绾跨▼ + continue; + } + }//end while + }//end else + }//end if(freeThreads.size() == 0) + else { + t = (MyThread) freeThreads.get(0); + freeThreads.remove(0); + } + busiThreads.add(t); + t.putJob(job); + } + } + + /** + * 绾跨▼宸ヤ綔瀹屾垚锛屼粠busiThreads鍥炲綊freeThreads + */ + protected void freeThread(MyThread t) { + synchronized (synObj) { + busiThreads.remove(t); + freeThreads.add(t); + synObj.notify(); + } + } + + /** + * 鐩戞帶瓒呮椂绾跨▼鐨勭嚎绋� + * @author Administrator + * + */ + protected class MonitorThread extends Thread { + private MyThreadPool pool ; + + private MyThread t ; + private Iterator<?> it ; + + /** + * + * @param pool + */ + public MonitorThread(MyThreadPool pool){ + this.pool = pool ; + } + /** + * + */ + @SuppressWarnings("finally") + public void run(){ + long interval = pool.freeTimeout ; + if(pool.busyTimeout > 0 && pool.busyTimeout < pool.freeTimeout){ + interval = pool.busyTimeout ; + } + boolean isException = false ; + while(true){ + t = null ; + it = null ; + try{ + MonitorThread.sleep(interval) ; + }catch(Exception e){ + isException = true ; + }finally{ + if(isException){ + isException = false ; + continue ; + } + } + try{ + synchronized (pool.synObj) { + if(pool.freeThreads.size() > pool.minNum){ + //濡傛灉绌洪棽绾跨▼澶т簬鏈�灏忕嚎绋嬫暟锛屽垯娓呯悊绌洪棽绾跨▼ + int num = pool.freeThreads.size() - pool.minNum ; + int count = 0 ; + it = pool.freeThreads.iterator() ; + while(it.hasNext()){ + if(count == num) { + break ; + } + count ++ ; + t = (MyThread)it.next() ; + if((System.currentTimeMillis() - t.time) >= pool.freeTimeout){ + it.remove() ; + pool.currNum-- ; + //log.debug("绾跨▼姹�(" + pool.poolName + ")涓竻闄や簡涓�涓秴鏃剁┖闂茬嚎绋�!"); + t.destroy() ; + t = null ; + } + } + }//end if + + if(pool.busyTimeout != -1 && pool.busyTimeout > 0){ + it = pool.busiThreads.iterator() ; + while(it.hasNext()){ + t = (MyThread)it.next() ; + if((System.currentTimeMillis() - t.time) >= pool.busyTimeout){ + it.remove() ; + pool.currNum-- ; + log.error("绾跨▼姹�(" + pool.poolName + ")涓竻闄や簡涓�涓秴鏃跺穿婧�(蹇欑)绾跨▼!"); + t.destroy() ; + t = null ; + } + } + } + }//end synchronized (pool.synObj) + }catch(Exception e){ + }finally{ + continue ; + } + }//end while(true) + } + } + + + } + + /** + * 姹犱腑绾跨▼瀹炵幇 + * @author Administrator + * + */ + public class MyThread extends Thread{ + + /** + * 鏍囩ず绾跨▼鏄椿鐨� + */ + private boolean living = true ; + + /** + * 绾跨▼蹇欑鎴栫┖闂茶鏃跺櫒 + */ + protected long time ; + + /** + * 褰撳墠绾跨▼鎵�澶勭殑绾跨▼姹� + */ + private MyThreadPool pool ; + /** + * 绾跨▼鍏蜂綋宸ヤ綔鐨勫洖璋冪被 + */ + private Job job ; + + /** + * 鎸囩ず绾跨▼鍙互宸ヤ綔 + */ + private Boolean canJob ; + + private Logger log = LogManager.getLogger(MyThread.class.getName()) ; + + protected MyThread(MyThreadPool pool) { + super(); + this.pool = pool ; + this.time = 0 ; + this.canJob = false ; + } + + /** + * 璁剧疆绾跨▼宸ヤ綔瀵硅薄 + * @param job + */ + protected void putJob(Job job) throws Exception { + if(job == null){ + this.job = new Job(){ + public void execute(){ + } + public void destroy(){ + } + public boolean isDestroy(){ + return false ; + } + }; + } + synchronized (this) { + this.job = job ; + this.canJob = true; + //蹇欑璁版椂寮�濮� + this.time = System.currentTimeMillis() ; + this.notify(); + } + } + + /** + * + */ + @SuppressWarnings("finally") + @Override + public void run(){ + while (living){ + synchronized (this){ + while(!canJob){ + //褰撲笉鑳藉伐浣滄椂 + try{ + this.wait(); + }catch (Exception e) { + log.error("绾跨▼姹�(" + pool.poolName + ")鐨勫伐浣滅嚎绋嬬瓑寰呭彲浠ュ伐浣滅殑淇″彿鏃跺彂鐢熺瓑寰呭紓甯�:\n" + e.getMessage(), e); + this.canJob = false ; + continue; + } + } + /////////////////////// + //琚敜閱掞紝鏈夋柊宸ヤ綔浜� + try{ + if(this.job != null){ + this.job.execute() ; + } + }catch (Exception ee) { + log.error("绾跨▼姹�(" + pool.poolName + ")鐨勫伐浣滅嚎绋嬪湪鎵ц宸ヤ綔鏃跺彂鐢熷紓甯�:\n" + ee.getMessage(), ee); + //ee.printStackTrace() ; + }finally { + this.canJob = false ; + this.job = null ; + if(living){ + //绾跨▼鏈閿�姣� + this.free() ; + } + continue; + } + }// end synchronized(this) + }// end while(living) + } + + public void free(){ + //浣挎湰绾跨▼鍥炲綊绌洪棽绾跨▼姹� + pool.freeThread(this); + //绌洪棽寮�濮嬭鏃� + this.time = System.currentTimeMillis() ; + // 娌℃湁鍙仛鐨勪簡 + this.canJob = false; + log.debug("绾跨▼姹�(" + this.pool.poolName + ")涓殑绾跨▼鍥炲綊绌洪棽闆嗗悎銆�"); + } + + /** + * 閿�姣佸伐浣滅嚎绋� + */ + public void destroy() { + //绾跨▼閿�姣佸墠锛岄鍏堟妸鍏舵墍鎸佹湁鐨勫伐浣滈攢姣侊紝鍚﹀垯绾跨▼鍋滀笉涓嬫潵锛屼篃閿�姣佷笉浜� + if(this.job != null){ + this.job.destroy(); + } + this.living = false ;//浣跨嚎绋嬩粠run鏂规硶鐨勯暱涔厀hile(living)涓��鍑烘潵锛屼粠绾跨▼鑷姩閿�姣� + this.job = null ; + } + } +} diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/TreadPoolFactory.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/TreadPoolFactory.java new file mode 100644 index 0000000..040913d --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/threadPool/TreadPoolFactory.java @@ -0,0 +1,79 @@ +package com.dy.common.threadPool; + + +public class TreadPoolFactory { + + private static ThreadPool.Pool pool_short ;//鐭伐浣滀换鍔$嚎绋嬫睜锛岀嚎绋嬪伐浣滅敤鏃惰緝鐭� + private static ThreadPool.Pool pool_long ;//闀垮伐浣滀换鍔$嚎绋嬫睜锛岀嚎绋嬪伐浣滅敤鏃惰緝闀� + + /** + * 鍒濆鍖栫嚎绋嬫睜 + * @param threadPoolName 绾跨▼姹犲拰绾跨▼鍚嶇О + * @param maxThreadNum 绾跨▼姹犳渶澶х嚎绋嬫暟 锛岃嫢涓�-1锛屼笉鍙楅檺鍒� + * @param minThreadNum 绾跨▼姹犳渶灏忕嚎绋嬫暟锛屾垨鍒濆绾跨▼鏁� + * @param freeTimeout 绌洪棽绾跨▼瓒呮椂鏃堕暱(绉�) + * @param busyTimeout 蹇欑绾跨▼瓒呮椂鏃堕暱(绉�)锛岃嫢涓�-1锛屼笉鍙楅檺鍒� + * @return 绾跨▼姹犲疄渚� + */ + public final static void initThreadPoolShort(String poolName, + int maxNum, + int minNum, + long freeTimeout, + long busyTimeout) throws Exception { + if(pool_short!= null){ + throw new Exception("绾跨▼姹犱笉鑳介噸澶嶅垵濮嬪寲锛�"); + } + if(pool_short == null){ + pool_short = new ThreadPoolImp().new MyThreadPool(poolName, maxNum, minNum, freeTimeout, busyTimeout); + } + } + + /** + * 鍒濆鍖栫嚎绋嬫睜 + * @param threadPoolName 绾跨▼姹犲拰绾跨▼鍚嶇О + * @param maxThreadNum 绾跨▼姹犳渶澶х嚎绋嬫暟锛岃嫢涓�-1锛屼笉鍙楅檺鍒� + * @param minThreadNum 绾跨▼姹犳渶灏忕嚎绋嬫暟锛屾垨鍒濆绾跨▼鏁� + * @param freeTimeout 绌洪棽绾跨▼瓒呮椂鏃堕暱(绉�) + * @param busyTimeout 蹇欑绾跨▼瓒呮椂鏃堕暱(绉�)锛岃嫢涓�-1锛屼笉鍙楅檺鍒� + * @return 绾跨▼姹犲疄渚� + */ + public final static void initThreadPoolLong(String poolName, + int maxNum, + int minNum, + long freeTimeout, + long busyTimeout) throws Exception { + if(pool_long!= null){ + throw new Exception("绾跨▼姹犱笉鑳介噸澶嶅垵濮嬪寲锛�"); + } + if(pool_long == null){ + pool_long = new ThreadPoolImp().new MyThreadPool(poolName, maxNum, minNum, freeTimeout, busyTimeout); + } + } + /** + * 寰楀埌鍞竴绾跨▼姹犲疄渚� + * @param dataSourceName + * @return + * @throws Exception + */ + public final static ThreadPool.Pool getThreadPoolShort() + throws Exception { + if (pool_short == null) { + throw new Exception("寰楀埌绾跨▼姹犲墠棣栧厛蹇呴』鍒濆鍖栵紒"); + } + return pool_short ; + } + /** + * 寰楀埌鍞竴绾跨▼姹犲疄渚� + * @param dataSourceName + * @return + * @throws Exception + */ + public final static ThreadPool.Pool getThreadPoolLong() + throws Exception { + if (pool_long == null) { + throw new Exception("寰楀埌绾跨▼姹犲墠棣栧厛蹇呴』鍒濆鍖栵紒"); + } + return pool_long ; + } + +} diff --git a/pipIrr-platform/pipIrr-global/src/main/resources/mapper/BaClientMapper.xml b/pipIrr-platform/pipIrr-global/src/main/resources/mapper/BaClientMapper.xml index 0a73af3..c5a0f21 100644 --- a/pipIrr-platform/pipIrr-global/src/main/resources/mapper/BaClientMapper.xml +++ b/pipIrr-platform/pipIrr-global/src/main/resources/mapper/BaClientMapper.xml @@ -19,8 +19,8 @@ <result column="district" jdbcType="VARCHAR" property="district" /> <result column="address" jdbcType="VARCHAR" property="address" /> <result column="remark" jdbcType="VARCHAR" property="remark" /> - <result column="disabled" jdbcType="TINYINT" property="disabled" /> - <result column="deleted" jdbcType="TINYINT" property="deleted" /> + <result property="disabled" column="disabled" typeHandler="com.dy.common.mybatis.envm.EnumCodeTypeHandler" javaType="com.dy.common.mybatis.envm.Disabled"/> + <result property="deleted" column="deleted" typeHandler="com.dy.common.mybatis.envm.EnumCodeTypeHandler" javaType="com.dy.common.mybatis.envm.Deleted"/> </resultMap> <sql id="Base_Column_List"> <!--@mbg.generated--> @@ -29,8 +29,8 @@ </sql> <sql id="part_Column_List"> <!--@mbg.generated--> - id, countyId, townId, villageId, blockId, divideId, typeId, `name`, num, phone, idCard, area, district,address, remark, - disabled + ${alias}.id, ${alias}.countyId, ${alias}.townId, ${alias}.villageId, ${alias}.blockId, ${alias}.divideId, ${alias}.typeId, ${alias}.name, ${alias}.num, ${alias}.phone, ${alias}.idCard, ${alias}.area, ${alias}.district,${alias}.address, ${alias}.remark, + ${alias}.disabled </sql> <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap"> @@ -49,38 +49,46 @@ <select id="selectTotal" parameterType="java.util.Map" resultType="java.lang.Long"> select count(*) - from ba_client - where deleted != 1 + from ba_client bc + left join ba_block bbk on bc.blockId = bbk.id + left join ba_client_type bct on bc.typeId = bct.id + where bc.deleted != 1 <trim prefix="and" suffixOverrides="and"> <if test="countyId != null"> - countyId = #{countyId,jdbcType=BIGINT} and + bc.countyId = #{countyId,jdbcType=BIGINT} and </if> <if test="townId != null"> - townId = #{townId,jdbcType=BIGINT} and + bc.townId = #{townId,jdbcType=BIGINT} and </if> <if test="villageId != null"> - villageId = #{villageId,jdbcType=BIGINT} and + bc.villageId = #{villageId,jdbcType=BIGINT} and </if> <if test="blockId != null"> - blockId = #{blockId,jdbcType=BIGINT} and + bc.blockId = #{blockId,jdbcType=BIGINT} and </if> <if test="name != null"> - name like concat('%', #{name}, '%') and + bc.name like concat('%', #{name}, '%') and </if> <if test="num != null"> - num = '#{num,jdbcType=VARCHAR}' and + bc.num = '#{num,jdbcType=VARCHAR}' and </if> <if test="phone != null"> - phone = '#{phone,jdbcType=VARCHAR}' and + bc.phone = '#{phone,jdbcType=VARCHAR}' and </if> </trim> </select> <select id="selectSome" parameterType="java.util.Map" resultMap="BaseResultMap"> <!--@mbg.generated--> select - <include refid="part_Column_List" /> - from ba_client - where deleted != 1 + <include refid="part_Column_List" > + <property name="alias" value="bc"/> + </include>, + bbk.name blockName, + bct.name typeName + from ba_client bc + left join ba_block bbk on bc.blockId = bbk.id + left join ba_client_type bct on bc.typeId = bct.id + where bc.deleted != 1 <trim prefix="and" suffixOverrides="and"> <if test="countyId != null"> countyId = #{countyId,jdbcType=BIGINT} and @@ -95,16 +103,16 @@ blockId = #{blockId,jdbcType=BIGINT} and </if> <if test="name != null"> - name like concat('%', #{name}, '%') and + bc.name like concat('%', #{name}, '%') and </if> <if test="num != null"> - num = '#{num,jdbcType=VARCHAR}' and + bc.num = '#{num,jdbcType=VARCHAR}' and </if> <if test="phone != null"> - phone = '#{phone,jdbcType=VARCHAR}' and + bc.phone = '#{phone,jdbcType=VARCHAR}' and </if> </trim> - order by id DESC + order by bc.id DESC <trim prefix="limit " > <if test="start != null and count != null"> #{start}, #{count} -- Gitblit v1.8.0