Skip to content

Commit

Permalink
Merge pull request #7 from kingschan1204/develop
Browse files Browse the repository at this point in the history
第二版正式发布
  • Loading branch information
kingschan1204 authored Aug 7, 2018
2 parents beb421c + 145c1f1 commit a3dd88c
Show file tree
Hide file tree
Showing 49 changed files with 2,095 additions and 1,035 deletions.
19 changes: 17 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>io.github.kingschan1204</groupId>
<artifactId>istock</artifactId>
<packaging>jar</packaging>
<version>1.5.7</version>
<version>1.6.3</version>
<name>pritice Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 继承父包 -->
Expand Down Expand Up @@ -110,7 +110,22 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

<!--开启 cache 缓存-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- ehcache 缓存 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<!--定时任务-->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
Expand Down
Binary file modified readme-res/stock-list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions src/main/java/io.github.kingschan1204.istock/Application.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package io.github.kingschan1204.istock;

import io.github.kingschan1204.istock.module.startup.InitQuartzTaskRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@EnableCaching
@SpringBootApplication
public class Application {

Expand All @@ -16,6 +20,11 @@ public String index() {
return "index";
}

@Bean
public InitQuartzTaskRunner startupRunner() {
return new InitQuartzTaskRunner();
}

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
Expand Down
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;
}
}

This file was deleted.

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();
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@ public class FileCommonOperactionTool {
/**
* 通过指定的文件下载URL以及下载目录下载文件
* @param url 下载url路径
* @referrer 来源
* @param dir 存放目录
* @param filename 文件名
* @throws Exception
*/
public static String downloadFile(String url, String dir, String filename) throws Exception {
public static String downloadFile(String url,String referrer, String dir, String filename) throws Exception {
log.info("start download file :{}",url);
//Open a URL Stream
Connection.Response resultResponse = Jsoup.connect(url)
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3346.9 Safari/537.36")
.referrer(referrer)
.ignoreContentType(true).execute();
String defaultFileName="";
if(resultResponse.statusCode()!=200){
log.error("文件下载失败:{}",url);
throw new Exception(String.format("文件下载失败:%s 返回码:%s",url,resultResponse.statusCode()));
}
if(resultResponse.contentType().contains("name")){
String[] list =resultResponse.contentType().split(";");
defaultFileName = Arrays.stream(list)
Expand All @@ -43,10 +49,12 @@ public static String downloadFile(String url, String dir, String filename) throw
out.write(resultResponse.bodyAsBytes());
}catch (Exception ex){
log.error("{}",ex);
log.error("文件下载失败:{}",url);
ex.printStackTrace();
}finally {
out.close();
}
return path;
}

}
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("恢复所有任务");
}
}
Loading

0 comments on commit a3dd88c

Please sign in to comment.