-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from kingschan1204/develop
第二版正式发布
- Loading branch information
Showing
49 changed files
with
2,095 additions
and
1,035 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/io.github.kingschan1204.istock/common/conf/MongoConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.github.kingschan1204.istock.common.conf; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.mongodb.MongoDbFactory; | ||
import org.springframework.data.mongodb.core.convert.*; | ||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext; | ||
|
||
/** | ||
* 去掉mongo 保存_class字段 | ||
* @author chenguoxiang | ||
* @create 2018-07-09 16:54 | ||
**/ | ||
@Configuration | ||
public class MongoConfig { | ||
private static Logger log = LoggerFactory.getLogger(MongoConfig.class); | ||
@Bean | ||
public MappingMongoConverter mappingMongoConverter(MongoDbFactory factory, MongoMappingContext context, BeanFactory beanFactory) { | ||
DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory); | ||
MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context); | ||
try { | ||
mappingConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class)); | ||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
log.error("{}",ex); | ||
} | ||
|
||
// Don't save _class to mongo | ||
mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null)); | ||
return mappingConverter; | ||
} | ||
} |
29 changes: 0 additions & 29 deletions
29
src/main/java/io.github.kingschan1204.istock/common/conf/ScheduleConfig.java
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
src/main/java/io.github.kingschan1204.istock/common/util/cache/EhcacheUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.github.kingschan1204.istock.common.util.cache; | ||
|
||
import net.sf.ehcache.Cache; | ||
import net.sf.ehcache.Element; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.cache.ehcache.EhCacheCacheManager; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* | ||
* @author chenguoxiang | ||
* @create 2018-07-10 15:02 | ||
**/ | ||
@Component | ||
public class EhcacheUtil { | ||
|
||
@Autowired | ||
EhCacheCacheManager ehCacheCacheManager; | ||
|
||
/** | ||
* 得到一个缓存的值 | ||
* @param cacheName 缓存名字 | ||
* @param key | ||
* @return | ||
*/ | ||
public Object getKey(String cacheName,String key){ | ||
Element em =ehCacheCacheManager.getCacheManager().getCache(cacheName).get(key); | ||
return null==em?null:em.getObjectValue(); | ||
} | ||
|
||
/** | ||
* 向缓存中写入一个key | ||
* @param cacheName | ||
* @param key | ||
* @param value | ||
*/ | ||
public void addKey(String cacheName,String key,Object value){ | ||
ehCacheCacheManager.getCacheManager().getCache(cacheName).put(new Element(key,value)); | ||
} | ||
|
||
/** | ||
* 得到一个缓存里key 的数量 | ||
* @param cacheName | ||
* @return | ||
*/ | ||
public int getCacheTotalKeys(String cacheName){ | ||
Cache cache=ehCacheCacheManager.getCacheManager().getCache(cacheName); | ||
return null==cache?0:cache.getKeys().size(); | ||
} | ||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
src/main/java/io.github.kingschan1204.istock/common/util/quartz/QuartzManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package io.github.kingschan1204.istock.common.util.quartz; | ||
|
||
import org.quartz.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.scheduling.quartz.SchedulerFactoryBean; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* quartz 任务管理工具类 | ||
* | ||
* @author chenguoxiang | ||
* @create 2018-07-13 14:46 | ||
**/ | ||
@Component | ||
public class QuartzManager { | ||
private static Logger log = LoggerFactory.getLogger(QuartzManager.class); | ||
@Autowired | ||
SchedulerFactoryBean schedulerFactoryBean; | ||
|
||
/** | ||
* @param jobName 任务名 | ||
* @param jobGroupName 任务组名 | ||
* @param triggerName 触发器名 | ||
* @param triggerGroupName 触发器组名 | ||
* @param jobClass 任务 | ||
* @param cron 时间设置,参考quartz说明文档 | ||
* @Description: 添加一个定时任务 | ||
*/ | ||
public void addJob(String jobName, String jobGroupName, | ||
String triggerName, String triggerGroupName, Class jobClass, String cron) { | ||
try { | ||
Scheduler sched = schedulerFactoryBean.getScheduler();//schedulerFactory.getScheduler(); | ||
|
||
// 任务名,任务组,任务执行类 | ||
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build(); | ||
// 触发器 | ||
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger(); | ||
// 触发器名,触发器组 | ||
triggerBuilder.withIdentity(triggerName, triggerGroupName); | ||
triggerBuilder.startNow(); | ||
// 触发器时间设定 | ||
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron)); | ||
// 创建Trigger对象 | ||
CronTrigger trigger = (CronTrigger) triggerBuilder.build(); | ||
// 调度容器设置JobDetail和Trigger | ||
sched.scheduleJob(jobDetail, trigger); | ||
// 启动 | ||
if (!sched.isShutdown()) { | ||
log.info("添加定时任务{} , class:{} 执行频率:{}", jobName, jobClass.getName(), cron); | ||
sched.start(); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* 修改一个任务的触发时间 | ||
* | ||
* @param jobName | ||
* @param jobGroupName | ||
* @param triggerName 触发器名 | ||
* @param triggerGroupName 触发器组名 | ||
* @param cron 时间设置,参考quartz说明文档 | ||
* @Description: 修改一个任务的触发时间 | ||
*/ | ||
public void modifyJobTime(String jobName, | ||
String jobGroupName, String triggerName, String triggerGroupName, String cron) { | ||
try { | ||
Scheduler sched = schedulerFactoryBean.getScheduler(); | ||
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName); | ||
CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey); | ||
if (trigger == null) { | ||
return; | ||
} | ||
|
||
String oldTime = trigger.getCronExpression(); | ||
if (!oldTime.equalsIgnoreCase(cron)) { | ||
/** 方式一 :调用 rescheduleJob 开始 */ | ||
// 触发器 | ||
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger(); | ||
// 触发器名,触发器组 | ||
triggerBuilder.withIdentity(triggerName, triggerGroupName); | ||
triggerBuilder.startNow(); | ||
// 触发器时间设定 | ||
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron)); | ||
// 创建Trigger对象 | ||
trigger = (CronTrigger) triggerBuilder.build(); | ||
// 方式一 :修改一个任务的触发时间 | ||
sched.rescheduleJob(triggerKey, trigger); | ||
log.info("添加定时任务{} , 执行频率:{}", jobName, cron); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* 删除一个job | ||
* | ||
* @param jobName | ||
* @param jobGroupName | ||
* @param triggerName | ||
* @param triggerGroupName | ||
* @Description: 移除一个任务 | ||
*/ | ||
public void removeJob(String jobName, String jobGroupName, | ||
String triggerName, String triggerGroupName) { | ||
try { | ||
Scheduler sched = schedulerFactoryBean.getScheduler(); | ||
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName); | ||
// 停止触发器 | ||
sched.pauseTrigger(triggerKey); | ||
// 移除触发器 | ||
sched.unscheduleJob(triggerKey); | ||
// 删除任务 | ||
sched.deleteJob(JobKey.jobKey(jobName, jobGroupName)); | ||
log.info("删除定时任务{}", jobName); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @Description:启动所有定时任务 | ||
*/ | ||
public void startJobs() { | ||
try { | ||
Scheduler sched = schedulerFactoryBean.getScheduler(); | ||
sched.start(); | ||
log.info("启动所有任务"); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* @Description:关闭所有定时任务 | ||
*/ | ||
public void shutdownJobs() { | ||
try { | ||
Scheduler sched = schedulerFactoryBean.getScheduler(); | ||
if (!sched.isShutdown()) { | ||
sched.shutdown(); | ||
log.info("关闭所有任务"); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* 暂停全部任务 | ||
* | ||
* @throws SchedulerException | ||
*/ | ||
public void pauseAll() throws Exception { | ||
schedulerFactoryBean.getScheduler().pauseAll(); | ||
log.info("暂停所有任务"); | ||
} | ||
|
||
|
||
/** | ||
* 恢复所有任务 | ||
* | ||
* @throws Exception | ||
*/ | ||
public void resumeAll() throws Exception { | ||
schedulerFactoryBean.getScheduler().resumeAll(); | ||
log.info("恢复所有任务"); | ||
} | ||
} |
Oops, something went wrong.