initial commit: lottery 项目集(三工程统一管理)
- apps/lotteryServer, lotteryCapture, lotteryOpen 三个业务工程 - 忽略 archive/, tools/, infra/data/ 大目录与敏感配置文件 - 移除 lotteryServer 原嵌套 git,并入顶层统一管理
This commit is contained in:
+37
@@ -0,0 +1,37 @@
|
||||
# ===== 归档压缩包(无需跟踪,222MB)=====
|
||||
archive/
|
||||
|
||||
# ===== 工具链 / 本地运行环境(472MB)=====
|
||||
tools/
|
||||
|
||||
# ===== 容器数据卷(244MB:MySQL/Redis 实数据)=====
|
||||
infra/data/
|
||||
|
||||
# ===== IDE / 编辑器 =====
|
||||
.idea/
|
||||
.vscode/
|
||||
*.iml
|
||||
|
||||
# ===== Maven / 编译产物 =====
|
||||
target/
|
||||
*.class
|
||||
*.jar
|
||||
*.war
|
||||
*.log
|
||||
|
||||
# ===== 敏感配置文件(数据库/Redis/支付连接凭据,杜绝外泄)=====
|
||||
**/config/datasource/*
|
||||
**/db.properties
|
||||
**/db.properties.bak
|
||||
**/db2.properties.temp
|
||||
**/redis.properties
|
||||
**/payapi.properties
|
||||
|
||||
# ===== 临时 / 备份文件 =====
|
||||
*.bak
|
||||
*.temp
|
||||
*.tmp
|
||||
|
||||
# ===== 系统文件 =====
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
@@ -0,0 +1,99 @@
|
||||
# nine_surplus 升级方案:Spring Boot 化 + 可执行 Jar + JDK 升级
|
||||
|
||||
## Context(背景与目标)
|
||||
|
||||
`nine_surplus`(11.1 万行、900 个 Java 文件)目前是一个基于 **Spring 4.1 + SpringMVC + Hibernate 4.2 + javax.servlet/JSP + 外部 Tomcat 8.5** 的 Web 应用,技术栈停留在 2013 年。当前部署方式是打成 `lotteryServer.war` 丢进 `apache-tomcat-8.5.100`,且此项目还在本机用 JDK8 运行。
|
||||
|
||||
用户明确诉求:
|
||||
1. **集成 Spring Boot**,引入内嵌 Tomcat
|
||||
2. **打包成可一键执行 `java -jar` 的 jar**,不再依赖外部 Tomcat
|
||||
3. **JDK 升级到 11/17**
|
||||
4. 本次只升级 `nine_surplus`,另外两个模块(lotteryCapture/lotteryOpen)不动
|
||||
|
||||
推荐技术选型(已和用户目标对齐):
|
||||
- **Spring Boot 2.7.x**(Spring 5.3 + javax.servlet 命名空间,与现有 `javax.*` 代码完全兼容,避免 11 万行改名)
|
||||
- **Hibernate 5.6.x**(Boot 2.7 配套,替换已从 spring-orm 移除的 hibernate4 支持)
|
||||
- **内嵌 Tomcat 9.x**(由 spring-boot-starter-web 提供)
|
||||
- **JDK 11**(先升 11 稳妥运行,17 作为后续可选)
|
||||
- **JSP/HTML 页面与现有 Spring XML 配置尽量保留**,降低改动面
|
||||
|
||||
> 注意:不采用 Spring Boot 3(jakarta 命名空间),否则全部 `javax.*` 要改名,风险不可控。
|
||||
|
||||
---
|
||||
|
||||
## 升级要点(改造点梳理)
|
||||
|
||||
### 1. 保留不动的内容(大块存量)
|
||||
- **59 个 JSP + 119 个 HTML 静态页**,原样保留(Boot 内嵌 Tomcat 可直接加载)
|
||||
- **spring-config.xml / spring-mvc.xml** 的 XML bean 定义,通过主类 `@ImportResource` 引导加载,不改业务 bean
|
||||
- **6 个支付/加密 system scope 黑盒 jar**:`cryptix-jce-provider`、`lzma-4.63-jio`、`dinpayRsaSignAPI`、`beecloud-java-sdk`、`yst_api`、`lzma`。这些是黑盒支付 SDK,改造时**不碰它们的实现**,仅确保能打进可执行 jar 的 classpath
|
||||
|
||||
### 2. 必须修改的部分
|
||||
|
||||
#### A. pom.xml 重构
|
||||
| 项 | 现状 | 改后 |
|
||||
|----|------|------|
|
||||
| packaging | `war` | `jar` |
|
||||
| parent | 无 | `spring-boot-starter-parent:2.7.x` |
|
||||
| spring | 4.1.0.RELEASE | 由 Boot 2.7 管理(Spring 5.3.x,去掉显式 `<spring.version>`) |
|
||||
| hibernate | 4.2.13.Final | 5.6.x(Boot 管理,去掉 `<hibernate.version>`) |
|
||||
| 依赖 | 各 spring/hibernate 手动声明 | 合并为 starter:`spring-boot-starter-web`、`spring-boot-starter-data-jpa` 或保留 jdbc/hibernate;redis 用 `spring-boot-starter-data-redis` 或保留 spring-data-redis+jedis |
|
||||
| system scope jar | cryptix/lzma/dinpay/beecloud/yst | 保留,但需处理打入 jar(Boot 默认不打包 system scope,需 `maven-jar-plugin` 或 spring-boot-maven-plugin 的 exclude/include 配置) |
|
||||
| maven-jar-plugin mainClass | 无 | 填 `com.xxx.LotteryServerApplication` |
|
||||
| Web 插件 | `maven-war-plugin` / `tomcat7-maven-plugin` | 移除,改用 `spring-boot-maven-plugin` |
|
||||
|
||||
**关键坑**:`LocalSessionFactoryBean` 从 `org.springframework.orm.hibernate4` 改为 `org.springframework.orm.hibernate5`。
|
||||
|
||||
#### B. 新增 Spring Boot 启动主类
|
||||
新建 `LotteryServerApplication`(`@SpringBootApplication` + `@ImportResource("classpath:config/spring/spring-config.xml")`),并在其中:
|
||||
- `@Bean` 配置 spring-mvc 的 DispatcherServlet 映射(或通过配置类 `implements WebMvcConfigurer` 引入 spring-mvc.xml)
|
||||
- `@Bean` 注册 CharacterEncodingFilter、springSessionRepositoryFilter(替代 web.xml 中的 filter)
|
||||
- 可选:让主类 `extends SpringBootServletInitializer` 以保留双部署能力
|
||||
|
||||
#### C. Hibernate 5 适配(核心工作量)
|
||||
集中在 DAO 基类,量小但关键:
|
||||
- [HibernateSuperDao.java](file:///c:/data/src/lottery/nine_surplus/nine_surplus/src/main/java/javautils/jdbc/hibernate/HibernateSuperDao.java)
|
||||
- [HibernateSuperReadDao.java](file:///c:/data/src/lottery/nine_surplus/nine_surplus/src/main/java/javautils/jdbc/hibernate/HibernateSuperReadDao.java)
|
||||
- 关注点:`getCurrentSession()`、`createCriteria`、`sessionFactory` 注入、事务管理器适配(`HibernateTransactionManager` → hibernate5 包)
|
||||
- spring-config.xml 中 3 个 `LocalSessionFactoryBean`(write/read/autoLoad)的 class 改为 hibernate5,`hibernate.cache.provider_class` 等属性检查
|
||||
|
||||
#### D. WebSocket 适配
|
||||
- [WebSocketConfig.java](file:///c:/data/src/lottery/nine_surplus/nine_surplus/src/main/java/lottery/web/websocket/WebSocketConfig.java) 用 `@EnableWebSocket`(Spring 自带,非原生 javax.websocket),Boot 2.7 兼容,基本无需改;仅确认 handler 依赖的容器对象注入方式
|
||||
|
||||
#### E. 资源/配置加载
|
||||
- `EncryptPropertyPlaceholderConfigurer`(显式读取 `.properties` + 解密)在 Boot 下需适配为 `PropertySourcesPlaceholderConfigurer` 风格或保留自定义类,确认 `${...}` 占位符在 Boot 环境正常解析
|
||||
- 确认 `config/datasource/*.properties`、`config/hibernate/*` 的 classpath 路径在打包 jar 后仍在类路径上
|
||||
|
||||
---
|
||||
|
||||
## 推荐实施步骤(分批执行)
|
||||
|
||||
1. **备份**:先对 `nine_surplus` 目录做完整备份 / 用 git 分支,确保可回滚
|
||||
2. **pom 改造 + 主类骨架**:改 packaging、引入 Boot parent/starter、新增 `LotteryServerApplication`,先让应用能`mvn spring-boot:run` 起来(不急着全功能)
|
||||
3. **Hibernate 5 适配**:替换 DAO 基类与 spring-config 中的 hibernate4→hibernate5,编译通过
|
||||
4. **Filter/DispatcherServlet 注册**:把 web.xml 里的 filter、servlet 迁移到 Boot 主类/配置类
|
||||
5. **system jar 打包确认**:验证支付 SDK 能被可执行 jar 正确加载
|
||||
6. **JDK 11 验证**:`mvn clean package` 产出可执行 jar,`java -jar lotteryServer.jar` 启动成功,访问登录页
|
||||
7. **回归测试**:登录、下单、余额/报表等核心链路 + Redis 会话 + WebSocket
|
||||
|
||||
## 代表性关键文件
|
||||
- `nine_surplus/pom.xml`(重构)
|
||||
- `nine_surplus/src/main/webapp/WEB-INF/web.xml`(配置迁移参照,可删除)
|
||||
- `nine_surplus/src/main/resources/config/spring/spring-config.xml`、`spring-mvc.xml`(保留 + hibernate5 适配)
|
||||
- 新增 `LotteryServerApplication.java`
|
||||
- `javautils/jdbc/hibernate/HibernateSuperDao.java`、`HibernateSuperReadDao.java`(Hibernate5 适配)
|
||||
- `lottery/web/websocket/WebSocketConfig.java`(确认兼容)
|
||||
|
||||
---
|
||||
|
||||
## 验证方式
|
||||
1. `mvn -f pom.xml clean package` 成功产出可执行 jar
|
||||
2. `java -jar target/lotteryServer.jar`(JDK11)启动,无 ClassNotFound/NoSuchMethod 异常
|
||||
3. 浏览器访问 `http://localhost:8080/`(或配置端口)能返回登录页(原 war 是 8080)
|
||||
4. 确认 Redis 会话、WebSocket、支付回调等核心链路可用
|
||||
5. 用 `jvisualvm`/启动日志确认内嵌 Tomcat 正常监听端口
|
||||
|
||||
## 风险与明确边界
|
||||
- **不会做**:不搬到 Spring Boot 3 / jakarta、不迁移 Thymeleaf、不重写 11 万行业务代码、不处理另两个模块
|
||||
- **高危点**:Hibernate 4→5 的 API 差异(主要在 2 个 DAO 基类 + 事务管理器)、system 支付 jar 的兼容性、`EncryptPropertyPlaceholderConfigurer` 解密在 Boot 环境的解析
|
||||
- **如遇 system jar 在 JDK11 上不兼容**(黑盒无从修改):降级方案是仅 JDK 升级到 11、保留 war,Boot 化分阶段单独进行,避免一次全上
|
||||
@@ -0,0 +1,23 @@
|
||||
把 lotteryCapture 和 lotteryOpen 跑起来,与已运行的 nine_surplus 组成完整三进程系统。
|
||||
|
||||
## 1. 配置改造(两个模块同样处理)
|
||||
- `lotteryCapture/src/main/resources/config/datasource/db.properties`:127.0.0.1:3306 → 127.0.0.1:33206(docker lottery-mysql),密码复用 nine_surplus 里已生成好的 druid 加密串(devpass123)
|
||||
- `lotteryOpen/.../db.properties`:同上
|
||||
- `lotteryCapture/.../redis.properties`:host/port 是 DES 加密值(`|e` 后缀)→ 直接改成明文 127.0.0.1 / 36379(EncryptPropertyPlaceholderConfigurer 对无 `|e` 后缀的值原样透传,nine_surplus 就是这么用的)
|
||||
- `lotteryOpen/.../redis.properties`:6379 → 36379
|
||||
|
||||
## 2. 构建并启动
|
||||
- JDK8 + 本地 Maven 分别 `mvn package`(jar,主类 lottery.bin.StartUp)
|
||||
- 后台 `java -jar`(或 mvn exec)启动 lotteryOpen —— 核心验证对象
|
||||
- 启动 lotteryCapture —— 预期其外部爬虫请求失败(站点已死),但要确认进程活着、只有爬虫报错
|
||||
|
||||
## 3. 验证链路
|
||||
- 观察 lotteryOpen 日志:自主彩开奖 Job(SelfLotteryLastOpenCodeJob 等)每 30 秒生成号码
|
||||
- Redis 出现 `OPEN_CODE:jsmmc` 等 key;MySQL `lottery_open_code` 表有新数据
|
||||
- lotteryOpen 的 LotteryOpenJob 每 5 秒扫描未开奖注单
|
||||
- 用 zz 账号在 jsmmc 上下一注测试注单,观察结算:user_bets 状态变化、user_bill 出现"派奖"记录、余额到账
|
||||
- Web 页面(8080)上该彩种开奖号码自动刷新(WebSocket 推送)
|
||||
|
||||
## 4. 收尾
|
||||
- 把两个进程的启动/停止命令补进 dev.bat 和 DEV-README.md
|
||||
- 明确记录:外部爬虫目标站已失效属预期现象,自主彩链路是可验证的部分
|
||||
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre7">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
||||
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>lotteryCapture</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,5 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding//src/main/java=UTF-8
|
||||
encoding//src/main/resources=UTF-8
|
||||
encoding//src/test/java=UTF-8
|
||||
encoding/<project>=UTF-8
|
||||
@@ -0,0 +1,13 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
||||
@@ -0,0 +1,4 @@
|
||||
activeProfiles=
|
||||
eclipse.preferences.version=1
|
||||
resolveWorkspaceProjects=true
|
||||
version=1
|
||||
@@ -0,0 +1,3 @@
|
||||
115.29.35.45
|
||||
121.42.10.226
|
||||
root Bocai2014
|
||||
@@ -0,0 +1,273 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>lotteryCapture</groupId>
|
||||
<artifactId>lotteryCapture</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>lotteryCapture</name>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.10</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
<version>2.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.plexus</groupId>
|
||||
<artifactId>plexus-utils</artifactId>
|
||||
<version>1.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.10</version>
|
||||
<type>jar</type>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>4.1.0.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context-support</artifactId>
|
||||
<version>4.1.0.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
<version>4.1.0.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-orm</artifactId>
|
||||
<version>4.1.0.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>4.1.0.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aspects</artifactId>
|
||||
<version>4.1.0.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>4.2.13.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-c3p0</artifactId>
|
||||
<version>4.2.13.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-entitymanager</artifactId>
|
||||
<version>4.2.13.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sf.json-lib</groupId>
|
||||
<artifactId>json-lib</artifactId>
|
||||
<version>2.4</version>
|
||||
<classifier>jdk15</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>xom</groupId>
|
||||
<artifactId>xom</artifactId>
|
||||
<version>1.2.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.4.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.htmlparser</groupId>
|
||||
<artifactId>htmlparser</artifactId>
|
||||
<version>1.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.quartz-scheduler</groupId>
|
||||
<artifactId>quartz</artifactId>
|
||||
<version>2.2.1</version>
|
||||
</dependency>
|
||||
<!-- <dependency>
|
||||
<groupId>cryptix-jce-provider</groupId>
|
||||
<artifactId>md5</artifactId>
|
||||
<version>1.0</version>
|
||||
</dependency> -->
|
||||
<dependency>
|
||||
<groupId>md5</groupId>
|
||||
<artifactId>md5</artifactId>
|
||||
<version>1.0</version>
|
||||
<type>jar</type>
|
||||
<scope>system</scope>
|
||||
<systemPath>${basedir}/doc/md5-1.0.jar</systemPath>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
<version>1.0.31</version>
|
||||
</dependency>
|
||||
|
||||
<!-- LOGGING begins -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.7</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.1.2</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 代码直接调用log4j会被桥接到slf4j -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>1.7.7</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<!-- 代码直接调用common-logging会被桥接到slf4j -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<version>1.7.7</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<!-- 代码直接调用java.util.logging会被桥接到slf4j -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jul-to-slf4j</artifactId>
|
||||
<version>1.7.7</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.ethz.ganymed</groupId>
|
||||
<artifactId>ganymed-ssh2</artifactId>
|
||||
<version>RELEASE</version>
|
||||
</dependency>
|
||||
<!-- LOGGING ends -->
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<version>2.7.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.28</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>lotteryCapture</finalName>
|
||||
<sourceDirectory>src/main/java</sourceDirectory>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.eclipse.m2e</groupId>
|
||||
<artifactId>lifecycle-mapping</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<configuration>
|
||||
<lifecycleMappingMetadata>
|
||||
<pluginExecutions>
|
||||
<pluginExecution>
|
||||
<pluginExecutionFilter>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<versionRange>[2.0,)</versionRange>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
</pluginExecutionFilter>
|
||||
<action>
|
||||
<ignore />
|
||||
</action>
|
||||
</pluginExecution>
|
||||
</pluginExecutions>
|
||||
</lifecycleMappingMetadata>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
<plugins>
|
||||
<!-- 设置源文件编码方式 -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<classpathPrefix>lib/</classpathPrefix>
|
||||
<mainClass>lottery.bin.StartUp</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<!-- 拷贝依赖的jar包到lib目录 -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>
|
||||
${project.build.directory}/lib
|
||||
</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- 解决资源文件的编码问题 -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>2.7</version>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,7 @@
|
||||
#!/bin/startup.sh
|
||||
jarName=LotteryCapture.jar
|
||||
ps -ef|grep -v grep|grep $jarName|while read u p o
|
||||
do
|
||||
kill -9 $p
|
||||
done
|
||||
exec java -jar -Xms128m -Xmx256m $jarName &
|
||||
@@ -0,0 +1,328 @@
|
||||
package javautils;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import javautils.date.Moment;
|
||||
import javautils.math.MathUtil;
|
||||
import javautils.regex.RegexUtil;
|
||||
|
||||
public class StringUtil {
|
||||
|
||||
/**
|
||||
* 判断字符串是否为空
|
||||
* @param s
|
||||
* @return
|
||||
*/
|
||||
public static boolean isNotNull(String s) {
|
||||
if(s == null) return false;
|
||||
if(s.trim().length() == 0) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断字符串能否转成int
|
||||
* @param s
|
||||
* @return
|
||||
*/
|
||||
public static boolean isInteger(String s) {
|
||||
try {
|
||||
Integer.parseInt(s);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断字符串能否转成double
|
||||
* @param s
|
||||
* @return
|
||||
*/
|
||||
public static boolean isDouble(String s) {
|
||||
try {
|
||||
Double.parseDouble(s);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断字符串能否转成float
|
||||
* @param s
|
||||
* @return
|
||||
*/
|
||||
public static boolean isFloat(String s) {
|
||||
try {
|
||||
Float.parseFloat(s);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断字符串能否转成boolean
|
||||
* @param s
|
||||
* @return
|
||||
*/
|
||||
public static boolean isBoolean(String s) {
|
||||
try {
|
||||
Boolean.parseBoolean(s);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断能否转成short
|
||||
* @param s
|
||||
* @return
|
||||
*/
|
||||
public static boolean isShort(String s) {
|
||||
try {
|
||||
Short.parseShort(s);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断能否转成long
|
||||
* @param s
|
||||
* @return
|
||||
*/
|
||||
public static boolean isLong(String s) {
|
||||
try {
|
||||
Long.parseLong(s);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动转类型
|
||||
* @param s
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
public static Object transObject(String s, Class<?> clazz) {
|
||||
if(clazz != null) {
|
||||
if(clazz == Integer.class) {
|
||||
if(isInteger(s)) {
|
||||
return Integer.parseInt(s);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if(clazz == Double.class) {
|
||||
if(isDouble(s)) {
|
||||
return Double.parseDouble(s);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if(clazz == Float.class) {
|
||||
if(isFloat(s)) {
|
||||
return Float.parseFloat(s);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if(clazz == Boolean.class) {
|
||||
if(isBoolean(s)) {
|
||||
return Boolean.parseBoolean(s);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if(clazz == Short.class) {
|
||||
if(isShort(s)) {
|
||||
return Short.parseShort(s);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if(clazz == Long.class) {
|
||||
if(isLong(s)) {
|
||||
return Long.parseLong(s);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public static String transArrayToString(Object[] array) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0, j = array.length; i < j; i++) {
|
||||
sb.append(array[i].toString());
|
||||
if(i < j - 1) {
|
||||
sb.append(",");
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String transArrayToString(Object[] array, String p) {
|
||||
String tempStr = new String();
|
||||
for (Object value : array) {
|
||||
tempStr += p + value.toString() + p + ",";
|
||||
}
|
||||
tempStr = tempStr.substring(0, tempStr.length() - 2);
|
||||
return tempStr;
|
||||
}
|
||||
|
||||
public static int[] transStringToIntArray(String string, String regex) {
|
||||
if (isNotNull(string)) {
|
||||
String[] sArray = string.split(regex);
|
||||
int[] intArray = new int[sArray.length];
|
||||
for (int i = 0; i < sArray.length; i++) {
|
||||
if(isIntegerString(sArray[i])) {
|
||||
intArray[i] = Integer.parseInt(sArray[i]);
|
||||
}
|
||||
}
|
||||
return intArray;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换文件大小
|
||||
* @param bytes
|
||||
* @return
|
||||
*/
|
||||
public static String transDataLong(long b) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
long KB = 1024;
|
||||
long MB = KB * 1024;
|
||||
long GB = MB * 1024;
|
||||
long TB = GB * 1024;
|
||||
if(b >= TB) {
|
||||
sb.append(MathUtil.doubleFormat((double)b/TB, 2) + "TB");
|
||||
} else if(b >= GB) {
|
||||
sb.append(MathUtil.doubleFormat((double)b/GB, 2) + "GB");
|
||||
} else if(b >= MB) {
|
||||
sb.append(MathUtil.doubleFormat((double)b/MB, 2) + "MB");
|
||||
} else if(b >= KB) {
|
||||
sb.append(MathUtil.doubleFormat((double)b/KB, 2) + "KB");
|
||||
} else {
|
||||
sb.append(b + "B");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断是否是int类型字符串
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static boolean isIntegerString(String str) {
|
||||
boolean flag = false;
|
||||
if(RegexUtil.isMatcher(str, RegexUtil.integer_regex)) {
|
||||
flag = true;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
public static boolean isDoubleString(String str) {
|
||||
boolean flag = false;
|
||||
if(RegexUtil.isMatcher(str, RegexUtil.rational_numbers_regex)) {
|
||||
flag = true;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
public static boolean isFloatString(String str) {
|
||||
boolean flag = false;
|
||||
if(RegexUtil.isMatcher(str, RegexUtil.rational_numbers_regex)) {
|
||||
flag = true;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断字符串是否是日期yyyy-MM-dd格式
|
||||
* @param s
|
||||
* @return
|
||||
*/
|
||||
public static boolean isDateString(String str) {
|
||||
try {
|
||||
if(str.length() != 10) {
|
||||
return false;
|
||||
}
|
||||
new SimpleDateFormat("yyyy-MM-dd").parse(str);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static String markWithSymbol(Object obj, String symbol) {
|
||||
return symbol + obj.toString() + symbol;
|
||||
}
|
||||
|
||||
//123456789
|
||||
public static Object[] split(String s) {
|
||||
char[] carr = s.toCharArray();
|
||||
Object[] arr = new Object[carr.length];
|
||||
for (int i = 0; i < carr.length; i++) {
|
||||
arr[i] = carr[i];
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
public static String substring(String s, String start, String end, boolean isInSub) {
|
||||
int idx = s.indexOf(start);
|
||||
int edx = s.indexOf(end);
|
||||
idx = idx == -1 ? 0 : idx + (isInSub ? 0 : start.length());
|
||||
edx = edx == -1 ? s.length() : edx + (isInSub ? end.length() : 0);
|
||||
return s.substring(idx, edx);
|
||||
}
|
||||
|
||||
public static String doubleFormat(double d) {
|
||||
DecimalFormat decimalformat = new DecimalFormat("#0.00");
|
||||
return decimalformat.format(d);
|
||||
}
|
||||
|
||||
public static String fromInputStream(InputStream inputStream) {
|
||||
try {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
byte[] bytes = new byte[1024];
|
||||
int len;
|
||||
while ((len = inputStream.read(bytes)) != -1) {
|
||||
sb.append(new String(bytes, 0, len));
|
||||
}
|
||||
inputStream.close();
|
||||
return sb.toString();
|
||||
} catch (Exception e) {}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证是否是服务时间
|
||||
* @param moment
|
||||
* @param times 格式为12:00~2:00
|
||||
* @return
|
||||
*/
|
||||
public static boolean isServiceTime(Moment moment, String times) {
|
||||
int thisMins = moment.hour() * 60 + moment.minute();
|
||||
String[] timeArr = times.split("~");
|
||||
int startMins = Integer.parseInt(timeArr[0].split(":")[0]) * 60 + Integer.parseInt(timeArr[0].split(":")[1]);
|
||||
int endMins = Integer.parseInt(timeArr[1].split(":")[0]) * 60 + Integer.parseInt(timeArr[1].split(":")[1]);
|
||||
if(startMins < endMins) {
|
||||
// 不在服务内的时间是 0 - start 和 end - 24
|
||||
if((thisMins > 0 && thisMins < startMins) || (thisMins > endMins && thisMins < 24 * 60)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// 不在服务内的时间是 end - start
|
||||
if(thisMins > endMins && thisMins < startMins) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package javautils.array;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ArrayUtils {
|
||||
|
||||
/**
|
||||
* 针对数据库in
|
||||
* @param ids
|
||||
* @return
|
||||
*/
|
||||
public static String transInIds(int[] ids) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0, j = ids.length; i < j; i++) {
|
||||
sb.append(ids[i]);
|
||||
if(i < j - 1) {
|
||||
sb.append(",");
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String transInIds(Integer[] ids) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0, j = ids.length; i < j; i++) {
|
||||
sb.append(ids[i].intValue());
|
||||
if(i < j - 1) {
|
||||
sb.append(",");
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 针对数据库in
|
||||
* @param ids
|
||||
* @return
|
||||
*/
|
||||
public static String transInIds(List<Integer> ids) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0, j = ids.size(); i < j; i++) {
|
||||
sb.append(ids.get(i).intValue());
|
||||
if(i < j - 1) {
|
||||
sb.append(",");
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 存进数据库
|
||||
* @param ids
|
||||
* @return
|
||||
*/
|
||||
public static String transInsertIds(int[] ids) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0, j = ids.length; i < j; i++) {
|
||||
sb.append("[" + ids[i] + "]");
|
||||
if(i < j - 1) {
|
||||
sb.append(",");
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取id的时候,解析成id数组
|
||||
* @param ids
|
||||
* @return
|
||||
*/
|
||||
public static int[] transGetIds(String ids) {
|
||||
String[] tmp = ids.replaceAll("\\[|\\]", "").split(",");
|
||||
int[] arr = new int[tmp.length];
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
arr[i] = Integer.parseInt(tmp[i]);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换成字符串
|
||||
* @param list
|
||||
* @return
|
||||
*/
|
||||
public static String toString(List<Integer> list) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0, j = list.size(); i < j; i++) {
|
||||
sb.append(list.get(i));
|
||||
if(i < j - 1) {
|
||||
sb.append(",");
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package javautils.datasource;
|
||||
|
||||
import com.alibaba.druid.filter.logging.Log4jFilter;
|
||||
import com.alibaba.druid.filter.stat.StatFilter;
|
||||
|
||||
/**
|
||||
* Created by Nick on 2016/07/06
|
||||
*/
|
||||
public class ConfigDruidDataSource {
|
||||
public static Log4jFilter configLog4jFilter() {
|
||||
Log4jFilter filter = new Log4jFilter();
|
||||
filter.setStatementExecuteQueryAfterLogEnabled(false);
|
||||
filter.setStatementExecuteUpdateAfterLogEnabled(false);
|
||||
filter.setStatementExecuteBatchAfterLogEnabled(false);
|
||||
filter.setStatementExecuteAfterLogEnabled(false);
|
||||
filter.setStatementExecutableSqlLogEnable(false);
|
||||
filter.setStatementParameterSetLogEnabled(false);
|
||||
filter.setStatementParameterClearLogEnable(false);
|
||||
filter.setStatementCreateAfterLogEnabled(false);
|
||||
filter.setStatementCloseAfterLogEnabled(false);
|
||||
filter.setStatementPrepareAfterLogEnabled(false);
|
||||
filter.setStatementPrepareCallAfterLogEnabled(false);
|
||||
filter.setStatementLogEnabled(false);
|
||||
filter.setStatementLogErrorEnabled(false);
|
||||
return filter;
|
||||
}
|
||||
|
||||
public static StatFilter configStatFilter() {
|
||||
StatFilter filter = new StatFilter();
|
||||
filter.setSlowSqlMillis(5000);
|
||||
filter.setLogSlowSql(true);
|
||||
filter.setMergeSql(true);
|
||||
return filter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package javautils.datasource;
|
||||
|
||||
import com.alibaba.druid.filter.Filter;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Nick on 2016/07/06
|
||||
*/
|
||||
public class CustomDruidDataSource extends DruidDataSource {
|
||||
@Override
|
||||
public List<Filter> getProxyFilters() {
|
||||
List<Filter> proxyFilters = new ArrayList<>();
|
||||
proxyFilters.add(ConfigDruidDataSource.configLog4jFilter());
|
||||
proxyFilters.add(ConfigDruidDataSource.configStatFilter());
|
||||
return proxyFilters;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,426 @@
|
||||
package javautils.date;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
public class DateUtil {
|
||||
|
||||
/**
|
||||
* 获取当前时间
|
||||
* @return
|
||||
*/
|
||||
public static String getCurrentTime() {
|
||||
GregorianCalendar g = new GregorianCalendar();
|
||||
return dateToString(g);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前日期
|
||||
* @return
|
||||
*/
|
||||
public static String getCurrentDate() {
|
||||
GregorianCalendar g = new GregorianCalendar();
|
||||
return dateToStringSim(g);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取昨天日期
|
||||
* @return
|
||||
*/
|
||||
public static String getYesterday() {
|
||||
GregorianCalendar g = new GregorianCalendar();
|
||||
g.set(Calendar.DATE, g.get(Calendar.DATE) - 1);
|
||||
return dateToStringSim(g);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取明天日期
|
||||
* @return
|
||||
*/
|
||||
public static String getTomorrow() {
|
||||
GregorianCalendar g = new GregorianCalendar();
|
||||
g.set(Calendar.DATE, g.get(Calendar.DATE) + 1);
|
||||
return dateToStringSim(g);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换成时间
|
||||
* @param ms
|
||||
* @return
|
||||
*/
|
||||
public static String getTime(long ms) {
|
||||
GregorianCalendar g = new GregorianCalendar();
|
||||
g.setTimeInMillis(ms);
|
||||
return dateToString(g);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据时间获取GregorianCalendar
|
||||
* @param time
|
||||
* @return
|
||||
*/
|
||||
private static GregorianCalendar getCalendarByTime(String time, String format) {
|
||||
GregorianCalendar g = new GregorianCalendar();
|
||||
try {
|
||||
g.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time));
|
||||
} catch (ParseException e) {
|
||||
g = null;
|
||||
}
|
||||
return g;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算时间
|
||||
* @param g
|
||||
* @param seconds
|
||||
* @return
|
||||
*/
|
||||
private static String calcDate(GregorianCalendar g, int seconds) {
|
||||
if(g == null) {
|
||||
g = new GregorianCalendar();
|
||||
}
|
||||
g.add(Calendar.SECOND, seconds);
|
||||
return dateToString(g);
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期转字符串
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static String dateToString(Date date) {
|
||||
try {
|
||||
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
||||
String s = formatter.format(date);
|
||||
return s;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转日期
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static Date stringToDate(String date) {
|
||||
try {
|
||||
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
||||
Date d = (Date) formatter.parse(date);
|
||||
return d;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转日期, 自定义格式
|
||||
* @param date
|
||||
* @param format
|
||||
* @return
|
||||
*/
|
||||
public static Date stringToDate(String date, String format) {
|
||||
try {
|
||||
DateFormat formatter = new SimpleDateFormat(format);
|
||||
Date d = (Date) formatter.parse(date);
|
||||
return d;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取日期相差毫秒数
|
||||
* @param subDate
|
||||
* @param minDate
|
||||
* @return
|
||||
*/
|
||||
public static long calcDate(String subDate, String minDate) {
|
||||
long lSubDate = getCalendarByTime(subDate, "yyyy-MM-dd HH:mm:ss").getTimeInMillis();
|
||||
long lMinDate = getCalendarByTime(minDate, "yyyy-MM-dd HH:mm:ss").getTimeInMillis();
|
||||
return (lSubDate - lMinDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据日期计算相差天数,忽略时分秒
|
||||
* @param date1
|
||||
* @param date2
|
||||
* @return
|
||||
*/
|
||||
public static int calcDays(Date date1, Date date2) {
|
||||
Calendar date1Calendar = Calendar.getInstance();
|
||||
date1Calendar.setTime(date1);
|
||||
|
||||
Calendar date2Calendar = Calendar.getInstance();
|
||||
date2Calendar.setTime(date2);
|
||||
|
||||
int day1 = date1Calendar.get(Calendar.DAY_OF_YEAR);
|
||||
int day2 = date2Calendar.get(Calendar.DAY_OF_YEAR);
|
||||
|
||||
return day1 - day2;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据差值计算出新的时间
|
||||
* @param time
|
||||
* @param seconds
|
||||
* @return
|
||||
*/
|
||||
public static String calcDateByTime(String time, int seconds) {
|
||||
GregorianCalendar g = getCalendarByTime(time, "yyyy-MM-dd HH:mm:ss");
|
||||
return calcDate(g, seconds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据天数计算出新的日期
|
||||
* @param date
|
||||
* @param days
|
||||
* @return
|
||||
*/
|
||||
public static String calcNewDay(String date, int days) {
|
||||
GregorianCalendar g = new GregorianCalendar();
|
||||
g.setTimeInMillis(formatTime(date, "yyyy-MM-dd"));
|
||||
g.set(Calendar.DATE, g.get(Calendar.DATE) + days);
|
||||
return dateToStringSim(g);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下一天
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static String calcNextDay(String date) {
|
||||
GregorianCalendar g = new GregorianCalendar();
|
||||
g.setTimeInMillis(formatTime(date, "yyyy-MM-dd"));
|
||||
g.set(Calendar.DATE, g.get(Calendar.DATE) + 1);
|
||||
return dateToStringSim(g);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上一天
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static String calcLastDay(String date) {
|
||||
GregorianCalendar g = new GregorianCalendar();
|
||||
g.setTimeInMillis(formatTime(date, "yyyy-MM-dd"));
|
||||
g.set(Calendar.DATE, g.get(Calendar.DATE) - 1);
|
||||
return dateToStringSim(g);
|
||||
}
|
||||
|
||||
private static Calendar getDateOfMonth(Calendar date, int num, boolean flag) {
|
||||
Calendar lastDate = (Calendar) date.clone();
|
||||
if(flag) {
|
||||
lastDate.add(Calendar.MONTH, num);
|
||||
}else {
|
||||
lastDate.add(Calendar.MONTH, -num);
|
||||
}
|
||||
|
||||
return lastDate;
|
||||
}
|
||||
|
||||
private static Calendar getDateOfLastMonth(String dateStr, int num, boolean flag) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
try {
|
||||
Date date = sdf.parse(dateStr);
|
||||
Calendar c = Calendar.getInstance();
|
||||
c.setTime(date);
|
||||
return getDateOfMonth(c, num, flag);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid date format(yyyyMMdd): " + dateStr);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上个月的同一天
|
||||
* @param date
|
||||
* @param num
|
||||
* @param flag
|
||||
* @return
|
||||
*/
|
||||
public static String getSameDateOfLastMonth(String date, int num, boolean flag) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
String lastDate = sdf.format(getDateOfLastMonth(date, num, flag).getTime());
|
||||
return lastDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* GregorianCalendar转成字符串
|
||||
* @param g
|
||||
* @return
|
||||
*/
|
||||
private static String dateToString(GregorianCalendar g) {
|
||||
String year = String.valueOf(g.get(Calendar.YEAR));
|
||||
String month = String.format("%02d", g.get(Calendar.MONTH) + 1);
|
||||
String day =String.format("%02d", g.get(Calendar.DAY_OF_MONTH));
|
||||
String hours = String.format("%02d", g.get(Calendar.HOUR_OF_DAY));
|
||||
String minutes = String.format("%02d", g.get(Calendar.MINUTE));
|
||||
String seconds = String.format("%02d", g.get(Calendar.SECOND));
|
||||
return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据时间转成MS
|
||||
* @param time
|
||||
* @param format
|
||||
* @return
|
||||
*/
|
||||
public static long stringToLong(String time, String format) {
|
||||
return stringToDate(time, format).getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* GregorianCalendar转成日期格式
|
||||
* @param g
|
||||
* @return
|
||||
*/
|
||||
public static String dateToStringSim(GregorianCalendar g) {
|
||||
String year = String.valueOf(g.get(Calendar.YEAR));
|
||||
String month = String.format("%02d", g.get(Calendar.MONTH) + 1);
|
||||
String day = String.format("%02d", g.get(Calendar.DAY_OF_MONTH));
|
||||
return year + "-" + month + "-" + day;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前时间
|
||||
* @return
|
||||
*/
|
||||
public static String getCurTimeStr() {
|
||||
GregorianCalendar g = new GregorianCalendar();
|
||||
String year = String.valueOf(g.get(Calendar.YEAR));
|
||||
String month = String.format("%02d", g.get(Calendar.MONTH) + 1);
|
||||
String day = String.format("%02d", g.get(Calendar.DAY_OF_MONTH));
|
||||
String hours = String.format("%02d", g.get(Calendar.HOUR_OF_DAY));
|
||||
String minutes = String.format("%02d", g.get(Calendar.MINUTE));
|
||||
String seconds = String.format("%02d", g.get(Calendar.SECOND));
|
||||
|
||||
return year + month + day + hours + minutes + seconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间
|
||||
* @param time
|
||||
* @param oldFormat
|
||||
* @param newFormat
|
||||
* @return
|
||||
*/
|
||||
public static String formatTime(String time, String oldFormat,
|
||||
String newFormat) {
|
||||
return new SimpleDateFormat(newFormat).format(stringToDate(time,
|
||||
oldFormat));
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期
|
||||
* @param date
|
||||
* @param format
|
||||
* @return
|
||||
*/
|
||||
public static String formatTime(Date date, String format) {
|
||||
return new SimpleDateFormat(format).format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间
|
||||
* @param time
|
||||
* @param format
|
||||
* @return
|
||||
*/
|
||||
public static String formatTime(long time, String format) {
|
||||
return formatTime(getTime(time), "yyyy-MM-dd HH:mm:ss", format);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时间, 根据格式化参数
|
||||
* @param time
|
||||
* @param format
|
||||
* @return
|
||||
*/
|
||||
public static long formatTime(String time, String format) {
|
||||
return stringToDate(time, format).getTime();
|
||||
}
|
||||
|
||||
public static String dateForm(String date, String config) {
|
||||
if ("MM/dd/yyyy".equals(config)) {
|
||||
String[] dateStrs = date.split("/");
|
||||
return dateStrs[2] + "-" + dateStrs[0] + "-" + dateStrs[1];
|
||||
}
|
||||
if ("MM-dd-yyyy".equals(config)) {
|
||||
String[] dateStrs = date.split("-");
|
||||
return dateStrs[2] + "-" + dateStrs[0] + "-" + dateStrs[1];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static int getYear(String time) {
|
||||
return Integer.parseInt(time.substring(0, 4));
|
||||
}
|
||||
|
||||
public static int getMonth(String time) {
|
||||
return Integer.parseInt(time.substring(5, 7));
|
||||
}
|
||||
|
||||
public static int getDay(String time) {
|
||||
return Integer.parseInt(time.substring(8, 10));
|
||||
}
|
||||
|
||||
public static int getHours(String time) {
|
||||
return Integer.parseInt(time.substring(11, 13));
|
||||
}
|
||||
|
||||
public static int getMinutes(String time) {
|
||||
return Integer.parseInt(time.substring(14, 16));
|
||||
}
|
||||
|
||||
public static int getSeconds(String time) {
|
||||
return Integer.parseInt(time.substring(17));
|
||||
}
|
||||
|
||||
public static int getYear() {
|
||||
GregorianCalendar g = new GregorianCalendar();
|
||||
return g.get(Calendar.YEAR);
|
||||
}
|
||||
|
||||
public static int getMonth() {
|
||||
GregorianCalendar g = new GregorianCalendar();
|
||||
return g.get(Calendar.MONTH) + 1;
|
||||
}
|
||||
|
||||
public static int getDay() {
|
||||
GregorianCalendar g = new GregorianCalendar();
|
||||
return g.get(Calendar.DAY_OF_MONTH);
|
||||
}
|
||||
|
||||
public static int getHours() {
|
||||
GregorianCalendar g = new GregorianCalendar();
|
||||
return g.get(Calendar.HOUR_OF_DAY);
|
||||
}
|
||||
|
||||
public static int getMinutes() {
|
||||
GregorianCalendar g = new GregorianCalendar();
|
||||
return g.get(Calendar.MINUTE);
|
||||
}
|
||||
|
||||
public static int getSeconds() {
|
||||
GregorianCalendar g = new GregorianCalendar();
|
||||
return g.get(Calendar.SECOND);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("年:" + getYear());
|
||||
System.out.println("月:" + getMonth());
|
||||
System.out.println("日:" + getDay());
|
||||
System.out.println("时:" + getHours());
|
||||
System.out.println("分:" + getMinutes());
|
||||
System.out.println("秒:" + getSeconds());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,861 @@
|
||||
package javautils.date;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class Moment {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Moment moment = new Moment();
|
||||
System.out.println(moment.difference(
|
||||
new Moment().fromDate("2015-01-18"), "hour"));
|
||||
}
|
||||
|
||||
static {
|
||||
TimeZone china = TimeZone.getTimeZone("Asia/Shanghai");
|
||||
TimeZone.setDefault(china);
|
||||
}
|
||||
|
||||
public GregorianCalendar gc;
|
||||
|
||||
public Moment() {
|
||||
gc = new GregorianCalendar();
|
||||
gc.setFirstDayOfWeek(Calendar.MONDAY);
|
||||
}
|
||||
|
||||
public Moment(GregorianCalendar gc) {
|
||||
this.gc = gc;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据日期获取对象
|
||||
*
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public Moment fromDate(String date) {
|
||||
try {
|
||||
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
||||
Date d = format.parse(date);
|
||||
gc.setTime(d);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据时间获取对象
|
||||
*
|
||||
* @param time
|
||||
* @return
|
||||
*/
|
||||
public Moment fromTime(String time) {
|
||||
try {
|
||||
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
Date d = format.parse(time);
|
||||
gc.setTime(d);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据时间+format
|
||||
*
|
||||
* @param time
|
||||
* @param formatStr
|
||||
* @return
|
||||
*/
|
||||
public Moment fromTime(String time, String formatStr) {
|
||||
try {
|
||||
DateFormat format = new SimpleDateFormat(formatStr);
|
||||
Date d = format.parse(time);
|
||||
gc.setTime(d);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换成普通时间 1900-01-01 12:00:00
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String toSimpleTime() {
|
||||
return year() + "-" + months() + "-" + days() + " " + hours() + ":"
|
||||
+ minutes() + ":" + seconds();
|
||||
}
|
||||
|
||||
public String toCurTime() {
|
||||
return hours() + ":" + minutes() + ":" + seconds();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换成完整时间1900-01-01 12:00:00.000
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String toFullTime() {
|
||||
return toSimpleTime() + "." + milliseconds();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转成日期格式1900-01-01
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String toSimpleDate() {
|
||||
return year() + "-" + months() + "-" + days();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取date对象
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Date toDate() {
|
||||
return gc.getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据参数格式化 y年 M月 d日 H时 m分 s秒
|
||||
*
|
||||
* @param format
|
||||
* @return
|
||||
*/
|
||||
public String format(String format) {
|
||||
return new SimpleDateFormat(format).format(toDate());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取年份
|
||||
*
|
||||
* @return
|
||||
* @return
|
||||
*/
|
||||
public int year() {
|
||||
return gc.get(Calendar.YEAR);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置年份
|
||||
*
|
||||
* @param year
|
||||
*/
|
||||
public Moment year(int year) {
|
||||
gc.set(Calendar.YEAR, year);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取月份
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int month() {
|
||||
return gc.get(Calendar.MONTH) + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取完整月份
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String months() {
|
||||
return String.format("%02d", month());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置月份
|
||||
*
|
||||
* @param month
|
||||
* @return
|
||||
*/
|
||||
public Moment month(int month) {
|
||||
if (month > 0 && month <= 12) {
|
||||
gc.set(Calendar.MONTH, month - 1);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置月份
|
||||
*
|
||||
* @param month
|
||||
* @return
|
||||
*/
|
||||
public Moment month(String month) {
|
||||
month(Integer.parseInt(month));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取天
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int day() {
|
||||
return gc.get(Calendar.DAY_OF_MONTH);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取完整天
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String days() {
|
||||
return String.format("%02d", day());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置天
|
||||
*
|
||||
* @param day
|
||||
* @return
|
||||
*/
|
||||
public Moment day(int day) {
|
||||
if (day > 0 && day <= 31) {
|
||||
gc.set(Calendar.DAY_OF_MONTH, day);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置天
|
||||
*
|
||||
* @param day
|
||||
* @return
|
||||
*/
|
||||
public Moment day(String day) {
|
||||
day(Integer.parseInt(day));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取小时
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int hour() {
|
||||
return gc.get(Calendar.HOUR_OF_DAY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取小时
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String hours() {
|
||||
return String.format("%02d", hour());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置小时
|
||||
*
|
||||
* @param hour
|
||||
* @return
|
||||
*/
|
||||
public Moment hour(int hour) {
|
||||
if (hour >= 0 && hour < 24) {
|
||||
gc.set(Calendar.HOUR_OF_DAY, hour);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置小时
|
||||
*
|
||||
* @param hour
|
||||
* @return
|
||||
*/
|
||||
public Moment hour(String hour) {
|
||||
hour(Integer.parseInt(hour));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分钟
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int minute() {
|
||||
return gc.get(Calendar.MINUTE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分钟
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String minutes() {
|
||||
return String.format("%02d", minute());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置分钟
|
||||
*
|
||||
* @param minute
|
||||
* @return
|
||||
*/
|
||||
public Moment minute(int minute) {
|
||||
if (minute >= 0 && minute < 60) {
|
||||
gc.set(Calendar.MINUTE, minute);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置分钟
|
||||
*
|
||||
* @param minute
|
||||
* @return
|
||||
*/
|
||||
public Moment minute(String minute) {
|
||||
minute(Integer.parseInt(minute));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取秒
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int second() {
|
||||
return gc.get(Calendar.SECOND);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取秒
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String seconds() {
|
||||
return String.format("%02d", second());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置秒
|
||||
*
|
||||
* @param second
|
||||
* @return
|
||||
*/
|
||||
public Moment second(int second) {
|
||||
if (second >= 0 && second < 60) {
|
||||
gc.set(Calendar.SECOND, second);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置秒
|
||||
*
|
||||
* @param second
|
||||
* @return
|
||||
*/
|
||||
public Moment second(String second) {
|
||||
second(Integer.parseInt(second));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取毫秒
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int millisecond() {
|
||||
return gc.get(Calendar.MILLISECOND);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取毫秒
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String milliseconds() {
|
||||
return String.format("%03d", millisecond());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置毫秒
|
||||
*
|
||||
* @param millisecond
|
||||
* @return
|
||||
*/
|
||||
public Moment millisecond(int millisecond) {
|
||||
if (millisecond >= 0 && millisecond < 1000) {
|
||||
gc.set(Calendar.MILLISECOND, millisecond);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置毫秒
|
||||
*
|
||||
* @param millisecond
|
||||
* @return
|
||||
*/
|
||||
public Moment millisecond(String millisecond) {
|
||||
millisecond(Integer.parseInt(millisecond));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前日期在本月的第几周
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int weekOfMonth() {
|
||||
return gc.get(Calendar.WEEK_OF_MONTH);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前日期在本年的第几周
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int weekOfYear() {
|
||||
return gc.get(Calendar.WEEK_OF_YEAR);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取星期几
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int dayOfWeek() {
|
||||
int field = gc.get(Calendar.DAY_OF_WEEK);
|
||||
switch (field) {
|
||||
case Calendar.SUNDAY:
|
||||
return 7;
|
||||
case Calendar.MONDAY:
|
||||
return 1;
|
||||
case Calendar.TUESDAY:
|
||||
return 2;
|
||||
case Calendar.WEDNESDAY:
|
||||
return 3;
|
||||
case Calendar.THURSDAY:
|
||||
return 4;
|
||||
case Calendar.FRIDAY:
|
||||
return 5;
|
||||
case Calendar.SATURDAY:
|
||||
return 6;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return gc.getFirstDayOfWeek();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置星期几
|
||||
*
|
||||
* @param week
|
||||
* @return
|
||||
*/
|
||||
public Moment dayOfWeek(int week) {
|
||||
switch (week) {
|
||||
case 7:
|
||||
gc.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
|
||||
break;
|
||||
case 1:
|
||||
gc.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
||||
break;
|
||||
case 2:
|
||||
gc.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
|
||||
break;
|
||||
case 3:
|
||||
gc.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
|
||||
break;
|
||||
case 4:
|
||||
gc.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
|
||||
break;
|
||||
case 5:
|
||||
gc.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
|
||||
break;
|
||||
case 6:
|
||||
gc.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前日期在本年的第几天
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int dayOfYear() {
|
||||
return gc.get(Calendar.DAY_OF_YEAR);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置日期为本年的第几天
|
||||
*
|
||||
* @param day
|
||||
* @return
|
||||
*/
|
||||
public Moment dayOfYear(int day) {
|
||||
gc.set(Calendar.DAY_OF_YEAR, day);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前日期所在季度 1月~3月 第一季度 4月~6月 第二季度 7月~9月 第三季度 10月~12月 第四季度
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int quarter() {
|
||||
int month = month();
|
||||
switch (month) {
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
return 1;
|
||||
case 4:
|
||||
case 5:
|
||||
case 6:
|
||||
return 2;
|
||||
case 7:
|
||||
case 8:
|
||||
case 9:
|
||||
return 3;
|
||||
case 10:
|
||||
case 11:
|
||||
case 12:
|
||||
return 4;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据参数获取
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public int get(String key) {
|
||||
switch (key) {
|
||||
case "year":
|
||||
return year();
|
||||
case "month":
|
||||
return month();
|
||||
case "day":
|
||||
case "date":
|
||||
return day();
|
||||
case "hour":
|
||||
return hour();
|
||||
case "minute":
|
||||
return minute();
|
||||
case "second":
|
||||
return second();
|
||||
case "millisecond":
|
||||
return millisecond();
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据参数设置值
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public Moment set(String key, int value) {
|
||||
switch (key) {
|
||||
case "year":
|
||||
year(value);
|
||||
break;
|
||||
case "month":
|
||||
month(value);
|
||||
break;
|
||||
case "day":
|
||||
case "date":
|
||||
day(value);
|
||||
break;
|
||||
case "hour":
|
||||
hour(value);
|
||||
break;
|
||||
case "minute":
|
||||
minute(value);
|
||||
break;
|
||||
case "second":
|
||||
second(value);
|
||||
break;
|
||||
case "millisecond":
|
||||
millisecond(value);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取millis时间
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public long millis() {
|
||||
return gc.getTimeInMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取其中时间最大的那个
|
||||
*
|
||||
* @param moment
|
||||
* @return
|
||||
*/
|
||||
public static Moment max(final Moment... moment) {
|
||||
Moment max = null;
|
||||
if (moment != null && moment.length > 0) {
|
||||
for (Moment tmp : moment) {
|
||||
if (max == null) {
|
||||
max = tmp;
|
||||
}
|
||||
if (max.millis() < tmp.millis()) {
|
||||
max = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取其中时间最小的那个
|
||||
*
|
||||
* @param moment
|
||||
* @return
|
||||
*/
|
||||
public static Moment min(final Moment... moment) {
|
||||
Moment min = null;
|
||||
if (moment != null && moment.length > 0) {
|
||||
for (Moment tmp : moment) {
|
||||
if (min == null) {
|
||||
min = tmp;
|
||||
}
|
||||
if (min.millis() > tmp.millis()) {
|
||||
min = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
return min;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加指定key的时间
|
||||
*
|
||||
* @param number
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public Moment add(int amount, String key) {
|
||||
switch (key) {
|
||||
case "years":
|
||||
case "y":
|
||||
gc.add(Calendar.YEAR, amount);
|
||||
break;
|
||||
case "quarters":
|
||||
case "Q":
|
||||
gc.add(Calendar.MONTH, amount * 3);
|
||||
break;
|
||||
case "months":
|
||||
case "M":
|
||||
gc.add(Calendar.MONTH, amount);
|
||||
break;
|
||||
case "weeks":
|
||||
case "w":
|
||||
gc.add(Calendar.WEEK_OF_YEAR, amount);
|
||||
break;
|
||||
case "days":
|
||||
case "d":
|
||||
gc.add(Calendar.DAY_OF_MONTH, amount);
|
||||
break;
|
||||
case "hours":
|
||||
case "h":
|
||||
gc.add(Calendar.HOUR_OF_DAY, amount);
|
||||
break;
|
||||
case "minutes":
|
||||
case "m":
|
||||
gc.add(Calendar.MINUTE, amount);
|
||||
break;
|
||||
case "seconds":
|
||||
case "s":
|
||||
gc.add(Calendar.SECOND, amount);
|
||||
break;
|
||||
case "milliseconds":
|
||||
case "ms":
|
||||
gc.add(Calendar.MILLISECOND, amount);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 减去指定key的时间
|
||||
*
|
||||
* @param number
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public Moment subtract(int amount, String key) {
|
||||
return add(-amount, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取key的开始时间
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public Moment startOf(String key) {
|
||||
Moment moment = new Moment((GregorianCalendar) this.gc.clone());
|
||||
switch (key) {
|
||||
case "year":
|
||||
moment.dayOfYear(1).hour(0).minute(0).second(0).millisecond(0);
|
||||
break;
|
||||
case "month":
|
||||
moment.day(1).hour(0).minute(0).second(0).millisecond(0);
|
||||
break;
|
||||
case "week":
|
||||
moment.dayOfWeek(1).hour(0).minute(0).second(0).millisecond(0);
|
||||
break;
|
||||
case "day":
|
||||
moment.hour(0).minute(0).second(0).millisecond(0);
|
||||
break;
|
||||
case "hour":
|
||||
moment.minute(0).second(0).millisecond(0);
|
||||
case "minute":
|
||||
moment.second(0).millisecond(0);
|
||||
break;
|
||||
case "second":
|
||||
moment.millisecond(0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return moment;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取key的结束时间
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public Moment endOf(String key) {
|
||||
Moment moment = new Moment();
|
||||
switch (key) {
|
||||
case "year":
|
||||
return moment.startOf(key).add(1, "years").subtract(1, "days")
|
||||
.hour(23).minute(59).second(59).millisecond(999);
|
||||
case "month":
|
||||
return moment.startOf(key).add(1, "months").subtract(1, "days")
|
||||
.hour(23).minute(59).second(59).millisecond(999);
|
||||
case "week":
|
||||
return moment.startOf(key).add(1, "weeks").subtract(1, "days")
|
||||
.hour(23).minute(59).second(59).millisecond(999);
|
||||
case "day":
|
||||
return moment.startOf(key).hour(23).minute(59).second(59)
|
||||
.millisecond(999);
|
||||
case "hour":
|
||||
return moment.startOf(key).minute(59).second(59).millisecond(999);
|
||||
case "minute":
|
||||
return moment.startOf(key).second(59).millisecond(999);
|
||||
case "second":
|
||||
return moment.startOf(key).millisecond(999);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return moment;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时间差值
|
||||
*
|
||||
* @param moment
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public int difference(Moment moment, String key) {
|
||||
switch (key) {
|
||||
case "year":
|
||||
return this.year() - moment.year();
|
||||
case "month":
|
||||
return difference(moment, "year") * 12
|
||||
+ (this.month() - moment.month());
|
||||
case "day":
|
||||
return (int) ((this.millis() - moment.millis()) / (24 * 60 * 60 * 1000));
|
||||
case "hour":
|
||||
return (int) ((this.millis() - moment.millis()) / (60 * 60 * 1000));
|
||||
case "minute":
|
||||
return (int) ((this.millis() - moment.millis()) / (60 * 1000));
|
||||
case "second":
|
||||
return (int) ((this.millis() - moment.millis()) / (1000));
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 大于另一个日期
|
||||
*
|
||||
* @param moment
|
||||
* @return
|
||||
*/
|
||||
public boolean gt(Moment moment) {
|
||||
return (this.millis() > moment.millis()) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 小于另一个日期
|
||||
*
|
||||
* @param moment
|
||||
* @return
|
||||
*/
|
||||
public boolean lt(Moment moment) {
|
||||
return (this.millis() < moment.millis()) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 大于或者等于另一个日期
|
||||
*
|
||||
* @param moment
|
||||
* @return
|
||||
*/
|
||||
public boolean ge(Moment moment) {
|
||||
return (this.millis() >= moment.millis()) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 小于或者等于另一个日期
|
||||
*
|
||||
* @param moment
|
||||
* @return
|
||||
*/
|
||||
public boolean le(Moment moment) {
|
||||
return (this.millis() <= moment.millis()) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在两个日期之间
|
||||
*
|
||||
* @param sMoment
|
||||
* @param eMoment
|
||||
* @return
|
||||
*/
|
||||
public boolean between(Moment sMoment, Moment eMoment) {
|
||||
return (this.ge(sMoment) && this.le(eMoment)) ? true : false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package javautils.encrypt;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
|
||||
/**
|
||||
* 对密码进行加密和验证的类
|
||||
*/
|
||||
public class CipherUtil {
|
||||
|
||||
// 十六进制下数字到字符的映射数组
|
||||
private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
|
||||
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
|
||||
|
||||
/** * 把inputString加密 */
|
||||
public static String generatePassword(String inputString) {
|
||||
return encodeByMD5(inputString);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证输入的密码是否正确
|
||||
*
|
||||
* @param password
|
||||
* 加密后的密码
|
||||
* @param inputString
|
||||
* 输入的字符串
|
||||
* @return 验证结果,TRUE:正确 FALSE:错误
|
||||
*/
|
||||
public static boolean validatePassword(String password, String inputString) {
|
||||
if (password.equals(encodeByMD5(inputString))) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** 对字符串进行MD5加密 */
|
||||
private static String encodeByMD5(String originString) {
|
||||
if (originString != null) {
|
||||
try {
|
||||
// 创建具有指定算法名称的信息摘要
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
// 使用指定的字节数组对摘要进行最后更新,然后完成摘要计算
|
||||
byte[] results = md.digest(originString.getBytes());
|
||||
// 将得到的字节数组变成字符串返回
|
||||
String resultString = byteArrayToHexString(results);
|
||||
return resultString.toUpperCase();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换字节数组为十六进制字符串
|
||||
*
|
||||
* @param 字节数组
|
||||
* @return 十六进制字符串
|
||||
*/
|
||||
private static String byteArrayToHexString(byte[] b) {
|
||||
StringBuffer resultSb = new StringBuffer();
|
||||
for (int i = 0; i < b.length; i++) {
|
||||
resultSb.append(byteToHexString(b[i]));
|
||||
}
|
||||
return resultSb.toString();
|
||||
}
|
||||
|
||||
/** 将一个字节转化成十六进制形式的字符串 */
|
||||
private static String byteToHexString(byte b) {
|
||||
int n = b;
|
||||
if (n < 0)
|
||||
n = 256 + n;
|
||||
int d1 = n / 16;
|
||||
int d2 = n % 16;
|
||||
return hexDigits[d1] + hexDigits[d2];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package javautils.encrypt;
|
||||
|
||||
import sun.misc.BASE64Decoder;
|
||||
import sun.misc.BASE64Encoder;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.SecretKeyFactory;
|
||||
import javax.crypto.spec.DESKeySpec;
|
||||
import java.security.Key;
|
||||
|
||||
/**
|
||||
* DES加密工具类
|
||||
*/
|
||||
public class DESUtil {
|
||||
|
||||
private DESUtil() {
|
||||
|
||||
}
|
||||
|
||||
public static DESUtil getInstance() {
|
||||
return new DESUtil();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据参数生成 KEY
|
||||
*/
|
||||
public Key getKey(String strKey) {
|
||||
Key key = null;
|
||||
try {
|
||||
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
||||
DESKeySpec keySpec = new DESKeySpec(strKey.getBytes());
|
||||
keyFactory.generateSecret(keySpec);
|
||||
key = keyFactory.generateSecret(keySpec);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密 String 明文输入 ,String 密文输出
|
||||
*/
|
||||
public String encryptStr(String strMing, String keyStr) {
|
||||
byte[] byteMi = null;
|
||||
byte[] byteMing = null;
|
||||
String strMi = "";
|
||||
BASE64Encoder base64en = new BASE64Encoder();
|
||||
try {
|
||||
byteMing = strMing.getBytes("UTF8");
|
||||
byteMi = encryptByte(byteMing, getKey(keyStr));
|
||||
strMi = base64en.encode(byteMi);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
base64en = null;
|
||||
byteMing = null;
|
||||
byteMi = null;
|
||||
}
|
||||
return strMi;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密 以 String 密文输入 ,String 明文输出
|
||||
*
|
||||
* @param strMi
|
||||
* @return
|
||||
*/
|
||||
public String decryptStr(String strMi, String keyStr) {
|
||||
byte[] byteMing = null;
|
||||
byte[] byteMi = null;
|
||||
String strMing = "";
|
||||
BASE64Decoder base64De = new BASE64Decoder();
|
||||
try {
|
||||
byteMi = base64De.decodeBuffer(strMi);
|
||||
byteMing = decryptByte(byteMi, getKey(keyStr));
|
||||
strMing = new String(byteMing, "UTF8");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
base64De = null;
|
||||
byteMing = null;
|
||||
byteMi = null;
|
||||
}
|
||||
return strMing;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密以 byte[] 明文输入 ,byte[] 密文输出
|
||||
*
|
||||
* @param byteS
|
||||
* @return
|
||||
*/
|
||||
private byte[] encryptByte(byte[] byteS, Key key) {
|
||||
byte[] byteFina = null;
|
||||
Cipher cipher;
|
||||
try {
|
||||
cipher = Cipher.getInstance("DES");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, key);
|
||||
byteFina = cipher.doFinal(byteS);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
cipher = null;
|
||||
}
|
||||
return byteFina;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密以 byte[] 密文输入 , 以 byte[] 明文输出
|
||||
*
|
||||
* @param byteD
|
||||
* @return
|
||||
*/
|
||||
private byte[] decryptByte(byte[] byteD, Key key) {
|
||||
byte[] byteFina = null;
|
||||
Cipher cipher;
|
||||
try {
|
||||
cipher = Cipher.getInstance("DES");
|
||||
cipher.init(Cipher.DECRYPT_MODE, key);
|
||||
byteFina = cipher.doFinal(byteD);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
cipher = null;
|
||||
}
|
||||
return byteFina;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,319 @@
|
||||
package javautils.encrypt;
|
||||
|
||||
/**
|
||||
* protect the password user inputed
|
||||
*/
|
||||
public class SHACode {
|
||||
private static final long SH0 = 0x67452301L;
|
||||
private static final long SH1 = 0xefcdab89L;
|
||||
private static final long SH2 = 0x98badcfeL;
|
||||
private static final long SH3 = 0x10325476L;
|
||||
private static final long SH4 = 0xc3d2e1f0L;
|
||||
|
||||
private static final long K0 = 0x5a827999L;
|
||||
private static final long K1 = 0x6ed9eba1L;
|
||||
private static final long K2 = 0x8f1bbcdcL;
|
||||
private static final long K3 = 0xca62c1d6L;
|
||||
|
||||
// df debug 12.11
|
||||
private static long[] W;
|
||||
private static byte[] BW;
|
||||
|
||||
private static int p0, p1, p2, p3, p4;
|
||||
private static long A, B, C, D, E, temp;
|
||||
|
||||
private static long f0(long x, long y, long z) {
|
||||
return (z ^ (x & (y ^ z)));
|
||||
}
|
||||
|
||||
private static long f1(long x, long y, long z) {
|
||||
return (x ^ y ^ z);
|
||||
}
|
||||
|
||||
private static long f2(long x, long y, long z) {
|
||||
return ((x & y) | (z & (x | y)));
|
||||
}
|
||||
|
||||
private static long f3(long x, long y, long z) {
|
||||
return (x ^ y ^ z);
|
||||
}
|
||||
|
||||
private static long S(int n, long X) {
|
||||
long tem = 0;
|
||||
X = X & 0x00000000FFFFFFFF;
|
||||
|
||||
if (n == 1) {
|
||||
tem = ((X >> 31) & 0x00000001);
|
||||
|
||||
}
|
||||
if (n == 5)
|
||||
tem = ((X >> 27) & 0x0000001F);
|
||||
if (n == 30)
|
||||
tem = ((X >> 2) & 0x3FFFFFFF);
|
||||
|
||||
return ((X << n) | tem);
|
||||
|
||||
}
|
||||
|
||||
private static void r0(int f, long K) {
|
||||
|
||||
temp = S(5, A) + f0(B, C, D) + E + W[p0++] + K;
|
||||
E = D;
|
||||
D = C;
|
||||
C = S(30, B);
|
||||
B = A;
|
||||
A = temp;
|
||||
}
|
||||
|
||||
private static void r1(int f, long K) {
|
||||
|
||||
temp = (W[p1++] ^ W[p2++] ^ W[p3++] ^ W[p4++]);
|
||||
|
||||
switch (f) {
|
||||
case 0:
|
||||
temp = S(5, A) + f0(B, C, D) + E + (W[p0++] = S(1, temp)) + K;
|
||||
break;
|
||||
case 1:
|
||||
temp = S(5, A) + f1(B, C, D) + E + (W[p0++] = S(1, temp)) + K;
|
||||
break;
|
||||
case 2:
|
||||
temp = S(5, A) + f2(B, C, D) + E + (W[p0++] = S(1, temp)) + K;
|
||||
break;
|
||||
case 3:
|
||||
temp = S(5, A) + f3(B, C, D) + E + (W[p0++] = S(1, temp)) + K;
|
||||
break;
|
||||
}
|
||||
|
||||
E = D;
|
||||
D = C;
|
||||
C = S(30, B);
|
||||
B = A;
|
||||
A = temp;
|
||||
}
|
||||
|
||||
public static long getCode(String mem) {
|
||||
int i, nread, nbits;
|
||||
int length = mem.toCharArray().length;
|
||||
int hi_length, lo_length;
|
||||
int padded;
|
||||
char[] s;
|
||||
int sp = 0;
|
||||
long h0, h1, h2, h3, h4;
|
||||
W = new long[80];
|
||||
BW = new byte[320];
|
||||
padded = 0;
|
||||
s = mem.toCharArray();
|
||||
h0 = SH0;
|
||||
h1 = SH1;
|
||||
h2 = SH2;
|
||||
h3 = SH3;
|
||||
h4 = SH4;
|
||||
for (hi_length = lo_length = 0;;) {
|
||||
if (length < 64)
|
||||
nread = length;
|
||||
else
|
||||
nread = 64;
|
||||
length -= nread;
|
||||
for (int m = 0; m < nread; m++)
|
||||
BW[m] = (byte) s[sp++];
|
||||
if (nread < 64) {
|
||||
nbits = nread * 8;
|
||||
|
||||
if ((lo_length += nbits) < (long) nbits)
|
||||
hi_length++;
|
||||
if ((nread < 64) && (padded == 0)) {
|
||||
BW[nread++] = (byte) 0x080;
|
||||
padded = 1;
|
||||
}
|
||||
|
||||
for (i = nread; i < 64; i++)
|
||||
BW[i] = 0;
|
||||
|
||||
byte[] tar = new byte[4];
|
||||
for (int z = 0; z < 64; z = z + 4) {
|
||||
for (int y = 0; y < 4; y++)
|
||||
tar[y] = BW[z + y];
|
||||
|
||||
W[z >> 2] = 0;
|
||||
W[z >> 2] = (W[z >> 2] | tar[0]) << 8;
|
||||
W[z >> 2] = (W[z >> 2] | (tar[1] & 0x00FF)) << 8;
|
||||
W[z >> 2] = ((W[z >> 2] | (tar[2] & 0x00FF)) << 8)
|
||||
| (tar[3] & 0x00FF);
|
||||
}
|
||||
|
||||
if (nread <= 56) {
|
||||
W[14] = hi_length;
|
||||
W[15] = lo_length;
|
||||
}
|
||||
} else {
|
||||
if ((lo_length += 512) < 512)
|
||||
hi_length++;
|
||||
|
||||
byte[] tar = new byte[4];
|
||||
for (int z = 0; z < 64; z = z + 4) {
|
||||
for (int y = 0; y < 4; y++)
|
||||
tar[y] = BW[z + y];
|
||||
|
||||
W[z >> 2] = 0;
|
||||
W[z >> 2] = (W[z >> 4] | tar[0]) << 8;
|
||||
W[z >> 2] = (W[z >> 2] | (tar[1] & 0x00FF)) << 8;
|
||||
W[z >> 2] = ((W[z >> 2] | (tar[2] & 0x00FF)) << 8)
|
||||
| (tar[3] & 0x00FF);
|
||||
}
|
||||
}
|
||||
p0 = 0;
|
||||
A = h0;
|
||||
B = h1;
|
||||
C = h2;
|
||||
D = h3;
|
||||
E = h4;
|
||||
|
||||
r0(0, K0);
|
||||
r0(0, K0);
|
||||
r0(0, K0);
|
||||
r0(0, K0);
|
||||
r0(0, K0);
|
||||
r0(0, K0);
|
||||
r0(0, K0);
|
||||
r0(0, K0);
|
||||
r0(0, K0);
|
||||
r0(0, K0);
|
||||
r0(0, K0);
|
||||
r0(0, K0);
|
||||
r0(0, K0);
|
||||
r0(0, K0);
|
||||
r0(0, K0);
|
||||
r0(0, K0);
|
||||
|
||||
p1 = 13;
|
||||
p2 = 8;
|
||||
p3 = 2;
|
||||
p4 = 0;
|
||||
r1(0, K0);
|
||||
r1(0, K0);
|
||||
r1(0, K0);
|
||||
r1(0, K0);
|
||||
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
r1(1, K1);
|
||||
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
r1(2, K2);
|
||||
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
r1(3, K3);
|
||||
|
||||
h0 += A;
|
||||
h1 += B;
|
||||
h2 += C;
|
||||
h3 += D;
|
||||
h4 += E;
|
||||
|
||||
// System.gc();
|
||||
if (nread <= 56)
|
||||
break;
|
||||
}
|
||||
|
||||
W = null;
|
||||
BW = null;
|
||||
s = null;
|
||||
A = 0;
|
||||
B = 0;
|
||||
C = 0;
|
||||
D = 0;
|
||||
E = 0;
|
||||
temp = 0;
|
||||
h4 = 0;
|
||||
h3 = 0;
|
||||
h2 = 0;
|
||||
h1 = 0;
|
||||
p0 = 0;
|
||||
p1 = 0;
|
||||
p2 = 0;
|
||||
p3 = 0;
|
||||
p4 = 0;
|
||||
|
||||
int highBit, lowBit;
|
||||
|
||||
lowBit = (int) (h0 & 0x0FFFF);
|
||||
highBit = (int) ((h0 >> 16) & 0x0FFFFF);
|
||||
h0 = 0;
|
||||
String shaHigh = new String(Integer.toHexString(highBit));
|
||||
|
||||
int leng = shaHigh.length();
|
||||
if (leng > 4) {
|
||||
shaHigh = shaHigh.substring(leng - 4);
|
||||
|
||||
} else if (leng < 4) {
|
||||
for (int m = 0; m < 4 - leng; m++)
|
||||
shaHigh = "0" + shaHigh;
|
||||
}
|
||||
|
||||
String shaLow = new String(Integer.toHexString(lowBit));
|
||||
|
||||
leng = shaLow.length();
|
||||
if (leng > 4) {
|
||||
shaLow = shaLow.substring(leng - 4);
|
||||
|
||||
} else if (leng < 4) {
|
||||
for (int m = 0; m < 4 - leng; m++)
|
||||
shaLow = "0" + shaLow;
|
||||
}
|
||||
return Long.parseLong(shaHigh + shaLow, 16);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package javautils.html;
|
||||
|
||||
import org.htmlparser.Parser;
|
||||
import org.htmlparser.filters.CssSelectorNodeFilter;
|
||||
import org.htmlparser.filters.TagNameFilter;
|
||||
import org.htmlparser.util.NodeList;
|
||||
import org.htmlparser.util.ParserException;
|
||||
|
||||
public class HtmlUtils {
|
||||
|
||||
private HtmlUtils() {
|
||||
|
||||
}
|
||||
|
||||
public static NodeList FilterCss(String html, String css, String charset) throws ParserException {
|
||||
Parser parser = Parser.createParser(html, charset);
|
||||
CssSelectorNodeFilter filter = new CssSelectorNodeFilter(css);
|
||||
NodeList nodeList = parser.extractAllNodesThatMatch(filter);
|
||||
return nodeList;
|
||||
}
|
||||
|
||||
public static NodeList FilterTag(String html, String tag, String charset) throws ParserException {
|
||||
Parser parser = Parser.createParser(html, charset);
|
||||
TagNameFilter filter = new TagNameFilter(tag);
|
||||
NodeList nodeList = parser.extractAllNodesThatMatch(filter);
|
||||
return nodeList;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
// package javautils.http;
|
||||
//
|
||||
// import javautils.StringUtil;
|
||||
// import org.apache.commons.httpclient.*;
|
||||
// import org.apache.commons.httpclient.methods.GetMethod;
|
||||
// import org.apache.commons.httpclient.methods.PostMethod;
|
||||
// import org.apache.commons.httpclient.methods.RequestEntity;
|
||||
// import org.apache.commons.httpclient.methods.StringRequestEntity;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
//
|
||||
// import java.io.InputStream;
|
||||
// import java.net.SocketTimeoutException;
|
||||
//
|
||||
// public class EasyHttpClient {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(EasyHttpClient.class);
|
||||
//
|
||||
// private HttpClient httpClient;
|
||||
// private final int TIMEOUT = 10 * 1000;
|
||||
// private final int SO_TIMEOUT = 10 * 1000;
|
||||
// private int REPEAT_TIMES = 3;
|
||||
//
|
||||
// public EasyHttpClient() {
|
||||
// init();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 初始化
|
||||
// */
|
||||
// public void init() {
|
||||
// MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
|
||||
// manager.getParams().setMaxTotalConnections(512); // 最大连接数
|
||||
// manager.getParams().setDefaultMaxConnectionsPerHost(512); // 每个主机默认最大连接数
|
||||
// httpClient = new HttpClient(manager);
|
||||
// setTimeOut(TIMEOUT, SO_TIMEOUT);
|
||||
// }
|
||||
//
|
||||
// private void setHeaders(HttpMethod method) {
|
||||
// method.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;");
|
||||
// method.setRequestHeader("Accept-Language", "zh-cn");
|
||||
// method.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3");
|
||||
// method.setRequestHeader("Accept-Charset", "utf-8");
|
||||
// method.setRequestHeader("Keep-Alive", "300");
|
||||
// method.setRequestHeader("Connection", "Keep-Alive");
|
||||
// method.setRequestHeader("Cache-Control", "no-cache");
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 设置超时时间
|
||||
// * @param TIMEOUT
|
||||
// * @param SO_TIMEOUT
|
||||
// * @param TIME_OUT_INIT
|
||||
// */
|
||||
// public void setTimeOut(int TIMEOUT, int SO_TIMEOUT) {
|
||||
// httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(TIMEOUT);
|
||||
// httpClient.getHttpConnectionManager().getParams().setSoTimeout(SO_TIMEOUT);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 设置请求重复次数
|
||||
// * @param times
|
||||
// */
|
||||
// public void setRepeatTimes(int times) {
|
||||
// this.REPEAT_TIMES = times;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * GET请求
|
||||
// * @param url
|
||||
// * @return
|
||||
// */
|
||||
// public String get(String url) {
|
||||
// GetMethod get = new GetMethod(url);
|
||||
// for (int i = 0; i < REPEAT_TIMES; i++) {
|
||||
// logger.debug("正在发送请求,当前第" + (i + 1) + "次...");
|
||||
// try {
|
||||
// setHeaders(get);
|
||||
// int state = httpClient.executeMethod(get);
|
||||
// logger.debug("请求状态" + state);
|
||||
// if(state == HttpStatus.SC_OK) {
|
||||
// InputStream inputStream = get.getResponseBodyAsStream();
|
||||
// String data = StringUtil.fromInputStream(inputStream);
|
||||
// logger.debug("成功获取到数据,长度为" + data.length());
|
||||
// return data;
|
||||
// }
|
||||
// } catch(ConnectTimeoutException e) {
|
||||
// logger.error("请求超时...Connect Timeout");
|
||||
// } catch(SocketTimeoutException e) {
|
||||
// logger.error("请求超时...Socket Timeout");
|
||||
// } catch (Exception e) {
|
||||
// logger.error("请求出错...", e);
|
||||
// } finally {
|
||||
// closeConnection(get);
|
||||
// }
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * POST请求
|
||||
// * @param url
|
||||
// * @param params
|
||||
// * @return
|
||||
// */
|
||||
// public String post(String url, NameValuePair[] params) {
|
||||
// PostMethod post = new PostMethod(url);
|
||||
// if(params != null) {
|
||||
// post.setRequestBody(params);
|
||||
// }
|
||||
// for (int i = 0; i < REPEAT_TIMES; i++) {
|
||||
// logger.debug("正在发送请求,当前第" + (i + 1) + "次...");
|
||||
// try {
|
||||
// int state = httpClient.executeMethod(post);
|
||||
// logger.debug("请求状态" + state);
|
||||
// if(state == HttpStatus.SC_OK) {
|
||||
// InputStream inputStream = post.getResponseBodyAsStream();
|
||||
// String data = StringUtil.fromInputStream(inputStream);
|
||||
// logger.debug("成功获取到数据,长度为" + data.length());
|
||||
// return data;
|
||||
// }
|
||||
// } catch(ConnectTimeoutException e) {
|
||||
// logger.error("请求超时...Connect Timeout");
|
||||
// } catch(SocketTimeoutException e) {
|
||||
// logger.error("请求超时...Socket Timeout");
|
||||
// } catch (Exception e) {
|
||||
// logger.error("请求出错...", e);
|
||||
// } finally {
|
||||
// closeConnection(post);
|
||||
// }
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// public String postString(String url, String string) {
|
||||
// PostMethod post = new PostMethod(url);
|
||||
// for (int i = 0; i < REPEAT_TIMES; i++) {
|
||||
// logger.debug("正在发送请求,当前第" + (i + 1) + "次...");
|
||||
// try {
|
||||
// RequestEntity requestEntity = new StringRequestEntity(string, null, null);
|
||||
// post.setRequestEntity(requestEntity);
|
||||
// int state = httpClient.executeMethod(post);
|
||||
// logger.debug("请求状态" + state);
|
||||
// if(state == HttpStatus.SC_OK) {
|
||||
// InputStream inputStream = post.getResponseBodyAsStream();
|
||||
// String data = StringUtil.fromInputStream(inputStream);
|
||||
// logger.debug("成功获取到数据,长度为" + data.length());
|
||||
// return data;
|
||||
// }
|
||||
// } catch(ConnectTimeoutException e) {
|
||||
// logger.error("请求超时...Connect Timeout");
|
||||
// } catch(SocketTimeoutException e) {
|
||||
// logger.error("请求超时...Socket Timeout");
|
||||
// } catch (Exception e) {
|
||||
// logger.error("请求出错...", e);
|
||||
// } finally {
|
||||
// closeConnection(post);
|
||||
// }
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 关闭连接
|
||||
// * @param method
|
||||
// */
|
||||
// private void closeConnection(HttpMethod method) {
|
||||
// method.releaseConnection();
|
||||
// MultiThreadedHttpConnectionManager manager = (MultiThreadedHttpConnectionManager) httpClient.getHttpConnectionManager();
|
||||
// manager.closeIdleConnections(0);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
@@ -0,0 +1,268 @@
|
||||
package javautils.http;
|
||||
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.conn.ConnectionKeepAliveStrategy;
|
||||
import org.apache.http.entity.InputStreamEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLDecoder;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* http通用工具类,适用普通https和http(非连接池),如果是带ssl密码的,请参考{@link lottery.domains.content.api.pt.Win88PTAPI}
|
||||
* Created by Nick on 2017-06-15.
|
||||
*/
|
||||
public class HttpClientUtil {
|
||||
private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class); //日志记录
|
||||
|
||||
private static CloseableHttpClient getHttpClient(int timeout) {
|
||||
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
|
||||
|
||||
// 是否要keep alive
|
||||
ConnectionKeepAliveStrategy keepAliveStrategy = HttpConfigBuilder.getInstance().buildKeepAliveStrategy(60 * 1000);
|
||||
clientBuilder.setKeepAliveStrategy(keepAliveStrategy);
|
||||
|
||||
RequestConfig requestConfig = HttpConfigBuilder.getInstance().buildRequestConfig(timeout);
|
||||
clientBuilder.setDefaultRequestConfig(requestConfig);
|
||||
|
||||
// 连接池
|
||||
// PoolingHttpClientConnectionManager connMgr = HttpConfigBuilder.getInstance().buildPoolingHttpClientConnectionManager(100, 100);
|
||||
// clientBuilder.setConnectionManager(connMgr);
|
||||
|
||||
CloseableHttpClient httpClient = clientBuilder.build();
|
||||
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
private static CloseableHttpClient getHttpsClient(int timeout, String sslVersion) throws NoSuchAlgorithmException, KeyManagementException {
|
||||
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
|
||||
|
||||
// 是否要keep alive
|
||||
ConnectionKeepAliveStrategy keepAliveStrategy = HttpConfigBuilder.getInstance().buildKeepAliveStrategy(60 * 1000);
|
||||
clientBuilder.setKeepAliveStrategy(keepAliveStrategy);
|
||||
|
||||
RequestConfig requestConfig = HttpConfigBuilder.getInstance().buildRequestConfig(timeout);
|
||||
clientBuilder.setDefaultRequestConfig(requestConfig);
|
||||
|
||||
// ssl连接
|
||||
BasicHttpClientConnectionManager connMgr;
|
||||
if (sslVersion == null) {
|
||||
connMgr = HttpConfigBuilder.getInstance().buildBasicHttpsClientConnectionManager();
|
||||
}
|
||||
else {
|
||||
connMgr = HttpConfigBuilder.getInstance().buildBasicHttpsClientConnectionManager(sslVersion);
|
||||
}
|
||||
clientBuilder.setConnectionManager(connMgr);
|
||||
|
||||
CloseableHttpClient httpClient = clientBuilder.build();
|
||||
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
public static String get(String url ,Map<String ,String > httpHeader, int timeout) throws Exception{
|
||||
CloseableHttpClient httpClient;
|
||||
if (url.startsWith("https://")) {
|
||||
httpClient = getHttpsClient(timeout, null);
|
||||
}
|
||||
else {
|
||||
httpClient = getHttpClient(timeout);
|
||||
}
|
||||
|
||||
return get(httpClient, url, httpHeader);
|
||||
}
|
||||
|
||||
public static String get(CloseableHttpClient httpClient, String url ,Map<String ,String > httpHeader) throws IOException{
|
||||
String strResult = null;
|
||||
try {
|
||||
//发送get请求
|
||||
HttpGet request = new HttpGet(url);
|
||||
|
||||
//设置http头参数
|
||||
if(httpHeader!=null && httpHeader.size()>0){
|
||||
for(Iterator<Map.Entry<String, String>> ies = httpHeader.entrySet().iterator(); ies.hasNext();){
|
||||
Map.Entry<String, String> entry = ies.next();
|
||||
String key =entry.getKey();
|
||||
String value = entry.getValue();
|
||||
request.addHeader(key, value);
|
||||
}
|
||||
}
|
||||
CloseableHttpResponse response = httpClient.execute(request);
|
||||
|
||||
/**请求发送成功,并得到响应**/
|
||||
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
||||
try {
|
||||
/**读取服务器返回过来的json字符串数据**/
|
||||
strResult = EntityUtils.toString(response.getEntity(),"UTF-8");
|
||||
url = URLDecoder.decode(url, "UTF-8");
|
||||
}catch(Exception e) {
|
||||
logger.error("get请求提交失败:" + url);
|
||||
}finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
logger.error("get请求提交失败:" + url, e);
|
||||
} finally {
|
||||
httpClient.close();
|
||||
}
|
||||
return strResult;
|
||||
}
|
||||
|
||||
public static String post(String url, Map<String, String> params, Map<String, String> headers, int timeout) throws Exception{
|
||||
CloseableHttpClient httpClient;
|
||||
if (url.startsWith("https://")) {
|
||||
httpClient = getHttpsClient(timeout, null);
|
||||
}
|
||||
else {
|
||||
httpClient = getHttpClient(timeout);
|
||||
}
|
||||
|
||||
return post(httpClient, url, params, headers);
|
||||
}
|
||||
|
||||
public static String postAsStream(String url, Map<String, String> params, Map<String, String> headers, int timeout) throws Exception{
|
||||
CloseableHttpClient httpClient;
|
||||
if (url.startsWith("https://")) {
|
||||
httpClient = getHttpsClient(timeout, null);
|
||||
}
|
||||
else {
|
||||
httpClient = getHttpClient(timeout);
|
||||
}
|
||||
|
||||
return postAsStream(httpClient, url, params, headers);
|
||||
}
|
||||
|
||||
public static String postSSL(String url, Map<String, String> params, Map<String, String> headers, int timeout, String sslVersion) throws Exception{
|
||||
CloseableHttpClient httpClient;
|
||||
if (url.startsWith("https://")) {
|
||||
httpClient = getHttpsClient(timeout, sslVersion);
|
||||
}
|
||||
else {
|
||||
httpClient = getHttpClient(timeout);
|
||||
}
|
||||
|
||||
return post(httpClient, url, params, headers);
|
||||
}
|
||||
|
||||
public static String post(CloseableHttpClient httpClient, String url, Map<String, String> params, Map<String, String> headers) throws IOException {
|
||||
String result = null;
|
||||
try {
|
||||
HttpPost request = new HttpPost(url);
|
||||
|
||||
//设置http头参数
|
||||
if(headers!=null && headers.size()>0) {
|
||||
for(Iterator<Map.Entry<String, String>> ies = headers.entrySet().iterator(); ies.hasNext();){
|
||||
Map.Entry<String, String> entry = ies.next();
|
||||
|
||||
String key =entry.getKey();
|
||||
String value = entry.getValue();
|
||||
|
||||
request.addHeader(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
if (params != null && params.size() > 0) {
|
||||
List list = new ArrayList();
|
||||
Iterator iterator = params.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry elem = (Map.Entry) iterator.next();
|
||||
list.add(new BasicNameValuePair((String) elem.getKey(), (String) elem.getValue()));
|
||||
}
|
||||
if (list.size() > 0) {
|
||||
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "utf-8");
|
||||
request.setEntity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
CloseableHttpResponse response = httpClient.execute(request);
|
||||
url = URLDecoder.decode(url, "UTF-8");
|
||||
|
||||
/**请求发送成功,并得到响应**/
|
||||
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
||||
try {
|
||||
/**读取服务器返回过来的字符串数据**/
|
||||
result = EntityUtils.toString(response.getEntity(),"UTF-8");
|
||||
} catch (Exception e) {
|
||||
logger.error("post请求提交失败:" + url, e);
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
} else {
|
||||
result = response.getStatusLine().getStatusCode()+"-"+response.getStatusLine().getReasonPhrase();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("post请求提交失败:" + url, e);
|
||||
} finally{
|
||||
httpClient.close();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String postAsStream(CloseableHttpClient httpClient, String url, Map<String, String> params, Map<String, String> headers) throws IOException {
|
||||
String result = null;
|
||||
try {
|
||||
HttpPost request = new HttpPost(url);
|
||||
|
||||
//设置http头参数
|
||||
if(headers!=null && headers.size()>0) {
|
||||
for(Iterator<Map.Entry<String, String>> ies = headers.entrySet().iterator(); ies.hasNext();){
|
||||
Map.Entry<String, String> entry = ies.next();
|
||||
|
||||
String key =entry.getKey();
|
||||
String value = entry.getValue();
|
||||
|
||||
request.addHeader(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
if (params != null && params.size() > 0) {
|
||||
String paramUrl = ToUrlParamUtils.toUrlParam(params);
|
||||
InputStream is = new ByteArrayInputStream(paramUrl.getBytes());
|
||||
InputStreamEntity inputEntry = new InputStreamEntity(is);
|
||||
request.setEntity(inputEntry);
|
||||
}
|
||||
|
||||
CloseableHttpResponse response = httpClient.execute(request);
|
||||
url = URLDecoder.decode(url, "UTF-8");
|
||||
|
||||
/**请求发送成功,并得到响应**/
|
||||
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
||||
try {
|
||||
/**读取服务器返回过来的字符串数据**/
|
||||
result = EntityUtils.toString(response.getEntity(),"UTF-8");
|
||||
} catch (Exception e) {
|
||||
logger.error("post请求提交失败:" + url, e);
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
} else {
|
||||
result = response.getStatusLine().getStatusCode()+"-"+response.getStatusLine().getReasonPhrase();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("post请求提交失败:" + url, e);
|
||||
} finally{
|
||||
httpClient.close();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
package javautils.http;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpRequestRetryHandler;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.config.Registry;
|
||||
import org.apache.http.config.RegistryBuilder;
|
||||
import org.apache.http.conn.ConnectionKeepAliveStrategy;
|
||||
import org.apache.http.conn.socket.ConnectionSocketFactory;
|
||||
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
|
||||
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
|
||||
import javax.net.ssl.*;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.security.*;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
/**
|
||||
* http 配置
|
||||
* @author Nick
|
||||
* @since 2015-10-2
|
||||
*/
|
||||
public class HttpConfigBuilder {
|
||||
private static final HttpConfigBuilder INSTANCE = new HttpConfigBuilder();
|
||||
private HttpConfigBuilder() {
|
||||
}
|
||||
|
||||
public static HttpConfigBuilder getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public ConnectionKeepAliveStrategy buildKeepAliveStrategy(final long duration) {
|
||||
return new ConnectionKeepAliveStrategy() {
|
||||
@Override
|
||||
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
|
||||
return duration;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public HttpRequestRetryHandler buildRetryHandler(final int retryCount) {
|
||||
return new DefaultHttpRequestRetryHandler(retryCount, false);
|
||||
}
|
||||
|
||||
public RequestConfig buildRequestConfig(final int timeout) {
|
||||
return RequestConfig.custom().setConnectionRequestTimeout(timeout).setConnectTimeout(timeout).setSocketTimeout(timeout).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 新建单个http连接
|
||||
*/
|
||||
public BasicHttpClientConnectionManager buildBasicHttpClientConnectionManager() {
|
||||
BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
|
||||
return connectionManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新建单个https连接,信任所有
|
||||
*/
|
||||
public BasicHttpClientConnectionManager buildBasicHttpsClientConnectionManager() {
|
||||
try {
|
||||
TrustManager[] trustAllCerts = new TrustManager[] {
|
||||
new X509TrustManager() {
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void checkClientTrusted(X509Certificate[] certs, String authType) { }
|
||||
|
||||
public void checkServerTrusted(X509Certificate[] certs, String authType) { }
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||
sslContext.init(null, trustAllCerts, new SecureRandom());
|
||||
|
||||
HostnameVerifier allHostsValid = new HostnameVerifier() {
|
||||
public boolean verify(String hostname, SSLSession session) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, allHostsValid);
|
||||
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register("http", PlainConnectionSocketFactory.getSocketFactory())
|
||||
.register("https", sslSocketFactory)
|
||||
.build();
|
||||
BasicHttpClientConnectionManager connMgr = new BasicHttpClientConnectionManager(socketFactoryRegistry);
|
||||
return connMgr;
|
||||
} catch (KeyManagementException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("初始化https连接出错");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("初始化https连接出错");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新建单个https连接,信任所有
|
||||
*/
|
||||
public BasicHttpClientConnectionManager buildBasicHttpsClientConnectionManager(String sslVersion) {
|
||||
try {
|
||||
TrustManager[] trustAllCerts = new TrustManager[] {
|
||||
new X509TrustManager() {
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void checkClientTrusted(X509Certificate[] certs, String authType) { }
|
||||
|
||||
public void checkServerTrusted(X509Certificate[] certs, String authType) { }
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||
sslContext.init(null, trustAllCerts, new SecureRandom());
|
||||
|
||||
HostnameVerifier allHostsValid = new HostnameVerifier() {
|
||||
public boolean verify(String hostname, SSLSession session) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, new String[]{sslVersion}, null, allHostsValid);
|
||||
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register("http", PlainConnectionSocketFactory.getSocketFactory())
|
||||
.register("https", sslSocketFactory)
|
||||
.build();
|
||||
BasicHttpClientConnectionManager connMgr = new BasicHttpClientConnectionManager(socketFactoryRegistry);
|
||||
return connMgr;
|
||||
} catch (KeyManagementException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("初始化https连接出错");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("初始化https连接出错");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新建https连接池,信任所有
|
||||
*/
|
||||
public PoolingHttpClientConnectionManager buildPoolingHttpsClientConnectionManager(final int maxTotal, final int defaultMaxPerRoute) {
|
||||
try {
|
||||
TrustManager[] trustAllCerts = new TrustManager[] {
|
||||
new X509TrustManager() {
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void checkClientTrusted(X509Certificate[] certs, String authType) { }
|
||||
|
||||
public void checkServerTrusted(X509Certificate[] certs, String authType) { }
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||
sslContext.init(null, trustAllCerts, new SecureRandom());
|
||||
|
||||
HostnameVerifier allHostsValid = new HostnameVerifier() {
|
||||
public boolean verify(String hostname, SSLSession session) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, allHostsValid);
|
||||
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register("http", PlainConnectionSocketFactory.getSocketFactory())
|
||||
.register("https", sslSocketFactory)
|
||||
.build();
|
||||
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
|
||||
connectionManager.setMaxTotal(maxTotal);
|
||||
connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
|
||||
return connectionManager;
|
||||
} catch (KeyManagementException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("初始化https连接出错");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("初始化https连接出错");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新建普通http连接池
|
||||
*/
|
||||
public PoolingHttpClientConnectionManager buildPoolingHttpClientConnectionManager(final int maxTotal, final int defaultMaxPerRoute) {
|
||||
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
|
||||
connectionManager.setMaxTotal(maxTotal);
|
||||
connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
|
||||
return connectionManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新建带SSL证书密码的连接池
|
||||
*/
|
||||
public PoolingHttpClientConnectionManager buildP12PoolingHttpClientConnectionManager(final int maxTotal, final int defaultMaxPerRoute, File p12File, String pwd) {
|
||||
try {
|
||||
KeyStore ks = KeyStore.getInstance("PKCS12");
|
||||
FileInputStream fis = new FileInputStream(p12File);
|
||||
ks.load(fis, pwd.toCharArray());
|
||||
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
|
||||
kmf.init(ks, pwd.toCharArray());
|
||||
KeyManager[] kms = kmf.getKeyManagers();
|
||||
|
||||
//Crete TrustManager to bypass trusted certificate check
|
||||
TrustManager[] trustAllCerts = new TrustManager[] {
|
||||
new X509TrustManager() {
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void checkClientTrusted(X509Certificate[] certs, String authType) { }
|
||||
|
||||
public void checkServerTrusted(X509Certificate[] certs, String authType) { }
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||
sslContext.init(kms, trustAllCerts, new SecureRandom());
|
||||
|
||||
HostnameVerifier allHostsValid = new HostnameVerifier() {
|
||||
public boolean verify(String hostname, SSLSession session) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, allHostsValid);
|
||||
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register("http", PlainConnectionSocketFactory.getSocketFactory())
|
||||
.register("https", sslSocketFactory)
|
||||
.build();
|
||||
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager( socketFactoryRegistry);
|
||||
connMgr.setMaxTotal(maxTotal);
|
||||
connMgr.setDefaultMaxPerRoute(defaultMaxPerRoute);
|
||||
|
||||
return connMgr;
|
||||
} catch (KeyStoreException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("初始化P12连接出错");
|
||||
} catch (CertificateException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("初始化P12连接出错");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("初始化P12连接出错");
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("初始化P12连接出错");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("初始化P12连接出错");
|
||||
} catch (UnrecoverableKeyException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("初始化P12连接出错");
|
||||
} catch (KeyManagementException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("初始化P12连接出错");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package javautils.http;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
* url工具类
|
||||
*
|
||||
* @author Mick
|
||||
* @since 2015-9-3
|
||||
*/
|
||||
public class ToUrlParamUtils {
|
||||
private static final String EMPTY = "";
|
||||
private static final String EQUALS = "=";
|
||||
private static final String DEFAULT_SEPARATOR = "&";
|
||||
|
||||
public static String toUrlParam(Map<String, String> params) {
|
||||
return toUrlParam(params, DEFAULT_SEPARATOR, true);
|
||||
}
|
||||
|
||||
public static String toUrlParam(Map<String, String> params, String separator) {
|
||||
return toUrlParam(params, separator, true);
|
||||
}
|
||||
|
||||
public static String toUrlParam(Map<String, String> params, String separator, boolean ignoreEmpty) {
|
||||
if (params == null || params.isEmpty()) {
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
StringBuffer url = new StringBuffer();
|
||||
|
||||
Iterator<Entry<String, String>> it = params.entrySet().iterator();
|
||||
while(it.hasNext()) {
|
||||
Entry<String, String> entry = it.next();
|
||||
String value = entry.getValue();
|
||||
boolean valueIsEmpty = isEmpty(value);
|
||||
if (ignoreEmpty && valueIsEmpty) { // 主动忽略空值并且值为空
|
||||
continue;
|
||||
}
|
||||
|
||||
url.append(entry.getKey()).append(EQUALS) .append(value);
|
||||
if (it.hasNext()) {
|
||||
url.append(separator);
|
||||
}
|
||||
}
|
||||
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
private static boolean isEmpty(String value) {
|
||||
return value == null || EMPTY.equals(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package javautils.jdbc;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 分页类
|
||||
* @author root
|
||||
*
|
||||
*/
|
||||
public class PageList {
|
||||
|
||||
private List<?> list;
|
||||
private int count;
|
||||
|
||||
public PageList() {
|
||||
this.list = new ArrayList<>();
|
||||
this.count = 0;
|
||||
}
|
||||
|
||||
public PageList(List<?> list, int count) {
|
||||
this.list = list;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public List<?> getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setList(List<?> list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(int count) {
|
||||
this.count = count;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package javautils.jdbc.helper;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONObject;
|
||||
|
||||
public class JDBCHelper {
|
||||
|
||||
public final static String MYSQL = "com.mysql.jdbc.Driver";
|
||||
|
||||
public static Connection openConnection(String url, String username,
|
||||
String password, String type) {
|
||||
Connection conn = null;
|
||||
try {
|
||||
Class.forName(type);
|
||||
conn = DriverManager.getConnection(url, username, password);
|
||||
} catch (Exception e) {}
|
||||
return conn;
|
||||
}
|
||||
|
||||
public static boolean executeUpdate(String sql, Connection conn) {
|
||||
boolean flag = false;
|
||||
if (conn != null) {
|
||||
PreparedStatement pst = null;
|
||||
try {
|
||||
pst = conn.prepareStatement(sql);
|
||||
int count = pst.executeUpdate();
|
||||
flag = count > 0 ? true : false;
|
||||
} catch (Exception e) {
|
||||
} finally {
|
||||
try {
|
||||
if (pst != null) pst.close();
|
||||
if (conn != null) conn.close();
|
||||
} catch (SQLException e) {}
|
||||
}
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
public static JSONArray executeQuery(String sql, Connection conn) {
|
||||
JSONArray list = null;
|
||||
if (conn != null) {
|
||||
PreparedStatement pst = null;
|
||||
try {
|
||||
pst = conn.prepareStatement(sql);
|
||||
ResultSet rs = pst.executeQuery();
|
||||
list = toJsonArray(rs);
|
||||
} catch (Exception e) {} finally {
|
||||
try {
|
||||
if (pst != null) pst.close();
|
||||
if (conn != null) conn.close();
|
||||
} catch (SQLException e) {}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static JSONArray toJsonArray(ResultSet rs) throws Exception {
|
||||
JSONArray list = new JSONArray();
|
||||
if (rs != null) {
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
||||
int columnCount = metaData.getColumnCount();
|
||||
while (rs.next()) {
|
||||
JSONObject obj = new JSONObject();
|
||||
for (int i = 0; i < columnCount; i++) {
|
||||
String columnName = metaData.getColumnLabel(i + 1);
|
||||
String value = rs.getString(columnName);
|
||||
obj.put(columnName, value);
|
||||
}
|
||||
list.add(obj);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* java:comp/env/jdbc/books
|
||||
* @param jndi
|
||||
* @return
|
||||
*/
|
||||
public static Connection openConnection(String jndi) {
|
||||
Connection conn = null;
|
||||
try {
|
||||
Context context = new InitialContext();
|
||||
DataSource source = (DataSource) context.lookup(jndi);
|
||||
conn = source.getConnection();
|
||||
} catch (Exception e) {}
|
||||
return conn;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String url = "jdbc:mysql://192.168.1.111:3306/test?useUnicode=true&characterEncoding=utf-8";
|
||||
String username = "root";
|
||||
String password = "root";
|
||||
Connection conn = JDBCHelper.openConnection(url, username, password, JDBCHelper.MYSQL);
|
||||
String sql = "select * from user";
|
||||
JDBCHelper.executeQuery(sql, conn);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,506 @@
|
||||
package javautils.jdbc.hibernate;
|
||||
|
||||
import javautils.jdbc.PageList;
|
||||
import javautils.jdbc.util.BatchSQLUtil;
|
||||
import org.hibernate.*;
|
||||
import org.hibernate.criterion.Criterion;
|
||||
import org.hibernate.criterion.Order;
|
||||
import org.hibernate.criterion.Projection;
|
||||
import org.hibernate.criterion.Projections;
|
||||
import org.hibernate.jdbc.Work;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@SuppressWarnings("unchecked")
|
||||
public class HibernateSuperDao<T> {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(HibernateSuperDao.class);
|
||||
|
||||
@Autowired
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
public Session getCurrentSession() {
|
||||
return sessionFactory.getCurrentSession();
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存
|
||||
* @param entity
|
||||
* @return
|
||||
*/
|
||||
@Transactional
|
||||
public boolean save(final Object entity) {
|
||||
boolean flag = false;
|
||||
try {
|
||||
getCurrentSession().save(entity);
|
||||
flag = true;
|
||||
} catch (Exception e) {
|
||||
logger.error("保存实体类异常", e);
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新
|
||||
* @param entity
|
||||
* @return
|
||||
*/
|
||||
@Transactional
|
||||
public boolean update(final Object entity) {
|
||||
boolean flag = false;
|
||||
try {
|
||||
getCurrentSession().update(entity);
|
||||
flag = true;
|
||||
} catch (Exception e) {
|
||||
logger.error("更新实体类异常", e);
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新
|
||||
* @param hql
|
||||
* @param values
|
||||
* @return
|
||||
*/
|
||||
@Transactional
|
||||
public boolean update(final String hql, final Object[] values) {
|
||||
boolean flag = false;
|
||||
try {
|
||||
Query query = getCurrentSession().createQuery(hql);
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
query.setParameter(String.valueOf(i), values[i]);
|
||||
}
|
||||
int result = query.executeUpdate();
|
||||
flag = result > 0 ? true : false;
|
||||
} catch (Exception e) {
|
||||
logger.error("更新实体类异常", e);
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
* @param entity
|
||||
* @return
|
||||
*/
|
||||
public boolean delete(final Object entity) {
|
||||
boolean flag = false;
|
||||
try {
|
||||
getCurrentSession().delete(entity);
|
||||
flag = true;
|
||||
} catch (Exception e) {
|
||||
logger.error("删除实体类异常", e);
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
* @param hql
|
||||
* @param values
|
||||
* @return
|
||||
*/
|
||||
@Transactional
|
||||
public boolean delete(final String hql, final Object[] values) {
|
||||
boolean flag = false;
|
||||
try {
|
||||
Query query = getCurrentSession().createQuery(hql);
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
query.setParameter(String.valueOf(i), values[i]);
|
||||
}
|
||||
int result = query.executeUpdate();
|
||||
flag = result > 0 ? true : false;
|
||||
} catch (Exception e) {
|
||||
logger.error("删除实体类异常", e);
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询
|
||||
* @param hql
|
||||
* @return
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public List<T> list(final String hql) {
|
||||
try {
|
||||
Query query = getCurrentSession().createQuery(hql);
|
||||
return query.list();
|
||||
} catch (Exception e) {
|
||||
logger.error("查询出错", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询
|
||||
* @param hql
|
||||
* @param start
|
||||
* @param limit
|
||||
* @return
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public List<T> list(final String hql, final int start, final int limit) {
|
||||
try {
|
||||
Query query = getCurrentSession().createQuery(hql);
|
||||
if(start >= 0 && limit > 0) {
|
||||
query.setFirstResult(start);
|
||||
query.setMaxResults(limit);
|
||||
}
|
||||
return query.list();
|
||||
} catch (Exception e) {
|
||||
logger.error("查询出错", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 唯一结果查询
|
||||
* @param hql
|
||||
* @return
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public Object unique(final String hql) {
|
||||
try {
|
||||
Query query = getCurrentSession().createQuery(hql);
|
||||
return query.uniqueResult();
|
||||
} catch (Exception e) {
|
||||
logger.error("查询出错", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 唯一结果查询
|
||||
* @param hql
|
||||
* @param values
|
||||
* @return
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public Object unique(final String hql, final Object[] values) {
|
||||
try {
|
||||
Query query = getCurrentSession().createQuery(hql);
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
query.setParameter(String.valueOf(i), values[i]);
|
||||
}
|
||||
return query.uniqueResult();
|
||||
} catch (Exception e) {
|
||||
logger.error("查询出错", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询
|
||||
* @param hql
|
||||
* @param values
|
||||
* @return
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public List<T> list(final String hql, final Object[] values) {
|
||||
try {
|
||||
Query query = getCurrentSession().createQuery(hql);
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
query.setParameter(String.valueOf(i), values[i]);
|
||||
}
|
||||
return query.list();
|
||||
} catch (Exception e) {
|
||||
logger.error("查询出错", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询
|
||||
* @param hql
|
||||
* @param values
|
||||
* @return
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public List<?> listObject(final String hql, final Object[] values) {
|
||||
try {
|
||||
Query query = getCurrentSession().createQuery(hql);
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
query.setParameter(String.valueOf(i), values[i]);
|
||||
}
|
||||
return query.list();
|
||||
} catch (Exception e) {
|
||||
logger.error("查询出错", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询
|
||||
* @param hql
|
||||
* @param values
|
||||
* @param start
|
||||
* @param limit
|
||||
* @return
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public List<T> list(final String hql, final Object[] values, final int start, final int limit) {
|
||||
try {
|
||||
Query query = getCurrentSession().createQuery(hql);
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
query.setParameter(String.valueOf(i), values[i]);
|
||||
}
|
||||
if(start >= 0 && limit > 0) {
|
||||
query.setFirstResult(start);
|
||||
query.setMaxResults(limit);
|
||||
}
|
||||
return query.list();
|
||||
} catch (Exception e) {
|
||||
logger.error("查询出错", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL查询
|
||||
* @param sql
|
||||
* @return
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public List<?> findWithSql(final String sql) {
|
||||
try {
|
||||
SQLQuery query = getCurrentSession().createSQLQuery(sql);
|
||||
return query.list();
|
||||
} catch (Exception e) {
|
||||
logger.error("查询出错", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL查询
|
||||
* @param sql
|
||||
* @return
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public Object uniqueWithSqlCount(final String sql) {
|
||||
try {
|
||||
SQLQuery query = getCurrentSession().createSQLQuery(sql);
|
||||
return query.uniqueResult();
|
||||
} catch (Exception e) {
|
||||
logger.error("查询出错", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
* @param querySql
|
||||
* @param countSql
|
||||
* @param start
|
||||
* @param limit
|
||||
* @return
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public PageList findPageList(final String querySql, final String countSql, final int start, final int limit) {
|
||||
try {
|
||||
PageList pList = new PageList();
|
||||
// 查询总数
|
||||
SQLQuery countQuery = getCurrentSession().createSQLQuery(countSql);
|
||||
Number result = (Number) countQuery.uniqueResult();
|
||||
int count = result.intValue();
|
||||
pList.setCount(count);
|
||||
// 查询集合
|
||||
SQLQuery listQuery = getCurrentSession().createSQLQuery(querySql);
|
||||
if(start >= 0 && limit > 0) {
|
||||
listQuery.setFirstResult(start);
|
||||
listQuery.setMaxResults(limit);
|
||||
}
|
||||
List<T> list = listQuery.list();
|
||||
pList.setList(list);
|
||||
return pList;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询出错", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
* @param clazz
|
||||
* @param criterions
|
||||
* @param orders
|
||||
* @param start
|
||||
* @param limit
|
||||
* @return
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public PageList findPageList(final Class<T> clazz, final List<Criterion> criterions, final List<Order> orders, final int start, final int limit) {
|
||||
try {
|
||||
PageList pList = new PageList();
|
||||
Criteria criteria = getCurrentSession().createCriteria(clazz);
|
||||
for (Criterion criterion : criterions) {
|
||||
criteria.add(criterion);
|
||||
}
|
||||
for (Order order : orders) {
|
||||
criteria.addOrder(order);
|
||||
}
|
||||
// 查询总数
|
||||
criteria.setProjection(Projections.rowCount());
|
||||
Number result = (Number) criteria.uniqueResult();
|
||||
int count = result.intValue();
|
||||
pList.setCount(count);
|
||||
criteria.setProjection(null);
|
||||
// 查询集合
|
||||
if(start >= 0 && limit > 0) {
|
||||
criteria.setFirstResult(start);
|
||||
criteria.setMaxResults(limit);
|
||||
}
|
||||
List<T> list = criteria.list();
|
||||
pList.setList(list);
|
||||
return pList;
|
||||
} catch (Exception e) {
|
||||
logger.error("查询出错", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Criteria查询
|
||||
* @param clazz
|
||||
* @param criterions
|
||||
* @param orders
|
||||
* @return
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public List<T> findByCriteria(final Class<T> clazz, final List<Criterion> criterions, final List<Order> orders) {
|
||||
try {
|
||||
Criteria criteria = getCurrentSession().createCriteria(clazz);
|
||||
for (Criterion criterion : criterions) {
|
||||
criteria.add(criterion);
|
||||
}
|
||||
for (Order order : orders) {
|
||||
criteria.addOrder(order);
|
||||
}
|
||||
return criteria.list();
|
||||
} catch (Exception e) {
|
||||
logger.error("查询出错", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Criteria查询
|
||||
* @param clazz
|
||||
* @param criterions
|
||||
* @param orders
|
||||
* @param limit
|
||||
* @return
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public List<T> findByCriteria(final Class<T> clazz, final List<Criterion> criterions, final List<Order> orders, final int start, final int limit) {
|
||||
try {
|
||||
Criteria criteria = getCurrentSession().createCriteria(clazz);
|
||||
for (Criterion criterion : criterions) {
|
||||
criteria.add(criterion);
|
||||
}
|
||||
for (Order order : orders) {
|
||||
criteria.addOrder(order);
|
||||
}
|
||||
if(start >= 0 && limit > 0) {
|
||||
criteria.setFirstResult(start);
|
||||
criteria.setMaxResults(limit);
|
||||
}
|
||||
return criteria.list();
|
||||
} catch (Exception e) {
|
||||
logger.error("查询出错", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Criteria查询
|
||||
* @param clazz
|
||||
* @param criterions
|
||||
* @param orders
|
||||
* @param projection
|
||||
* @param start
|
||||
* @param limit
|
||||
* @return
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public List<?> findByCriteria(final Class<T> clazz, final List<Criterion> criterions, final List<Order> orders, final Projection projection, final int start, final int limit) {
|
||||
try {
|
||||
Criteria criteria = getCurrentSession().createCriteria(clazz);
|
||||
for (Criterion criterion : criterions) {
|
||||
criteria.add(criterion);
|
||||
}
|
||||
for (Order order : orders) {
|
||||
criteria.addOrder(order);
|
||||
}
|
||||
criteria.setProjection(projection);
|
||||
if(start >= 0 && limit > 0) {
|
||||
criteria.setFirstResult(start);
|
||||
criteria.setMaxResults(limit);
|
||||
}
|
||||
return criteria.list();
|
||||
} catch (Exception e) {
|
||||
logger.error("查询出错", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Criteria查询
|
||||
* @param clazz
|
||||
* @param criterions
|
||||
* @param orders
|
||||
* @param projection
|
||||
* @param start
|
||||
* @param limit
|
||||
* @return
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public List<?> findByCriteria(final Class<T> clazz, final List<Criterion> criterions, final List<Order> orders, final Projection projection) {
|
||||
try {
|
||||
Criteria criteria = getCurrentSession().createCriteria(clazz);
|
||||
for (Criterion criterion : criterions) {
|
||||
criteria.add(criterion);
|
||||
}
|
||||
for (Order order : orders) {
|
||||
criteria.addOrder(order);
|
||||
}
|
||||
criteria.setProjection(projection);
|
||||
return criteria.list();
|
||||
} catch (Exception e) {
|
||||
logger.error("查询出错", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public boolean doWork(final String sql, final List<Object[]> params) {
|
||||
boolean flag = false;
|
||||
try {
|
||||
getCurrentSession().doWork(new Work() {
|
||||
@Override
|
||||
public void execute(Connection connection) throws SQLException {
|
||||
BatchSQLUtil sqlUtil = new BatchSQLUtil(connection, sql);
|
||||
for (Object[] param : params) {
|
||||
sqlUtil.addCount(param);
|
||||
}
|
||||
sqlUtil.commit();
|
||||
}
|
||||
});
|
||||
flag = true;
|
||||
} catch (Exception e) {
|
||||
logger.error("doWork出错", e);
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package javautils.jdbc.util;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class BatchSQLUtil {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(BatchSQLUtil.class);
|
||||
|
||||
private Connection conn;
|
||||
private PreparedStatement ps;
|
||||
|
||||
public BatchSQLUtil(Connection conn, String sql) {
|
||||
try {
|
||||
this.conn = conn;
|
||||
this.conn.setAutoCommit(false);
|
||||
ps = this.conn.prepareStatement(sql);
|
||||
} catch (Exception e) {
|
||||
logger.error("BatchSQLUtil异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void addCount(Object[] param) {
|
||||
try {
|
||||
for (int i = 0; i < param.length; i++) {
|
||||
ps.setObject(i + 1, param[i]);
|
||||
}
|
||||
ps.addBatch();
|
||||
} catch (SQLException e) {
|
||||
logger.error("addCount异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void commit() {
|
||||
try {
|
||||
ps.executeBatch();
|
||||
conn.commit();
|
||||
ps.clearBatch();
|
||||
} catch (SQLException e) {
|
||||
logger.error("commit异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
ps.close();
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
logger.error("close异常", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package javautils.jdbc.util;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SQLUtil {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SQLUtil.class);
|
||||
|
||||
// ------------------------------- 增删改通用 -------------------------------//
|
||||
public boolean aduSql(Connection conn, String sql) {
|
||||
boolean flag = false;
|
||||
try {
|
||||
if (conn != null) {
|
||||
PreparedStatement ps = conn.prepareStatement(sql);
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
flag = true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("aduSql异常", e);
|
||||
flag = false;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
public static boolean aduSql(Connection conn, String sql, Object[] params) {
|
||||
boolean flag = false;
|
||||
try {
|
||||
if (conn != null) {
|
||||
PreparedStatement ps = conn.prepareStatement(sql);
|
||||
if (params != null) {
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
ps.setObject(i + 1, params[i]);
|
||||
}
|
||||
}
|
||||
ps.executeUpdate();
|
||||
flag = true;
|
||||
ps.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("aduSql异常", e);
|
||||
flag = false;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
public static List<Object[]> exList(Connection conn, String sql, Object params[]) {
|
||||
ArrayList<Object[]> list = new ArrayList<Object[]>();
|
||||
try {
|
||||
if (conn != null) {
|
||||
PreparedStatement ps = conn.prepareStatement(sql);
|
||||
if (params != null) {
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
ps.setObject(i + 1, params[i]);
|
||||
}
|
||||
}
|
||||
ResultSet rs = ps.executeQuery();
|
||||
while (rs.next()) {
|
||||
int columns = rs.getMetaData().getColumnCount();
|
||||
Object[] objArr = new Object[columns];
|
||||
for (int i = 0; i < columns; i++) {
|
||||
objArr[i] = rs.getObject(i + 1);
|
||||
}
|
||||
list.add(objArr);
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("exList异常", e);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package javautils.json;
|
||||
|
||||
import net.sf.json.JSON;
|
||||
import net.sf.json.xml.XMLSerializer;
|
||||
|
||||
public class JSONUtil {
|
||||
|
||||
public static JSON toJSONString(String xml) {
|
||||
XMLSerializer xmlSerializer = new XMLSerializer();
|
||||
JSON json = xmlSerializer.read(xml);
|
||||
return json;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package javautils.math;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class MathUtil {
|
||||
|
||||
public static double decimalFormat(BigDecimal bd, int point) {
|
||||
return bd.setScale(point, BigDecimal.ROUND_HALF_DOWN).doubleValue();
|
||||
}
|
||||
|
||||
public static double doubleFormat(double d, int point) {
|
||||
try {
|
||||
BigDecimal bd = new BigDecimal(d);
|
||||
return bd.setScale(point, BigDecimal.ROUND_HALF_DOWN).doubleValue();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
public static float floatFormat(float f, int point) {
|
||||
try {
|
||||
BigDecimal bd = new BigDecimal(f);
|
||||
return bd.setScale(point, BigDecimal.ROUND_HALF_DOWN).floatValue();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return f;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2005, 2014 springside.github.io
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
*******************************************************************************/
|
||||
package javautils.redis;
|
||||
|
||||
import javautils.redis.pool.JedisPool;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.exceptions.JedisDataException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 装载并执行Lua Script,
|
||||
*
|
||||
* 如果服务器上因为集群多台服务器或重启等原因没有装载script,会自动重新装载后重试。
|
||||
*
|
||||
* 因为涉及到的key未知,暂时不支持Sharding。
|
||||
*/
|
||||
public class JedisScriptExecutor {
|
||||
private static Logger logger = LoggerFactory.getLogger(JedisScriptExecutor.class);
|
||||
|
||||
private JedisTemplate jedisTemplate;
|
||||
|
||||
private String script;
|
||||
private String sha1;
|
||||
|
||||
public JedisScriptExecutor(JedisPool jedisPool) {
|
||||
this.jedisTemplate = new JedisTemplate(jedisPool);
|
||||
}
|
||||
|
||||
public JedisScriptExecutor(JedisTemplate jedisTemplate) {
|
||||
this.jedisTemplate = jedisTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 装载Lua Script。
|
||||
* 如果Script出错,抛出JedisDataException。
|
||||
*/
|
||||
public void load(final String scriptContent) throws JedisDataException {
|
||||
sha1 = jedisTemplate.execute(new JedisTemplate.JedisAction<String>() {
|
||||
public String action(Jedis jedis) {
|
||||
return jedis.scriptLoad(scriptContent);
|
||||
}
|
||||
});
|
||||
script = scriptContent;
|
||||
|
||||
logger.debug("Script \"{}\" had been loaded as {}", scriptContent, sha1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从文件加载Lua Script, 文件路径格式为Spring Resource的格式.
|
||||
*/
|
||||
public void loadFromFile(final String scriptPath) throws JedisDataException {
|
||||
String scriptContent;
|
||||
try {
|
||||
Resource resource = new DefaultResourceLoader().getResource(scriptPath);
|
||||
scriptContent = FileUtils.readFileToString(resource.getFile(), "UTF-8");
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException(scriptPath + " is not exist.", e);
|
||||
}
|
||||
|
||||
load(scriptContent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行Lua Script, 如果Redis服务器上还没装载Script则自动装载并重试。
|
||||
* keys与args不允许为null.
|
||||
*/
|
||||
public Object execute(final String[] keys, final String[] args) throws IllegalArgumentException {
|
||||
Assert.notNull(keys, "keys can't be null.");
|
||||
Assert.notNull(args, "args can't be null.");
|
||||
return execute(Arrays.asList(keys), Arrays.asList(args));
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行Lua Script, 如果Redis服务器上还没装载Script则自动装载并重试。
|
||||
* keys与args不允许为null.
|
||||
*/
|
||||
public Object execute(final List<String> keys, final List<String> args) throws IllegalArgumentException {
|
||||
Assert.notNull(keys, "keys can't be null.");
|
||||
Assert.notNull(args, "args can't be null.");
|
||||
|
||||
return jedisTemplate.execute(new JedisTemplate.JedisAction<Object>() {
|
||||
public Object action(Jedis jedis) {
|
||||
try {
|
||||
return jedis.evalsha(sha1, keys, args);
|
||||
} catch (JedisDataException e) {
|
||||
logger.warn(
|
||||
"Script {} is not loaded in server yet or the script is wrong, try to reload and run it again.",
|
||||
script, e);
|
||||
return jedis.eval(script, keys, args);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,714 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2005, 2014 springside.github.io
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
*******************************************************************************/
|
||||
package javautils.redis;
|
||||
|
||||
import javautils.redis.pool.JedisPool;
|
||||
import redis.clients.jedis.Tuple;
|
||||
import redis.clients.jedis.exceptions.JedisException;
|
||||
import redis.clients.util.Hashing;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* JedisShardTemplate is the JedisTemplate, which has key sharding feature.
|
||||
*
|
||||
* Pass more than one JedisPool to the it, it will calculate which jedisPool will handle the key. If
|
||||
* only one jedisPool passed, it won't do the calculation, so please use JedisShardedTemplate by default.
|
||||
*
|
||||
* But it doesn't support multi-key actions by default, like:
|
||||
* 1. Pipelined and Transaction process multi-keys
|
||||
* 2. Methods in @redis.clients.jedis.MultiKeyCommands like mget, rpoplpush....
|
||||
*
|
||||
* To support them, please use a common sharding key relate to these keys when invoke the APIs.
|
||||
*/
|
||||
public class JedisShardedTemplate {
|
||||
|
||||
private final Hashing algo = Hashing.MURMUR_HASH;
|
||||
private TreeMap<Long, JedisTemplate> nodes = new TreeMap<Long, JedisTemplate>();
|
||||
private JedisTemplate singleTemplate = null;
|
||||
|
||||
public JedisShardedTemplate(JedisPool... jedisPools) {
|
||||
if (jedisPools.length == 1) {
|
||||
singleTemplate = new JedisTemplate(jedisPools[0]);
|
||||
} else {
|
||||
initNodes(jedisPools);
|
||||
}
|
||||
}
|
||||
|
||||
public JedisShardedTemplate(List<JedisPool> jedisPools) {
|
||||
this(jedisPools.toArray(new JedisPool[jedisPools.size()]));
|
||||
}
|
||||
|
||||
private void initNodes(JedisPool... jedisPools) {
|
||||
for (int i = 0; i != jedisPools.length; i++) {
|
||||
// more entry the make the hash ring be more balance
|
||||
for (int n = 0; n < 128; n++) {
|
||||
final JedisPool jedisPool = jedisPools[i];
|
||||
nodes.put(this.algo.hash("SHARD-" + i + "-NODE-" + n), new JedisTemplate(jedisPool));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hash the key and get the jedisTemplate from the hash ring. Get idea from Jedis's Sharded.java
|
||||
*/
|
||||
public JedisTemplate getShard(String key) {
|
||||
if (singleTemplate != null) {
|
||||
return singleTemplate;
|
||||
}
|
||||
|
||||
SortedMap<Long, JedisTemplate> tail = nodes.tailMap(algo.hash(key));
|
||||
if (tail.isEmpty()) {
|
||||
// the last node, back to first.
|
||||
return nodes.get(nodes.firstKey());
|
||||
}
|
||||
return tail.get(tail.firstKey());
|
||||
}
|
||||
|
||||
/*
|
||||
* Execute the action.
|
||||
*
|
||||
* @key the action must process only this key, or this key is a sharding key.
|
||||
*/
|
||||
public <T> T execute(String key, JedisTemplate.JedisAction<T> jedisAction) throws JedisException {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.execute(jedisAction);
|
||||
}
|
||||
|
||||
/*
|
||||
* Execute the action.
|
||||
*
|
||||
* @key the action must process only this key, or this key is a sharding key.
|
||||
*/
|
||||
public void execute(String key, JedisTemplate.JedisActionNoResult jedisAction) throws JedisException {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
jedisTemplate.execute(jedisAction);
|
||||
}
|
||||
|
||||
/*
|
||||
* Execute the pipeline.
|
||||
*
|
||||
* @key the action must process only this key, or this key is a sharding key.
|
||||
*/
|
||||
public List<Object> execute(String key, JedisTemplate.PipelineAction pipelineAction) throws JedisException {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.execute(pipelineAction);
|
||||
}
|
||||
|
||||
/*
|
||||
* Execute the pipeline.
|
||||
*
|
||||
* @key the action must process only this key, or this key is a sharding key.
|
||||
*/
|
||||
public void execute(String key, JedisTemplate.PipelineActionNoResult pipelineAction) throws JedisException {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
jedisTemplate.execute(pipelineAction);
|
||||
}
|
||||
|
||||
// / Common Actions ///
|
||||
|
||||
public Boolean del(final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.del(key);
|
||||
}
|
||||
|
||||
public Boolean del(final String shardingKey, final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.del(key);
|
||||
}
|
||||
|
||||
// / String Actions ///
|
||||
|
||||
public String get(final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.get(key);
|
||||
}
|
||||
|
||||
public String get(final String shardingKey, final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.get(key);
|
||||
}
|
||||
|
||||
public Long getAsLong(final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.getAsLong(key);
|
||||
}
|
||||
|
||||
public Long getAsLong(final String shardingKey, final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.getAsLong(key);
|
||||
}
|
||||
|
||||
public Integer getAsInt(final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.getAsInt(key);
|
||||
}
|
||||
|
||||
public Integer getAsInt(final String shardingKey, final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.getAsInt(key);
|
||||
}
|
||||
|
||||
public void set(final String key, final String value) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
jedisTemplate.set(key, value);
|
||||
}
|
||||
|
||||
public void set(final String shardingKey, final String key, final String value) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
jedisTemplate.set(key, value);
|
||||
}
|
||||
|
||||
public void setex(final String key, final String value, final int seconds) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
jedisTemplate.setex(key, value, seconds);
|
||||
}
|
||||
|
||||
public void setex(final String shardingKey, final String key, final String value, final int seconds) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
jedisTemplate.setex(key, value, seconds);
|
||||
}
|
||||
|
||||
public Boolean setnx(final String key, final String value) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.setnx(key, value);
|
||||
}
|
||||
|
||||
public Boolean setnx(final String shardingKey, final String key, final String value) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.setnx(key, value);
|
||||
}
|
||||
|
||||
public Boolean setnxex(final String key, final String value, final int seconds) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.setnxex(key, value, seconds);
|
||||
}
|
||||
|
||||
public Boolean setnxex(final String shardingKey, final String key, final String value, final int seconds) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.setnxex(key, value, seconds);
|
||||
}
|
||||
|
||||
public String getSet(final String key, final String value) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.getSet(key, value);
|
||||
}
|
||||
|
||||
public String getSet(final String shardingKey, final String key, final String value) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.getSet(key, value);
|
||||
}
|
||||
|
||||
public Long incr(final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.incr(key);
|
||||
}
|
||||
|
||||
public Long incr(final String shardingKey, final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.incr(key);
|
||||
}
|
||||
|
||||
public Long incrBy(final String key, final Long increment) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.incrBy(key, increment);
|
||||
}
|
||||
|
||||
public Long incrBy(final String shardingKey, final String key, final Long increment) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.incrBy(key, increment);
|
||||
}
|
||||
|
||||
public Double incrByFloat(final String key, final double increment) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.incrByFloat(key, increment);
|
||||
}
|
||||
|
||||
public Double incrByFloat(final String shardingKey, final String key, final double increment) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.incrByFloat(key, increment);
|
||||
}
|
||||
|
||||
public Long decr(final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.decr(key);
|
||||
}
|
||||
|
||||
public Long decr(final String shardingKey, final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.decr(key);
|
||||
}
|
||||
|
||||
public Long decrBy(final String key, final Long decrement) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.decrBy(key, decrement);
|
||||
}
|
||||
|
||||
public Long decrBy(final String shardingKey, final String key, final Long decrement) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.decrBy(key, decrement);
|
||||
}
|
||||
|
||||
// / Hash Actions ///
|
||||
|
||||
public String hget(final String key, final String field) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.hget(key, field);
|
||||
}
|
||||
|
||||
public String hget(final String shardingKey, final String key, final String field) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.hget(key, field);
|
||||
}
|
||||
|
||||
public List<String> hmget(final String key, final String field) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.hmget(key, field);
|
||||
}
|
||||
|
||||
public List<String> hmget(final String key, final String[] fields) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.hmget(key, fields);
|
||||
}
|
||||
|
||||
public List<String> hmget(final String shardingKey, final String key, final String field) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.hmget(key, field);
|
||||
}
|
||||
|
||||
public List<String> hmget(final String shardingKey, final String key, final String[] fields) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.hmget(key, fields);
|
||||
}
|
||||
|
||||
public Map<String, String> hgetAll(final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.hgetAll(key);
|
||||
}
|
||||
|
||||
public Map<String, String> hgetAll(final String shardingKey, final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.hgetAll(key);
|
||||
}
|
||||
|
||||
public void hset(final String key, final String field, final String value) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
jedisTemplate.hset(key, field, value);
|
||||
}
|
||||
|
||||
public void hset(final String shardingKey, final String key, final String field, final String value) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
jedisTemplate.hset(key, field, value);
|
||||
}
|
||||
|
||||
public void hmset(final String key, final Map<String, String> map) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
jedisTemplate.hmset(key, map);
|
||||
}
|
||||
|
||||
public void hmset(final String shardingKey, final String key, final Map<String, String> map) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
jedisTemplate.hmset(key, map);
|
||||
}
|
||||
|
||||
public Boolean hsetnx(final String key, final String fieldName, final String value) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.hsetnx(key, fieldName, value);
|
||||
}
|
||||
|
||||
public Boolean hsetnx(final String shardingKey, final String key, final String fieldName, final String value) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.hsetnx(key, fieldName, value);
|
||||
}
|
||||
|
||||
public Long hincrBy(final String key, final String fieldName, final long increment) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.hincrBy(key, fieldName, increment);
|
||||
}
|
||||
|
||||
public Long hincrBy(final String shardingKey, final String key, final String fieldName, final long increment) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.hincrBy(key, fieldName, increment);
|
||||
}
|
||||
|
||||
public Double hincrByFloat(final String key, final String fieldName, final double increment) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.hincrByFloat(key, fieldName, increment);
|
||||
}
|
||||
|
||||
public Double hincrByFloat(final String shardingKey, final String key, final String fieldName,
|
||||
final double increment) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.hincrByFloat(key, fieldName, increment);
|
||||
}
|
||||
|
||||
public Long hdel(final String key, final String fieldsName) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.hdel(key, fieldsName);
|
||||
}
|
||||
|
||||
public Long hdel(final String key, final String[] fieldsNames) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.hdel(key, fieldsNames);
|
||||
}
|
||||
|
||||
public Long hdel(final String shardingKey, final String key, final String fieldsName) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.hdel(key, fieldsName);
|
||||
}
|
||||
|
||||
public Long hdel(final String shardingKey, final String key, final String[] fieldsNames) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.hdel(key, fieldsNames);
|
||||
}
|
||||
|
||||
public Boolean hexists(final String key, final String fieldName) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.hexists(key, fieldName);
|
||||
}
|
||||
|
||||
public Boolean hexists(final String shardingKey, final String key, final String fieldName) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.hexists(key, fieldName);
|
||||
}
|
||||
|
||||
public Set<String> hkeys(final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.hkeys(key);
|
||||
}
|
||||
|
||||
public Set<String> hkeys(final String shardingKey, final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.hkeys(key);
|
||||
}
|
||||
|
||||
public Long hlen(final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.hlen(key);
|
||||
}
|
||||
|
||||
public Long hlen(final String shardingKey, final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.hlen(key);
|
||||
}
|
||||
|
||||
// / List Actions ///
|
||||
|
||||
public Long lpush(final String key, final String value) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.lpush(key, value);
|
||||
}
|
||||
|
||||
public Long lpush(final String key, final String[] values) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.lpush(key, values);
|
||||
}
|
||||
|
||||
public Long lpush(final String shardingKey, final String key, final String value) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.lpush(key, value);
|
||||
}
|
||||
|
||||
public Long lpush(final String shardingKey, final String key, final String[] values) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.lpush(key, values);
|
||||
}
|
||||
|
||||
public String rpop(final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.rpop(key);
|
||||
}
|
||||
|
||||
public String rpop(final String shardingKey, final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.rpop(key);
|
||||
}
|
||||
|
||||
public String brpop(final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.brpop(key);
|
||||
}
|
||||
|
||||
public String brpop(final String shardingKey, final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.brpop(key);
|
||||
}
|
||||
|
||||
public String brpop(final int timeout, final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.brpop(timeout, key);
|
||||
}
|
||||
|
||||
public String brpop(final String shardingKey, final int timeout, final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.brpop(timeout, key);
|
||||
}
|
||||
|
||||
public Long llen(final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.llen(key);
|
||||
}
|
||||
|
||||
public Long llen(final String shardingKey, final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.llen(key);
|
||||
}
|
||||
|
||||
public String lindex(final String key, final long index) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.lindex(key, index);
|
||||
}
|
||||
|
||||
public String lindex(final String shardingKey, final String key, final long index) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.lindex(key, index);
|
||||
}
|
||||
|
||||
public List<String> lrange(final String key, final int start, final int end) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.lrange(key, start, end);
|
||||
}
|
||||
|
||||
public List<String> lrange(final String shardingKey, final String key, final int start, final int end) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.lrange(key, start, end);
|
||||
}
|
||||
|
||||
public void ltrim(final String key, final int start, final int end) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
jedisTemplate.ltrim(key, start, end);
|
||||
}
|
||||
|
||||
public void ltrim(final String shardingKey, final String key, final int start, final int end) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
jedisTemplate.ltrim(key, start, end);
|
||||
}
|
||||
|
||||
public void ltrimFromLeft(final String key, final int size) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
jedisTemplate.ltrimFromLeft(key, size);
|
||||
}
|
||||
|
||||
public void ltrimFromLeft(final String shardingKey, final String key, final int size) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
jedisTemplate.ltrimFromLeft(key, size);
|
||||
}
|
||||
|
||||
public Boolean lremFirst(final String key, final String value) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.lremFirst(key, value);
|
||||
}
|
||||
|
||||
public Boolean lremFirst(final String shardingKey, final String key, final String value) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.lremFirst(key, value);
|
||||
}
|
||||
|
||||
public Boolean lremAll(final String key, final String value) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.lremAll(key, value);
|
||||
}
|
||||
|
||||
public Boolean lremAll(final String shardingKey, final String key, final String value) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.lremAll(key, value);
|
||||
}
|
||||
|
||||
// / Set Actions ///
|
||||
public Boolean sadd(final String key, final String member) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.sadd(key, member);
|
||||
}
|
||||
|
||||
public Boolean sadd(final String shardingKey, final String key, final String member) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.sadd(key, member);
|
||||
}
|
||||
|
||||
public Set<String> smembers(final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.smembers(key);
|
||||
}
|
||||
|
||||
public Set<String> smembers(final String shardingKey, final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.smembers(key);
|
||||
}
|
||||
|
||||
// / Sorted Set Actions ///
|
||||
|
||||
public Boolean zadd(final String key, final double score, final String member) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.zadd(key, score, member);
|
||||
}
|
||||
|
||||
public Boolean zadd(final String shardingKey, final String key, final double score, final String member) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.zadd(key, score, member);
|
||||
}
|
||||
|
||||
public Double zscore(final String key, final String member) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.zscore(key, member);
|
||||
}
|
||||
|
||||
public Double zscore(final String shardingKey, final String key, final String member) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.zscore(key, member);
|
||||
}
|
||||
|
||||
public Long zrank(final String key, final String member) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.zrank(key, member);
|
||||
}
|
||||
|
||||
public Long zrank(final String shardingKey, final String key, final String member) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.zrank(key, member);
|
||||
}
|
||||
|
||||
public Long zrevrank(final String key, final String member) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.zrevrank(key, member);
|
||||
}
|
||||
|
||||
public Long zrevrank(final String shardingKey, final String key, final String member) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.zrevrank(key, member);
|
||||
}
|
||||
|
||||
public Long zcount(final String key, final double start, final double end) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.zcount(key, start, end);
|
||||
}
|
||||
|
||||
public Long zcount(final String shardingKey, final String key, final double start, final double end) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.zcount(key, start, end);
|
||||
}
|
||||
|
||||
public Set<String> zrange(final String key, final int start, final int end) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.zrange(key, start, end);
|
||||
}
|
||||
|
||||
public Set<String> zrange(final String shardingKey, final String key, final int start, final int end) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.zrange(key, start, end);
|
||||
}
|
||||
|
||||
public Set<Tuple> zrangeWithScores(final String key, final int start, final int end) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.zrangeWithScores(key, start, end);
|
||||
}
|
||||
|
||||
public Set<Tuple> zrangeWithScores(final String shardingKey, final String key, final int start, final int end) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.zrangeWithScores(key, start, end);
|
||||
}
|
||||
|
||||
public Set<String> zrevrange(final String key, final int start, final int end) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.zrevrange(key, start, end);
|
||||
}
|
||||
|
||||
public Set<String> zrevrange(final String shardingKey, final String key, final int start, final int end) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.zrevrange(key, start, end);
|
||||
}
|
||||
|
||||
public Set<Tuple> zrevrangeWithScores(final String key, final int start, final int end) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.zrevrangeWithScores(key, start, end);
|
||||
}
|
||||
|
||||
public Set<Tuple> zrevrangeWithScores(final String shardingKey, final String key, final int start, final int end) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.zrevrangeWithScores(key, start, end);
|
||||
}
|
||||
|
||||
public Set<String> zrangeByScore(final String key, final double min, final double max) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.zrangeByScore(key, min, max);
|
||||
}
|
||||
|
||||
public Set<String> zrangeByScore(final String shardingKey, final String key, final double min, final double max) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.zrangeByScore(key, min, max);
|
||||
}
|
||||
|
||||
public Set<Tuple> zrangeByScoreWithScores(final String key, final double min, final double max) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.zrangeByScoreWithScores(key, min, max);
|
||||
}
|
||||
|
||||
public Set<Tuple> zrangeByScoreWithScores(final String shardingKey, final String key, final double min,
|
||||
final double max) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.zrangeByScoreWithScores(key, min, max);
|
||||
}
|
||||
|
||||
public Set<String> zrevrangeByScore(final String key, final double max, final double min) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.zrevrangeByScore(key, max, min);
|
||||
}
|
||||
|
||||
public Set<String> zrevrangeByScore(final String shardingKey, final String key, final double max, final double min) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.zrevrangeByScore(key, max, min);
|
||||
}
|
||||
|
||||
public Set<Tuple> zrevrangeByScoreWithScores(final String key, final double max, final double min) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.zrevrangeByScoreWithScores(key, max, min);
|
||||
}
|
||||
|
||||
public Set<Tuple> zrevrangeByScoreWithScores(final String shardingKey, final String key, final double max,
|
||||
final double min) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.zrevrangeByScoreWithScores(key, max, min);
|
||||
}
|
||||
|
||||
public Boolean zrem(final String key, final String member) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.zrem(key, member);
|
||||
}
|
||||
|
||||
public Boolean zrem(final String shardingKey, final String key, final String member) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.zrem(key, member);
|
||||
}
|
||||
|
||||
public Long zremByScore(final String key, final double min, final double max) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.zremByScore(key, min, max);
|
||||
}
|
||||
|
||||
public Long zremByScore(final String shardingKey, final String key, final double min, final double max) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.zremByScore(key, min, max);
|
||||
}
|
||||
|
||||
public Long zremByRank(final String key, final long start, final long end) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.zremByRank(key, start, end);
|
||||
}
|
||||
|
||||
public Long zremByRank(final String shardingKey, final String key, final long start, final long end) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.zremByRank(key, start, end);
|
||||
}
|
||||
|
||||
public Long zcard(final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(key);
|
||||
return jedisTemplate.zcard(key);
|
||||
}
|
||||
|
||||
public Long zcard(final String shardingKey, final String key) {
|
||||
JedisTemplate jedisTemplate = getShard(shardingKey);
|
||||
return jedisTemplate.zcard(key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,902 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2005, 2014 springside.github.io
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
*******************************************************************************/
|
||||
package javautils.redis;
|
||||
|
||||
import javautils.redis.pool.JedisPool;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.Pipeline;
|
||||
import redis.clients.jedis.Tuple;
|
||||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||
import redis.clients.jedis.exceptions.JedisDataException;
|
||||
import redis.clients.jedis.exceptions.JedisException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* JedisTemplate 提供了一个template方法,负责对Jedis连接的获取与归还。
|
||||
* JedisAction<T> 和 JedisActionNoResult两种回调接口,适用于有无返回值两种情况。
|
||||
* PipelineAction 与 PipelineActionResult两种接口,适合于pipeline中批量传输命令的情况。
|
||||
*
|
||||
* 同时提供一些JedisOperation中定义的 最常用函数的封装, 如get/set/zadd等。
|
||||
*/
|
||||
public class JedisTemplate {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(JedisTemplate.class);
|
||||
|
||||
private JedisPool jedisPool;
|
||||
|
||||
public JedisTemplate(JedisPool jedisPool) {
|
||||
this.jedisPool = jedisPool;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback interface for template.
|
||||
*/
|
||||
public interface JedisAction<T> {
|
||||
T action(Jedis jedis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback interface for template without result.
|
||||
*/
|
||||
public interface JedisActionNoResult {
|
||||
void action(Jedis jedis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback interface for template.
|
||||
*/
|
||||
public interface PipelineAction {
|
||||
List<Object> action(Pipeline Pipeline);
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback interface for template without result.
|
||||
*/
|
||||
public interface PipelineActionNoResult {
|
||||
void action(Pipeline Pipeline);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute with a call back action with result.
|
||||
*/
|
||||
public <T> T execute(JedisAction<T> jedisAction) throws JedisException {
|
||||
Jedis jedis = null;
|
||||
boolean broken = false;
|
||||
try {
|
||||
jedis = jedisPool.getResource();
|
||||
return jedisAction.action(jedis);
|
||||
} catch (JedisException e) {
|
||||
broken = handleJedisException(e);
|
||||
throw e;
|
||||
} finally {
|
||||
closeResource(jedis, broken);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute with a call back action without result.
|
||||
*/
|
||||
public void execute(JedisActionNoResult jedisAction) throws JedisException {
|
||||
Jedis jedis = null;
|
||||
boolean broken = false;
|
||||
try {
|
||||
jedis = jedisPool.getResource();
|
||||
jedisAction.action(jedis);
|
||||
} catch (JedisException e) {
|
||||
broken = handleJedisException(e);
|
||||
throw e;
|
||||
} finally {
|
||||
closeResource(jedis, broken);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute with a call back action with result in pipeline.
|
||||
*/
|
||||
public List<Object> execute(PipelineAction pipelineAction) throws JedisException {
|
||||
Jedis jedis = null;
|
||||
boolean broken = false;
|
||||
try {
|
||||
jedis = jedisPool.getResource();
|
||||
Pipeline pipeline = jedis.pipelined();
|
||||
pipelineAction.action(pipeline);
|
||||
return pipeline.syncAndReturnAll();
|
||||
} catch (JedisException e) {
|
||||
broken = handleJedisException(e);
|
||||
throw e;
|
||||
} finally {
|
||||
closeResource(jedis, broken);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute with a call back action without result in pipeline.
|
||||
*/
|
||||
public void execute(PipelineActionNoResult pipelineAction) throws JedisException {
|
||||
Jedis jedis = null;
|
||||
boolean broken = false;
|
||||
try {
|
||||
jedis = jedisPool.getResource();
|
||||
Pipeline pipeline = jedis.pipelined();
|
||||
pipelineAction.action(pipeline);
|
||||
pipeline.sync();
|
||||
} catch (JedisException e) {
|
||||
broken = handleJedisException(e);
|
||||
throw e;
|
||||
} finally {
|
||||
closeResource(jedis, broken);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the internal JedisPool.
|
||||
*/
|
||||
public JedisPool getJedisPool() {
|
||||
return jedisPool;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle jedisException, write log and return whether the connection is broken.
|
||||
*/
|
||||
protected boolean handleJedisException(JedisException jedisException) {
|
||||
if (jedisException instanceof JedisConnectionException) {
|
||||
logger.error("Redis connection " + jedisPool.getAddress() + " lost.", jedisException);
|
||||
} else if (jedisException instanceof JedisDataException) {
|
||||
if ((jedisException.getMessage() != null) && (jedisException.getMessage().indexOf("READONLY") != -1)) {
|
||||
logger.error("Redis connection " + jedisPool.getAddress() + " are read-only slave.", jedisException);
|
||||
} else {
|
||||
// dataException, isBroken=false
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
logger.error("Jedis exception happen.", jedisException);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return jedis connection to the pool, call different return methods depends on the conectionBroken status.
|
||||
*/
|
||||
protected void closeResource(Jedis jedis, boolean conectionBroken) {
|
||||
try {
|
||||
if (conectionBroken) {
|
||||
jedisPool.returnBrokenResource(jedis);
|
||||
} else {
|
||||
jedisPool.returnResource(jedis);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("return back jedis failed, will fore close the jedis.", e);
|
||||
JedisUtils.destroyJedis(jedis);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// / Common Actions ///
|
||||
|
||||
/**
|
||||
* Remove the specified keys. If a given key does not exist no operation is
|
||||
* performed for this key.
|
||||
*
|
||||
* return false if one of the key is not exist.
|
||||
*/
|
||||
public Boolean del(final String... keys) {
|
||||
return execute(new JedisAction<Boolean>() {
|
||||
|
||||
@Override
|
||||
public Boolean action(Jedis jedis) {
|
||||
return jedis.del(keys) == keys.length ? true : false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void flushDB() {
|
||||
execute(new JedisActionNoResult() {
|
||||
|
||||
@Override
|
||||
public void action(Jedis jedis) {
|
||||
jedis.flushDB();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// / String Actions ///
|
||||
|
||||
/**
|
||||
* Get the value of the specified key. If the key does not exist null is
|
||||
* returned. If the value stored at key is not a string an error is returned
|
||||
* because GET can only handle string values.
|
||||
*/
|
||||
public String get(final String key) {
|
||||
return execute(new JedisAction<String>() {
|
||||
|
||||
@Override
|
||||
public String action(Jedis jedis) {
|
||||
return jedis.get(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Set<String> keys(final String key) {
|
||||
return execute(new JedisAction<Set<String>>() {
|
||||
|
||||
@Override
|
||||
public Set<String> action(Jedis jedis) {
|
||||
return jedis.keys(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of the specified key as Long.If the key does not exist null is returned.
|
||||
*/
|
||||
public Long getAsLong(final String key) {
|
||||
String result = get(key);
|
||||
return result != null ? Long.valueOf(result) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of the specified key as Integer.If the key does not exist null is returned.
|
||||
*/
|
||||
public Integer getAsInt(final String key) {
|
||||
String result = get(key);
|
||||
return result != null ? Integer.valueOf(result) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of the specified key as Double.If the key does not exist null is returned.
|
||||
*/
|
||||
public Double getAsDouble(final String key) {
|
||||
String result = get(key);
|
||||
return result != null ? Double.valueOf(result) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the values of all the specified keys. If one or more keys dont exist
|
||||
* or is not of type String, a 'nil' value is returned instead of the value
|
||||
* of the specified key, but the operation never fails.
|
||||
*/
|
||||
public List<String> mget(final String... keys) {
|
||||
return execute(new JedisAction<List<String>>() {
|
||||
|
||||
@Override
|
||||
public List<String> action(Jedis jedis) {
|
||||
return jedis.mget(keys);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the string value as value of the key.
|
||||
* The string can't be longer than 1073741824 bytes (1 GB).
|
||||
*/
|
||||
public void set(final String key, final String value) {
|
||||
execute(new JedisActionNoResult() {
|
||||
|
||||
@Override
|
||||
public void action(Jedis jedis) {
|
||||
jedis.set(key, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void expire(final String key, final int seconds) {
|
||||
execute(new JedisActionNoResult() {
|
||||
|
||||
@Override
|
||||
public void action(Jedis jedis) {
|
||||
jedis.expire(key, seconds);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The command is exactly equivalent to the following group of commands: {@link #set(String, String) SET} +
|
||||
* {@link #expire(String, int) EXPIRE}.
|
||||
* The operation is atomic.
|
||||
*/
|
||||
public void setex(final String key, final String value, final int seconds) {
|
||||
execute(new JedisActionNoResult() {
|
||||
|
||||
@Override
|
||||
public void action(Jedis jedis) {
|
||||
jedis.setex(key, seconds, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* SETNX works exactly like {@link #setNX(String, String) SET} with the only
|
||||
* difference that if the key already exists no operation is performed.
|
||||
* SETNX actually means "SET if Not eXists".
|
||||
*
|
||||
* return true if the key was set.
|
||||
*/
|
||||
public Boolean setnx(final String key, final String value) {
|
||||
return execute(new JedisAction<Boolean>() {
|
||||
|
||||
@Override
|
||||
public Boolean action(Jedis jedis) {
|
||||
return jedis.setnx(key, value) == 1 ? true : false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The command is exactly equivalent to the following group of commands: {@link #setex(String, String, int) SETEX} +
|
||||
* {@link #sexnx(String, String) SETNX}.
|
||||
* The operation is atomic.
|
||||
*/
|
||||
public Boolean setnxex(final String key, final String value, final int seconds) {
|
||||
return execute(new JedisAction<Boolean>() {
|
||||
|
||||
@Override
|
||||
public Boolean action(Jedis jedis) {
|
||||
String result = jedis.set(key, value, "NX", "EX", seconds);
|
||||
return JedisUtils.isStatusOk(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* GETSET is an atomic set this value and return the old value command. Set
|
||||
* key to the string value and return the old value stored at key. The
|
||||
* string can't be longer than 1073741824 bytes (1 GB).
|
||||
*/
|
||||
public String getSet(final String key, final String value) {
|
||||
return execute(new JedisAction<String>() {
|
||||
|
||||
@Override
|
||||
public String action(Jedis jedis) {
|
||||
return jedis.getSet(key, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment the number stored at key by one. If the key does not exist or
|
||||
* contains a value of a wrong type, set the key to the value of "0" before
|
||||
* to perform the increment operation.
|
||||
* <p>
|
||||
* INCR commands are limited to 64 bit signed integers.
|
||||
* <p>
|
||||
* Note: this is actually a string operation, that is, in Redis there are not "integer" types. Simply the string
|
||||
* stored at the key is parsed as a base 10 64 bit signed integer, incremented, and then converted back as a string.
|
||||
*
|
||||
* @return Integer reply, this commands will reply with the new value of key
|
||||
* after the increment.
|
||||
*/
|
||||
public Long incr(final String key) {
|
||||
return execute(new JedisAction<Long>() {
|
||||
@Override
|
||||
public Long action(Jedis jedis) {
|
||||
return jedis.incr(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Long incrBy(final String key, final long increment) {
|
||||
return execute(new JedisAction<Long>() {
|
||||
@Override
|
||||
public Long action(Jedis jedis) {
|
||||
return jedis.incrBy(key, increment);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Double incrByFloat(final String key, final double increment) {
|
||||
return execute(new JedisAction<Double>() {
|
||||
@Override
|
||||
public Double action(Jedis jedis) {
|
||||
return jedis.incrByFloat(key, increment);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrement the number stored at key by one. If the key does not exist or
|
||||
* contains a value of a wrong type, set the key to the value of "0" before
|
||||
* to perform the decrement operation.
|
||||
*/
|
||||
public Long decr(final String key) {
|
||||
return execute(new JedisAction<Long>() {
|
||||
@Override
|
||||
public Long action(Jedis jedis) {
|
||||
return jedis.decr(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Long decrBy(final String key, final long decrement) {
|
||||
return execute(new JedisAction<Long>() {
|
||||
@Override
|
||||
public Long action(Jedis jedis) {
|
||||
return jedis.decrBy(key, decrement);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// / Hash Actions ///
|
||||
/**
|
||||
* If key holds a hash, retrieve the value associated to the specified
|
||||
* field.
|
||||
* <p>
|
||||
* If the field is not found or the key does not exist, a special 'nil' value is returned.
|
||||
*/
|
||||
public String hget(final String key, final String fieldName) {
|
||||
return execute(new JedisAction<String>() {
|
||||
@Override
|
||||
public String action(Jedis jedis) {
|
||||
return jedis.hget(key, fieldName);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public List<String> hmget(final String key, final String... fieldsNames) {
|
||||
return execute(new JedisAction<List<String>>() {
|
||||
@Override
|
||||
public List<String> action(Jedis jedis) {
|
||||
return jedis.hmget(key, fieldsNames);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Map<String, String> hgetAll(final String key) {
|
||||
return execute(new JedisAction<Map<String, String>>() {
|
||||
@Override
|
||||
public Map<String, String> action(Jedis jedis) {
|
||||
return jedis.hgetAll(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void hset(final String key, final String fieldName, final String value) {
|
||||
execute(new JedisActionNoResult() {
|
||||
|
||||
@Override
|
||||
public void action(Jedis jedis) {
|
||||
jedis.hset(key, fieldName, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void hmset(final String key, final Map<String, String> map) {
|
||||
execute(new JedisActionNoResult() {
|
||||
|
||||
@Override
|
||||
public void action(Jedis jedis) {
|
||||
jedis.hmset(key, map);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public Boolean hsetnx(final String key, final String fieldName, final String value) {
|
||||
return execute(new JedisAction<Boolean>() {
|
||||
|
||||
@Override
|
||||
public Boolean action(Jedis jedis) {
|
||||
return jedis.hsetnx(key, fieldName, value) == 1 ? true : false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Long hincrBy(final String key, final String fieldName, final long increment) {
|
||||
return execute(new JedisAction<Long>() {
|
||||
@Override
|
||||
public Long action(Jedis jedis) {
|
||||
return jedis.hincrBy(key, fieldName, increment);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Double hincrByFloat(final String key, final String fieldName, final double increment) {
|
||||
return execute(new JedisAction<Double>() {
|
||||
@Override
|
||||
public Double action(Jedis jedis) {
|
||||
return jedis.hincrByFloat(key, fieldName, increment);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Long hdel(final String key, final String... fieldsNames) {
|
||||
return execute(new JedisAction<Long>() {
|
||||
@Override
|
||||
public Long action(Jedis jedis) {
|
||||
return jedis.hdel(key, fieldsNames);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Boolean hexists(final String key, final String fieldName) {
|
||||
return execute(new JedisAction<Boolean>() {
|
||||
@Override
|
||||
public Boolean action(Jedis jedis) {
|
||||
return jedis.hexists(key, fieldName);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Set<String> hkeys(final String key) {
|
||||
return execute(new JedisAction<Set<String>>() {
|
||||
@Override
|
||||
public Set<String> action(Jedis jedis) {
|
||||
return jedis.hkeys(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public List<String> hvals(final String key) {
|
||||
return execute(new JedisAction<List<String>>() {
|
||||
@Override
|
||||
public List<String> action(Jedis jedis) {
|
||||
return jedis.hvals(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Long hlen(final String key) {
|
||||
return execute(new JedisAction<Long>() {
|
||||
@Override
|
||||
public Long action(Jedis jedis) {
|
||||
return jedis.hlen(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// / List Actions ///
|
||||
|
||||
public Long lpush(final String key, final String... values) {
|
||||
return execute(new JedisAction<Long>() {
|
||||
@Override
|
||||
public Long action(Jedis jedis) {
|
||||
return jedis.lpush(key, values);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Long rpush(final String key, final String... values) {
|
||||
return execute(new JedisAction<Long>() {
|
||||
@Override
|
||||
public Long action(Jedis jedis) {
|
||||
return jedis.rpush(key, values);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public String rpop(final String key) {
|
||||
return execute(new JedisAction<String>() {
|
||||
|
||||
@Override
|
||||
public String action(Jedis jedis) {
|
||||
return jedis.rpop(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public String brpop(final String key) {
|
||||
return execute(new JedisAction<String>() {
|
||||
|
||||
@Override
|
||||
public String action(Jedis jedis) {
|
||||
List<String> nameValuePair = jedis.brpop(key);
|
||||
if (nameValuePair != null) {
|
||||
return nameValuePair.get(1);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public String brpop(final int timeout, final String key) {
|
||||
return execute(new JedisAction<String>() {
|
||||
|
||||
@Override
|
||||
public String action(Jedis jedis) {
|
||||
List<String> nameValuePair = jedis.brpop(timeout, key);
|
||||
if (nameValuePair != null) {
|
||||
return nameValuePair.get(1);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Not support for sharding.
|
||||
*/
|
||||
public String rpoplpush(final String sourceKey, final String destinationKey) {
|
||||
return execute(new JedisAction<String>() {
|
||||
|
||||
@Override
|
||||
public String action(Jedis jedis) {
|
||||
return jedis.rpoplpush(sourceKey, destinationKey);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Not support for sharding.
|
||||
*/
|
||||
public String brpoplpush(final String source, final String destination, final int timeout) {
|
||||
return execute(new JedisAction<String>() {
|
||||
|
||||
@Override
|
||||
public String action(Jedis jedis) {
|
||||
return jedis.brpoplpush(source, destination, timeout);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Long llen(final String key) {
|
||||
return execute(new JedisAction<Long>() {
|
||||
|
||||
@Override
|
||||
public Long action(Jedis jedis) {
|
||||
return jedis.llen(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public String lindex(final String key, final long index) {
|
||||
return execute(new JedisAction<String>() {
|
||||
|
||||
@Override
|
||||
public String action(Jedis jedis) {
|
||||
return jedis.lindex(key, index);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public List<String> lrange(final String key, final int start, final int end) {
|
||||
return execute(new JedisAction<List<String>>() {
|
||||
|
||||
@Override
|
||||
public List<String> action(Jedis jedis) {
|
||||
return jedis.lrange(key, start, end);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void ltrim(final String key, final int start, final int end) {
|
||||
execute(new JedisActionNoResult() {
|
||||
@Override
|
||||
public void action(Jedis jedis) {
|
||||
jedis.ltrim(key, start, end);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void ltrimFromLeft(final String key, final int size) {
|
||||
execute(new JedisActionNoResult() {
|
||||
@Override
|
||||
public void action(Jedis jedis) {
|
||||
jedis.ltrim(key, 0, size - 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Boolean lremFirst(final String key, final String value) {
|
||||
return execute(new JedisAction<Boolean>() {
|
||||
@Override
|
||||
public Boolean action(Jedis jedis) {
|
||||
Long count = jedis.lrem(key, 1, value);
|
||||
return (count == 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Boolean lremAll(final String key, final String value) {
|
||||
return execute(new JedisAction<Boolean>() {
|
||||
@Override
|
||||
public Boolean action(Jedis jedis) {
|
||||
Long count = jedis.lrem(key, 0, value);
|
||||
return (count > 0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// / Set Actions ///
|
||||
public Boolean sadd(final String key, final String member) {
|
||||
return execute(new JedisAction<Boolean>() {
|
||||
|
||||
@Override
|
||||
public Boolean action(Jedis jedis) {
|
||||
return jedis.sadd(key, member) == 1 ? true : false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Set<String> smembers(final String key) {
|
||||
return execute(new JedisAction<Set<String>>() {
|
||||
|
||||
@Override
|
||||
public Set<String> action(Jedis jedis) {
|
||||
return jedis.smembers(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// / Ordered Set Actions ///
|
||||
/**
|
||||
* return true for add new element, false for only update the score.
|
||||
*/
|
||||
public Boolean zadd(final String key, final double score, final String member) {
|
||||
return execute(new JedisAction<Boolean>() {
|
||||
|
||||
@Override
|
||||
public Boolean action(Jedis jedis) {
|
||||
return jedis.zadd(key, score, member) == 1 ? true : false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Double zscore(final String key, final String member) {
|
||||
return execute(new JedisAction<Double>() {
|
||||
|
||||
@Override
|
||||
public Double action(Jedis jedis) {
|
||||
return jedis.zscore(key, member);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Long zrank(final String key, final String member) {
|
||||
return execute(new JedisAction<Long>() {
|
||||
|
||||
@Override
|
||||
public Long action(Jedis jedis) {
|
||||
return jedis.zrank(key, member);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Long zrevrank(final String key, final String member) {
|
||||
return execute(new JedisAction<Long>() {
|
||||
|
||||
@Override
|
||||
public Long action(Jedis jedis) {
|
||||
return jedis.zrevrank(key, member);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Long zcount(final String key, final double min, final double max) {
|
||||
return execute(new JedisAction<Long>() {
|
||||
|
||||
@Override
|
||||
public Long action(Jedis jedis) {
|
||||
return jedis.zcount(key, min, max);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Set<String> zrange(final String key, final int start, final int end) {
|
||||
return execute(new JedisAction<Set<String>>() {
|
||||
|
||||
@Override
|
||||
public Set<String> action(Jedis jedis) {
|
||||
return jedis.zrange(key, start, end);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Set<Tuple> zrangeWithScores(final String key, final int start, final int end) {
|
||||
return execute(new JedisAction<Set<Tuple>>() {
|
||||
|
||||
@Override
|
||||
public Set<Tuple> action(Jedis jedis) {
|
||||
return jedis.zrangeWithScores(key, start, end);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Set<String> zrevrange(final String key, final int start, final int end) {
|
||||
return execute(new JedisAction<Set<String>>() {
|
||||
|
||||
@Override
|
||||
public Set<String> action(Jedis jedis) {
|
||||
return jedis.zrevrange(key, start, end);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Set<Tuple> zrevrangeWithScores(final String key, final int start, final int end) {
|
||||
return execute(new JedisAction<Set<Tuple>>() {
|
||||
|
||||
@Override
|
||||
public Set<Tuple> action(Jedis jedis) {
|
||||
return jedis.zrevrangeWithScores(key, start, end);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Set<String> zrangeByScore(final String key, final double min, final double max) {
|
||||
return execute(new JedisAction<Set<String>>() {
|
||||
|
||||
@Override
|
||||
public Set<String> action(Jedis jedis) {
|
||||
return jedis.zrangeByScore(key, min, max);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Set<Tuple> zrangeByScoreWithScores(final String key, final double min, final double max) {
|
||||
return execute(new JedisAction<Set<Tuple>>() {
|
||||
|
||||
@Override
|
||||
public Set<Tuple> action(Jedis jedis) {
|
||||
return jedis.zrangeByScoreWithScores(key, min, max);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Set<String> zrevrangeByScore(final String key, final double max, final double min) {
|
||||
return execute(new JedisAction<Set<String>>() {
|
||||
|
||||
@Override
|
||||
public Set<String> action(Jedis jedis) {
|
||||
return jedis.zrevrangeByScore(key, max, min);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Set<Tuple> zrevrangeByScoreWithScores(final String key, final double max, final double min) {
|
||||
return execute(new JedisAction<Set<Tuple>>() {
|
||||
|
||||
@Override
|
||||
public Set<Tuple> action(Jedis jedis) {
|
||||
return jedis.zrevrangeByScoreWithScores(key, max, min);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Boolean zrem(final String key, final String member) {
|
||||
return execute(new JedisAction<Boolean>() {
|
||||
|
||||
@Override
|
||||
public Boolean action(Jedis jedis) {
|
||||
return jedis.zrem(key, member) == 1 ? true : false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Long zremByScore(final String key, final double start, final double end) {
|
||||
return execute(new JedisAction<Long>() {
|
||||
|
||||
@Override
|
||||
public Long action(Jedis jedis) {
|
||||
return jedis.zremrangeByScore(key, start, end);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Long zremByRank(final String key, final long start, final long end) {
|
||||
return execute(new JedisAction<Long>() {
|
||||
|
||||
@Override
|
||||
public Long action(Jedis jedis) {
|
||||
return jedis.zremrangeByRank(key, start, end);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Long zcard(final String key) {
|
||||
return execute(new JedisAction<Long>() {
|
||||
|
||||
@Override
|
||||
public Long action(Jedis jedis) {
|
||||
return jedis.zcard(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2005, 2014 springside.github.io
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
*******************************************************************************/
|
||||
package javautils.redis;
|
||||
|
||||
import javautils.redis.pool.JedisPool;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.exceptions.JedisException;
|
||||
|
||||
public class JedisUtils {
|
||||
|
||||
private static final String OK_CODE = "OK";
|
||||
private static final String OK_MULTI_CODE = "+OK";
|
||||
|
||||
/**
|
||||
* 判断 返回值是否ok.
|
||||
*/
|
||||
public static boolean isStatusOk(String status) {
|
||||
return (status != null) && (OK_CODE.equals(status) || OK_MULTI_CODE.equals(status));
|
||||
}
|
||||
|
||||
/**
|
||||
* 在Pool以外强行销毁Jedis.
|
||||
*/
|
||||
public static void destroyJedis(Jedis jedis) {
|
||||
if ((jedis != null) && jedis.isConnected()) {
|
||||
try {
|
||||
try {
|
||||
jedis.quit();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
jedis.disconnect();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ping the jedis instance, return true is the result is PONG.
|
||||
*/
|
||||
public static boolean ping(JedisPool pool) {
|
||||
JedisTemplate template = new JedisTemplate(pool);
|
||||
try {
|
||||
String result = template.execute(new JedisTemplate.JedisAction<String>() {
|
||||
public String action(Jedis jedis) {
|
||||
return jedis.ping();
|
||||
}
|
||||
});
|
||||
return (result != null) && result.equals("PONG");
|
||||
} catch (JedisException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2005, 2014 springside.github.io
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
*******************************************************************************/
|
||||
package javautils.redis.pool;
|
||||
|
||||
import redis.clients.jedis.Protocol;
|
||||
|
||||
/**
|
||||
* All information for redis connection.
|
||||
*/
|
||||
public class ConnectionInfo {
|
||||
|
||||
public static final String DEFAULT_PASSWORD = null;
|
||||
|
||||
private int database = Protocol.DEFAULT_DATABASE;
|
||||
private String password = DEFAULT_PASSWORD;
|
||||
private int timeout = Protocol.DEFAULT_TIMEOUT;
|
||||
|
||||
public ConnectionInfo() {
|
||||
}
|
||||
|
||||
public ConnectionInfo(int database, String password, int timeout) {
|
||||
this.timeout = timeout;
|
||||
if (password != null && password.length() > 0) {
|
||||
this.password = password;
|
||||
}
|
||||
else {
|
||||
this.password = DEFAULT_PASSWORD;
|
||||
}
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
public int getDatabase() {
|
||||
return database;
|
||||
}
|
||||
|
||||
public void setDatabase(int database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
if (password != null && password.length() > 0) {
|
||||
this.password = password;
|
||||
}
|
||||
else {
|
||||
this.password = DEFAULT_PASSWORD;
|
||||
}
|
||||
}
|
||||
|
||||
public int getTimeout() {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
public void setTimeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ConnectionInfo [database=" + database + ", password=" + password + ", timeout=" + timeout + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2005, 2014 springside.github.io
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
*******************************************************************************/
|
||||
package javautils.redis.pool;
|
||||
|
||||
import redis.clients.jedis.HostAndPort;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
/**
|
||||
* Pool which connect to redis instance directly.
|
||||
*/
|
||||
public class JedisDirectPool extends JedisPool {
|
||||
|
||||
public JedisDirectPool(HostAndPort address, JedisPoolConfig config) {
|
||||
initInternalPool(address, new ConnectionInfo(), config);
|
||||
}
|
||||
|
||||
public JedisDirectPool(HostAndPort address, ConnectionInfo connectionInfo, JedisPoolConfig config) {
|
||||
initInternalPool(address, connectionInfo, config);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2005, 2014 springside.github.io
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
*******************************************************************************/
|
||||
package javautils.redis.pool;
|
||||
|
||||
import org.apache.commons.pool2.PooledObject;
|
||||
import org.apache.commons.pool2.PooledObjectFactory;
|
||||
import org.apache.commons.pool2.impl.DefaultPooledObject;
|
||||
import redis.clients.jedis.BinaryJedis;
|
||||
import redis.clients.jedis.Jedis;
|
||||
|
||||
/**
|
||||
* Copy from redis.clients.jedis.JedisFactory, because it is not a public class.
|
||||
*/
|
||||
public class JedisFactory implements PooledObjectFactory<Jedis> {
|
||||
private final String host;
|
||||
private final int port;
|
||||
private final int timeout;
|
||||
private final String password;
|
||||
private final int database;
|
||||
private final String clientName;
|
||||
|
||||
public JedisFactory(final String host, final int port, final int timeout, final String password, final int database) {
|
||||
this(host, port, timeout, password, database, null);
|
||||
}
|
||||
|
||||
public JedisFactory(final String host, final int port, final int timeout, final String password,
|
||||
final int database, final String clientName) {
|
||||
super();
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.timeout = timeout;
|
||||
this.password = password;
|
||||
this.database = database;
|
||||
this.clientName = clientName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void activateObject(PooledObject<Jedis> pooledJedis) throws Exception {
|
||||
final BinaryJedis jedis = pooledJedis.getObject();
|
||||
if (jedis.getDB() != database) {
|
||||
jedis.select(database);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroyObject(PooledObject<Jedis> pooledJedis) throws Exception {
|
||||
final BinaryJedis jedis = pooledJedis.getObject();
|
||||
if (jedis.isConnected()) {
|
||||
try {
|
||||
try {
|
||||
jedis.quit();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
jedis.disconnect();
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public PooledObject<Jedis> makeObject() throws Exception {
|
||||
final Jedis jedis = new Jedis(this.host, this.port, this.timeout);
|
||||
|
||||
jedis.connect();
|
||||
if (null != this.password) {
|
||||
jedis.auth(this.password);
|
||||
}
|
||||
if (database != 0) {
|
||||
jedis.select(database);
|
||||
}
|
||||
if (clientName != null) {
|
||||
jedis.clientSetname(clientName);
|
||||
}
|
||||
|
||||
return new DefaultPooledObject<Jedis>(jedis);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void passivateObject(PooledObject<Jedis> pooledJedis) throws Exception {
|
||||
// TODO maybe should select db 0? Not sure right now.
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validateObject(PooledObject<Jedis> pooledJedis) {
|
||||
final BinaryJedis jedis = pooledJedis.getObject();
|
||||
try {
|
||||
return jedis.isConnected() && jedis.ping().equals("PONG");
|
||||
} catch (final Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2005, 2014 springside.github.io
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
*******************************************************************************/
|
||||
package javautils.redis.pool;
|
||||
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||
import redis.clients.jedis.HostAndPort;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
import redis.clients.util.Pool;
|
||||
|
||||
/**
|
||||
* Jedis Pool base class.
|
||||
*/
|
||||
public abstract class JedisPool extends Pool<Jedis> {
|
||||
|
||||
protected HostAndPort address;
|
||||
|
||||
protected ConnectionInfo connectionInfo;
|
||||
|
||||
/**
|
||||
* Create a JedisPoolConfig with new maxPoolSize becasuse JedisPoolConfig's default maxPoolSize is only 8.
|
||||
* Also reset the idle checking time to 10 minutes, the default value is half minute.
|
||||
* Also rest the max idle to zero, the default value is 8 too.
|
||||
* The default idle time is 60 seconds.
|
||||
*/
|
||||
public static JedisPoolConfig createPoolConfig(int maxPoolSize) {
|
||||
JedisPoolConfig config = new JedisPoolConfig();
|
||||
config.setMaxTotal(maxPoolSize);
|
||||
config.setMaxIdle(maxPoolSize);
|
||||
|
||||
config.setTimeBetweenEvictionRunsMillis(600 * 1000);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the internal pool with connection info and pool config.
|
||||
*/
|
||||
protected void initInternalPool(HostAndPort address, ConnectionInfo connectionInfo, JedisPoolConfig config) {
|
||||
this.address = address;
|
||||
this.connectionInfo = connectionInfo;
|
||||
JedisFactory factory = new JedisFactory(address.getHost(), address.getPort(), connectionInfo.getTimeout(),
|
||||
connectionInfo.getPassword(), connectionInfo.getDatabase());
|
||||
|
||||
internalPool = new GenericObjectPool(factory, config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a broken jedis connection back to pool.
|
||||
*/
|
||||
@Override
|
||||
public void returnBrokenResource(final Jedis resource) {
|
||||
if (resource != null) {
|
||||
returnBrokenResourceObject(resource);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a available jedis connection back to pool.
|
||||
*/
|
||||
@Override
|
||||
public void returnResource(final Jedis resource) {
|
||||
if (resource != null) {
|
||||
resource.resetState();
|
||||
returnResourceObject(resource);
|
||||
}
|
||||
}
|
||||
|
||||
public HostAndPort getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public ConnectionInfo getConnectionInfo() {
|
||||
return connectionInfo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2005, 2014 springside.github.io
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
*******************************************************************************/
|
||||
package javautils.redis.pool;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import redis.clients.jedis.HostAndPort;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
import redis.clients.jedis.Protocol;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Build JedisPool smartly。
|
||||
* Depends on masterName whether prefix with "direct:", it will build JedisSentinelPool or JedisDirectPool.
|
||||
*/
|
||||
public class JedisPoolBuilder {
|
||||
|
||||
public static final String DIRECT_POOL_PREFIX = "direct:";
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(JedisPoolBuilder.class);
|
||||
|
||||
private String[] sentinelHosts;
|
||||
private int sentinelPort = Protocol.DEFAULT_SENTINEL_PORT;
|
||||
|
||||
private String masterName;
|
||||
private String[] shardedMasterNames;
|
||||
|
||||
private int poolSize = -1;
|
||||
|
||||
private int database = Protocol.DEFAULT_DATABASE;
|
||||
private String password = ConnectionInfo.DEFAULT_PASSWORD;
|
||||
private int timeout = Protocol.DEFAULT_TIMEOUT;
|
||||
|
||||
public JedisPoolBuilder setHosts(String[] hosts) {
|
||||
this.sentinelHosts = hosts;
|
||||
return this;
|
||||
}
|
||||
|
||||
public JedisPoolBuilder setHosts(String hosts) {
|
||||
if (hosts != null) {
|
||||
this.sentinelHosts = hosts.split(",");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public JedisPoolBuilder setPort(int port) {
|
||||
this.sentinelPort = port;
|
||||
return this;
|
||||
}
|
||||
|
||||
public JedisPoolBuilder setMasterName(String masterName) {
|
||||
this.masterName = masterName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public JedisPoolBuilder setShardedMasterNames(String[] shardedMasterNames) {
|
||||
this.shardedMasterNames = shardedMasterNames;
|
||||
return this;
|
||||
}
|
||||
|
||||
public JedisPoolBuilder setShardedMasterNames(String shardedMasterNames) {
|
||||
if (shardedMasterNames != null) {
|
||||
this.shardedMasterNames = shardedMasterNames.split(",");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public JedisPoolBuilder setDirectHostAndPort(String host, String port) {
|
||||
this.masterName = host + ":" + port;
|
||||
return this;
|
||||
}
|
||||
|
||||
public JedisPoolBuilder setPoolSize(int poolSize) {
|
||||
this.poolSize = poolSize;
|
||||
return this;
|
||||
}
|
||||
|
||||
public JedisPoolBuilder setDatabase(int database) {
|
||||
this.database = database;
|
||||
return this;
|
||||
}
|
||||
|
||||
public JedisPoolBuilder setPassword(String password) {
|
||||
this.password = password;
|
||||
return this;
|
||||
}
|
||||
|
||||
public JedisPoolBuilder setTimeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
public JedisPool buildPool() {
|
||||
|
||||
if ((masterName == null) || "".equals(masterName)) {
|
||||
throw new IllegalArgumentException("masterName is null or empty");
|
||||
}
|
||||
|
||||
if (poolSize < 1) {
|
||||
throw new IllegalArgumentException("poolSize is less then one");
|
||||
}
|
||||
|
||||
JedisPoolConfig config = JedisPool.createPoolConfig(poolSize);
|
||||
ConnectionInfo connectionInfo = new ConnectionInfo(database, password, timeout);
|
||||
|
||||
if (isDirect(masterName)) {
|
||||
return buildDirectPool(masterName, connectionInfo, config);
|
||||
} else {
|
||||
if ((sentinelHosts == null) || (sentinelHosts.length == 0)) {
|
||||
throw new IllegalArgumentException("sentinelHosts is null or empty");
|
||||
}
|
||||
return buildSentinelPool(masterName, connectionInfo, config);
|
||||
}
|
||||
}
|
||||
|
||||
public List<JedisPool> buildShardedPools() {
|
||||
|
||||
if ((shardedMasterNames == null) || (shardedMasterNames.length == 0) || "".equals(shardedMasterNames[0])) {
|
||||
throw new IllegalArgumentException("shardedMasterNames is null or empty");
|
||||
}
|
||||
|
||||
if (poolSize < 1) {
|
||||
throw new IllegalArgumentException("poolSize is less then one");
|
||||
}
|
||||
|
||||
JedisPoolConfig config = JedisPool.createPoolConfig(poolSize);
|
||||
ConnectionInfo connectionInfo = new ConnectionInfo(database, password, timeout);
|
||||
|
||||
List<JedisPool> jedisPools = new ArrayList<JedisPool>();
|
||||
|
||||
if (isDirect(shardedMasterNames[0])) {
|
||||
for (String theMasterName : shardedMasterNames) {
|
||||
jedisPools.add(buildDirectPool(theMasterName, connectionInfo, config));
|
||||
}
|
||||
} else {
|
||||
|
||||
if ((sentinelHosts == null) || (sentinelHosts.length == 0)) {
|
||||
throw new IllegalArgumentException("sentinelHosts is null or empty");
|
||||
}
|
||||
|
||||
for (String theMasterName : shardedMasterNames) {
|
||||
jedisPools.add(buildSentinelPool(theMasterName, connectionInfo, config));
|
||||
}
|
||||
}
|
||||
return jedisPools;
|
||||
}
|
||||
|
||||
private JedisPool buildDirectPool(String directMasterName, ConnectionInfo connectionInfo, JedisPoolConfig config) {
|
||||
String hostPortStr = directMasterName.substring(directMasterName.indexOf(":") + 1, directMasterName.length());
|
||||
String[] hostPort = hostPortStr.split(":");
|
||||
|
||||
logger.info("Building JedisDirectPool, on redis server " + hostPort[0] + " ,sentinelPort is " + hostPort[1]);
|
||||
|
||||
HostAndPort masterAddress = new HostAndPort(hostPort[0], Integer.parseInt(hostPort[1]));
|
||||
return new JedisDirectPool(masterAddress, config);
|
||||
}
|
||||
|
||||
private JedisPool buildSentinelPool(String sentinelMasterName, ConnectionInfo connectionInfo, JedisPoolConfig config) {
|
||||
logger.info("Building JedisSentinelPool, on sentinel sentinelHosts:" + Arrays.toString(sentinelHosts)
|
||||
+ " ,sentinelPort is " + sentinelPort + " ,masterName is " + sentinelMasterName);
|
||||
|
||||
HostAndPort[] sentinelAddress = new HostAndPort[sentinelHosts.length];
|
||||
for (int i = 0; i < sentinelHosts.length; i++) {
|
||||
sentinelAddress[i] = new HostAndPort(sentinelHosts[i], sentinelPort);
|
||||
}
|
||||
|
||||
return new JedisSentinelPool(sentinelAddress, sentinelMasterName, connectionInfo, config);
|
||||
}
|
||||
|
||||
private static boolean isDirect(String masterName) {
|
||||
return masterName.startsWith(DIRECT_POOL_PREFIX);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2005, 2014 springside.github.io
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
*******************************************************************************/
|
||||
package javautils.redis.pool;
|
||||
|
||||
import javautils.redis.JedisTemplate;
|
||||
import javautils.redis.JedisUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import redis.clients.jedis.HostAndPort;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
import redis.clients.jedis.JedisPubSub;
|
||||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* Pool which to get redis master address get from sentinel instances.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Pool which to get redis master address get from sentinel instances.
|
||||
*/
|
||||
public final class JedisSentinelPool extends JedisPool {
|
||||
|
||||
private static final String NO_ADDRESS_YET = "I dont know because no sentinel up";
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(JedisSentinelPool.class);
|
||||
|
||||
private List<JedisPool> sentinelPools = new ArrayList<JedisPool>();
|
||||
private MasterSwitchListener masterSwitchListener;
|
||||
|
||||
private String masterName;
|
||||
private JedisPoolConfig masterPoolConfig;
|
||||
private ConnectionInfo masterConnectionInfo;
|
||||
private CountDownLatch poolInit = new CountDownLatch(1);
|
||||
|
||||
/**
|
||||
* Creates a new instance of <code>JedisSentinelPool</code>.
|
||||
*
|
||||
* All parameters can be null or empty.
|
||||
*
|
||||
* @param sentinelAddresses Array of HostAndPort to sentinel instances.
|
||||
* @param masterName One sentinel can monitor several redis master-slave pair, use master name to identify them.
|
||||
* @param masterConnectionInfo The the other information like password,timeout.
|
||||
* @param masterPoolConfig Configuration of redis pool.
|
||||
*
|
||||
*/
|
||||
public JedisSentinelPool(HostAndPort[] sentinelAddresses, String masterName, ConnectionInfo masterConnectionInfo,
|
||||
JedisPoolConfig masterPoolConfig) {
|
||||
// sentinelAddresses
|
||||
assertArgument(((sentinelAddresses == null) || (sentinelAddresses.length == 0)), "seintinelInfos is not set");
|
||||
|
||||
for (HostAndPort sentinelInfo : sentinelAddresses) {
|
||||
JedisPool sentinelPool = new JedisDirectPool(sentinelInfo, new JedisPoolConfig());
|
||||
sentinelPools.add(sentinelPool);
|
||||
}
|
||||
|
||||
// masterConnectionInfo
|
||||
assertArgument(masterConnectionInfo == null, "masterConnectionInfo is not set");
|
||||
this.masterConnectionInfo = masterConnectionInfo;
|
||||
|
||||
// masterName
|
||||
assertArgument(((masterName == null) || masterName.isEmpty()), "masterName is not set");
|
||||
this.masterName = masterName;
|
||||
|
||||
// poolConfig
|
||||
assertArgument(masterPoolConfig == null, "masterPoolConfig is not set");
|
||||
this.masterPoolConfig = masterPoolConfig;
|
||||
|
||||
// Start MasterSwitchListener thread ,internal poll will be start in the thread
|
||||
masterSwitchListener = new MasterSwitchListener();
|
||||
masterSwitchListener.start();
|
||||
|
||||
try {
|
||||
if (!poolInit.await(5, TimeUnit.SECONDS)) {
|
||||
logger.warn("the sentiel pool can't not init in 5 seconds");
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
public JedisSentinelPool(HostAndPort[] sentinelAddresses, String masterName, JedisPoolConfig masterPoolConfig) {
|
||||
this(sentinelAddresses, masterName, new ConnectionInfo(), masterPoolConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
// shutdown the listener thread
|
||||
masterSwitchListener.shutdown();
|
||||
|
||||
// destroy sentinel pools
|
||||
for (JedisPool sentinel : sentinelPools) {
|
||||
sentinel.destroy();
|
||||
}
|
||||
|
||||
// destroy redis pool
|
||||
destroyInternelPool();
|
||||
|
||||
// wait for the masterSwitchListener thread finish
|
||||
try {
|
||||
logger.info("Waiting for MasterSwitchListener thread finish");
|
||||
masterSwitchListener.join();
|
||||
logger.info("MasterSwitchListener thread finished");
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
protected void destroyInternelPool() {
|
||||
closeInternalPool();
|
||||
|
||||
address = null;
|
||||
connectionInfo = null;
|
||||
internalPool = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert the argurment, throw IllegalArgumentException if the expression is true.
|
||||
*/
|
||||
private static void assertArgument(boolean expression, String message) {
|
||||
if (expression) {
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
}
|
||||
|
||||
// for test
|
||||
public MasterSwitchListener getMasterSwitchListener() {
|
||||
return masterSwitchListener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Listener thread to listen master switch message from sentinel.
|
||||
*/
|
||||
public class MasterSwitchListener extends Thread {
|
||||
public static final String THREAD_NAME_PREFIX = "MasterSwitchListener-";
|
||||
|
||||
private JedisPubSub subscriber;
|
||||
private JedisPool sentinelPool;
|
||||
private Jedis sentinelJedis;
|
||||
private AtomicBoolean running = new AtomicBoolean(true);
|
||||
private HostAndPort previousMasterAddress;
|
||||
|
||||
public MasterSwitchListener() {
|
||||
super(THREAD_NAME_PREFIX + masterName);
|
||||
}
|
||||
|
||||
// stop the blocking subscription and interrupt the thread
|
||||
public void shutdown() {
|
||||
// interrupt the thread
|
||||
running.getAndSet(false);
|
||||
this.interrupt();
|
||||
|
||||
// stop the blocking subscription
|
||||
try {
|
||||
if (subscriber != null) {
|
||||
subscriber.unsubscribe();
|
||||
}
|
||||
} finally {
|
||||
JedisUtils.destroyJedis(sentinelJedis);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (running.get()) {
|
||||
try {
|
||||
sentinelPool = pickupSentinel();
|
||||
|
||||
if (sentinelPool != null) {
|
||||
|
||||
HostAndPort masterAddress = queryMasterAddress();
|
||||
|
||||
if ((internalPool != null) && isAddressChange(masterAddress)) {
|
||||
logger.info("The internalPool {} had changed, destroy it now.", previousMasterAddress);
|
||||
destroyInternelPool();
|
||||
}
|
||||
|
||||
if (internalPool == null) {
|
||||
logger.info("The internalPool {} is not init or the address had changed, init it now.",
|
||||
masterAddress);
|
||||
initInternalPool(masterAddress, masterConnectionInfo, masterPoolConfig);
|
||||
poolInit.countDown();
|
||||
}
|
||||
|
||||
previousMasterAddress = masterAddress;
|
||||
|
||||
sentinelJedis = sentinelPool.getResource();
|
||||
subscriber = new MasterSwitchSubscriber();
|
||||
sentinelJedis.subscribe(subscriber, "+switch-master", "+redirect-to-master");
|
||||
} else {
|
||||
logger.info("All sentinels down, sleep 2 seconds and try to connect again.");
|
||||
// When the system startup but the sentinels not yet, init a urgly address to prevent null point
|
||||
// exception. change the logic later.
|
||||
if (internalPool == null) {
|
||||
HostAndPort masterAddress = new HostAndPort(NO_ADDRESS_YET, 6379);
|
||||
initInternalPool(masterAddress, masterConnectionInfo, masterPoolConfig);
|
||||
previousMasterAddress = masterAddress;
|
||||
}
|
||||
sleep(2000);
|
||||
}
|
||||
} catch (JedisConnectionException e) {
|
||||
|
||||
if (sentinelJedis != null) {
|
||||
sentinelPool.returnBrokenResource(sentinelJedis);
|
||||
}
|
||||
|
||||
if (running.get()) {
|
||||
logger.error("Lost connection with Sentinel " + sentinelPool.getAddress()
|
||||
+ ", sleep 1 seconds and try to connect other one. ");
|
||||
sleep(1000);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
sleep(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public HostAndPort getCurrentMasterAddress() {
|
||||
return previousMasterAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pickup the first available sentinel, if all sentinel down, return null.
|
||||
*/
|
||||
private JedisPool pickupSentinel() {
|
||||
for (JedisPool pool : sentinelPools) {
|
||||
if (JedisUtils.ping(pool)) {
|
||||
return pool;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean isAddressChange(HostAndPort currentMasterAddress) {
|
||||
if (previousMasterAddress == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return !previousMasterAddress.equals(currentMasterAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* Query master address from sentinel.
|
||||
*/
|
||||
private HostAndPort queryMasterAddress() {
|
||||
JedisTemplate sentinelTemplate = new JedisTemplate(sentinelPool);
|
||||
List<String> address = sentinelTemplate.execute(new JedisTemplate.JedisAction<List<String>>() {
|
||||
@Override
|
||||
public List<String> action(Jedis jedis) {
|
||||
return jedis.sentinelGetMasterAddrByName(masterName);
|
||||
}
|
||||
});
|
||||
|
||||
if ((address == null) || address.isEmpty()) {
|
||||
throw new IllegalArgumentException("Master name " + masterName + " is not in sentinel.conf");
|
||||
}
|
||||
|
||||
return new HostAndPort(address.get(0), Integer.valueOf(address.get(1)));
|
||||
}
|
||||
|
||||
private void sleep(int millseconds) {
|
||||
try {
|
||||
Thread.sleep(millseconds);
|
||||
} catch (InterruptedException e1) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscriber for the master switch message from sentinel and init the new pool.
|
||||
*/
|
||||
private class MasterSwitchSubscriber extends JedisPubSub {
|
||||
@Override
|
||||
public void onMessage(String channel, String message) {
|
||||
// message example: +switch-master: mymaster 127.0.0.1 6379 127.0.0.1 6380
|
||||
// +redirect-to-master default 127.0.0.1 6380 127.0.0.1 6381 (if slave-master fail-over quick enough)
|
||||
logger.info("Sentinel " + sentinelPool.getAddress() + " published: " + message);
|
||||
String[] switchMasterMsg = message.split(" ");
|
||||
// if the master name equals my master name, destroy the old pool and init a new pool
|
||||
if (masterName.equals(switchMasterMsg[0])) {
|
||||
|
||||
HostAndPort masterAddress = new HostAndPort(switchMasterMsg[3],
|
||||
Integer.parseInt(switchMasterMsg[4]));
|
||||
logger.info("Switch master to " + masterAddress);
|
||||
destroyInternelPool();
|
||||
initInternalPool(masterAddress, masterConnectionInfo, masterPoolConfig);
|
||||
previousMasterAddress = masterAddress;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPMessage(String pattern, String channel, String message) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubscribe(String channel, int subscribedChannels) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUnsubscribe(String channel, int subscribedChannels) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPUnsubscribe(String pattern, int subscribedChannels) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPSubscribe(String pattern, int subscribedChannels) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
package javautils.regex;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javautils.StringUtil;
|
||||
|
||||
public final class RegexUtil {
|
||||
|
||||
private RegexUtil() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 匹配图象
|
||||
* 格式: /相对路径/文件名.后缀 (后缀为gif,dmp,png,jpg)
|
||||
* 匹配 : /forum/head_icon/admini2005111_ff.gif 或 admini2005111.dmp
|
||||
* 不匹配: c:/admins4512.gif
|
||||
*/
|
||||
public static final String image_regex = "^(/{0,1}\\w){1,}\\.(gif|dmp|png|jpg|jpeg)$|^\\w{1,}\\.(gif|dmp|png|jpg|jpeg)$";
|
||||
|
||||
/**
|
||||
* 匹配email地址
|
||||
* 匹配 : foo@bar.com 或 foobar@foobar.com.au
|
||||
* 不匹配: foo@bar 或 $$$@bar.com
|
||||
*/
|
||||
public static final String email_regex = "(?:\\w[-._\\w]*\\w@\\w[-._\\w]*\\w\\.\\w{2,3}$)";
|
||||
|
||||
/**
|
||||
* 匹配匹配并提取url
|
||||
* 匹配 : http://www.suncer.com 或news://www
|
||||
* 提取(MatchResult matchResult=matcher.getMatch()): matchResult.group(0)=
|
||||
* http://www.suncer.com:8080/index.html?login=true matchResult.group(1) =
|
||||
* http matchResult.group(2) = www.suncer.com matchResult.group(3) = :8080
|
||||
* matchResult.group(4) = /index.html?login=true
|
||||
* 不匹配: c:\window
|
||||
*/
|
||||
public static final String url_regex = "(\\w+)://([^/:]+)(:\\d*)?([^\\s]*)";
|
||||
|
||||
/**
|
||||
* 匹配并提取http
|
||||
* 匹配 : http://www.suncer.com:8080/index.html?login=true
|
||||
* 提取(MatchResult matchResult=matcher.getMatch()): matchResult.group(0)=
|
||||
* http://www.suncer.com:8080/index.html?login=true matchResult.group(1) =
|
||||
* http matchResult.group(2) = www.suncer.com matchResult.group(3) = :8080
|
||||
* matchResult.group(4) = /index.html?login=true
|
||||
* 不匹配: news://www
|
||||
*/
|
||||
public static final String http_regex = "(http|https|ftp)://([^/:]+)(:\\d*)?([^\\s]*)";
|
||||
|
||||
/**
|
||||
* 匹配日期
|
||||
* 范围:1900--2099
|
||||
* 匹配 : 2005-04-04
|
||||
* 不匹配: 01-01-01
|
||||
*/
|
||||
public static final String date_regex = "^((((19){1}|(20){1})d{2})|d{2})[-\\s]{1}[01]{1}d{1}[-\\s]{1}[0-3]{1}d{1}$";// 匹配日期
|
||||
|
||||
/**
|
||||
* 匹配电话
|
||||
* 匹配 : 0371-123456 或 (0371)1234567 或 (0371)12345678 或 010-123456 或
|
||||
* 010-12345678 或 12345678912
|
||||
* 不匹配: 1111-134355 或 0123456789
|
||||
*/
|
||||
public static final String phone_regex = "^(?:0[0-9]{2,3}[-\\s]{1}|\\(0[0-9]{2,4}\\))[0-9]{6,8}$|^[1-9]{1}[0-9]{5,7}$|^[1-9]{1}[0-9]{10}$";
|
||||
|
||||
/**
|
||||
* 匹配身份证
|
||||
* 匹配 : 0123456789123
|
||||
* 不匹配: 0123456
|
||||
*/
|
||||
public static final String ID_card_regex = "^\\d{10}|\\d{13}|\\d{15}|\\d{18}$";
|
||||
|
||||
/**
|
||||
* 匹配邮编代码
|
||||
* 匹配 : 012345
|
||||
* 不匹配: 0123456
|
||||
*/
|
||||
public static final String ZIP_regex = "^[0-9]{6}$";// 匹配邮编代码
|
||||
|
||||
/**
|
||||
* 不包括特殊字符的匹配 (字符串中不包括符号 数学次方号^ 单引号' 双引号" 分号; 逗号, 帽号: 数学减号- 右尖括号> 左尖括号< 反斜杠\
|
||||
* 即空格,制表符,回车符等 )
|
||||
* 格式为: x 或 一个一上的字符
|
||||
* 匹配 : 012345
|
||||
* 不匹配: 0123456
|
||||
*/
|
||||
public static final String non_special_char_regex = "^[^'\"\\;,:-<>\\s].+$";// 匹配邮编代码
|
||||
|
||||
/**
|
||||
* 匹配非负整数(正整数 + 0)
|
||||
*/
|
||||
public static final String non_negative_integers_regex = "^\\d+$";
|
||||
|
||||
/**
|
||||
* 匹配不包括零的非负整数(正整数 > 0)
|
||||
*/
|
||||
public static final String non_zero_negative_integers_regex = "^[1-9]+\\d*$";
|
||||
|
||||
/**
|
||||
* 匹配正整数
|
||||
*/
|
||||
public static final String positive_integer_regex = "^[0-9]*[1-9][0-9]*$";
|
||||
|
||||
/**
|
||||
* 匹配非正整数(负整数 + 0)
|
||||
*/
|
||||
public static final String non_positive_integers_regex = "^((-\\d+)|(0+))$";
|
||||
|
||||
/**
|
||||
* 匹配负整数
|
||||
*/
|
||||
public static final String negative_integers_regex = "^-[0-9]*[1-9][0-9]*$";
|
||||
|
||||
/**
|
||||
* 匹配整数
|
||||
*/
|
||||
public static final String integer_regex = "^-?\\d+$";
|
||||
|
||||
/**
|
||||
* 匹配非负浮点数(正浮点数 + 0)
|
||||
*/
|
||||
public static final String non_negative_rational_numbers_regex = "^\\d+(\\.\\d+)?$";
|
||||
|
||||
/**
|
||||
* 匹配正浮点数
|
||||
*/
|
||||
public static final String positive_rational_numbers_regex = "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
|
||||
|
||||
/**
|
||||
* 匹配非正浮点数(负浮点数 + 0)
|
||||
*/
|
||||
public static final String non_positive_rational_numbers_regex = "^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$";
|
||||
|
||||
/**
|
||||
* 匹配负浮点数
|
||||
*/
|
||||
public static final String negative_rational_numbers_regex = "^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$";
|
||||
|
||||
/**
|
||||
* 匹配浮点数
|
||||
*/
|
||||
public static final String rational_numbers_regex = "^(-?\\d+)(\\.\\d+)?$";
|
||||
|
||||
/**
|
||||
* 匹配由26个英文字母组成的字符串
|
||||
*/
|
||||
public static final String letter_regex = "^[A-Za-z]+$";
|
||||
|
||||
/**
|
||||
* 匹配由26个英文字母的大写组成的字符串
|
||||
*/
|
||||
public static final String upward_letter_regex = "^[A-Z]+$";
|
||||
|
||||
/**
|
||||
* 匹配由26个英文字母的小写组成的字符串
|
||||
*/
|
||||
public static final String lower_letter_regex = "^[a-z]+$";
|
||||
|
||||
/**
|
||||
* 匹配由数字和26个英文字母组成的字符串
|
||||
*/
|
||||
public static final String letter_number_regex = "^[A-Za-z0-9]+$";
|
||||
|
||||
/**
|
||||
* 匹配由数字、26个英文字母或者下划线组成的字符串
|
||||
*/
|
||||
public static final String letter_number_underline_regex = "^\\w+$";
|
||||
|
||||
public static boolean isMatcher(String s, String regex){
|
||||
boolean flag = false;
|
||||
try {
|
||||
if(StringUtil.isNotNull(s)) {
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(s);
|
||||
flag = matcher.matches();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("匹配失败!");
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package javautils.spring;
|
||||
|
||||
import javautils.StringUtil;
|
||||
import javautils.encrypt.DESUtil;
|
||||
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
||||
|
||||
/**
|
||||
* Created by Nick on 2016/10/3.
|
||||
*/
|
||||
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
|
||||
private static final String KEY = "#$ddw134R$#G#DSFW@#?!@#!@#$CCCREW1";
|
||||
private static final DESUtil DES_UTIL = DESUtil.getInstance();
|
||||
|
||||
@Override
|
||||
protected String convertProperty(String propertyName, String propertyValue) {
|
||||
if (StringUtil.isNotNull(propertyValue) && propertyValue.endsWith("|e")) {
|
||||
String tempValue = propertyValue.substring(0, propertyValue.length() - 2);
|
||||
String decryptValue = DES_UTIL.decryptStr(tempValue, KEY);
|
||||
return decryptValue;
|
||||
} else {
|
||||
return propertyValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package lottery.bin;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
// import lottery.domains.capture.jobs.K3CrawlerJob;
|
||||
// import lottery.domains.capture.jobs.OpenCaiJob;
|
||||
// import lottery.domains.capture.jobs.OthersCrawlerJob;
|
||||
// import lottery.domains.capture.jobs.SSCCrawlerJob;
|
||||
// import lottery.domains.capture.jobs.X511CrawlerJob;
|
||||
import lottery.domains.content.global.JobConfig;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Catalina {
|
||||
// int corePoolSize = 5;
|
||||
// int maxPoolSize = 10;
|
||||
// long keepAliveTime = 5000;
|
||||
//
|
||||
// ExecutorService threadPoolExecutor = new ThreadPoolExecutor(corePoolSize,
|
||||
// maxPoolSize, keepAliveTime, TimeUnit.MILLISECONDS,
|
||||
// new LinkedBlockingQueue<Runnable>());
|
||||
ExecutorService newFixedThreadPool;
|
||||
|
||||
@Autowired
|
||||
private JobConfig config;
|
||||
|
||||
// @Autowired
|
||||
// private OpenCaiJob mOpenCaiJob;
|
||||
|
||||
/**
|
||||
* OpenCai
|
||||
*/
|
||||
public void executeOpenCai() {
|
||||
config.isExeOpenCai = true;
|
||||
config.isExeOthers = true;
|
||||
// mOthersCrawlerJob.executeHN481();
|
||||
}
|
||||
|
||||
// @Autowired
|
||||
// private SSCCrawlerJob mSscCrawlerJob;
|
||||
|
||||
/**
|
||||
* 时时彩
|
||||
*/
|
||||
public void executeSSC() {
|
||||
// config.isExeSsc = true;
|
||||
// mSscCrawlerJob.executeCQSSC();
|
||||
// //mSscCrawlerJob.executeJXSSC();
|
||||
// mSscCrawlerJob.executeTJSSC();
|
||||
// mSscCrawlerJob.executeXJSSC();
|
||||
}
|
||||
|
||||
// @Autowired
|
||||
// private X511CrawlerJob mX511CrawlerJob;
|
||||
|
||||
/**
|
||||
* 11选5
|
||||
*/
|
||||
public void execute11x5() {
|
||||
// config.isExe11x5 = true;
|
||||
// mX511CrawlerJob.executeAH11x5();
|
||||
// mX511CrawlerJob.executeGD11x5();
|
||||
// mX511CrawlerJob.executeJX11x5();
|
||||
// mX511CrawlerJob.executeSD11x5();
|
||||
// mX511CrawlerJob.executeSH11x5();
|
||||
}
|
||||
|
||||
// @Autowired
|
||||
// private K3CrawlerJob mK3CrawlerJob;
|
||||
|
||||
/**
|
||||
* 快三
|
||||
*/
|
||||
public void executeK3() {
|
||||
// config.isExeK3 = true;
|
||||
// mK3CrawlerJob.executeAHK3();
|
||||
// mK3CrawlerJob.executeHBK3();
|
||||
// mK3CrawlerJob.executeJLK3();
|
||||
// mK3CrawlerJob.executeJSK3();
|
||||
}
|
||||
|
||||
// @Autowired
|
||||
// private OthersCrawlerJob mOthersCrawlerJob;
|
||||
|
||||
/**
|
||||
* 北京快乐8、北京pk0、福彩3d、排列三
|
||||
*/
|
||||
public void executeOthers() {
|
||||
// config.isExeOthers = true;
|
||||
// mOthersCrawlerJob.executeBJKL8();
|
||||
// mOthersCrawlerJob.executeBJPK8();
|
||||
// mOthersCrawlerJob.executeFC3D();
|
||||
// mOthersCrawlerJob.executePL3();
|
||||
// mOthersCrawlerJob.executeHN481();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package lottery.bin;
|
||||
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ExecuteServer {
|
||||
private static final Logger mLogger = LoggerFactory.getLogger(ExecuteServer.class);
|
||||
// 每1小时点触发
|
||||
private static final String CRON = "0 0 0/1 * * *";
|
||||
// private static final String CRON = "0 0/1 * * * *";
|
||||
|
||||
@Autowired
|
||||
Catalina mCatalina;
|
||||
|
||||
@Scheduled(cron = CRON)
|
||||
public void execute() {
|
||||
mLogger.info("重启====>>>>>>>>>");
|
||||
mCatalina.executeOpenCai();
|
||||
//
|
||||
// mCatalina.executeSSC();
|
||||
//
|
||||
// mCatalina.execute11x5();
|
||||
// mCatalina.executeK3();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package lottery.bin;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class StartUp {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(StartUp.class);
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
public static void main(String[] args) {
|
||||
logger.debug("start up main method.");
|
||||
String[] configLocations = { "classpath:config/spring/spring-config.xml" };
|
||||
applicationContext = new ClassPathXmlApplicationContext(configLocations);
|
||||
ExecuteServer mExecuteServer = (ExecuteServer) applicationContext
|
||||
.getBean("executeServer");
|
||||
mExecuteServer.execute();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
package lottery.domains.capture.jobs;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import javautils.date.Moment;
|
||||
import javautils.http.HttpClientUtil;
|
||||
import lottery.domains.capture.sites.b1cp.BoyiBean;
|
||||
import lottery.domains.capture.utils.CodeValidate;
|
||||
import lottery.domains.capture.utils.ExpectValidate;
|
||||
import lottery.domains.content.biz.LotteryOpenCodeService;
|
||||
import lottery.domains.content.entity.LotteryOpenCode;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 博易,主要用来获取腾讯分分彩数据,但当前期不获取,当前期以PCQQJob为准
|
||||
*/
|
||||
@Component
|
||||
public class BoyiJob {
|
||||
private static final Logger logger = LoggerFactory.getLogger(BoyiJob.class);
|
||||
private static boolean isRuning = false;
|
||||
private static final String TOKEN = "93022be9ac9cfc75"; // token,注册后有 setyourtokenhere替换为自己的token 在
|
||||
private static final String URL = "http://api.b1cp.com/api?p=json&t=txffc&limit=10&token=" + TOKEN;
|
||||
|
||||
// 彩票code列表
|
||||
private static final Map<String, String> LOTTERIES = new HashMap<>();
|
||||
static {
|
||||
// key:对方code,value:我方code
|
||||
LOTTERIES.put("txffc", "txffc"); // 腾讯分分彩
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private LotteryOpenCodeService lotteryOpenCodeService;
|
||||
|
||||
|
||||
// @Scheduled(cron = "0/20 * * * * *") // 注意频率,每次间隔大于1秒
|
||||
@Scheduled(cron = "6,10,15,20,25,30,35 * * * * *")
|
||||
// @PostConstruct
|
||||
public void execute() {
|
||||
synchronized (BoyiJob.class) {
|
||||
if (isRuning == true) {
|
||||
return;
|
||||
}
|
||||
isRuning = true;
|
||||
}
|
||||
|
||||
try {
|
||||
logger.debug("开始抓取博易腾讯分分彩开奖数据>>>>>>>>>>>>>>>>");
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
start();
|
||||
long spend = System.currentTimeMillis() - start;
|
||||
|
||||
logger.debug("完成抓取博易腾讯分分彩开奖数据>>>>>>>>>>>>>>>>耗时{}", spend);
|
||||
} catch (Exception e) {
|
||||
logger.error("抓取博易腾讯分分彩开奖数据出错", e);
|
||||
} finally {
|
||||
isRuning = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void start() {
|
||||
for (String lottery : LOTTERIES.keySet()) {
|
||||
try {
|
||||
String realName = LOTTERIES.get(lottery);
|
||||
|
||||
String result = getResult(lottery, 10);
|
||||
|
||||
handleData(realName, result);
|
||||
} catch (Exception e) {
|
||||
logger.error("抓取博易腾讯分分彩"+lottery+"开奖数据出错", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getResult(String name, int num) {
|
||||
String result = get(URL);
|
||||
return result;
|
||||
}
|
||||
|
||||
private void handleData(String realName, String result) {
|
||||
if (StringUtils.isEmpty(result)) {
|
||||
return;
|
||||
}
|
||||
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
JSONArray jsonArr = jsonObject.getJSONArray("data");
|
||||
List<BoyiBean> openCodes = JSON.parseArray(jsonArr.toJSONString(), BoyiBean.class);
|
||||
if (CollectionUtils.isEmpty(openCodes)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 处理数据
|
||||
for (BoyiBean openCode : openCodes) {
|
||||
handleBean(realName, openCode);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean handleBean(String realName, BoyiBean openCode) {
|
||||
openCode.setExpect(formartExpect(openCode.getExpect()));
|
||||
LotteryOpenCode dbData = lotteryOpenCodeService.get(realName, openCode.getExpect());
|
||||
if (dbData != null) {
|
||||
if (!dbData.getCode().equals(openCode.getOpencode())) {
|
||||
logger.error("博易抓取时遇到错误:抓取{}期开奖号码{}与数据库已有开奖号码{}不符", openCode.getExpect(), openCode.getOpencode(), dbData.getCode());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 如果本期和上奖开奖号码相同,那么把开奖号码状态改为无效撤单
|
||||
LotteryOpenCode lotteryOpenCode = new LotteryOpenCode();
|
||||
lotteryOpenCode.setInterfaceTime(openCode.getOpentime());
|
||||
lotteryOpenCode.setLottery(realName);
|
||||
lotteryOpenCode.setTime(new Moment().toSimpleTime());
|
||||
lotteryOpenCode.setOpenStatus(0);
|
||||
lotteryOpenCode.setRemarks("b1cp.com");
|
||||
|
||||
switch (realName) {
|
||||
// 腾讯分分彩
|
||||
case "txffc":
|
||||
lotteryOpenCode.setCode(openCode.getOpencode());
|
||||
lotteryOpenCode.setExpect(openCode.getExpect());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(lotteryOpenCode.getCode()) || StringUtils.isEmpty(lotteryOpenCode.getExpect())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (CodeValidate.validate(realName, lotteryOpenCode.getCode()) == false) {
|
||||
logger.error("博易腾讯分分彩" + realName + "抓取号码" + lotteryOpenCode.getCode() + "错误");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ExpectValidate.validate(realName, lotteryOpenCode.getExpect()) == false) {
|
||||
logger.error("博易腾讯分分彩" + realName + "抓取期数" + lotteryOpenCode.getExpect() + "错误");
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean added = lotteryOpenCodeService.add(lotteryOpenCode, true);
|
||||
if (added) {
|
||||
logger.info("博易官网成功抓取腾讯分分彩{}期开奖号码{}", openCode.getExpect(), openCode.getOpencode());
|
||||
// 腾讯龙虎斗
|
||||
if ("txffc".equals(realName)) {
|
||||
LotteryOpenCode txlhdCode = new LotteryOpenCode("txlhd", lotteryOpenCode.getExpect(), lotteryOpenCode.getCode(), lotteryOpenCode.getTime(), lotteryOpenCode.getOpenStatus(), null, lotteryOpenCode.getRemarks());
|
||||
txlhdCode.setInterfaceTime(lotteryOpenCode.getInterfaceTime());
|
||||
lotteryOpenCodeService.add(txlhdCode, false);
|
||||
}
|
||||
}
|
||||
|
||||
return added;
|
||||
}
|
||||
|
||||
private static String getExpectByTime(String time) {
|
||||
Moment moment = new Moment().fromTime(time);
|
||||
int hour = moment.get("hour");
|
||||
int minute = moment.get("minute");
|
||||
|
||||
if (hour == 0 && minute == 0) {
|
||||
// 如果是0点0分,那么就是昨天的最后一期,即1440期
|
||||
moment = moment.add(-1, "minutes");
|
||||
hour = 24;
|
||||
}
|
||||
String date = moment.format("yyyyMMdd");
|
||||
int dayExpect = (hour * 60) + minute;
|
||||
String expect = date + "-" + (String.format("%04d", dayExpect));
|
||||
return expect;
|
||||
}
|
||||
|
||||
private String formartExpect(String expect){
|
||||
String date = expect.substring(0,8);
|
||||
String num = String.format("%04d", Integer.valueOf(expect.substring(8)));
|
||||
return date + "-" +num;
|
||||
}
|
||||
|
||||
public static String get(String urlAll) {
|
||||
try {
|
||||
String result = HttpClientUtil.get(urlAll, null, 5000);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
logger.error("请求博易腾讯分分彩出错", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,292 @@
|
||||
package lottery.domains.capture.jobs;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import javautils.date.Moment;
|
||||
import javautils.http.HttpClientUtil;
|
||||
import lottery.domains.capture.sites.cpk.CPKBean;
|
||||
import lottery.domains.capture.utils.CodeValidate;
|
||||
import lottery.domains.capture.utils.ExpectValidate;
|
||||
import lottery.domains.content.biz.LotteryOpenCodeService;
|
||||
import lottery.domains.content.dao.LotteryCrawlerStatusDao;
|
||||
import lottery.domains.content.entity.LotteryOpenCode;
|
||||
import net.sf.json.JSONException;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 彩票控开奖源 注意频率,同一彩种间隔必须大于3秒,否则会封IP,该接口只能用于一个环境,否则会封IP
|
||||
*/
|
||||
@Component
|
||||
public class CaipiaokongJob {
|
||||
private static final String UID = "974888"; // 用户ID,购买后有 setyouridhere替换为自己的id
|
||||
private static final String TOKEN = "6739843043cc3046bc1b6ed01cf56a9e885dc300"; // token,购买后有 setyourtokenhere替换为自己的token
|
||||
private static final String NUM = "10"; // 每次获取条数 1-50
|
||||
private static final String URL = "http://api.kaijiangtong.com/lottery/?&format=json&uid=" + UID + "&token=" + TOKEN + "&num=" + NUM;
|
||||
|
||||
// 彩票code列表
|
||||
private static final Map<String, String> LOTTERIES = new HashMap<>();
|
||||
static {
|
||||
LOTTERIES.put("cqssc", "cqssc"); // 重庆时时彩
|
||||
LOTTERIES.put("xjssc", "xjssc"); // 新疆时时彩
|
||||
LOTTERIES.put("tjssc", "tjssc"); // 天津时时彩
|
||||
// LOTTERIES.put("twbg", "tw5fc"); // 台湾5分彩/台湾快乐8
|
||||
// LOTTERIES.put("bjklb", "bj5fc"); // 北京5分彩/北京快乐8
|
||||
// LOTTERIES.put("jndklb", "jnd3d5fc"); // 加拿大3.5
|
||||
|
||||
LOTTERIES.put("gdsyxw", "gd11x5"); // 广东11选5
|
||||
LOTTERIES.put("jxsyxw", "jx11x5"); // 江西11选5
|
||||
LOTTERIES.put("ahsyxw", "ah11x5"); // 安徽11选5
|
||||
LOTTERIES.put("shsyxw", "sh11x5"); // 上海11选5
|
||||
LOTTERIES.put("sdsyydj", "sd11x5"); // 山东11选5
|
||||
|
||||
// LOTTERIES.put("jsks", "jsk3"); // 江苏快3
|
||||
// LOTTERIES.put("gxks", "gxk3"); //广西快3
|
||||
// LOTTERIES.put("bjks", "bjk3"); //北京快3
|
||||
// LOTTERIES.put("hubks", "hbk3"); //湖北快3
|
||||
// LOTTERIES.put("hbks", "hebk3"); //河北快3
|
||||
LOTTERIES.put("ahks", "ahk3"); // 安徽快3
|
||||
// LOTTERIES.put("shks", "shk3"); // 上海快3
|
||||
// LOTTERIES.put("gsks", "gsk3"); //甘肃快3
|
||||
// LOTTERIES.put("jxks", "jxk3"); //江西快3
|
||||
|
||||
// LOTTERIES.put("jlks", "jlk3"); // 吉林快3
|
||||
|
||||
LOTTERIES.put("sd", "fc3d"); // 福彩3D
|
||||
LOTTERIES.put("pls", "pl3"); // 排列三
|
||||
|
||||
LOTTERIES.put("bjpks", "bjpk10"); // 北京PK10
|
||||
}
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(CaipiaokongJob.class);
|
||||
|
||||
@Autowired
|
||||
private LotteryOpenCodeService lotteryOpenCodeService;
|
||||
|
||||
@Autowired
|
||||
private LotteryCrawlerStatusDao lotteryCrawlerStatusDao;
|
||||
|
||||
private static boolean isRuning = false;
|
||||
|
||||
@Scheduled(cron = "0,5,10,15,20,25,30,35,40,45,50,55 * 0-3,7-23 * * *") // 注意频率,同一彩种间隔必须大于3秒,否则会封IP,该接口只能用于一个环境,否则会封IP
|
||||
// @PostConstruct
|
||||
public void execute() {
|
||||
synchronized (CaipiaokongJob.class) {
|
||||
if (isRuning == true) {
|
||||
return;
|
||||
}
|
||||
isRuning = true;
|
||||
}
|
||||
|
||||
try {
|
||||
logger.debug("开始抓取彩票控开奖数据>>>>>>>>>>>>>>>>");
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
start();
|
||||
long spend = System.currentTimeMillis() - start;
|
||||
|
||||
logger.debug("完成抓取彩票控开奖数据>>>>>>>>>>>>>>>>耗时{}", spend);
|
||||
} catch (Exception e) {
|
||||
logger.error("抓取彩票控开奖数据出错", e);
|
||||
} finally {
|
||||
isRuning = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void start() {
|
||||
for (String lottery : LOTTERIES.keySet()) {
|
||||
try {
|
||||
String result = getResult(lottery);
|
||||
handleData(lottery, result);
|
||||
} catch (Exception e) {
|
||||
logger.error("抓取彩票控"+lottery+"开奖数据出错", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getResult(String name) {
|
||||
String url = URL + "&name="+name+"&_=" + System.currentTimeMillis();
|
||||
String charset = "UTF-8";
|
||||
String result = get(url, charset);
|
||||
return result;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private void handleData(String name, String result) {
|
||||
if (StringUtils.isEmpty(result)) {
|
||||
return;
|
||||
}
|
||||
|
||||
HashMap<String, Object> hashMap = JSON.parseObject(result, HashMap.class);
|
||||
if (hashMap == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<CPKBean> list = new ArrayList<>();
|
||||
|
||||
try {
|
||||
Set<String> set = hashMap.keySet();
|
||||
Iterator<String> iterator = set.iterator();
|
||||
while(iterator.hasNext()) {
|
||||
String expect = iterator.next();
|
||||
String value = hashMap.get(expect).toString();
|
||||
CPKBean cpkBean = JSON.parseObject(value, CPKBean.class);
|
||||
cpkBean.setExpect(expect);
|
||||
list.add(cpkBean);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
logger.error("获取彩票控" + name +"数据时出错", e);
|
||||
}
|
||||
|
||||
// 处理数据
|
||||
for (CPKBean bean : list) {
|
||||
handleBean(name, bean);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean handleBean(String name, CPKBean bean) {
|
||||
String expect = null;
|
||||
String date = null;
|
||||
String code = null;
|
||||
String realName = LOTTERIES.get(name);
|
||||
switch (realName) {
|
||||
// 时时彩
|
||||
case "cqssc":
|
||||
case "xjssc":
|
||||
case "tjssc":
|
||||
case "sd11x5":
|
||||
case "jx11x5":
|
||||
case "sh11x5":
|
||||
case "ahk3":
|
||||
case "hbk3":
|
||||
case "hebk3":
|
||||
date = bean.getExpect().substring(0, 8);
|
||||
expect = bean.getExpect().substring(8);
|
||||
if (expect.length() == 2) {
|
||||
expect = "0" + expect;
|
||||
}
|
||||
expect = date + "-" + expect;
|
||||
code = bean.getNumber();
|
||||
break;
|
||||
// 11选5
|
||||
case "ah11x5":
|
||||
case "gd11x5":
|
||||
case "jsk3":
|
||||
case "jlk3":
|
||||
case "shk3":
|
||||
case "gxk3":
|
||||
case "gsk3":
|
||||
case "jxk3":
|
||||
date = bean.getExpect().substring(0, 6);
|
||||
if (date.length() == 6) {
|
||||
date = "20" + date;
|
||||
}
|
||||
expect = bean.getExpect().substring(6);
|
||||
if (expect.length() == 2) {
|
||||
expect = "0" + expect;
|
||||
}
|
||||
expect = date + "-" + expect;
|
||||
code = bean.getNumber();
|
||||
break;
|
||||
// 低频彩
|
||||
case "fc3d":
|
||||
case "pl3":
|
||||
expect = bean.getExpect().substring(2);
|
||||
code = bean.getNumber();
|
||||
break;
|
||||
case "bj5fc":
|
||||
expect = bean.getExpect();
|
||||
code = bean.getNumber().substring(0, 59);
|
||||
code = convertFFCCode(code);
|
||||
break;
|
||||
case "bjpk10":
|
||||
case "bjk3":
|
||||
expect = bean.getExpect();
|
||||
code = bean.getNumber();
|
||||
break;
|
||||
// 台湾5分彩
|
||||
case "tw5fc":
|
||||
case "jnd3d5fc":
|
||||
expect = bean.getExpect();
|
||||
code = bean.getNumber().substring(0, 59);
|
||||
code = convertFFCCode(code);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (expect == null || code == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (CodeValidate.validate(realName, code) == false) {
|
||||
logger.error("彩票控" + realName + "抓取号码" + code + "错误");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ExpectValidate.validate(realName, expect) == false) {
|
||||
logger.error("彩票控" + realName + "抓取期数" + expect + "错误");
|
||||
return false;
|
||||
}
|
||||
|
||||
LotteryOpenCode lotteryOpenCode = new LotteryOpenCode(realName, expect, code, new Moment().toSimpleTime(), 0, null, "CPK");
|
||||
if (StringUtils.isNotEmpty(bean.getDateline())) {
|
||||
lotteryOpenCode.setInterfaceTime(bean.getDateline());
|
||||
}
|
||||
else {
|
||||
lotteryOpenCode.setInterfaceTime(new Moment().toSimpleTime());
|
||||
}
|
||||
|
||||
boolean added = lotteryOpenCodeService.add(lotteryOpenCode, false);
|
||||
if (added) {
|
||||
if ("bj5fc".equals(realName)) {
|
||||
LotteryOpenCode bjkl8Code = new LotteryOpenCode("bjkl8", expect, bean.getNumber().substring(0, 59), new Moment().toSimpleTime(), 0, null, "CPK");
|
||||
bjkl8Code.setInterfaceTime(lotteryOpenCode.getInterfaceTime());
|
||||
lotteryOpenCodeService.add(bjkl8Code, false);
|
||||
}
|
||||
}
|
||||
|
||||
return added;
|
||||
}
|
||||
|
||||
public static String get(String urlAll, String charset) {
|
||||
try {
|
||||
String result = HttpClientUtil.get(urlAll, null, 30000);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
logger.error("请求彩票控出错", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 转换1.5分彩号码
|
||||
*/
|
||||
private String convertFFCCode(String openCode) {
|
||||
String[] codes = openCode.split(",");
|
||||
String code1 = mergeFFCCode(codes[0], codes[1], codes[2], codes[3]);
|
||||
String code2 = mergeFFCCode(codes[4], codes[5], codes[6], codes[7]);
|
||||
String code3 = mergeFFCCode(codes[8], codes[9], codes[10], codes[11]);
|
||||
String code4 = mergeFFCCode(codes[12], codes[13], codes[14], codes[15]);
|
||||
String code5 = mergeFFCCode(codes[16], codes[17], codes[18], codes[19]);
|
||||
|
||||
return code1 + "," + code2 + "," + code3 + "," + code4 + "," + code5;
|
||||
}
|
||||
private String mergeFFCCode(String code1, String code2, String code3, String code4) {
|
||||
int codeInt1 = Integer.valueOf(code1);
|
||||
int codeInt2 = Integer.valueOf(code2);
|
||||
int codeInt3 = Integer.valueOf(code3);
|
||||
int codeInt4 = Integer.valueOf(code4);
|
||||
|
||||
int codeSum = codeInt1 + codeInt2 + codeInt3 + codeInt4;
|
||||
String codeSumStr = codeSum+"";
|
||||
String finalCode = codeSumStr.substring(codeSumStr.length() - 1);
|
||||
return finalCode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
//package lottery.domains.capture.jobs;
|
||||
//
|
||||
//import com.alibaba.fastjson.JSON;
|
||||
//import javautils.date.Moment;
|
||||
//import javautils.http.HttpClientUtil;
|
||||
//import lottery.domains.capture.sites.jiangyuan365.JiangYuan365Bean;
|
||||
//import lottery.domains.capture.utils.CodeValidate;
|
||||
//import lottery.domains.capture.utils.ExpectValidate;
|
||||
//import lottery.domains.content.biz.LotteryOpenCodeService;
|
||||
//import lottery.domains.content.entity.LotteryOpenCode;
|
||||
//import org.apache.commons.collections.CollectionUtils;
|
||||
//import org.apache.commons.lang.StringUtils;
|
||||
//import org.slf4j.Logger;
|
||||
//import org.slf4j.LoggerFactory;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.scheduling.annotation.Scheduled;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//
|
||||
//import java.util.*;
|
||||
//
|
||||
///**
|
||||
// * 奖源365,主要用来获取腾讯分分彩数据,但当前期不获取,当前期以PCQQJob为准
|
||||
// * 在http://www.jiangyuan365.com注册账号后获取token
|
||||
// */
|
||||
//@Component
|
||||
//public class JiangYuan365Job {
|
||||
// private static final Logger logger = LoggerFactory.getLogger(JiangYuan365Job.class);
|
||||
// private static boolean isRuning = false;
|
||||
// private static final String TOKEN = "K25aacd7f708267"; // token,注册后有 setyourtokenhere替换为自己的token 在
|
||||
// private static final String URL = "http://free.jiangyuan365.com/" + TOKEN + "/%s-%s.json";
|
||||
//
|
||||
// // 彩票code列表
|
||||
// private static final Map<String, String> LOTTERIES = new HashMap<>();
|
||||
// static {
|
||||
// // key:对方code,value:我方code
|
||||
// LOTTERIES.put("txffc", "txffc"); // 腾讯分分彩
|
||||
// }
|
||||
//
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryOpenCodeService lotteryOpenCodeService;
|
||||
//
|
||||
//
|
||||
//// @Scheduled(cron = "0/20 * * * * *") // 注意频率,每次间隔大于1秒
|
||||
//// @Scheduled(cron = "8,15,20,25,30,35 * * * * *")
|
||||
// // @PostConstruct
|
||||
// public void execute() {
|
||||
// synchronized (JiangYuan365Job.class) {
|
||||
// if (isRuning == true) {
|
||||
// return;
|
||||
// }
|
||||
// isRuning = true;
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// logger.debug("开始抓取奖源365腾讯分分彩开奖数据>>>>>>>>>>>>>>>>");
|
||||
//
|
||||
// long start = System.currentTimeMillis();
|
||||
// start();
|
||||
// long spend = System.currentTimeMillis() - start;
|
||||
//
|
||||
// logger.debug("完成抓取奖源365腾讯分分彩开奖数据>>>>>>>>>>>>>>>>耗时{}", spend);
|
||||
// } catch (Exception e) {
|
||||
// logger.error("抓取奖源365腾讯分分彩开奖数据出错", e);
|
||||
// } finally {
|
||||
// isRuning = false;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private void start() {
|
||||
// for (String lottery : LOTTERIES.keySet()) {
|
||||
// try {
|
||||
// String realName = LOTTERIES.get(lottery);
|
||||
//
|
||||
// String result = getResult(lottery, 10);
|
||||
//
|
||||
// handleData(realName, result);
|
||||
// } catch (Exception e) {
|
||||
// logger.error("抓取奖源365腾讯分分彩"+lottery+"开奖数据出错", e);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public String getResult(String name, int num) {
|
||||
// String url = String.format(URL, name, num) +"?_=" + System.currentTimeMillis();
|
||||
// String result = get(url);
|
||||
// return result;
|
||||
// }
|
||||
//
|
||||
// @SuppressWarnings("rawtypes")
|
||||
// private void handleData(String realName, String result) {
|
||||
// if (StringUtils.isEmpty(result)) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// List<JiangYuan365Bean> openCodes = JSON.parseArray(result, JiangYuan365Bean.class);
|
||||
// if (CollectionUtils.isEmpty(openCodes)) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// // 按期号升序
|
||||
// Collections.sort(openCodes, new Comparator<JiangYuan365Bean>() {
|
||||
// @Override
|
||||
// public int compare(JiangYuan365Bean o1, JiangYuan365Bean o2) {
|
||||
// if ( o1.getIssue().compareTo(o2.getIssue()) > 0) {
|
||||
// return 1;
|
||||
// }
|
||||
// else if ( o1.getIssue().compareTo(o2.getIssue()) < 0) {
|
||||
// return -1;
|
||||
// }
|
||||
//
|
||||
// return 0;
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// if ("txffc".equals(realName)) {
|
||||
// // 腾讯分分彩移掉最新的2期
|
||||
// if (openCodes.size() <= 2) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// openCodes.remove(openCodes.size()-1);
|
||||
// openCodes.remove(openCodes.size()-2);
|
||||
// }
|
||||
//
|
||||
// if (CollectionUtils.isEmpty(openCodes)) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// // 处理数据
|
||||
// for (JiangYuan365Bean openCode : openCodes) {
|
||||
// handleBean(realName, openCode);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private boolean handleBean(String realName, JiangYuan365Bean openCode) {
|
||||
// LotteryOpenCode lotteryOpenCode = new LotteryOpenCode();
|
||||
// lotteryOpenCode.setInterfaceTime(openCode.getOpendate());
|
||||
// lotteryOpenCode.setLottery(realName);
|
||||
// lotteryOpenCode.setTime(new Moment().toSimpleTime());
|
||||
// lotteryOpenCode.setOpenStatus(0);
|
||||
// lotteryOpenCode.setRemarks("JiangYuan365");
|
||||
//
|
||||
// switch (realName) {
|
||||
// // 腾讯分分彩
|
||||
// case "txffc":
|
||||
// lotteryOpenCode.setCode(openCode.getCode());
|
||||
// lotteryOpenCode.setExpect(openCode.getIssue());
|
||||
// break;
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
//
|
||||
// if (StringUtils.isEmpty(lotteryOpenCode.getCode()) || StringUtils.isEmpty(lotteryOpenCode.getExpect())) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// if (CodeValidate.validate(realName, lotteryOpenCode.getCode()) == false) {
|
||||
// logger.error("奖源365腾讯分分彩" + realName + "抓取号码" + lotteryOpenCode.getCode() + "错误");
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// if (ExpectValidate.validate(realName, lotteryOpenCode.getExpect()) == false) {
|
||||
// logger.error("奖源365腾讯分分彩" + realName + "抓取期数" + lotteryOpenCode.getExpect() + "错误");
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// boolean added = lotteryOpenCodeService.add(lotteryOpenCode, true);
|
||||
// if (added) {
|
||||
// // 腾讯龙虎斗
|
||||
// if ("txffc".equals(realName)) {
|
||||
// LotteryOpenCode txlhdCode = new LotteryOpenCode("txlhd", lotteryOpenCode.getExpect(), lotteryOpenCode.getCode(), lotteryOpenCode.getTime(), lotteryOpenCode.getOpenStatus(), null, lotteryOpenCode.getRemarks());
|
||||
// txlhdCode.setInterfaceTime(lotteryOpenCode.getInterfaceTime());
|
||||
// lotteryOpenCodeService.add(txlhdCode, false);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return added;
|
||||
// }
|
||||
//
|
||||
// public static String get(String urlAll) {
|
||||
// try {
|
||||
// String result = HttpClientUtil.get(urlAll, null, 5000);
|
||||
// return result;
|
||||
// } catch (Exception e) {
|
||||
// logger.error("请求奖源365腾讯分分彩出错", e);
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,221 @@
|
||||
// package lottery.domains.capture.jobs;
|
||||
//
|
||||
// import lottery.domains.capture.sites.kj168.Kai168H;
|
||||
// import lottery.domains.capture.sites.shishicai.ShiShiCaiCrawler;
|
||||
// import lottery.domains.capture.utils.LotteryCrons;
|
||||
// import lottery.domains.content.global.JobConfig;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
// import org.springframework.beans.factory.annotation.Autowired;
|
||||
// import org.springframework.scheduling.annotation.Scheduled;
|
||||
// import org.springframework.scheduling.annotation.Schedules;
|
||||
// import org.springframework.stereotype.Component;
|
||||
//
|
||||
// import java.util.concurrent.Callable;
|
||||
// import java.util.concurrent.ExecutorService;
|
||||
// import java.util.concurrent.Executors;
|
||||
// import java.util.concurrent.Future;
|
||||
//
|
||||
// /**
|
||||
// * 快三任务,废弃不再使用
|
||||
// *
|
||||
// * @author ROOT
|
||||
// *
|
||||
// */
|
||||
// @Component
|
||||
// public class K3CrawlerJob {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(K3CrawlerJob.class);
|
||||
//
|
||||
// @Autowired
|
||||
// private JobConfig config;
|
||||
//
|
||||
// @Autowired
|
||||
// private ShiShiCaiCrawler shishicai;
|
||||
//
|
||||
// @Autowired
|
||||
// private Kai168H mKai168H;
|
||||
//
|
||||
// boolean isRunningAHK3 = true;
|
||||
//
|
||||
// /**
|
||||
// * 安徽快三
|
||||
// */
|
||||
// // @Schedules({ @Scheduled(cron = LotteryCrons.ahk3_8),
|
||||
// // @Scheduled(cron = LotteryCrons.ahk3_9_21),
|
||||
// // @Scheduled(cron = LotteryCrons.ahk3_22), })
|
||||
// public void executeAHK3() {
|
||||
// final String lotteryName = "ahk3";
|
||||
// logger.debug(lotteryName + " execute");
|
||||
// if (config.isExeK3 && isRunningAHK3) {
|
||||
// try {
|
||||
// isRunningAHK3 = false;
|
||||
// while (true) {
|
||||
// ExecutorService newFixedThreadPool = Executors
|
||||
// .newFixedThreadPool(4);
|
||||
// Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// @Override
|
||||
// public boolean[] call() throws Exception {
|
||||
// boolean result1 = mKai168H.start(lotteryName);
|
||||
// boolean result2 = shishicai.start(lotteryName);
|
||||
// boolean[] arr = new boolean[] { result1, result2 };
|
||||
// return arr;
|
||||
// }
|
||||
// };
|
||||
// Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// newFixedThreadPool.shutdown();
|
||||
// boolean[] arr = (boolean[]) submit.get();
|
||||
// if (arr[0] || arr[1]) {
|
||||
// break;
|
||||
// }
|
||||
// Thread.sleep(10 * 1000);
|
||||
// logger.debug("安徽快三--->执行完成...");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("安徽快三-->执行出错...", e);
|
||||
// } finally {
|
||||
// isRunningAHK3 = true;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// boolean isRunningHBK3 = true;
|
||||
//
|
||||
// /**
|
||||
// * 湖北快3
|
||||
// */
|
||||
// // @Schedules({ @Scheduled(cron = LotteryCrons.hbk3_9_21),
|
||||
// // @Scheduled(cron = LotteryCrons.hbk3_22) })
|
||||
// public void executeHBK3() {
|
||||
// final String lotteryName = "hbk3";
|
||||
// logger.debug(lotteryName + " execute");
|
||||
// if (config.isExeK3 && isRunningHBK3) {
|
||||
// try {
|
||||
// isRunningHBK3 = false;
|
||||
// while (true) {
|
||||
// ExecutorService newFixedThreadPool = Executors
|
||||
// .newFixedThreadPool(4);
|
||||
// Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// @Override
|
||||
// public boolean[] call() throws Exception {
|
||||
// // boolean result1 = mKai168H.start(lotteryName);
|
||||
// boolean result1 = false;
|
||||
// boolean result2 = shishicai.start(lotteryName);
|
||||
// boolean[] arr = new boolean[] { result1, result2 };
|
||||
// return arr;
|
||||
// }
|
||||
// };
|
||||
// Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// newFixedThreadPool.shutdown();
|
||||
// boolean[] arr = (boolean[]) submit.get();
|
||||
// if (arr[0] || arr[1]) {
|
||||
// break;
|
||||
// }
|
||||
// Thread.sleep(10 * 1000);
|
||||
// logger.debug("湖北快3--->执行完成...");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("湖北快3-->执行出错...", e);
|
||||
// } finally {
|
||||
// isRunningHBK3 = true;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// boolean isRunningJLK3 = true;
|
||||
//
|
||||
// /**
|
||||
// * 吉林快3
|
||||
// */
|
||||
// // @Schedules({ @Scheduled(cron = LotteryCrons.jlk3_8),
|
||||
// // @Scheduled(cron = LotteryCrons.jlk3_9_11),
|
||||
// // @Scheduled(cron = LotteryCrons.jlk3_12),
|
||||
// // @Scheduled(cron = LotteryCrons.jlk3_13_14),
|
||||
// // @Scheduled(cron = LotteryCrons.jlk3_15),
|
||||
// // @Scheduled(cron = LotteryCrons.jlk3_16_17),
|
||||
// // @Scheduled(cron = LotteryCrons.jlk3_18),
|
||||
// // @Scheduled(cron = LotteryCrons.jlk3_19_20),
|
||||
// // @Scheduled(cron = LotteryCrons.jlk3_21) })
|
||||
// public void executeJLK3() {
|
||||
// final String lotteryName = "jlk3";
|
||||
// logger.debug(lotteryName + " execute");
|
||||
// if (config.isExeK3 && isRunningJLK3) {
|
||||
// try {
|
||||
// isRunningJLK3 = false;
|
||||
// while (true) {
|
||||
// ExecutorService newFixedThreadPool = Executors
|
||||
// .newFixedThreadPool(4);
|
||||
// Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// @Override
|
||||
// public boolean[] call() throws Exception {
|
||||
// boolean result1 = mKai168H.start(lotteryName);
|
||||
// boolean result2 = shishicai.start(lotteryName);
|
||||
// boolean[] arr = new boolean[] { result1, result2 };
|
||||
// return arr;
|
||||
// }
|
||||
// };
|
||||
// Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// newFixedThreadPool.shutdown();
|
||||
// boolean[] arr = (boolean[]) submit.get();
|
||||
// if (arr[0] || arr[1]) {
|
||||
// break;
|
||||
// }
|
||||
// Thread.sleep(10 * 1000);
|
||||
// logger.debug("吉林快3--->执行完成...");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("吉林快3-->执行出错...", e);
|
||||
// } finally {
|
||||
// isRunningJLK3 = true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// boolean isRunningJSK3 = true;
|
||||
//
|
||||
// /**
|
||||
// * 江苏快3
|
||||
// */
|
||||
// // @Schedules({ @Scheduled(cron = LotteryCrons.jsk3_8),
|
||||
// // @Scheduled(cron = LotteryCrons.jsk3_9_15),
|
||||
// // @Scheduled(cron = LotteryCrons.jsk3_16_20),
|
||||
// // @Scheduled(cron = LotteryCrons.jsk3_21),
|
||||
// // @Scheduled(cron = LotteryCrons.jsk3_22) })
|
||||
// public void executeJSK3() {
|
||||
// final String lotteryName = "jsk3";
|
||||
// logger.debug(lotteryName + " execute");
|
||||
// if (config.isExeK3 && isRunningJSK3) {
|
||||
// try {
|
||||
// isRunningJSK3 = false;
|
||||
// while (true) {
|
||||
// ExecutorService newFixedThreadPool = Executors
|
||||
// .newFixedThreadPool(4);
|
||||
// Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// @Override
|
||||
// public boolean[] call() throws Exception {
|
||||
// boolean result1 = mKai168H.start(lotteryName);
|
||||
// boolean result2 = shishicai.start(lotteryName);
|
||||
// boolean[] arr = new boolean[] { result1, result2 };
|
||||
// return arr;
|
||||
// }
|
||||
// };
|
||||
// Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// newFixedThreadPool.shutdown();
|
||||
// boolean[] arr = (boolean[]) submit.get();
|
||||
// if (arr[0] || arr[1]) {
|
||||
// break;
|
||||
// }
|
||||
// Thread.sleep(10 * 1000);
|
||||
// logger.debug("江苏快3--->执行完成...");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("江苏快3-->执行出错...", e);
|
||||
// } finally {
|
||||
// isRunningJSK3 = true;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,302 @@
|
||||
//package lottery.domains.capture.jobs;
|
||||
//
|
||||
//import javautils.StringUtil;
|
||||
//import javautils.date.Moment;
|
||||
//import javautils.http.HttpClientUtil;
|
||||
//import lottery.domains.capture.sites.opencai.OpenCaiBean;
|
||||
//import lottery.domains.capture.utils.CodeValidate;
|
||||
//import lottery.domains.capture.utils.ExpectValidate;
|
||||
//import lottery.domains.content.biz.LotteryOpenCodeService;
|
||||
//import lottery.domains.content.entity.LotteryOpenCode;
|
||||
//import net.sf.json.JSONArray;
|
||||
//import net.sf.json.JSONObject;
|
||||
//import org.apache.commons.lang.StringUtils;
|
||||
//import org.slf4j.Logger;
|
||||
//import org.slf4j.LoggerFactory;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.scheduling.annotation.Scheduled;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//
|
||||
//import java.util.*;
|
||||
//
|
||||
///**
|
||||
// * 时时彩 注意间隔,不允许有多线程 否则会封IP
|
||||
// */
|
||||
//@Component
|
||||
//public class OpenCaiJob {
|
||||
// private static final Logger logger = LoggerFactory.getLogger(OpenCaiJob.class);
|
||||
//// private static final String SUB_URL = "/newly.do?rows=5&format=json";
|
||||
// private static final String SUB_URL = "/newly.do?rows=5&format=json";
|
||||
//// private static final String[] SITES = {"http://z.apiplus.net", "http://a.apiplus.net", "http://e.apiplus.net", "http://b.apiplus.net", "http://c.apiplus.net", "http://d.apiplus.net"};
|
||||
// private static final String[] SITES = {"http://b.apiplus.net", "http://z.apiplus.net"};
|
||||
//// private static final String[] SITES = {"http://z.apiplus.net", "http://a.apiplus.net", "http://e.apiplus.net", "http://b.apiplus.net", "http://c.apiplus.net", "http://d.apiplus.net"};
|
||||
//// private static final String[] SITES = {"http://xx.xx.xx.xx:xxxx", "http://z.apiplus.net", "http://a.apiplus.net", "http://e.apiplus.net", "http://b.apiplus.net", "http://c.apiplus.net", "http://d.apiplus.net"};
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryOpenCodeService lotteryOpenCodeService;
|
||||
//
|
||||
// private static boolean isRuning = false;
|
||||
//
|
||||
// public void execute() {
|
||||
// synchronized (OpenCaiJob.class) {
|
||||
// if (isRuning == true) {
|
||||
// return;
|
||||
// }
|
||||
// isRuning = true;
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// start();
|
||||
// } catch (Exception e) {
|
||||
// logger.error("抓取OpenCai开奖数据出错", e);
|
||||
// } finally {
|
||||
// isRuning = false;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public void start() {
|
||||
// for (int i=0; i<SITES.length; i++) {
|
||||
// String site = SITES[i];
|
||||
// logger.debug("开始抓取OpenCai[{}]开奖数据", site);
|
||||
// long start = System.currentTimeMillis();
|
||||
//
|
||||
// String url = site + SUB_URL + "&_=" + System.currentTimeMillis();
|
||||
//
|
||||
// String result = getHttpResult(url);
|
||||
//
|
||||
// boolean succeed = false;
|
||||
// if (StringUtils.isNotEmpty(result)) {
|
||||
// succeed = handleData(result, site);
|
||||
// }
|
||||
//
|
||||
// long spend = System.currentTimeMillis() - start;
|
||||
// if (succeed) {
|
||||
// logger.debug("成功抓取OpenCai[{}]开奖数据,并处理成功,耗时{}", site, spend);
|
||||
// break;
|
||||
// }
|
||||
// else {
|
||||
// logger.warn("完成抓取OpenCai[{}]开奖数据,但处理失败,耗时{}", site, spend);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private boolean handleData(String data, String site) {
|
||||
// try {
|
||||
// if (StringUtil.isNotNull(data)) {
|
||||
// Object object = JSONObject.fromObject(data).get("data");
|
||||
// JSONArray array = JSONArray.fromObject(object);
|
||||
// List<OpenCaiBean> list = new ArrayList<OpenCaiBean>();
|
||||
// for (Iterator iter = array.iterator(); iter.hasNext();) {
|
||||
// JSONObject jsonObject = (JSONObject) iter.next();
|
||||
// OpenCaiBean bean = (OpenCaiBean) JSONObject.toBean(jsonObject,
|
||||
// OpenCaiBean.class);
|
||||
// list.add(bean);
|
||||
// }
|
||||
// Collections.reverse(list);
|
||||
// for (OpenCaiBean bean : list) {
|
||||
// handleOpenCaiBean(bean, site);
|
||||
// }
|
||||
//
|
||||
// return true;
|
||||
// }
|
||||
// return false;
|
||||
// } catch (Exception e) {
|
||||
// logger.error("解析OpenCai数据出错:" + data + ",URL:" + site, e);
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private boolean handleOpenCaiBean(OpenCaiBean bean, String site) {
|
||||
// LotteryOpenCode lotteryOpenCode = null;
|
||||
// String expect;
|
||||
// switch (bean.getCode()) {
|
||||
// // 时时彩
|
||||
// case "cqssc":
|
||||
// case "jxssc":
|
||||
// case "xjssc":
|
||||
// case "tjssc":
|
||||
// expect = bean.getExpect();
|
||||
// expect = expect.substring(0, 8) + "-" + expect.substring(8);
|
||||
// lotteryOpenCode = new LotteryOpenCode(bean.getCode(), expect,
|
||||
// bean.getOpencode(), bean.getOpentime(), 0);
|
||||
// break;
|
||||
// // 11选5
|
||||
// case "ah11x5":
|
||||
// case "gd11x5":
|
||||
// case "sd11x5":
|
||||
// case "sh11x5":
|
||||
// case "jx11x5":
|
||||
// expect = bean.getExpect();
|
||||
// expect = expect.substring(0, 8) + "-0" + expect.substring(8);
|
||||
// lotteryOpenCode = new LotteryOpenCode(bean.getCode(), expect,
|
||||
// bean.getOpencode(), bean.getOpentime(), 0);
|
||||
// break;
|
||||
// // 低频彩
|
||||
// case "fc3d":
|
||||
// case "pl3":
|
||||
// expect = bean.getExpect();
|
||||
// expect = expect.substring(2, 7);
|
||||
// lotteryOpenCode = new LotteryOpenCode(bean.getCode(), expect,
|
||||
// bean.getOpencode(), bean.getOpentime(), 0);
|
||||
// break;
|
||||
// // 快三
|
||||
// case "ahk3":
|
||||
// case "jsk3":
|
||||
// case "jlk3":
|
||||
// case "hubk3":
|
||||
// case "shk3":
|
||||
// case "gxk3":
|
||||
// case "hebk3":
|
||||
// if (bean.getCode().equals("hubk3")) {
|
||||
// bean.setCode("hbk3");
|
||||
// }
|
||||
// expect = bean.getExpect();
|
||||
// expect = expect.substring(0, 8) + "-" + expect.substring(8);
|
||||
// lotteryOpenCode = new LotteryOpenCode(bean.getCode(), expect,
|
||||
// bean.getOpencode(), bean.getOpentime(), 0);
|
||||
// break;
|
||||
// case "bjkl8":
|
||||
// String openCode = bean.getOpencode().split("\\+")[0];
|
||||
// String bjkl8Code = convertFFCCode(openCode);
|
||||
// lotteryOpenCode = new LotteryOpenCode(bean.getCode(), bean.getExpect(),
|
||||
// bjkl8Code, bean.getOpentime(), 0);
|
||||
// lotteryOpenCode.setLottery("bj5fc");
|
||||
// break;
|
||||
// case "bjpk10":
|
||||
// expect = bean.getExpect();
|
||||
// lotteryOpenCode = new LotteryOpenCode(bean.getCode(), expect,
|
||||
// bean.getOpencode(), bean.getOpentime(), 0);
|
||||
// break;
|
||||
// case "krkeno":
|
||||
// // 07,14,18,19,20,22,25,29,33,34,36,40,46,51,56,61,67,71,75,80
|
||||
// String krkenoCode = convertFFCCode(bean.getOpencode());
|
||||
// lotteryOpenCode = new LotteryOpenCode(bean.getCode(), bean.getExpect(),
|
||||
// krkenoCode, bean.getOpentime(), 0);
|
||||
// lotteryOpenCode.setLottery("hgffc");//项目使用code为 hgffc
|
||||
// break;
|
||||
// case "jpkeno":
|
||||
// // 4,14,18,21,28,34,35,37,39,44,45,46,47,50,54,55,65,72,77,79
|
||||
// String jpkenoCode = convertFFCCode(bean.getOpencode());
|
||||
// expect = bean.getExpect();
|
||||
// expect = expect.substring(0, 8) + "-" + expect.substring(8);
|
||||
// lotteryOpenCode = new LotteryOpenCode(bean.getCode(), expect,
|
||||
// jpkenoCode, bean.getOpentime(), 0);
|
||||
// lotteryOpenCode.setLottery("djffc");
|
||||
// break;
|
||||
// case "twbingo":
|
||||
// openCode = bean.getOpencode().split("\\+")[0];
|
||||
// String twbingoCode = convertFFCCode(openCode);
|
||||
// lotteryOpenCode = new LotteryOpenCode(bean.getCode(), bean.getExpect(),
|
||||
// twbingoCode, bean.getOpentime(), 0);
|
||||
// lotteryOpenCode.setLottery("tw5fc");
|
||||
// break;
|
||||
// // 加拿大3.5
|
||||
// case "cakeno":
|
||||
// openCode = bean.getOpencode().substring(0, 59);
|
||||
// String cakenoCode = convertFFCCode(openCode);
|
||||
// lotteryOpenCode = new LotteryOpenCode(bean.getCode(), bean.getExpect(),
|
||||
// cakenoCode, bean.getOpentime(), 0);
|
||||
// lotteryOpenCode.setLottery("jnd3d5fc");
|
||||
// break;
|
||||
// // // 新加坡2分彩
|
||||
// case "sgkeno":
|
||||
// String sgkenoCode = convertFFCCode(bean.getOpencode());
|
||||
// lotteryOpenCode = new LotteryOpenCode(bean.getCode(), bean.getExpect(),
|
||||
// sgkenoCode, bean.getOpentime(), 0);
|
||||
// lotteryOpenCode.setLottery("xjp2fc");
|
||||
// break;
|
||||
// case "shssl":
|
||||
// expect = bean.getExpect();
|
||||
// expect = expect.substring(0, 8) + "-" + expect.substring(8);
|
||||
// lotteryOpenCode = new LotteryOpenCode(bean.getCode(), expect,
|
||||
// bean.getOpencode(), bean.getOpentime(), 0);
|
||||
// break;
|
||||
// case "pl5":
|
||||
// expect = bean.getExpect();
|
||||
// lotteryOpenCode = new LotteryOpenCode(bean.getCode(), expect,
|
||||
// bean.getOpencode(), bean.getOpentime(), 0);
|
||||
// break;
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
//
|
||||
// if (lotteryOpenCode == null) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// if (CodeValidate.validate(lotteryOpenCode.getLottery(), lotteryOpenCode.getCode()) == false) {
|
||||
// logger.error("开彩网" + lotteryOpenCode.getLottery() + "抓取号码" + lotteryOpenCode.getCode() + "错误");
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// if (ExpectValidate.validate(lotteryOpenCode.getLottery(), lotteryOpenCode.getExpect()) == false) {
|
||||
// logger.error("开彩网" + lotteryOpenCode.getLottery() + "抓取期数" + lotteryOpenCode.getExpect() + "错误");
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// lotteryOpenCode.setRemarks(site);
|
||||
// lotteryOpenCode.setTime(new Moment().toSimpleTime());
|
||||
// if (StringUtils.isNotEmpty(bean.getOpentime())) {
|
||||
// lotteryOpenCode.setInterfaceTime(bean.getOpentime());
|
||||
// }
|
||||
// else {
|
||||
// lotteryOpenCode.setInterfaceTime(new Moment().toSimpleTime());
|
||||
// }
|
||||
//
|
||||
// boolean add = lotteryOpenCodeService.add(lotteryOpenCode, false);
|
||||
// if (add) {
|
||||
// // 北京快乐8
|
||||
// if ("bj5fc".equals(lotteryOpenCode.getLottery())) {
|
||||
// LotteryOpenCode bjkl8Code = new LotteryOpenCode("bjkl8", lotteryOpenCode.getExpect(), bean.getOpencode().split("\\+")[0], new Moment().toSimpleTime(), 0, null, site);
|
||||
// bjkl8Code.setInterfaceTime(lotteryOpenCode.getInterfaceTime());
|
||||
// lotteryOpenCodeService.add(bjkl8Code, false);
|
||||
// }
|
||||
// }
|
||||
// return add;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取数据
|
||||
// */
|
||||
// public static String getHttpResult(String url) {
|
||||
// try {
|
||||
// // logger.debug("开始请求OpenCai,URL:{}", url);
|
||||
//
|
||||
// // 设置头
|
||||
// Map<String, String> header = new HashMap<>();
|
||||
// header.put("referer", "http://www.baidu.com/");
|
||||
// header.put("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36");
|
||||
//
|
||||
// String data = HttpClientUtil.post(url, null, header, 10000);
|
||||
// return data;
|
||||
// } catch (Exception e) {
|
||||
// logger.error("请求OpenCai出错,URL:" + url, e);
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 转换1.5分彩号码
|
||||
// */
|
||||
// private String convertFFCCode(String openCode) {
|
||||
// String[] codes = openCode.split(",");
|
||||
// String code1 = mergeFFCCode(codes[0], codes[1], codes[2], codes[3]);
|
||||
// String code2 = mergeFFCCode(codes[4], codes[5], codes[6], codes[7]);
|
||||
// String code3 = mergeFFCCode(codes[8], codes[9], codes[10], codes[11]);
|
||||
// String code4 = mergeFFCCode(codes[12], codes[13], codes[14], codes[15]);
|
||||
// String code5 = mergeFFCCode(codes[16], codes[17], codes[18], codes[19]);
|
||||
//
|
||||
// return code1 + "," + code2 + "," + code3 + "," + code4 + "," + code5;
|
||||
// }
|
||||
// private String mergeFFCCode(String code1, String code2, String code3, String code4) {
|
||||
// int codeInt1 = Integer.valueOf(code1);
|
||||
// int codeInt2 = Integer.valueOf(code2);
|
||||
// int codeInt3 = Integer.valueOf(code3);
|
||||
// int codeInt4 = Integer.valueOf(code4);
|
||||
//
|
||||
// int codeSum = codeInt1 + codeInt2 + codeInt3 + codeInt4;
|
||||
// String codeSumStr = codeSum+"";
|
||||
// String finalCode = codeSumStr.substring(codeSumStr.length() - 1);
|
||||
// return finalCode;
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,279 @@
|
||||
// package lottery.domains.capture.jobs;
|
||||
//
|
||||
// import lottery.domains.capture.sites.bwlc.BwlcCrawler;
|
||||
// import lottery.domains.capture.sites.bwlc.BwlcCrawlerFC3D;
|
||||
// import lottery.domains.capture.sites.hn481.Hn481Crawler;
|
||||
// import lottery.domains.capture.sites.kj168.Kai168BJ;
|
||||
// import lottery.domains.capture.sites.kj168.Kai168L;
|
||||
// import lottery.domains.capture.sites.lecai.LecaiCrawlerBJC;
|
||||
// import lottery.domains.capture.sites.lecai.LecaiCrawlerPl3;
|
||||
// import lottery.domains.capture.utils.LotteryCrons;
|
||||
// import lottery.domains.content.global.JobConfig;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
// import org.springframework.beans.factory.annotation.Autowired;
|
||||
// import org.springframework.scheduling.annotation.Scheduled;
|
||||
// import org.springframework.scheduling.annotation.Schedules;
|
||||
// import org.springframework.stereotype.Component;
|
||||
//
|
||||
// import java.util.concurrent.Callable;
|
||||
// import java.util.concurrent.ExecutorService;
|
||||
// import java.util.concurrent.Executors;
|
||||
// import java.util.concurrent.Future;
|
||||
//
|
||||
// /**
|
||||
// * 其他单个菜种的地址,废弃不再使用
|
||||
// *
|
||||
// * @author ROOT
|
||||
// *
|
||||
// */
|
||||
// @Component
|
||||
// public class OthersCrawlerJob {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(OthersCrawlerJob.class);
|
||||
//
|
||||
// @Autowired
|
||||
// private JobConfig config;
|
||||
//
|
||||
// @Autowired
|
||||
// private Kai168BJ mKai168bj;
|
||||
//
|
||||
// @Autowired
|
||||
// private Kai168L mKai168L;
|
||||
//
|
||||
// @Autowired
|
||||
// private BwlcCrawler mBwlcCrawlerJob;
|
||||
//
|
||||
// @Autowired
|
||||
// private BwlcCrawlerFC3D mBwlcCrawlerFC3D;
|
||||
//
|
||||
// @Autowired
|
||||
// private LecaiCrawlerBJC mLecaiCrawlerBJC;
|
||||
//
|
||||
// @Autowired
|
||||
// private LecaiCrawlerPl3 mLecaiCrawlerPl3;
|
||||
//
|
||||
// @Autowired
|
||||
// private Hn481Crawler hn481Job1;
|
||||
//
|
||||
// boolean isRunningBJKL8 = true;
|
||||
//
|
||||
// /**
|
||||
// * 北京快乐8
|
||||
// */
|
||||
// // @Scheduled(cron = LotteryCrons.bjkl8)
|
||||
// public void executeBJKL8() {
|
||||
// final String lotteryName = "bjkl8";
|
||||
// if (config.isExeOthers && isRunningBJKL8) {
|
||||
// isRunningBJKL8 = false;
|
||||
// try {
|
||||
// while (true) {
|
||||
// ExecutorService newFixedThreadPool = Executors
|
||||
// .newFixedThreadPool(4);
|
||||
// logger.debug(lotteryName + " execute");
|
||||
// Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// @Override
|
||||
// public boolean[] call() throws Exception {
|
||||
// boolean result1 = mKai168bj.start(lotteryName);
|
||||
// boolean result2 = mBwlcCrawlerJob
|
||||
// .start(lotteryName);
|
||||
// boolean[] arr = new boolean[] { result1, result2 };
|
||||
// return arr;
|
||||
// }
|
||||
// };
|
||||
// Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// newFixedThreadPool.shutdown();
|
||||
// boolean[] arr = (boolean[]) submit.get();
|
||||
// if (arr[0] || arr[1]) {
|
||||
// break;
|
||||
// }
|
||||
// Thread.sleep(10 * 1000);
|
||||
// logger.debug("北京快乐8--->执行完成...");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("北京快乐8-->执行出错...", e);
|
||||
// } finally {
|
||||
// isRunningBJKL8 = true;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// boolean isRunningBJPK10 = true;
|
||||
//
|
||||
// /**
|
||||
// * 北京PK拾
|
||||
// */
|
||||
// // @Scheduled(cron = LotteryCrons.bjpk10)
|
||||
// public void executeBJPK8() {
|
||||
// final String lotteryName = "bjpk10";
|
||||
// if (config.isExeOthers && isRunningBJPK10) {
|
||||
// try {
|
||||
// isRunningBJPK10 = false;
|
||||
// while (true) {
|
||||
// ExecutorService newFixedThreadPool = Executors
|
||||
// .newFixedThreadPool(4);
|
||||
// logger.debug(lotteryName + " execute");
|
||||
// Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// @Override
|
||||
// public boolean[] call() throws Exception {
|
||||
// boolean result1 = mKai168bj.start(lotteryName);
|
||||
// boolean result2 = mBwlcCrawlerJob
|
||||
// .start(lotteryName);
|
||||
// // boolean result3 = mLecaiCrawlerBJC
|
||||
// // .start(lotteryName);
|
||||
// boolean result3 = false;
|
||||
// boolean[] arr = new boolean[] { result1, result2,
|
||||
// result3 };
|
||||
// return arr;
|
||||
// }
|
||||
// };
|
||||
// Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// newFixedThreadPool.shutdown();
|
||||
// boolean[] arr = (boolean[]) submit.get();
|
||||
// if (arr[0] || arr[1] || arr[2]) {
|
||||
// break;
|
||||
// }
|
||||
// Thread.sleep(10 * 1000);
|
||||
// logger.debug("北京PK拾--->执行完成...");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("北京PK拾-->执行出错...", e);
|
||||
// } finally {
|
||||
// isRunningBJPK10 = true;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// boolean isRunningFC3D = true;
|
||||
//
|
||||
// /**
|
||||
// * 福彩3d
|
||||
// */
|
||||
// // @Scheduled(cron = LotteryCrons.fc3d)
|
||||
// public void executeFC3D() {
|
||||
// final String lotteryName = "fc3d";
|
||||
// if (config.isExeOthers && isRunningFC3D) {
|
||||
// isRunningFC3D = false;
|
||||
// try {
|
||||
// while (true) {
|
||||
// ExecutorService newFixedThreadPool = Executors
|
||||
// .newFixedThreadPool(4);
|
||||
// logger.debug(lotteryName + " execute");
|
||||
// Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// @Override
|
||||
// public boolean[] call() throws Exception {
|
||||
// boolean result1 = mKai168L.start(lotteryName);
|
||||
// boolean result2 = mBwlcCrawlerFC3D
|
||||
// .start(lotteryName);
|
||||
// boolean[] arr = new boolean[] { result1, result2 };
|
||||
// return arr;
|
||||
// }
|
||||
// };
|
||||
// Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// newFixedThreadPool.shutdown();
|
||||
// boolean[] arr = (boolean[]) submit.get();
|
||||
// if (arr[0] || arr[1]) {
|
||||
// break;
|
||||
// }
|
||||
// Thread.sleep(10 * 1000);
|
||||
// logger.debug("福彩3d--->执行完成...");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("福彩3d-->执行出错...", e);
|
||||
// } finally {
|
||||
// isRunningFC3D = true;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// boolean isRunningPL3 = true;
|
||||
//
|
||||
// /**
|
||||
// * 排列三
|
||||
// */
|
||||
// // @Scheduled(cron = LotteryCrons.pl3)
|
||||
// public void executePL3() {
|
||||
// final String lotteryName = "pl3";
|
||||
// if (config.isExeOthers && isRunningPL3) {
|
||||
// try {
|
||||
// isRunningPL3 = false;
|
||||
// while (true) {
|
||||
// ExecutorService newFixedThreadPool = Executors
|
||||
// .newFixedThreadPool(4);
|
||||
// logger.debug(lotteryName + " execute");
|
||||
// Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// @Override
|
||||
// public boolean[] call() throws Exception {
|
||||
// boolean result1 = mKai168L.start(lotteryName);
|
||||
// boolean result2 = mLecaiCrawlerPl3
|
||||
// .start(lotteryName);
|
||||
// boolean[] arr = new boolean[] { result1, result2 };
|
||||
// return arr;
|
||||
// }
|
||||
// };
|
||||
// Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// newFixedThreadPool.shutdown();
|
||||
// boolean[] arr = (boolean[]) submit.get();
|
||||
// if (arr[0] || arr[1]) {
|
||||
// break;
|
||||
// }
|
||||
// Thread.sleep(10 * 1000);
|
||||
// logger.debug("排列三--->执行完成...");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("排列三-->执行出错...", e);
|
||||
// } finally {
|
||||
// isRunningPL3 = true;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// boolean isRunningHN481 = true;
|
||||
//
|
||||
// /**
|
||||
// * 河南481
|
||||
// */
|
||||
//
|
||||
// // @Schedules({ @Scheduled(cron = LotteryCrons.hn481_9),
|
||||
// // @Scheduled(cron = LotteryCrons.hn481_10_21),
|
||||
// // @Scheduled(cron = LotteryCrons.hn481_22) })
|
||||
// public void executeHN481() {
|
||||
// final String lotteryName = "hn481";
|
||||
// if (config.isExeOthers && isRunningHN481) {
|
||||
// try {
|
||||
// isRunningHN481 = false;
|
||||
// while (true) {
|
||||
// ExecutorService newFixedThreadPool = Executors
|
||||
// .newFixedThreadPool(4);
|
||||
// logger.debug(lotteryName + " execute");
|
||||
// Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// @Override
|
||||
// public boolean[] call() throws Exception {
|
||||
// boolean result1 = hn481Job1.start(lotteryName);
|
||||
// boolean result2 = false;
|
||||
// boolean[] arr = new boolean[] { result1, result2 };
|
||||
// return arr;
|
||||
// }
|
||||
// };
|
||||
// Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// newFixedThreadPool.shutdown();
|
||||
// boolean[] arr = (boolean[]) submit.get();
|
||||
// if (arr[0] || arr[1]) {
|
||||
// break;
|
||||
// }
|
||||
// Thread.sleep(10 * 1000);
|
||||
// logger.debug("河南481--->执行完成...");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("河南481-->执行出错...", e);
|
||||
// } finally {
|
||||
// isRunningHN481 = true;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,397 @@
|
||||
package lottery.domains.capture.jobs;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
import javautils.date.Moment;
|
||||
import javautils.http.HttpClientUtil;
|
||||
import lottery.domains.capture.sites.pcqq.PCQQBean;
|
||||
import lottery.domains.capture.utils.CodeValidate;
|
||||
import lottery.domains.capture.utils.ExpectValidate;
|
||||
import lottery.domains.content.biz.LotteryOpenCodeService;
|
||||
import lottery.domains.content.entity.LotteryOpenCode;
|
||||
|
||||
/**
|
||||
* 腾讯分分彩,官网抓取,不需要修改,但必须保证抓取服务器时间和腾讯服务器时间间隔在5秒之内,统一采用东8区时间即可
|
||||
*/
|
||||
@Component
|
||||
public class PCQQJob {
|
||||
private static final String ONLINE_URL = "https://mma.qq.com/cgi-bin/im/online"; // PCQQ在线人数
|
||||
private static final String CGI_SVRTIME_URL = "http://cgi.im.qq.com/cgi-bin/cgi_svrtime"; // 腾讯服务器时间
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PCQQJob.class);
|
||||
private static final String NAME = "txffc";
|
||||
|
||||
private static ConcurrentHashMap<String, String> HIS_OPEN_CODES = new ConcurrentHashMap<>(); // 历史开奖号码,只存当天的数据
|
||||
|
||||
@Autowired
|
||||
private LotteryOpenCodeService lotteryOpenCodeService;
|
||||
|
||||
private static boolean isRuning = false;
|
||||
|
||||
@Scheduled(cron = "8,15,20,25,30,35 * * * * *")
|
||||
// @Scheduled(cron = "0/3 * * * * *")
|
||||
// @PostConstruct
|
||||
public void schedule() {
|
||||
synchronized (PCQQJob.class) {
|
||||
if (isRuning == true) {
|
||||
return;
|
||||
}
|
||||
isRuning = true;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
logger.debug("开始抓取PCQQ官网在线人数数据>>>>>>>>>>>>>>>>" );
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
|
||||
start();
|
||||
long spend = System.currentTimeMillis() - start;
|
||||
|
||||
logger.debug("完成抓取PCQQ官网在线人数数据>>>>>>>>>>>>>>>>耗时{}", spend);
|
||||
} catch (Exception e) {
|
||||
logger.error("抓取PCQQ官网在线人数数据出错", e);
|
||||
} finally {
|
||||
isRuning = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void start() {
|
||||
int startMinute = new Moment().get("minute"); // 开始抓取的分钟
|
||||
|
||||
// // 验证本地时间和腾讯服务器时间,如果相差大于4秒,则不处理数据
|
||||
// boolean validateTime = validateTencentServerTime();
|
||||
// if (validateTime == false) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
String result = getOnlineNum();
|
||||
|
||||
int endMinute = new Moment().get("minute"); // 结束抓取的分钟
|
||||
if (startMinute != endMinute) {
|
||||
logger.error("PCQQ官网在线人数抓取超时,开始分钟{},结束分钟{}", startMinute, endMinute);
|
||||
return;
|
||||
}
|
||||
|
||||
int endSecond = new Moment().get("second");
|
||||
if (endSecond >= 40) {
|
||||
logger.error("PCQQ官网在线人数抓取不处理,因为处理完成的秒数{}>=40秒,可能是下一期的数据", endSecond);
|
||||
return;
|
||||
}
|
||||
|
||||
handleData(result);
|
||||
}
|
||||
|
||||
private boolean validateTencentServerTime() {
|
||||
Moment tencentServerTime = getTencentServerTime();
|
||||
if (tencentServerTime == null) {
|
||||
return false;
|
||||
}
|
||||
Moment now = new Moment();
|
||||
|
||||
int diffSeconds = now.difference(tencentServerTime, "second");
|
||||
|
||||
if (diffSeconds >= 5 || diffSeconds <= -5) {
|
||||
logger.error("当前服务器时间超过腾讯服务器时间太多,抓取服务器时间{},PCQQ官网时间{},本次不处理", now.toSimpleTime(), tencentServerTime.toSimpleTime());
|
||||
return false;
|
||||
}
|
||||
|
||||
logger.error("抓取服务器时间{},PCQQ官网时间{}", now.toSimpleTime(), tencentServerTime.toSimpleTime());
|
||||
return true;
|
||||
}
|
||||
|
||||
private void handleData(String result) {
|
||||
if (StringUtils.isEmpty(result)) {
|
||||
return;
|
||||
}
|
||||
|
||||
PCQQBean pcqqBean = JSON.parseObject(result, PCQQBean.class);
|
||||
if (pcqqBean == null) {
|
||||
logger.error("解析PCQQ返回数据出错,返回数据" + result);
|
||||
return;
|
||||
}
|
||||
|
||||
// 处理数据
|
||||
handleBean(pcqqBean);
|
||||
}
|
||||
|
||||
private boolean handleBean(PCQQBean bean) {
|
||||
boolean valid = checkData(bean);
|
||||
if (!valid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 当期期号
|
||||
String currentExpect = getExpectByTime(bean.getOnlinetime());
|
||||
if (ExpectValidate.validate(NAME, currentExpect) == false) {
|
||||
logger.error("PCQQ官网腾讯分分彩抓取期数" + currentExpect + "错误");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 如果已经有本期的数据了,则不再处理
|
||||
if (HIS_OPEN_CODES.containsKey(currentExpect)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 算出当前期开奖号码
|
||||
String currentCode = convertCode(bean.getOnlinenumber());
|
||||
if (CodeValidate.validate(NAME, currentCode) == false) {
|
||||
logger.error("PCQQ官网腾讯分分彩抓取号码" + currentCode + "错误");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 验证这一期是否已经抓取过了
|
||||
LotteryOpenCode dbData = lotteryOpenCodeService.get(NAME, currentExpect);
|
||||
if (dbData != null) {
|
||||
HIS_OPEN_CODES.put(currentExpect, dbData.getCode());
|
||||
|
||||
if (!dbData.getCode().equals(currentCode)) {
|
||||
logger.error("PCQQ官网抓取时遇到错误:抓取{}期开奖号码{}与数据库已有开奖号码{}不符", currentExpect, currentCode, dbData.getCode());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 抓取回来的数据放到本地缓存里
|
||||
HIS_OPEN_CODES.put(currentExpect, currentCode);
|
||||
|
||||
// 上期期号
|
||||
String lastExpectTime = new Moment().fromTime(bean.getOnlinetime()).subtract(1, "minutes").toSimpleTime();
|
||||
String lastExpect = getExpectByTime(lastExpectTime);
|
||||
|
||||
if (!HIS_OPEN_CODES.containsKey(lastExpect)) {
|
||||
LotteryOpenCode lastExpectCode = lotteryOpenCodeService.get(NAME, lastExpect);
|
||||
if (lastExpectCode == null) {
|
||||
logger.warn("PCQQ官网抓取时没有获取到上期"+lastExpect+"的开奖数据,本次不处理,本期:" + currentExpect);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 上期的号码放到本地缓存里
|
||||
HIS_OPEN_CODES.put(lastExpect, lastExpectCode.getCode());
|
||||
}
|
||||
|
||||
// 上期开奖号码
|
||||
String lastExpectCode = HIS_OPEN_CODES.get(lastExpect);
|
||||
if (StringUtils.isEmpty(lastExpectCode)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 如果本期和上奖开奖号码相同,那么把开奖号码状态改为无效撤单
|
||||
int status = 0;
|
||||
if (lastExpectCode.equals(currentCode)) {
|
||||
// 0:待开奖;1:已开奖;2:无效待撤单;3:无效已撤单
|
||||
status = 2;
|
||||
}
|
||||
|
||||
LotteryOpenCode lotteryOpenCode = new LotteryOpenCode(NAME, currentExpect, currentCode, new Moment().toSimpleTime(), status, null, "PCQQ");
|
||||
lotteryOpenCode.setInterfaceTime(bean.getOnlinetime());
|
||||
|
||||
boolean added = lotteryOpenCodeService.add(lotteryOpenCode, false);
|
||||
|
||||
if (added) {
|
||||
logger.info("PCQQ官网成功抓取腾讯分分彩{}期开奖号码{},上期{}开奖号码{},是否自动撤单:{}", currentExpect, currentCode, lastExpect, lastExpectCode, status == 2 ? "是" : "否");
|
||||
|
||||
// 腾讯龙虎斗
|
||||
if ("txffc".equals(NAME)) {
|
||||
LotteryOpenCode txlhdCode = new LotteryOpenCode("txlhd", lotteryOpenCode.getExpect(), lotteryOpenCode.getCode(), lotteryOpenCode.getTime(), lotteryOpenCode.getOpenStatus(), null, lotteryOpenCode.getRemarks());
|
||||
txlhdCode.setInterfaceTime(lotteryOpenCode.getInterfaceTime());
|
||||
lotteryOpenCodeService.add(txlhdCode, false);
|
||||
}
|
||||
}
|
||||
|
||||
return added;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取腾讯服务器时间
|
||||
*/
|
||||
public static Moment getTencentServerTime() {
|
||||
try {
|
||||
String url = CGI_SVRTIME_URL + "?_=" + System.currentTimeMillis();
|
||||
|
||||
String data = getHttpResult(url);
|
||||
if (data != null) {
|
||||
Moment moment = new Moment().fromTime(data);
|
||||
return moment;
|
||||
}
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
logger.error("获取PCQQ官网服务器时间出错", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取在线人数
|
||||
*/
|
||||
public static String getOnlineNum() {
|
||||
try {
|
||||
String url = ONLINE_URL + "?_=" + System.currentTimeMillis();
|
||||
|
||||
String data = getHttpResult(url);
|
||||
if (data != null && data.indexOf("online_resp") > -1) {
|
||||
data = data.substring(12);
|
||||
data = data.substring(0, data.length() -1);
|
||||
return data;
|
||||
}
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
logger.error("获取PCQQ官网在线人数出错", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取腾讯数据
|
||||
*/
|
||||
public static String getHttpResult(String url) {
|
||||
try {
|
||||
// 设置头
|
||||
Map<String, String> header = new HashMap<>();
|
||||
header.put("referer", "http://im.qq.com/pcqq/");
|
||||
header.put("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36");
|
||||
|
||||
String data = HttpClientUtil.post(url, null, header, 10000);
|
||||
return data;
|
||||
} catch (Exception e) {
|
||||
logger.error("请求PCQQ官网出错", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private boolean checkData(PCQQBean bean) {
|
||||
if (bean == null) {
|
||||
logger.error("PCQQ官网数据非法,空数据");
|
||||
return false;
|
||||
}
|
||||
if (StringUtils.isEmpty(bean.getOnlinetime()) || bean.getOnlinenumber() <= 0) {
|
||||
logger.error("PCQQ官网数据非法:" + JSON.toJSONString(bean));
|
||||
return false;
|
||||
}
|
||||
|
||||
// 最小要1000才能组成开奖号码,否则是会出错的
|
||||
if (bean.getOnlinenumber() < 1000) {
|
||||
logger.error("PCQQ官网数据非法:" + JSON.toJSONString(bean));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* 转换号码
|
||||
* 万位:onlinenumber所有数值相加取尾数
|
||||
* 千位:onlinenumber倒数第4位
|
||||
* 百位:onlinenumber倒数第3位
|
||||
* 十位:onlinenumber倒数第2位
|
||||
* 个位:onlinenumber倒数第1位
|
||||
*/
|
||||
private String convertCode(int onlinenumber) {
|
||||
String[] chars = (onlinenumber+"").split("");
|
||||
int sum = 0;
|
||||
for (String aChar : chars) {
|
||||
if (aChar != null && !"".equals(aChar)) {
|
||||
sum += (Integer.valueOf(aChar));
|
||||
}
|
||||
}
|
||||
|
||||
// 万位
|
||||
String wan = sum + "";
|
||||
wan = wan.substring(wan.length() - 1);
|
||||
|
||||
String qian = chars[chars.length - 4] + "";
|
||||
String bai = chars[chars.length - 3] + "";
|
||||
String shi = chars[chars.length - 2] + "";
|
||||
String ge = chars[chars.length - 1] + "";
|
||||
|
||||
return wan + "," + qian + "," + bai + "," + shi + "," + ge;
|
||||
}
|
||||
|
||||
private static String getExpectByTime(String time) {
|
||||
Moment moment = new Moment().fromTime(time);
|
||||
int hour = moment.get("hour");
|
||||
int minute = moment.get("minute");
|
||||
|
||||
if (hour == 0 && minute == 0) {
|
||||
// 如果是0点0分,那么就是昨天的最后一期,即1440期
|
||||
moment = moment.add(-1, "minutes");
|
||||
hour = 24;
|
||||
}
|
||||
String date = moment.format("yyyyMMdd");
|
||||
int dayExpect = (hour * 60) + minute;
|
||||
String expect = date + "-" + (String.format("%04d", dayExpect));
|
||||
return expect;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String time = "2017-09-18 00:01:59";
|
||||
String curExpect = getExpectByTime(time);
|
||||
System.out.println(curExpect);
|
||||
|
||||
String lastExpectTime = new Moment().fromTime(time).subtract(1, "minutes").toSimpleTime();
|
||||
String lastExpect = getExpectByTime(lastExpectTime);
|
||||
|
||||
System.out.println(lastExpect);
|
||||
|
||||
|
||||
// String url = ONLINE_URL + "?_=" + System.currentTimeMillis();
|
||||
// System.out.println(url);
|
||||
// String data = getData(url);
|
||||
// System.out.println(data);
|
||||
|
||||
// System.out.println(convertCode2(214616782));
|
||||
|
||||
// Moment moment = new Moment().fromTime("2017-08-13 00:02:00");
|
||||
// int hour = moment.get("hour");
|
||||
// int minute = moment.get("minute");
|
||||
//
|
||||
// if (hour == 0 && minute == 0) {
|
||||
// // 如果是0点0分,那么就是昨天的最后一期,即1440期
|
||||
// moment = moment.add(-1, "minutes");
|
||||
// hour = 24;
|
||||
// }
|
||||
// String date = moment.format("yyyyMMdd");
|
||||
// // hour = moment.get("hour");
|
||||
// // minute = moment.get("minute");
|
||||
//
|
||||
// int dayExpect = (hour * 60) + minute;
|
||||
//
|
||||
// String expect = date + "-" + (String.format("%04d", dayExpect));
|
||||
//
|
||||
// System.out.println(expect);
|
||||
}
|
||||
|
||||
public static String convertCode2(int onlinenumber) {
|
||||
String[] chars = (onlinenumber+"").split("");
|
||||
int sum = 0;
|
||||
for (String aChar : chars) {
|
||||
if (aChar != null && !"".equals(aChar)) {
|
||||
sum += (Integer.valueOf(aChar));
|
||||
}
|
||||
}
|
||||
|
||||
// 万位
|
||||
String wan = sum + "";
|
||||
wan = wan.substring(wan.length() - 1);
|
||||
|
||||
String qian = chars[chars.length - 4] + "";
|
||||
String bai = chars[chars.length - 3] + "";
|
||||
String shi = chars[chars.length - 2] + "";
|
||||
String ge = chars[chars.length - 1] + "";
|
||||
|
||||
return wan + "," + qian + "," + bai + "," + shi + "," + ge;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
// package lottery.domains.capture.jobs;
|
||||
//
|
||||
// import com.alibaba.fastjson.JSON;
|
||||
// import javautils.date.Moment;
|
||||
// import javautils.http.HttpClientUtil;
|
||||
// import lottery.domains.capture.sites.qqtj.QQTJBean;
|
||||
// import lottery.domains.capture.utils.CodeValidate;
|
||||
// import lottery.domains.capture.utils.ExpectValidate;
|
||||
// import lottery.domains.content.biz.LotteryOpenCodeService;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import org.apache.commons.collections.CollectionUtils;
|
||||
// import org.apache.commons.lang.StringUtils;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
// import org.springframework.beans.factory.annotation.Autowired;
|
||||
// import org.springframework.scheduling.annotation.Scheduled;
|
||||
// import org.springframework.stereotype.Component;
|
||||
//
|
||||
// import java.util.Collections;
|
||||
// import java.util.List;
|
||||
//
|
||||
// /**
|
||||
// * 奇趣网腾讯分分彩,废弃不再使用
|
||||
// */
|
||||
// @Component
|
||||
// public class QQCJob {
|
||||
// private static final String URL = "http://www.77tj.org/api/tencent/onlineim";
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(QQCJob.class);
|
||||
// private static final String NAME = "txffc";
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryOpenCodeService lotteryOpenCodeService;
|
||||
//
|
||||
// private static boolean isRuning = false;
|
||||
//
|
||||
// @Scheduled(cron = "0/3 * * * * *")
|
||||
// // @PostConstruct
|
||||
// public void execute() {
|
||||
// synchronized (QQCJob.class) {
|
||||
// if (isRuning == true) {
|
||||
// return;
|
||||
// }
|
||||
// isRuning = true;
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// logger.debug("开始抓取奇趣网腾讯分分彩开奖数据>>>>>>>>>>>>>>>>");
|
||||
//
|
||||
// long start = System.currentTimeMillis();
|
||||
// start();
|
||||
// long spend = System.currentTimeMillis() - start;
|
||||
//
|
||||
// logger.debug("完成抓取奇趣网腾讯分分彩开奖数据>>>>>>>>>>>>>>>>耗时{}", spend);
|
||||
// } catch (Exception e) {
|
||||
// logger.error("抓取奇趣网腾讯分分彩开奖数据出错", e);
|
||||
// } finally {
|
||||
// isRuning = false;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private void start() {
|
||||
// String result = getResult();
|
||||
// handleData(result);
|
||||
// }
|
||||
//
|
||||
// public String getResult() {
|
||||
// String url = URL + "?_=" + System.currentTimeMillis();
|
||||
// String charset = "UTF-8";
|
||||
// String result = get(url, charset);
|
||||
// return result;
|
||||
// }
|
||||
//
|
||||
// @SuppressWarnings("rawtypes")
|
||||
// private void handleData(String result) {
|
||||
// if (StringUtils.isEmpty(result)) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// List<QQTJBean> openCodes = JSON.parseArray(result, QQTJBean.class);
|
||||
// if (CollectionUtils.isEmpty(openCodes)) {
|
||||
// logger.error("没有获取到奇趣网数据");
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// Collections.reverse(openCodes);
|
||||
// // 处理数据
|
||||
// for (QQTJBean bean : openCodes) {
|
||||
// handleBean(bean);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private boolean handleBean(QQTJBean bean) {
|
||||
// boolean valid = checkData(bean);
|
||||
// if (!valid) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// Moment moment = new Moment().fromTime(bean.getOnlinetime());
|
||||
// int hour = moment.get("hour");
|
||||
// int minute = moment.get("minute");
|
||||
//
|
||||
// if (hour == 0 && minute == 0) {
|
||||
// // 如果是0点0分,那么就是昨天的最后一期,即1440期
|
||||
// moment = moment.add(-1, "minutes");
|
||||
// hour = 24;
|
||||
// }
|
||||
// String date = moment.format("yyyyMMdd");
|
||||
// // hour = moment.get("hour");
|
||||
// // minute = moment.get("minute");
|
||||
//
|
||||
// int dayExpect = (hour * 60) + minute;
|
||||
//
|
||||
// String expect = date + "-" + (String.format("%04d", dayExpect));
|
||||
//
|
||||
// String code = convertCode(bean.getOnlinenumber());
|
||||
//
|
||||
// if (CodeValidate.validate(NAME, code) == false) {
|
||||
// logger.error("奇趣网腾讯分分彩抓取号码" + code + "错误");
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// if (ExpectValidate.validate(NAME, expect) == false) {
|
||||
// logger.error("奇趣网腾讯分分彩抓取期数" + expect + "错误");
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// // 如果人数无变化,那么把开奖号码状态改为无效撤单
|
||||
// int status = 0;
|
||||
// if (bean.getOnlinechange() == 0) {
|
||||
// // 0:待开奖;1:已开奖;2:无效待撤单;3:无效已撤单
|
||||
// status = 2;
|
||||
// }
|
||||
//
|
||||
// LotteryOpenCode lotteryOpenCode = new LotteryOpenCode(NAME, expect, code, new Moment().toSimpleTime(), status, null, "QQTJ");
|
||||
// if (StringUtils.isNotEmpty(bean.getOnlinetime())) {
|
||||
// lotteryOpenCode.setInterfaceTime(bean.getOnlinetime());
|
||||
// }
|
||||
// else {
|
||||
// lotteryOpenCode.setInterfaceTime(new Moment().toSimpleTime());
|
||||
// }
|
||||
//
|
||||
// boolean added = lotteryOpenCodeService.add(lotteryOpenCode, false);
|
||||
//
|
||||
// return added;
|
||||
// }
|
||||
//
|
||||
// public static String get(String urlAll, String charset) {
|
||||
// try {
|
||||
// String result = HttpClientUtil.get(urlAll, null, 5000);
|
||||
// return result;
|
||||
// } catch (Exception e) {
|
||||
// logger.error("请求奇趣网出错", e);
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// private boolean checkData(QQTJBean bean) {
|
||||
// if (bean == null) {
|
||||
// logger.error("奇趣网数据非法,空数据");
|
||||
// return false;
|
||||
// }
|
||||
// if (StringUtils.isEmpty(bean.getOnlinetime()) || bean.getOnlinenumber() <= 0) {
|
||||
// logger.error("奇趣网数据非法:" + JSON.toJSONString(bean));
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// // 最小要1000才能组成开奖号码,否则是会出错的
|
||||
// if (bean.getOnlinenumber() < 1000) {
|
||||
// logger.error("奇趣网数据非法:" + JSON.toJSONString(bean));
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// return true;
|
||||
// }
|
||||
// /**
|
||||
// *
|
||||
// * 转换号码
|
||||
// * 万位:onlinenumber所有数值相加取尾数
|
||||
// * 千位:onlinenumber倒数第4位
|
||||
// * 百位:onlinenumber倒数第3位
|
||||
// * 十位:onlinenumber倒数第2位
|
||||
// * 个位:onlinenumber倒数第1位
|
||||
// */
|
||||
// private String convertCode(int onlinenumber) {
|
||||
// String[] chars = (onlinenumber+"").split("");
|
||||
// int sum = 0;
|
||||
// for (String aChar : chars) {
|
||||
// if (aChar != null && !"".equals(aChar)) {
|
||||
// sum += (Integer.valueOf(aChar));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // 万位
|
||||
// String wan = sum + "";
|
||||
// wan = wan.substring(wan.length() - 1);
|
||||
//
|
||||
// String qian = chars[chars.length - 4] + "";
|
||||
// String bai = chars[chars.length - 3] + "";
|
||||
// String shi = chars[chars.length - 2] + "";
|
||||
// String ge = chars[chars.length - 1] + "";
|
||||
//
|
||||
// return wan + "," + qian + "," + bai + "," + shi + "," + ge;
|
||||
// }
|
||||
//
|
||||
// public static void main(String[] args) {
|
||||
// // System.out.println(convertCode2(214616782));
|
||||
//
|
||||
// Moment moment = new Moment().fromTime("2017-08-13 00:02:00");
|
||||
// int hour = moment.get("hour");
|
||||
// int minute = moment.get("minute");
|
||||
//
|
||||
// if (hour == 0 && minute == 0) {
|
||||
// // 如果是0点0分,那么就是昨天的最后一期,即1440期
|
||||
// moment = moment.add(-1, "minutes");
|
||||
// hour = 24;
|
||||
// }
|
||||
// String date = moment.format("yyyyMMdd");
|
||||
// // hour = moment.get("hour");
|
||||
// // minute = moment.get("minute");
|
||||
//
|
||||
// int dayExpect = (hour * 60) + minute;
|
||||
//
|
||||
// String expect = date + "-" + (String.format("%04d", dayExpect));
|
||||
//
|
||||
// System.out.println(expect);
|
||||
// }
|
||||
//
|
||||
// public static String convertCode2(int onlinenumber) {
|
||||
// String[] chars = (onlinenumber+"").split("");
|
||||
// int sum = 0;
|
||||
// for (String aChar : chars) {
|
||||
// if (aChar != null && !"".equals(aChar)) {
|
||||
// sum += (Integer.valueOf(aChar));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // 万位
|
||||
// String wan = sum + "";
|
||||
// wan = wan.substring(wan.length() - 1);
|
||||
//
|
||||
// String qian = chars[chars.length - 4] + "";
|
||||
// String bai = chars[chars.length - 3] + "";
|
||||
// String shi = chars[chars.length - 2] + "";
|
||||
// String ge = chars[chars.length - 1] + "";
|
||||
//
|
||||
// return wan + "," + qian + "," + bai + "," + shi + "," + ge;
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,231 @@
|
||||
// package lottery.domains.capture.jobs;
|
||||
//
|
||||
// import lottery.domains.capture.sites.kj168.Kai168H;
|
||||
// import lottery.domains.capture.sites.shishicai.ShiShiCaiCrawler;
|
||||
// import lottery.domains.capture.sites.tjflcpw.TjflcpwCrawler;
|
||||
// import lottery.domains.capture.sites.xjflcp.XjflcpCrawler;
|
||||
// import lottery.domains.capture.utils.LotteryCrons;
|
||||
// import lottery.domains.content.global.JobConfig;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
// import org.springframework.beans.factory.annotation.Autowired;
|
||||
// import org.springframework.scheduling.annotation.Scheduled;
|
||||
// import org.springframework.scheduling.annotation.Schedules;
|
||||
// import org.springframework.stereotype.Component;
|
||||
//
|
||||
// import java.util.concurrent.Callable;
|
||||
// import java.util.concurrent.ExecutorService;
|
||||
// import java.util.concurrent.Executors;
|
||||
// import java.util.concurrent.Future;
|
||||
//
|
||||
// /**
|
||||
// * 重庆时时彩,废弃不再使用
|
||||
// *
|
||||
// * @author root
|
||||
// */
|
||||
// @Component
|
||||
// public class SSCCrawlerJob {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(SSCCrawlerJob.class);
|
||||
//
|
||||
// @Autowired
|
||||
// private JobConfig config;
|
||||
//
|
||||
// @Autowired
|
||||
// private Kai168H mKai168H;
|
||||
//
|
||||
// @Autowired
|
||||
// private ShiShiCaiCrawler shishicai;
|
||||
//
|
||||
// public boolean isRunningCQSSC = true;
|
||||
// public boolean isRunningXJSSC = true;
|
||||
// public boolean isRunningJXSSC = true;
|
||||
// public boolean isRunningTJSSC = true;
|
||||
//
|
||||
// /**
|
||||
// * 重庆时时彩
|
||||
// */
|
||||
// // @Schedules({ @Scheduled(cron = LotteryCrons.cqssc_5),
|
||||
// // @Scheduled(cron = LotteryCrons.cqssc_10) })
|
||||
// public void executeCQSSC() {
|
||||
// final String lotteryName = "cqssc";
|
||||
// if (config.isExeSsc && isRunningCQSSC) {
|
||||
// isRunningCQSSC = false;
|
||||
// try {
|
||||
// while (true) {
|
||||
// ExecutorService newFixedThreadPool = Executors
|
||||
// .newFixedThreadPool(4);
|
||||
// logger.debug(lotteryName + ">>> execute");
|
||||
// Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// @Override
|
||||
// public boolean[] call() throws Exception {
|
||||
// boolean result1 = mKai168H.start(lotteryName);
|
||||
// boolean result2 = shishicai.start(lotteryName);
|
||||
// boolean[] arr = new boolean[] { result1, result2 };
|
||||
// return arr;
|
||||
// }
|
||||
// };
|
||||
// Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// newFixedThreadPool.shutdown();
|
||||
// boolean[] arr = (boolean[]) submit.get();
|
||||
// if (arr[0] || arr[1]){
|
||||
// isRunningCQSSC = true;
|
||||
// break;
|
||||
// }
|
||||
// Thread.sleep(10 * 1000);
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("时时彩数据抓取异常...", e);
|
||||
// } finally {
|
||||
// isRunningCQSSC = true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // ==========================
|
||||
// /**
|
||||
// * 江西时时彩
|
||||
// */
|
||||
// // @Schedules({ @Scheduled(cron = LotteryCrons.jxssc_9),
|
||||
// // @Scheduled(cron = LotteryCrons.jxssc_10),
|
||||
// // @Scheduled(cron = LotteryCrons.jxssc_11),
|
||||
// // @Scheduled(cron = LotteryCrons.jxssc_12),
|
||||
// // @Scheduled(cron = LotteryCrons.jxssc_13),
|
||||
// // @Scheduled(cron = LotteryCrons.jxssc_14),
|
||||
// // @Scheduled(cron = LotteryCrons.jxssc_15),
|
||||
// // @Scheduled(cron = LotteryCrons.jxssc_16),
|
||||
// // @Scheduled(cron = LotteryCrons.jxssc_17),
|
||||
// // @Scheduled(cron = LotteryCrons.jxssc_18),
|
||||
// // @Scheduled(cron = LotteryCrons.jxssc_19),
|
||||
// // @Scheduled(cron = LotteryCrons.jxssc_20),
|
||||
// // @Scheduled(cron = LotteryCrons.jxssc_21),
|
||||
// // @Scheduled(cron = LotteryCrons.jxssc_22),
|
||||
// // @Scheduled(cron = LotteryCrons.jxssc_23) })
|
||||
// public void executeJXSSC() {
|
||||
// // final String lotteryName = "jxssc";
|
||||
// // boolean isRunningJXSSC = true;
|
||||
// // if (config.isExeSsc && isRunningJXSSC) {
|
||||
// // isRunningJXSSC = false;
|
||||
// // try {
|
||||
// // while (true) {
|
||||
// // ExecutorService newFixedThreadPool = Executors
|
||||
// // .newFixedThreadPool(4);
|
||||
// // logger.debug(lotteryName + ">>>>> execute");
|
||||
// // Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// // @Override
|
||||
// // public boolean[] call() throws Exception {
|
||||
// // boolean result1 = mKai168H.start(lotteryName);
|
||||
// // boolean result2 = shishicai.start(lotteryName);
|
||||
// // boolean[] arr = new boolean[] { result1, result2 };
|
||||
// // return arr;
|
||||
// // }
|
||||
// // };
|
||||
// // Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// // newFixedThreadPool.shutdown();
|
||||
// // boolean[] arr = (boolean[]) submit.get();
|
||||
// // if (arr[0] || arr[1])
|
||||
// // break;
|
||||
// // Thread.sleep(10 * 1000);
|
||||
// // logger.debug("执行完成...");
|
||||
// // }
|
||||
// // } catch (Exception e) {
|
||||
// // logger.error("执行出错...", e);
|
||||
// // } finally {
|
||||
// // isRunningJXSSC = true;
|
||||
// // }
|
||||
// //
|
||||
// // }
|
||||
// }
|
||||
//
|
||||
// // ===================天津=====
|
||||
// @Autowired
|
||||
// private TjflcpwCrawler tjflcpw;
|
||||
//
|
||||
// /**
|
||||
// * 天津时时彩
|
||||
// */
|
||||
// // @Schedules({ @Scheduled(cron = LotteryCrons.tjssc) })
|
||||
// public void executeTJSSC() {
|
||||
// final String lotteryName = "tjssc";
|
||||
// if (config.isExeSsc && isRunningTJSSC) {
|
||||
// isRunningTJSSC = false;
|
||||
// try {
|
||||
// while (true) {
|
||||
// ExecutorService newFixedThreadPool = Executors
|
||||
// .newFixedThreadPool(4);
|
||||
// logger.debug(lotteryName + " execute");
|
||||
// Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// @Override
|
||||
// public boolean[] call() throws Exception {
|
||||
// boolean result1 = mKai168H.start(lotteryName);
|
||||
// // shishicai.cn 没有这个获取
|
||||
// // boolean result2 =
|
||||
// // shishicai.start(lotteryName);
|
||||
// boolean result2 = false;
|
||||
// boolean[] arr = new boolean[] { result1, result2 };
|
||||
// return arr;
|
||||
// }
|
||||
// };
|
||||
// Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// newFixedThreadPool.shutdown();
|
||||
// boolean[] arr = (boolean[]) submit.get();
|
||||
// if (arr[0] || arr[1])
|
||||
// break;
|
||||
// Thread.sleep(10 * 1000);
|
||||
// logger.debug("天津scc 执行完成...");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("天津ssc 执行出错...", e);
|
||||
// } finally {
|
||||
// isRunningTJSSC = true;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // ----------------------新疆-------
|
||||
// @Autowired
|
||||
// private XjflcpCrawler xjflcp;
|
||||
//
|
||||
// /**
|
||||
// * 新疆时时彩
|
||||
// */
|
||||
// // @Schedules({ @Scheduled(cron = LotteryCrons.xjssc_0_1),
|
||||
// // @Scheduled(cron = LotteryCrons.xjssc_2),
|
||||
// // @Scheduled(cron = LotteryCrons.xjssc_10),
|
||||
// // @Scheduled(cron = LotteryCrons.xjssc_11_23) })
|
||||
// public void executeXJSSC() {
|
||||
// final String lotteryName = "xjssc";
|
||||
// if (config.isExeSsc && isRunningXJSSC) {
|
||||
// try {
|
||||
// isRunningXJSSC = false;
|
||||
// while (true) {
|
||||
// ExecutorService newFixedThreadPool = Executors
|
||||
// .newFixedThreadPool(4);
|
||||
// logger.debug(lotteryName + ">>>>> execute");
|
||||
// Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// @Override
|
||||
// public boolean[] call() throws Exception {
|
||||
// boolean result1 = mKai168H.start(lotteryName);
|
||||
// // boolean result2 = xjflcp.start(lotteryName);
|
||||
// boolean result2 = false;
|
||||
// boolean[] arr = new boolean[] { result1, result2 };
|
||||
// return arr;
|
||||
// }
|
||||
// };
|
||||
// Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// newFixedThreadPool.shutdown();
|
||||
// boolean[] arr = (boolean[]) submit.get();
|
||||
// if (arr[0] || arr[1])
|
||||
// break;
|
||||
// Thread.sleep(10 * 1000);
|
||||
// logger.debug("新疆 ssc 执行完成...");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("新疆ssc 执行出错...", e);
|
||||
// } finally {
|
||||
// isRunningXJSSC = true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,250 @@
|
||||
// package lottery.domains.capture.jobs;
|
||||
//
|
||||
// import lottery.domains.capture.sites.kj168.Kai168H;
|
||||
// import lottery.domains.capture.sites.shishicai.ShiShiCaiCrawler;
|
||||
// import lottery.domains.capture.utils.LotteryCrons;
|
||||
// import lottery.domains.content.global.JobConfig;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
// import org.springframework.beans.factory.annotation.Autowired;
|
||||
// import org.springframework.scheduling.annotation.Scheduled;
|
||||
// import org.springframework.scheduling.annotation.Schedules;
|
||||
// import org.springframework.stereotype.Component;
|
||||
//
|
||||
// import java.util.concurrent.Callable;
|
||||
// import java.util.concurrent.ExecutorService;
|
||||
// import java.util.concurrent.Executors;
|
||||
// import java.util.concurrent.Future;
|
||||
//
|
||||
// /**
|
||||
// * 11选5任务,废弃不再使用
|
||||
// *
|
||||
// * @author root
|
||||
// */
|
||||
// @Component
|
||||
// public class X511CrawlerJob {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(X511CrawlerJob.class);
|
||||
//
|
||||
// @Autowired
|
||||
// private JobConfig config;
|
||||
//
|
||||
// @Autowired
|
||||
// private Kai168H mKai168H;
|
||||
//
|
||||
// @Autowired
|
||||
// private ShiShiCaiCrawler shishicai;
|
||||
//
|
||||
// boolean isRunningAH11X5 = true;
|
||||
//
|
||||
// /**
|
||||
// * 安徽11选5
|
||||
// */
|
||||
// // @Schedules({ @Scheduled(cron = LotteryCrons.ah11x5_8),
|
||||
// // @Scheduled(cron = LotteryCrons.ah11x5_9_21),
|
||||
// // @Scheduled(cron = LotteryCrons.ah11x5_22) })
|
||||
// public void executeAH11x5() {
|
||||
// final String lotteryName = "ah11x5";
|
||||
// logger.debug(lotteryName + " execute");
|
||||
// if (config.isExe11x5 && isRunningAH11X5) {
|
||||
// try {
|
||||
// isRunningAH11X5 = false;
|
||||
// while (true) {
|
||||
// ExecutorService newFixedThreadPool = Executors
|
||||
// .newFixedThreadPool(4);
|
||||
// Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// @Override
|
||||
// public boolean[] call() throws Exception {
|
||||
// boolean result1 = mKai168H.start(lotteryName);
|
||||
// boolean result2 = shishicai.start(lotteryName);
|
||||
// boolean[] arr = new boolean[] { result1, result2 };
|
||||
// return arr;
|
||||
// }
|
||||
// };
|
||||
// Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// newFixedThreadPool.shutdown();
|
||||
// boolean[] arr = (boolean[]) submit.get();
|
||||
// if (arr[0] || arr[1]) {
|
||||
// break;
|
||||
// }
|
||||
// Thread.sleep(10 * 1000);
|
||||
// logger.debug("安徽11选5--->执行完成...");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("安徽11选5-->执行出错...", e);
|
||||
// } finally {
|
||||
// isRunningAH11X5 = true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// boolean isRunningHG11X5 = true;
|
||||
//
|
||||
// /**
|
||||
// * 广东11选5
|
||||
// */
|
||||
// // @Schedules({ @Scheduled(cron = LotteryCrons.gd11x5_9),
|
||||
// // @Scheduled(cron = LotteryCrons.gd11x5_10_22),
|
||||
// // @Scheduled(cron = LotteryCrons.gd11x5_23) })
|
||||
// public void executeGD11x5() {
|
||||
// final String lotteryName = "gd11x5";
|
||||
// logger.debug(lotteryName + " execute");
|
||||
// if (config.isExe11x5 && isRunningHG11X5) {
|
||||
// try {
|
||||
// isRunningHG11X5 = false;
|
||||
// while (true) {
|
||||
// ExecutorService newFixedThreadPool = Executors
|
||||
// .newFixedThreadPool(4);
|
||||
// Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// @Override
|
||||
// public boolean[] call() throws Exception {
|
||||
// boolean result1 = mKai168H.start(lotteryName);
|
||||
// boolean result2 = shishicai.start(lotteryName);
|
||||
// boolean[] arr = new boolean[] { result1, result2 };
|
||||
// return arr;
|
||||
// }
|
||||
// };
|
||||
// Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// newFixedThreadPool.shutdown();
|
||||
// boolean[] arr = (boolean[]) submit.get();
|
||||
// if (arr[0] || arr[1]) {
|
||||
// break;
|
||||
// }
|
||||
// Thread.sleep(10 * 1000);
|
||||
// logger.debug("广东11选5--->执行完成...");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("广东11选5-->执行出错...", e);
|
||||
// } finally {
|
||||
// isRunningHG11X5 = true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// boolean isRunningSH11X5 = true;
|
||||
//
|
||||
// /**
|
||||
// * 上海11选5
|
||||
// */
|
||||
// // @Schedules({ @Scheduled(cron = LotteryCrons.sh11x5_9_23) })
|
||||
// public void executeSH11x5() {
|
||||
// final String lotteryName = "sh11x5";
|
||||
// logger.debug(lotteryName + " execute");
|
||||
// if (config.isExe11x5 && isRunningSH11X5) {
|
||||
// try {
|
||||
// isRunningSH11X5 = false;
|
||||
// while (true) {
|
||||
// ExecutorService newFixedThreadPool = Executors
|
||||
// .newFixedThreadPool(4);
|
||||
// Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// @Override
|
||||
// public boolean[] call() throws Exception {
|
||||
// boolean result1 = mKai168H.start(lotteryName);
|
||||
// boolean result2 = shishicai.start(lotteryName);
|
||||
// boolean[] arr = new boolean[] { result1, result2 };
|
||||
// return arr;
|
||||
// }
|
||||
// };
|
||||
// Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// newFixedThreadPool.shutdown();
|
||||
// boolean[] arr = (boolean[]) submit.get();
|
||||
// if (arr[0] || arr[1]) {
|
||||
// break;
|
||||
// }
|
||||
// Thread.sleep(10 * 1000);
|
||||
// logger.debug("上海11选5--->执行完成...");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("上海11选5-->执行出错...", e);
|
||||
// } finally {
|
||||
// isRunningSH11X5 = true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// boolean isRunningSD11X5 = true;
|
||||
//
|
||||
// /**
|
||||
// * 山东11选5/十一夺运
|
||||
// */
|
||||
// // @Schedules({ @Scheduled(cron = LotteryCrons.sd11x5_9_21) })
|
||||
// public void executeSD11x5() {
|
||||
// final String lotteryName = "sd11x5";
|
||||
// logger.debug(lotteryName + " execute");
|
||||
// if (config.isExe11x5 && isRunningSD11X5) {
|
||||
// try {
|
||||
// isRunningSD11X5 = false;
|
||||
// while (true) {
|
||||
// ExecutorService newFixedThreadPool = Executors
|
||||
// .newFixedThreadPool(4);
|
||||
// Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// @Override
|
||||
// public boolean[] call() throws Exception {
|
||||
// boolean result1 = mKai168H.start(lotteryName);
|
||||
// boolean result2 = shishicai.start(lotteryName);
|
||||
// boolean[] arr = new boolean[] { result1, result2 };
|
||||
// return arr;
|
||||
// }
|
||||
// };
|
||||
// Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// newFixedThreadPool.shutdown();
|
||||
// boolean[] arr = (boolean[]) submit.get();
|
||||
// if (arr[0] || arr[1]) {
|
||||
// break;
|
||||
// }
|
||||
// Thread.sleep(10 * 1000);
|
||||
// logger.debug("山东11选5---> 执行完成...");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("山东11选5-->执行出错...", e);
|
||||
// } finally {
|
||||
// isRunningSD11X5 = true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// boolean isRunningJX11X5 = true;
|
||||
//
|
||||
// /**
|
||||
// * 江西11选5
|
||||
// */
|
||||
// // @Schedules({ @Scheduled(cron = LotteryCrons.jx11x5_9),
|
||||
// // @Scheduled(cron = LotteryCrons.jx11x5_10_21),
|
||||
// // @Scheduled(cron = LotteryCrons.jx11x5_22) })
|
||||
// public void executeJX11x5() {
|
||||
// final String lotteryName = "jx11x5";
|
||||
//
|
||||
// logger.debug(lotteryName + " execute");
|
||||
// if (config.isExe11x5 && isRunningJX11X5) {
|
||||
// try {
|
||||
// isRunningJX11X5 = false;
|
||||
// while (true) {
|
||||
// ExecutorService newFixedThreadPool = Executors
|
||||
// .newFixedThreadPool(4);
|
||||
// Callable<boolean[]> aCallable = new Callable<boolean[]>() {
|
||||
// @Override
|
||||
// public boolean[] call() throws Exception {
|
||||
// boolean result1 = mKai168H.start(lotteryName);
|
||||
// boolean result2 = shishicai.start(lotteryName);
|
||||
// boolean[] arr = new boolean[] { result1, result2 };
|
||||
// return arr;
|
||||
// }
|
||||
// };
|
||||
// Future<?> submit = newFixedThreadPool.submit(aCallable);
|
||||
// newFixedThreadPool.shutdown();
|
||||
// boolean[] arr = (boolean[]) submit.get();
|
||||
// if (arr[0] || arr[1]) {
|
||||
// break;
|
||||
// }
|
||||
// Thread.sleep(10 * 1000);
|
||||
// logger.debug("江西11选5---> 执行完成...");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("江西11选5-->执行出错...", e);
|
||||
// } finally {
|
||||
// isRunningJX11X5 = true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
@@ -0,0 +1,38 @@
|
||||
package lottery.domains.capture.sites.b1cp;
|
||||
|
||||
/**
|
||||
* 博易网 www.b1cp.com
|
||||
*
|
||||
* @author Max
|
||||
*
|
||||
*/
|
||||
public class BoyiBean {
|
||||
private String opentime;
|
||||
private String expect;
|
||||
private String opencode;
|
||||
|
||||
public String getOpentime() {
|
||||
return opentime;
|
||||
}
|
||||
|
||||
public void setOpentime(String opentime) {
|
||||
this.opentime = opentime;
|
||||
}
|
||||
|
||||
public String getExpect() {
|
||||
return expect;
|
||||
}
|
||||
|
||||
public void setExpect(String expect) {
|
||||
this.expect = expect;
|
||||
}
|
||||
|
||||
public String getOpencode() {
|
||||
return opencode;
|
||||
}
|
||||
|
||||
public void setOpencode(String opencode) {
|
||||
this.opencode = opencode;
|
||||
}
|
||||
|
||||
}
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
// package lottery.domains.capture.sites.bwlc;
|
||||
//
|
||||
// import javautils.date.DateUtil;
|
||||
// import lottery.domains.capture.utils.open.OpenTimeTransUtil;
|
||||
// import lottery.domains.capture.utils.open.OpenTimeUtil;
|
||||
// import lottery.domains.content.dao.LotteryCrawlerStatusDao;
|
||||
// import lottery.domains.content.dao.LotteryOpenCodeDao;
|
||||
// import lottery.domains.content.entity.LotteryCrawlerStatus;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import lottery.domains.content.entity.LotteryOpenTime;
|
||||
// import lottery.domains.content.pool.LotteryDataFactory;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
// import org.springframework.beans.factory.annotation.Autowired;
|
||||
// import org.springframework.beans.factory.annotation.Qualifier;
|
||||
// import org.springframework.stereotype.Component;
|
||||
//
|
||||
// import java.util.List;
|
||||
//
|
||||
// @Component
|
||||
// public class BwlcCrawler {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(BwlcCrawler.class);
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryCrawlerStatusDao lotteryCrawlerStatusDao;
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryOpenCodeDao lotteryOpenCodeDao;
|
||||
//
|
||||
// public boolean start(String lotteryName) {
|
||||
// logger.debug("开始抓取" + lotteryName + "数据>>>>>>>>>>>>>>>>");
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao
|
||||
// .get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// String expect = getNextExpect(lastExpect);
|
||||
// logger.debug("目标期数:" + expect);
|
||||
// List<LotteryOpenCode> list = getOpenCode(lotteryName, expect);
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// for (LotteryOpenCode lotteryOpenCode : list) {
|
||||
// // 设置数据来源
|
||||
// lotteryOpenCode.setRemarks("www.bwlc.net");
|
||||
// String currExpect = lotteryOpenCode.getExpect();
|
||||
// // 如果大于上一期,插入
|
||||
// if (currExpect.compareTo(lastExpect) > 0) {
|
||||
// boolean isSucc = lotteryOpenCodeDao.add(lotteryOpenCode);
|
||||
// if (isSucc) {
|
||||
// // 更新状态,最后成功抓到数据的期数、时间
|
||||
// lotteryCrawlerStatusDao.update(lotteryName, currExpect,
|
||||
// currTime);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // 如果不是当前期,那么就继续抓吧
|
||||
// if (!isCurrExpect(lotteryName, lotteryCrawlerStatus.getTimes())) {
|
||||
// logger.debug("不是最新的数据,10秒后继续抓...");
|
||||
// return false;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取开奖数据
|
||||
// *
|
||||
// * @param lotteryName
|
||||
// * @param date
|
||||
// * @return
|
||||
// */
|
||||
// private List<LotteryOpenCode> getOpenCode(String lotteryName, String expect) {
|
||||
// switch (lotteryName) {
|
||||
// case "bjkl8":
|
||||
// return BwlcCrawlerUtil.getBjkl8OpenCode(lotteryName, expect);
|
||||
// case "bjpk10":
|
||||
// return BwlcCrawlerUtil.getBjpk10OpenCode(lotteryName, expect);
|
||||
// case "fc3d":
|
||||
// return BwlcCrawlerUtil.getFc3dOpenCode(lotteryName, expect);
|
||||
// default:
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Autowired
|
||||
// @Qualifier(value = "highOpenTimeUtil")
|
||||
// private OpenTimeUtil highOpenTimeUtil;
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryDataFactory df;
|
||||
//
|
||||
// /**
|
||||
// * 判断是否是当前期
|
||||
// *
|
||||
// * @param lotteryName
|
||||
// * @return
|
||||
// */
|
||||
// public boolean isCurrExpect(String lotteryName, int times) {
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// String expect = highOpenTimeUtil.getCurrExpect(lotteryName, currTime);
|
||||
// String refLotteryName = lotteryName + "_ref"; // 获取坐标名称
|
||||
// List<LotteryOpenTime> opList = df.listLotteryOpenTime(refLotteryName);
|
||||
// LotteryOpenTime lotteryOpenTime = opList.get(0);
|
||||
// String refDate = lotteryOpenTime.getOpenTime();
|
||||
// int refExpect = Integer.parseInt(lotteryOpenTime.getExpect());
|
||||
// String currExpect = OpenTimeTransUtil.trans(expect, refDate, refExpect,
|
||||
// times);
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao
|
||||
// .get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// logger.debug("当前期:" + currExpect + " 最后一期:" + lastExpect);
|
||||
// return lastExpect.equals(currExpect);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取下一期
|
||||
// *
|
||||
// * @param lastExpect
|
||||
// * @return
|
||||
// */
|
||||
// public String getNextExpect(String lastExpect) {
|
||||
// return String.valueOf(Integer.parseInt(lastExpect) + 1);
|
||||
// }
|
||||
// }
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
// package lottery.domains.capture.sites.bwlc;
|
||||
//
|
||||
// import javautils.date.DateUtil;
|
||||
// import lottery.domains.capture.utils.open.OpenTimeUtil;
|
||||
// import lottery.domains.content.dao.LotteryCrawlerStatusDao;
|
||||
// import lottery.domains.content.dao.LotteryOpenCodeDao;
|
||||
// import lottery.domains.content.entity.LotteryCrawlerStatus;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import lottery.domains.content.pool.LotteryDataFactory;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
// import org.springframework.beans.factory.annotation.Autowired;
|
||||
// import org.springframework.beans.factory.annotation.Qualifier;
|
||||
// import org.springframework.stereotype.Component;
|
||||
//
|
||||
// import java.util.List;
|
||||
//
|
||||
// @Component
|
||||
// public class BwlcCrawlerFC3D {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(BwlcCrawlerFC3D.class);
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryCrawlerStatusDao lotteryCrawlerStatusDao;
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryOpenCodeDao lotteryOpenCodeDao;
|
||||
//
|
||||
// public boolean start(String lotteryName) {
|
||||
// logger.debug("开始抓取" + lotteryName + "数据>>>>>>>>>>>>>>>>");
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao.get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// String expect = getNextExpect(lotteryName, lastExpect);
|
||||
// logger.debug("目标期数:" + expect);
|
||||
// List<LotteryOpenCode> list = getOpenCode(lotteryName, expect);
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// for (LotteryOpenCode lotteryOpenCode : list) {
|
||||
// // 设置数据来源
|
||||
// lotteryOpenCode.setRemarks("www.bwlc.net");
|
||||
// String currExpect = lotteryOpenCode.getExpect();
|
||||
// // 如果大于上一期,插入
|
||||
// if(currExpect.compareTo(lastExpect) > 0) {
|
||||
// boolean isSucc = lotteryOpenCodeDao.add(lotteryOpenCode);
|
||||
// if(isSucc) {
|
||||
// // 更新状态,最后成功抓到数据的期数、时间
|
||||
// lotteryCrawlerStatusDao.update(lotteryName, currExpect, currTime);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // 如果不是当前期,那么就继续抓吧
|
||||
// if(!isCurrExpect(lotteryName)) {
|
||||
// logger.debug("不是最新的数据,1分钟后继续抓...");
|
||||
// return false;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取开奖数据
|
||||
// * @param lotteryName
|
||||
// * @param date
|
||||
// * @return
|
||||
// */
|
||||
// private List<LotteryOpenCode> getOpenCode(String lotteryName, String expect) {
|
||||
// switch (lotteryName) {
|
||||
// case "fc3d":
|
||||
// return BwlcCrawlerUtil.getFc3dOpenCode(lotteryName, expect);
|
||||
// default:
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Autowired
|
||||
// @Qualifier(value = "lowOpenTimeUtil")
|
||||
// private OpenTimeUtil lowOpenTimeUtil;
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryDataFactory df;
|
||||
//
|
||||
// /**
|
||||
// * 判断是否是当前期
|
||||
// * @param lotteryName
|
||||
// * @return
|
||||
// */
|
||||
// public boolean isCurrExpect(String lotteryName) {
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// String currExpect = lowOpenTimeUtil.getCurrExpect(lotteryName, currTime);
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao.get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// logger.debug("当前期:" + currExpect + " 最后一期:" + lastExpect);
|
||||
// return lastExpect.equals(currExpect);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取下一期
|
||||
// * @param lastExpect
|
||||
// * @return
|
||||
// */
|
||||
// public String getNextExpect(String lotteryName, String lastExpect) {
|
||||
// return lowOpenTimeUtil.getNextExpect(lotteryName, lastExpect);
|
||||
// }
|
||||
// }
|
||||
+175
@@ -0,0 +1,175 @@
|
||||
// package lottery.domains.capture.sites.bwlc;
|
||||
//
|
||||
// import javautils.StringUtil;
|
||||
// import javautils.date.DateUtil;
|
||||
// import javautils.html.HtmlUtils;
|
||||
// import javautils.http.EasyHttpClient;
|
||||
// import lottery.domains.capture.utils.CodeValidate;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import net.sf.json.JSONObject;
|
||||
// import org.htmlparser.Node;
|
||||
// import org.htmlparser.util.NodeList;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
//
|
||||
// import java.util.ArrayList;
|
||||
// import java.util.HashMap;
|
||||
// import java.util.List;
|
||||
// import java.util.Map;
|
||||
//
|
||||
// public class BwlcCrawlerUtil {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(BwlcCrawlerUtil.class);
|
||||
// private static Map<String, EasyHttpClient> httpClientPool = new HashMap<>();
|
||||
//
|
||||
// private static EasyHttpClient getHttpClient(String lotteryName) {
|
||||
// if (!httpClientPool.containsKey(lotteryName)) {
|
||||
// httpClientPool.put(lotteryName, new EasyHttpClient());
|
||||
// }
|
||||
// return httpClientPool.get(lotteryName);
|
||||
// }
|
||||
//
|
||||
// private BwlcCrawlerUtil() {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getBjkl8OpenCode(String lotteryName, String expect) {
|
||||
// logger.info("获取北京快乐8开奖号码....");
|
||||
// String url = "http://www.bwlc.net/bulletin/prevkeno.html?num=" + expect;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// List<LotteryOpenCode> tmplist = getBjkl8AndBjpk10(lotteryName, data);
|
||||
// List<LotteryOpenCode> list = new ArrayList<>();
|
||||
// for (LotteryOpenCode tmpBean : tmplist) {
|
||||
// if(CodeValidate.isBjkl8(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getBjpk10OpenCode(String lotteryName, String expect) {
|
||||
// logger.info("获取北京PK拾开奖号码....");
|
||||
// String url = "http://www.bwlc.net/bulletin/prevtrax.html?num=" + expect;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// List<LotteryOpenCode> tmplist = getBjkl8AndBjpk10(lotteryName, data);
|
||||
// List<LotteryOpenCode> list = new ArrayList<>();
|
||||
// for (LotteryOpenCode tmpBean : tmplist) {
|
||||
// if(CodeValidate.isBjpk10(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getFc3dOpenCode(String lotteryName, String expect) {
|
||||
// logger.info("获取福彩3d开奖号码....");
|
||||
// String url = "http://www.bwlc.net/bulletin/prevpk3.html?num=20" + expect;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// List<LotteryOpenCode> tmplist = getFc3dCode(lotteryName, data);
|
||||
// List<LotteryOpenCode> list = new ArrayList<>();
|
||||
// for (LotteryOpenCode tmpBean : tmplist) {
|
||||
// if(CodeValidate.is3d(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
//
|
||||
// public static void main(String[] args) {
|
||||
// //List<LotteryOpenCode> codes = getBjkl8OpenCode("bjkl8", "680841");
|
||||
// List<LotteryOpenCode> codes = getFc3dOpenCode("fc3d", "15088");
|
||||
// for (LotteryOpenCode lotteryOpenCode : codes) {
|
||||
// System.out.println(JSONObject.fromObject(lotteryOpenCode));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private static List<LotteryOpenCode> getBjkl8AndBjpk10(String lotteryName, String data) {
|
||||
// List<LotteryOpenCode> list = new ArrayList<>();
|
||||
// if(StringUtil.isNotNull(data)) {
|
||||
// try {
|
||||
// String resource = data.replaceAll("(\t|\r|\n)", "");
|
||||
// NodeList nList = HtmlUtils.FilterCss(resource, ".tb", "UTF-8");
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// if(nList.size() > 0) {
|
||||
// NodeList tdList = HtmlUtils.FilterTag(nList.elementAt(0).toHtml(), "td", "UTF-8");
|
||||
// Node[] nodes = tdList.toNodeArray();
|
||||
// LotteryOpenCode lotteryOpenCode = new LotteryOpenCode();
|
||||
// lotteryOpenCode.setLottery(lotteryName);
|
||||
// lotteryOpenCode.setTime(currTime);
|
||||
// lotteryOpenCode.setOpenStatus(0);
|
||||
// for (int i = 0; i < nodes.length; i++) {
|
||||
// if(nodes[i].getChildren() != null) {
|
||||
// for (Node node : nodes[i].getChildren().toNodeArray()) {
|
||||
// String value = node.toHtml();
|
||||
// if(StringUtil.isNotNull(value)) {
|
||||
// if(i == 0) {
|
||||
// lotteryOpenCode.setExpect(value);
|
||||
// }
|
||||
// if(i == 1) {
|
||||
// lotteryOpenCode.setCode(value);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if(lotteryOpenCode.getCode() != null) {
|
||||
// list.add(lotteryOpenCode);
|
||||
// }
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("解析数据失败...", e);
|
||||
// }
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
//
|
||||
// private static List<LotteryOpenCode> getFc3dCode(String lotteryName, String data) {
|
||||
// List<LotteryOpenCode> list = new ArrayList<>();
|
||||
// if(StringUtil.isNotNull(data)) {
|
||||
// try {
|
||||
// String resource = data.replaceAll("(\t|\r|\n)", "");
|
||||
// NodeList nList = HtmlUtils.FilterCss(resource, ".tb", "UTF-8");
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// if(nList.size() > 0) {
|
||||
// NodeList tdList = HtmlUtils.FilterTag(nList.elementAt(0).toHtml(), "td", "UTF-8");
|
||||
// Node[] nodes = tdList.toNodeArray();
|
||||
// LotteryOpenCode lotteryOpenCode = new LotteryOpenCode();
|
||||
// lotteryOpenCode.setLottery(lotteryName);
|
||||
// lotteryOpenCode.setTime(currTime);
|
||||
// StringBuffer code = new StringBuffer();
|
||||
// for (int i = 0; i < nodes.length; i++) {
|
||||
// if(nodes[i].getChildren() != null) {
|
||||
// for (Node node : nodes[i].getChildren().toNodeArray()) {
|
||||
// String value = node.toHtml();
|
||||
// if(StringUtil.isNotNull(value)) {
|
||||
// if(i == 0) {
|
||||
// lotteryOpenCode.setExpect(value.substring(2));
|
||||
// }
|
||||
// if(i == 1) {
|
||||
// code.append(value + ",");
|
||||
// }
|
||||
// if(i == 2) {
|
||||
// code.append(value + ",");
|
||||
// }
|
||||
// if(i == 3) {
|
||||
// code.append(value);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// lotteryOpenCode.setCode(code.toString());
|
||||
// if(lotteryOpenCode.getCode() != null) {
|
||||
// list.add(lotteryOpenCode);
|
||||
// }
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("解析数据失败...", e);
|
||||
// }
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,34 @@
|
||||
package lottery.domains.capture.sites.cpk;
|
||||
|
||||
/**
|
||||
* Created by Nick on 2016/12/15.
|
||||
*/
|
||||
public class CPKBean {
|
||||
private String expect;
|
||||
private String number;
|
||||
private String dateline;
|
||||
|
||||
public String getExpect() {
|
||||
return expect;
|
||||
}
|
||||
|
||||
public void setExpect(String expect) {
|
||||
this.expect = expect;
|
||||
}
|
||||
|
||||
public String getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(String number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public String getDateline() {
|
||||
return dateline;
|
||||
}
|
||||
|
||||
public void setDateline(String dateline) {
|
||||
this.dateline = dateline;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package lottery.domains.capture.sites.cpk;
|
||||
|
||||
import net.sf.json.JSONException;
|
||||
import net.sf.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class CPKUtils {
|
||||
public static void main(String[] args) {
|
||||
// 此处需要传递的3个参数,必填项
|
||||
String name = "";// 彩种
|
||||
String uid = "";// 用户ID
|
||||
String token = "";// token
|
||||
|
||||
String url = "http://api.caipiaokong.com/lottery/";
|
||||
url += "?name=" + name;
|
||||
url += "&format=json";// 数据格式,此文件仅支持json
|
||||
url += "&uid=" + uid;
|
||||
url += "&token=" + token;
|
||||
|
||||
String urlAll = new StringBuffer(url).toString();
|
||||
String charset = "UTF-8";
|
||||
String jsonResult = get(urlAll, charset);// 得到JSON字符串
|
||||
JSONObject object = JSONObject.fromObject(jsonResult);// 转化为JSON类
|
||||
try {
|
||||
Iterator it = object.keys();
|
||||
while (it.hasNext()) {
|
||||
String key = (String) it.next();
|
||||
String value = object.getString(key);
|
||||
JSONObject object1 = JSONObject.fromObject(value);// 转化为JSON类
|
||||
String outputStr = "id:" + key;
|
||||
outputStr += " number:" + object1.getString("number");
|
||||
outputStr += " dateline:" + object1.getString("dateline");
|
||||
System.out.println(outputStr);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param urlAll:请求接口
|
||||
* @param charset:字符编码
|
||||
* @return 返回json结果
|
||||
*/
|
||||
public static String get(String urlAll, String charset) {
|
||||
BufferedReader reader = null;
|
||||
String result = null;
|
||||
StringBuffer sbf = new StringBuffer();
|
||||
String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";// 模拟浏览器
|
||||
try {
|
||||
URL url = new URL(urlAll);
|
||||
HttpURLConnection connection = (HttpURLConnection) url
|
||||
.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
connection.setReadTimeout(30000);
|
||||
connection.setConnectTimeout(30000);
|
||||
connection.setRequestProperty("User-agent", userAgent);
|
||||
connection.connect();
|
||||
InputStream is = connection.getInputStream();
|
||||
reader = new BufferedReader(new InputStreamReader(is, charset));
|
||||
String strRead = null;
|
||||
while ((strRead = reader.readLine()) != null) {
|
||||
sbf.append(strRead);
|
||||
sbf.append("\r\n");
|
||||
}
|
||||
reader.close();
|
||||
result = sbf.toString();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
// package lottery.domains.capture.sites.hn481;
|
||||
//
|
||||
// import javautils.date.DateUtil;
|
||||
// import lottery.domains.capture.utils.open.OpenTimeUtil;
|
||||
// import lottery.domains.content.dao.LotteryCrawlerStatusDao;
|
||||
// import lottery.domains.content.dao.LotteryOpenCodeDao;
|
||||
// import lottery.domains.content.entity.LotteryCrawlerStatus;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
// import org.springframework.beans.factory.annotation.Autowired;
|
||||
// import org.springframework.beans.factory.annotation.Qualifier;
|
||||
// import org.springframework.stereotype.Component;
|
||||
//
|
||||
// import java.util.List;
|
||||
//
|
||||
// @Component
|
||||
// public class Hn481Crawler {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(Hn481Crawler.class);
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryCrawlerStatusDao lotteryCrawlerStatusDao;
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryOpenCodeDao lotteryOpenCodeDao;
|
||||
//
|
||||
// public boolean start(String lotteryName) {
|
||||
// logger.debug("开始抓取" + lotteryName + "数据>>>>>>>>>>>>>>>>");
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao
|
||||
// .get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// String date = DateUtil.formatTime(lastExpect.substring(0, 8),
|
||||
// "yyyyMMdd", "yyyy-MM-dd");
|
||||
// String expect = lastExpect.substring(9);
|
||||
// // 如果是最后一期,那么就抓明天的
|
||||
// if (lotteryCrawlerStatus.getTimes() == Integer.parseInt(expect)) {
|
||||
// date = DateUtil.calcNextDay(date);
|
||||
// }
|
||||
// logger.debug("目标日期:" + date);
|
||||
// List<LotteryOpenCode> list = Hn481CrawlerUtil.getOpenCode(lotteryName);
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// for (LotteryOpenCode lotteryOpenCode : list) {
|
||||
// // 设置数据来源
|
||||
// lotteryOpenCode.setRemarks("www.hn481.com");
|
||||
// String currExpect = lotteryOpenCode.getExpect();
|
||||
// // 如果大于上一期,插入
|
||||
// if (currExpect.compareTo(lastExpect) > 0) {
|
||||
// boolean isSucc = lotteryOpenCodeDao.add(lotteryOpenCode);
|
||||
// if (isSucc) {
|
||||
// // 更新状态,最后成功抓到数据的期数、时间
|
||||
// lotteryCrawlerStatusDao.update(lotteryName, currExpect,
|
||||
// currTime);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // 如果不是当前期,那么就继续抓吧
|
||||
// if (!isCurrExpect(lotteryName)) {
|
||||
// logger.debug("不是最新的数据,10秒后继续抓...");
|
||||
// return false;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// @Autowired
|
||||
// @Qualifier(value = "highOpenTimeUtil")
|
||||
// private OpenTimeUtil highOpenTimeUtil;
|
||||
//
|
||||
// /**
|
||||
// * 判断是否是当前期
|
||||
// *
|
||||
// * @param lotteryName
|
||||
// * @return
|
||||
// */
|
||||
// public boolean isCurrExpect(String lotteryName) {
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// String currExpect = highOpenTimeUtil.getCurrExpect(lotteryName,
|
||||
// currTime);
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao
|
||||
// .get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// logger.debug("当前期:" + currExpect + " 最后一期:" + lastExpect);
|
||||
// return lastExpect.equals(currExpect);
|
||||
// }
|
||||
// }
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
// package lottery.domains.capture.sites.hn481;
|
||||
//
|
||||
// import cryptix.jce.provider.MD5;
|
||||
// import javautils.date.Moment;
|
||||
// import javautils.http.EasyHttpClient;
|
||||
// import javautils.json.JSONUtil;
|
||||
// import lottery.domains.capture.utils.CodeValidate;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import net.sf.json.JSONArray;
|
||||
// import net.sf.json.JSONObject;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
// import sun.misc.BASE64Decoder;
|
||||
// import sun.misc.BASE64Encoder;
|
||||
//
|
||||
// import java.util.*;
|
||||
//
|
||||
// public class Hn481CrawlerUtil {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(Hn481CrawlerUtil.class);
|
||||
// private static Map<String, EasyHttpClient> httpClientPool = new HashMap<>();
|
||||
//
|
||||
// private static EasyHttpClient getHttpClient(String lotteryName) {
|
||||
// if (!httpClientPool.containsKey(lotteryName)) {
|
||||
// httpClientPool.put(lotteryName, new EasyHttpClient());
|
||||
// }
|
||||
// return httpClientPool.get(lotteryName);
|
||||
// }
|
||||
//
|
||||
// private Hn481CrawlerUtil() {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getOpenCode(
|
||||
// String lotteryName) {
|
||||
// logger.info("获取河南481开奖号码....");
|
||||
// String url = "http://m.hn481.com:8098/TradeSystem/SearchLotteryResultListService.aspx";
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String result = httpClient.postString(url, getRequestString());
|
||||
// List<LotteryOpenCode> tmplist = decode(lotteryName, result);
|
||||
// List<LotteryOpenCode> list = new ArrayList<>();
|
||||
// for (LotteryOpenCode tmpBean : tmplist) {
|
||||
// if (CodeValidate.isKy481(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
//
|
||||
// public static List<LotteryOpenCode> decode(String lotteryName, String data) {
|
||||
// List<LotteryOpenCode> list = new ArrayList<>();
|
||||
// try {
|
||||
// byte[] decodeBuffer = new BASE64Decoder().decodeBuffer(data);
|
||||
// String result = new String(decodeBuffer);
|
||||
// result = result.trim().replaceFirst("^([\\W]+)<", "<");
|
||||
// JSONObject json = JSONObject.fromObject(JSONUtil
|
||||
// .toJSONString(result));
|
||||
// JSONObject body = (JSONObject) json.get("body");
|
||||
// JSONArray elements = body.getJSONArray("elements");
|
||||
// Map<String, LotteryOpenCode> tmpMap = new HashMap<>();
|
||||
// String time = new Moment().toSimpleTime();
|
||||
// for (Object o : elements) {
|
||||
// JSONObject r = (JSONObject) o;
|
||||
// String expect = formatExpect(r.getString("issue"));
|
||||
// String code = formatCode(r.getString("result"));
|
||||
// LotteryOpenCode bean = new LotteryOpenCode(lotteryName, expect,
|
||||
// code, time, 0);
|
||||
// tmpMap.put(expect, bean);
|
||||
// }
|
||||
// Object[] keys = tmpMap.keySet().toArray();
|
||||
// Arrays.sort(keys);
|
||||
// for (Object o : keys) {
|
||||
// list.add(tmpMap.get(o));
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("解析河南481号码失败!", e);
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
//
|
||||
// public static void main(String[] args) {
|
||||
// List<LotteryOpenCode> list = getOpenCode("hn481");
|
||||
// for (LotteryOpenCode lotteryOpenCode : list) {
|
||||
// System.out.println(JSONObject.fromObject(lotteryOpenCode));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public static String getRequestString() {
|
||||
// int lotteryId = 81, merchantid = 88;
|
||||
// StringBuilder bodyBuilder = new StringBuilder();
|
||||
// bodyBuilder.append("<body>");
|
||||
// bodyBuilder.append("<lotteryid>" + lotteryId + "</lotteryid>");
|
||||
// bodyBuilder.append("</body>");
|
||||
// StringBuffer xmlBuffer = new StringBuffer();
|
||||
// String messengerid = new Moment().format("yyyyMMddHHmmss");
|
||||
// String digest = new MD5()
|
||||
// .toMD5(messengerid + "000001" + messengerid + merchantid
|
||||
// + bodyBuilder).toLowerCase();
|
||||
// xmlBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
|
||||
// xmlBuffer.append("<message>");
|
||||
// xmlBuffer.append("<header>");
|
||||
// xmlBuffer
|
||||
// .append("<messengerid>" + messengerid + "000001</messengerid>");
|
||||
// xmlBuffer.append("<timestamp>" + messengerid + "</timestamp>");
|
||||
// xmlBuffer.append("<digest>" + digest + "</digest>");
|
||||
// xmlBuffer.append("<merchantid>" + merchantid + "</merchantid>");
|
||||
// xmlBuffer.append("</header>");
|
||||
// xmlBuffer.append(bodyBuilder);
|
||||
// xmlBuffer.append("</message>");
|
||||
// return new BASE64Encoder().encode(xmlBuffer.toString().getBytes());
|
||||
// }
|
||||
//
|
||||
// public static String formatExpect(String expect) {
|
||||
// return "20" + expect.substring(0, 6) + "-"
|
||||
// + String.format("%03d", Integer.parseInt(expect.substring(6)));
|
||||
// }
|
||||
//
|
||||
// public static String formatCode(String code) {
|
||||
// StringBuffer sb = new StringBuffer();
|
||||
// for (int i = 0, j = code.length(); i < j; i++) {
|
||||
// sb.append(code.substring(i, i + 1));
|
||||
// if (i != j - 1)
|
||||
// sb.append(",");
|
||||
// }
|
||||
// return sb.toString();
|
||||
// }
|
||||
//
|
||||
// }
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package lottery.domains.capture.sites.jiangyuan365;
|
||||
|
||||
/**
|
||||
* Created by Nick on 2017-09-19.
|
||||
*/
|
||||
public class JiangYuan365Bean{
|
||||
private String issue;
|
||||
private String opendate;
|
||||
private String code;
|
||||
private String lotterycode;
|
||||
|
||||
public String getIssue() {
|
||||
return issue;
|
||||
}
|
||||
|
||||
public void setIssue(String issue) {
|
||||
this.issue = issue;
|
||||
}
|
||||
|
||||
public String getOpendate() {
|
||||
return opendate;
|
||||
}
|
||||
|
||||
public void setOpendate(String opendate) {
|
||||
this.opendate = opendate;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getLotterycode() {
|
||||
return lotterycode;
|
||||
}
|
||||
|
||||
public void setLotterycode(String lotterycode) {
|
||||
this.lotterycode = lotterycode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
// package lottery.domains.capture.sites.kj168;
|
||||
//
|
||||
// import javautils.date.DateUtil;
|
||||
// import lottery.domains.capture.utils.open.OpenTimeTransUtil;
|
||||
// import lottery.domains.capture.utils.open.OpenTimeUtil;
|
||||
// import lottery.domains.content.dao.LotteryCrawlerStatusDao;
|
||||
// import lottery.domains.content.dao.LotteryOpenCodeDao;
|
||||
// import lottery.domains.content.entity.LotteryCrawlerStatus;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import lottery.domains.content.entity.LotteryOpenTime;
|
||||
// import lottery.domains.content.pool.LotteryDataFactory;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
// import org.springframework.beans.factory.annotation.Autowired;
|
||||
// import org.springframework.beans.factory.annotation.Qualifier;
|
||||
// import org.springframework.stereotype.Component;
|
||||
//
|
||||
// import java.util.List;
|
||||
//
|
||||
// /**
|
||||
// * 北京快乐8
|
||||
// * 北京pk10
|
||||
// */
|
||||
// @Component
|
||||
// public class Kai168BJ {
|
||||
//
|
||||
// private final static Logger logger = LoggerFactory.getLogger(Kai168BJ.class);
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryCrawlerStatusDao lotteryCrawlerStatusDao;
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryOpenCodeDao lotteryOpenCodeDao;
|
||||
//
|
||||
// public boolean start(String lotteryName) {
|
||||
// logger.debug("开始抓取" + lotteryName + "数据>>>>>>>>>>>>>>>>");
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao.get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// String expect = getNextExpect(lotteryName, lastExpect, lotteryCrawlerStatus.getTimes());
|
||||
// String date = DateUtil.formatTime(expect.substring(0, 8), "yyyyMMdd", "yyyy-MM-dd");
|
||||
// logger.debug("目标期数:" + expect);
|
||||
// List<LotteryOpenCode> list = getOpenCode(lotteryName, date);
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// for (LotteryOpenCode lotteryOpenCode : list) {
|
||||
// // 设置数据来源
|
||||
// lotteryOpenCode.setRemarks("kai168.com");
|
||||
// String currExpect = lotteryOpenCode.getExpect();
|
||||
// // 如果大于上一期,插入
|
||||
// if(currExpect.compareTo(lastExpect) > 0) {
|
||||
// boolean isSucc = lotteryOpenCodeDao.add(lotteryOpenCode);
|
||||
// if(isSucc) {
|
||||
// // 更新状态,最后成功抓到数据的期数、时间
|
||||
// lotteryCrawlerStatusDao.update(lotteryName, currExpect, currTime);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // 如果不是当前期,那么就继续抓吧
|
||||
// if(!isCurrExpect(lotteryName, lotteryCrawlerStatus.getTimes())) {
|
||||
// logger.debug("不是最新的数据,10秒后继续抓...");
|
||||
// return false;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取开奖数据
|
||||
// * @param lotteryName
|
||||
// * @param date
|
||||
// * @return
|
||||
// */
|
||||
// private List<LotteryOpenCode> getOpenCode(String lotteryName, String date) {
|
||||
// switch (lotteryName) {
|
||||
// case "bjkl8":
|
||||
// return Kai168CrawlerUtil.getBjkl8OpenCode(lotteryName, date);
|
||||
// case "bjpk10":
|
||||
// return Kai168CrawlerUtil.getBjpk10OpenCode(lotteryName, date);
|
||||
// default:
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Autowired
|
||||
// @Qualifier(value = "highOpenTimeUtil")
|
||||
// private OpenTimeUtil highOpenTimeUtil;
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryDataFactory df;
|
||||
//
|
||||
// /**
|
||||
// * 判断是否是当前期
|
||||
// * @param lotteryName
|
||||
// * @return
|
||||
// */
|
||||
// public boolean isCurrExpect(String lotteryName, int times) {
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// String expect = highOpenTimeUtil.getCurrExpect(lotteryName, currTime);
|
||||
// String refLotteryName = lotteryName + "_ref"; // 获取坐标名称
|
||||
// List<LotteryOpenTime> opList = df.listLotteryOpenTime(refLotteryName);
|
||||
// LotteryOpenTime lotteryOpenTime = opList.get(0);
|
||||
// String refDate = lotteryOpenTime.getOpenTime();
|
||||
// int refExpect = Integer.parseInt(lotteryOpenTime.getExpect());
|
||||
// String currExpect = OpenTimeTransUtil.trans(expect, refDate, refExpect, times);
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao.get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// logger.debug("当前期:" + currExpect + " 最后一期:" + lastExpect);
|
||||
// return lastExpect.equals(currExpect);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取下一期
|
||||
// * @param lastExpect
|
||||
// * @return
|
||||
// */
|
||||
// public String getNextExpect(String lotteryName, String lastExpect, int times) {
|
||||
// int nextExpect = Integer.parseInt(lastExpect) + 1;
|
||||
// logger.debug("获取下一期:" + nextExpect);
|
||||
// String refLotteryName = lotteryName + "_ref"; // 获取坐标名称
|
||||
// List<LotteryOpenTime> opList = df.listLotteryOpenTime(refLotteryName);
|
||||
// LotteryOpenTime lotteryOpenTime = opList.get(0);
|
||||
// String refDate = lotteryOpenTime.getOpenTime();
|
||||
// int refExpect = Integer.parseInt(lotteryOpenTime.getExpect());
|
||||
// return OpenTimeTransUtil.trans(nextExpect, refDate, refExpect, times);
|
||||
// }
|
||||
// }
|
||||
+418
@@ -0,0 +1,418 @@
|
||||
// package lottery.domains.capture.sites.kj168;
|
||||
//
|
||||
// import javautils.StringUtil;
|
||||
// import javautils.date.DateUtil;
|
||||
// import javautils.http.EasyHttpClient;
|
||||
// import lottery.domains.capture.utils.CodeValidate;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import net.sf.json.JSONArray;
|
||||
// import net.sf.json.JSONObject;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
//
|
||||
// import java.util.*;
|
||||
//
|
||||
// public class Kai168CrawlerUtil {
|
||||
//
|
||||
// /**
|
||||
// * 重庆时时彩 http://www.168kai.com/History/HisList?id=10011&date=2014-10-20
|
||||
// * 江西时时彩 1002 新疆时时彩 10022 ------不需要 天津时时彩 10021 菲博分分彩 广东11选5 1007 江西11选5
|
||||
// * 1001 重庆11选5 10012 安徽11选5 10025 上海11选5 10024 十一运夺金 1003 江苏快3 1006 安徽快3
|
||||
// * 10043 湖北快3 吉林快3 10013 福彩3D 2002 排列三 2007 北京快乐8 10014 北京PK拾 10016
|
||||
// */
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(Kai168CrawlerUtil.class);
|
||||
// private static Map<String, EasyHttpClient> httpClientPool = new HashMap<>();
|
||||
//
|
||||
// private static EasyHttpClient getHttpClient(String lotteryName) {
|
||||
// if (!httpClientPool.containsKey(lotteryName)) {
|
||||
// httpClientPool.put(lotteryName, new EasyHttpClient());
|
||||
// }
|
||||
// return httpClientPool.get(lotteryName);
|
||||
// }
|
||||
//
|
||||
// private Kai168CrawlerUtil() {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getXjsscOpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.info("获取新疆时时彩开奖号码....");
|
||||
// String code = "10022";
|
||||
// String type = "ssc";
|
||||
// String url = "http://www.168kai.com/History/HisList?id=" + code
|
||||
// + "&date=" + date;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getCqsscOpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.info("获取重庆时时彩开奖号码....");
|
||||
// String code = "10011";
|
||||
// String type = "ssc";
|
||||
// String url = "http://www.168kai.com/History/HisList?id=" + code
|
||||
// + "&date=" + date;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getJxsscOpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.info("获取江西时时彩开奖号码....");
|
||||
// String code = "1002";
|
||||
// String type = "ssc";
|
||||
// String url = "http://www.168kai.com/History/HisList?id=" + code
|
||||
// + "&date=" + date;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getTjsscOpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.info("获取天津时时彩开奖号码....");
|
||||
// String code = "10021";
|
||||
// String type = "ssc";
|
||||
// String url = "http://www.168kai.com/History/HisList?id=" + code
|
||||
// + "&date=" + date;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getGd11x5OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.info("获取广东11选5开奖号码....");
|
||||
// String code = "1007";
|
||||
// String type = "11x5";
|
||||
// String url = "http://www.168kai.com/History/HisList?id=" + code
|
||||
// + "&date=" + date;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getJx11x5OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.info("获取江西11选5开奖号码....");
|
||||
// String code = "1001";
|
||||
// String type = "11x5";
|
||||
// String url = "http://www.168kai.com/History/HisList?id=" + code
|
||||
// + "&date=" + date;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getCq11x5OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.info("获取重庆11选5开奖号码....");
|
||||
// String code = "10012";
|
||||
// String type = "11x5";
|
||||
// String url = "http://www.168kai.com/History/HisList?id=" + code
|
||||
// + "&date=" + date;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getAh11x5OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.info("获取安徽11选5开奖号码....");
|
||||
// String code = "10025";
|
||||
// String type = "11x5";
|
||||
// String url = "http://www.168kai.com/History/HisList?id=" + code
|
||||
// + "&date=" + date;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getSh11x5OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.info("获取上海11选5开奖号码....");
|
||||
// String code = "10024";
|
||||
// String type = "11x5";
|
||||
// String url = "http://www.168kai.com/History/HisList?id=" + code
|
||||
// + "&date=" + date;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getSd11x5OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.info("获取山东11选5开奖号码....");
|
||||
// String code = "1003";
|
||||
// String type = "11x5";
|
||||
// String url = "http://www.168kai.com/History/HisList?id=" + code
|
||||
// + "&date=" + date;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getJsk3OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.info("获取江苏快3开奖号码....");
|
||||
// String code = "1006";
|
||||
// String type = "k3";
|
||||
// String url = "http://www.168kai.com/History/HisList?id=" + code
|
||||
// + "&date=" + date;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getAhk3OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.info("获取安徽快3开奖号码....");
|
||||
// String code = "10043";
|
||||
// String type = "k3";
|
||||
// String url = "http://www.168kai.com/History/HisList?id=" + code
|
||||
// + "&date=" + date;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getJlk3OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.info("获取吉林快3开奖号码....");
|
||||
// String code = "10013";
|
||||
// String type = "k3";
|
||||
// String url = "http://www.168kai.com/History/HisList?id=" + code
|
||||
// + "&date=" + date;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getFc3dOpenCode(
|
||||
// String lotteryName) {
|
||||
// logger.info("获取福彩3d开奖号码....");
|
||||
// String code = "2002";
|
||||
// String type = "3d";
|
||||
// String url = "http://www.168kai.com/History/HisList?id=" + code;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getPl3OpenCode(
|
||||
// String lotteryName) {
|
||||
// logger.info("获取排列3开奖号码....");
|
||||
// String code = "2007";
|
||||
// String type = "3d";
|
||||
// String url = "http://www.168kai.com/History/HisList?id=" + code;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getBjkl8OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.info("获取北京快乐8开奖号码....");
|
||||
// String code = "10014";
|
||||
// String type = "bjkl8";
|
||||
// String url = "http://www.168kai.com/History/HisList?id=" + code
|
||||
// + "&date=" + date;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getBjpk10OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.info("获取北京pk拾开奖号码....");
|
||||
// String code = "10016";
|
||||
// String type = "bjpk10";
|
||||
// String url = "http://www.168kai.com/History/HisList?id=" + code
|
||||
// + "&date=" + date;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// private static List<LotteryOpenCode> handleData(String lotteryName,
|
||||
// String type, String data) {
|
||||
// List<LotteryOpenCode> list = new ArrayList<>();
|
||||
// if (StringUtil.isNotNull(data)) {
|
||||
// Map<String, LotteryOpenCode> tmpMap = new HashMap<String, LotteryOpenCode>();
|
||||
// try {
|
||||
// Object object = JSONObject.fromObject(data).get("list");
|
||||
// JSONArray jsonArr = JSONArray.fromObject(object);
|
||||
// String time = DateUtil.getCurrentTime();
|
||||
// for (Object o : jsonArr) {
|
||||
// JSONObject jsonObj = (JSONObject) o;
|
||||
// String expect = jsonObj.getString("c_t");
|
||||
// String code = jsonObj.getString("c_r");
|
||||
// switch (type) {
|
||||
// case "ssc":
|
||||
// expect = expect.substring(0, 8)
|
||||
// + "-"
|
||||
// + String.format("%03d",
|
||||
// Integer.parseInt(expect.substring(8)));
|
||||
// break;
|
||||
// case "11x5":
|
||||
// if ("jx11x5".equals(lotteryName)
|
||||
// || "sh11x5".equals(lotteryName)) {
|
||||
// try {
|
||||
// expect = expect.substring(0, 8)
|
||||
// + "-"
|
||||
// + String.format("%03d", Integer
|
||||
// .parseInt(expect.substring(8)));
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
//
|
||||
// } else {
|
||||
// expect = DateUtil.formatTime(
|
||||
// expect.substring(0, 6), "yyMMdd",
|
||||
// "yyyyMMdd")
|
||||
// + "-"
|
||||
// + String.format("%03d", Integer
|
||||
// .parseInt(expect.substring(6)));
|
||||
// }
|
||||
// try {
|
||||
// String[] codeArr = code.split(",");
|
||||
// String codeFormat = "";
|
||||
// for (String tmpCode : codeArr) {
|
||||
// codeFormat += String.format("%02d",
|
||||
// Integer.parseInt(tmpCode))
|
||||
// + ",";
|
||||
// }
|
||||
// code = codeFormat.substring(0,
|
||||
// codeFormat.length() - 1);
|
||||
// } catch (Exception e) {
|
||||
// }
|
||||
// break;
|
||||
// case "k3":
|
||||
// expect = DateUtil.formatTime(expect.substring(0, 6),
|
||||
// "yyMMdd", "yyyyMMdd")
|
||||
// + "-"
|
||||
// + expect.substring(6);
|
||||
// break;
|
||||
// case "3d":
|
||||
// if ("fc3d".equals(lotteryName)) {
|
||||
// expect = expect.substring(2);
|
||||
// }
|
||||
// break;
|
||||
// case "bjpk10":
|
||||
// try {
|
||||
// String[] codeArr = code.split(",");
|
||||
// String codeFormat = "";
|
||||
// for (String tmpCode : codeArr) {
|
||||
// codeFormat += String.format("%02d",
|
||||
// Integer.parseInt(tmpCode))
|
||||
// + ",";
|
||||
// }
|
||||
// code = codeFormat.substring(0,
|
||||
// codeFormat.length() - 1);
|
||||
// } catch (Exception e) {
|
||||
// }
|
||||
// break;
|
||||
// case "bjkl8":
|
||||
// try {
|
||||
// String[] codeArr = code.split(",");
|
||||
// String codeFormat = "";
|
||||
// for (int i = 0; i < 20; i++) {
|
||||
// codeFormat += String.format("%02d",
|
||||
// Integer.parseInt(codeArr[i]))
|
||||
// + ",";
|
||||
// }
|
||||
// code = codeFormat.substring(0,
|
||||
// codeFormat.length() - 1);
|
||||
// } catch (Exception e) {
|
||||
// }
|
||||
// break;
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
// LotteryOpenCode bean = new LotteryOpenCode(lotteryName,
|
||||
// expect, code, time, 0);
|
||||
// tmpMap.put(expect, bean);
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("解析数据失败...", e);
|
||||
// }
|
||||
// Object[] keys = tmpMap.keySet().toArray();
|
||||
// Arrays.sort(keys);
|
||||
// for (Object o : keys) {
|
||||
// LotteryOpenCode tmpBean = tmpMap.get(o);
|
||||
// switch (type) {
|
||||
// case "ssc":
|
||||
// if (CodeValidate.isSsc(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// break;
|
||||
// case "11x5":
|
||||
// if (CodeValidate.is11x5(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// break;
|
||||
// case "k3":
|
||||
// if (CodeValidate.isK3(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// break;
|
||||
// case "3d":
|
||||
// if (CodeValidate.is3d(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// break;
|
||||
// case "bjkl8":
|
||||
// if (CodeValidate.isBjkl8(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// break;
|
||||
// case "bjpk10":
|
||||
// if (CodeValidate.isBjpk10(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// break;
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
//
|
||||
// public static void main(String[] args) {
|
||||
// // List<LotteryOpenCode> list = getCqsscOpenCode("cqssc", "2014-12-20");
|
||||
// // List<LotteryOpenCode> list = getJxsscOpenCode("jxssc", "2014-12-25");
|
||||
// // List<LotteryOpenCode> list = getTjsscOpenCode("tjssc", "2014-12-25");
|
||||
//
|
||||
// // List<LotteryOpenCode> list = getGd11x5OpenCode("gd11x5",
|
||||
// // "2014-12-25");
|
||||
// // List<LotteryOpenCode> list = getJx11x5OpenCode("jx11x5",
|
||||
// // "2014-12-25");
|
||||
// // List<LotteryOpenCode> list = getCq11x5OpenCode("cq11x5",
|
||||
// // "2014-12-25");
|
||||
// // List<LotteryOpenCode> list = getAh11x5OpenCode("ah11x5",
|
||||
// // "2014-12-25");
|
||||
//
|
||||
// // List<LotteryOpenCode> list = getSh11x5OpenCode("sh11x5",
|
||||
// // "2014-12-25");
|
||||
// // List<LotteryOpenCode> list = getSd11x5OpenCode("sd11x5",
|
||||
// // "2014-12-25");
|
||||
// // List<LotteryOpenCode> list = getJsk3OpenCode("jsk3", "2014-12-25");
|
||||
// // List<LotteryOpenCode> list = getAhk3OpenCode("ahk3", "2014-12-25");
|
||||
// // List<LotteryOpenCode> list = getJlk3OpenCode("jsk3", "2014-12-25");
|
||||
//
|
||||
// // List<LotteryOpenCode> list = getFc3dOpenCode("fc3d");
|
||||
// // List<LotteryOpenCode> list = getPl3OpenCode("pl3");
|
||||
//
|
||||
// List<LotteryOpenCode> list = getBjkl8OpenCode("bjkl8", "2014-12-25");
|
||||
// // List<LotteryOpenCode> list = getBjpk10OpenCode("bjpk10",
|
||||
// // "2014-12-25");
|
||||
// for (LotteryOpenCode lotteryOpenCode : list) {
|
||||
// System.out.println(JSONObject.fromObject(lotteryOpenCode));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
@@ -0,0 +1,135 @@
|
||||
// package lottery.domains.capture.sites.kj168;
|
||||
//
|
||||
// import javautils.date.DateUtil;
|
||||
// import lottery.domains.capture.utils.open.OpenTimeUtil;
|
||||
// import lottery.domains.content.dao.LotteryCrawlerStatusDao;
|
||||
// import lottery.domains.content.dao.LotteryOpenCodeDao;
|
||||
// import lottery.domains.content.entity.LotteryCrawlerStatus;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
// import org.springframework.beans.factory.annotation.Autowired;
|
||||
// import org.springframework.beans.factory.annotation.Qualifier;
|
||||
// import org.springframework.stereotype.Component;
|
||||
//
|
||||
// import java.util.List;
|
||||
//
|
||||
// /**
|
||||
// * 时时彩 11选5 快3
|
||||
// */
|
||||
// @Component
|
||||
// public class Kai168H {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(Kai168H.class);
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryCrawlerStatusDao lotteryCrawlerStatusDao;
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryOpenCodeDao lotteryOpenCodeDao;
|
||||
//
|
||||
// public boolean start(String lotteryName) {
|
||||
// logger.debug("开始抓取" + lotteryName + "数据>>>>>>>>>>>>>>>>");
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao
|
||||
// .get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
//
|
||||
// String date = DateUtil.formatTime(lastExpect.substring(0, 8),
|
||||
// "yyyyMMdd", "yyyy-MM-dd");
|
||||
// String expect = lastExpect.substring(9);
|
||||
// // 如果是最后一期,那么就抓明天的
|
||||
// if (lotteryCrawlerStatus.getTimes() == Integer.parseInt(expect)) {
|
||||
// date = DateUtil.calcNextDay(date);
|
||||
// }
|
||||
// logger.debug("目标日期:" + date);
|
||||
// List<LotteryOpenCode> list = getOpenCode(lotteryName, date);
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// for (LotteryOpenCode lotteryOpenCode : list) {
|
||||
// // 设置数据来源
|
||||
// lotteryOpenCode.setRemarks("kai168.com");
|
||||
// String currExpect = lotteryOpenCode.getExpect();
|
||||
// // 如果大于上一期,插入
|
||||
// if (currExpect.compareTo(lastExpect) > 0) {
|
||||
// boolean isSucc = lotteryOpenCodeDao.add(lotteryOpenCode);
|
||||
// if (isSucc) {
|
||||
// // 更新状态,最后成功抓到数据的期数、时间
|
||||
// lotteryCrawlerStatusDao.update(lotteryName, currExpect,
|
||||
// currTime);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // 如果不是当前期,那么就继续抓吧
|
||||
// if (!isCurrExpect(lotteryName)) {
|
||||
// logger.debug("不是最新的数据,10秒后继续抓...");
|
||||
// return false;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取开奖数据
|
||||
// *
|
||||
// * @param lotteryName
|
||||
// * @param date
|
||||
// * @return
|
||||
// */
|
||||
// private List<LotteryOpenCode> getOpenCode(String lotteryName, String date) {
|
||||
// switch (lotteryName) {
|
||||
// case "xjssc":
|
||||
// return Kai168CrawlerUtil.getXjsscOpenCode(lotteryName, date);
|
||||
// case "cqssc":
|
||||
// return Kai168CrawlerUtil.getCqsscOpenCode(lotteryName, date);
|
||||
// // case "jxssc":
|
||||
// // return Kai168CrawlerUtil.getJxsscOpenCode(lotteryName, date);
|
||||
// case "tjssc":
|
||||
// return Kai168CrawlerUtil.getTjsscOpenCode(lotteryName, date);
|
||||
// case "gd11x5":
|
||||
// return Kai168CrawlerUtil.getGd11x5OpenCode(lotteryName, date);
|
||||
// case "jx11x5":
|
||||
// return Kai168CrawlerUtil.getJx11x5OpenCode(lotteryName, date);
|
||||
// case "cq11x5":
|
||||
// return Kai168CrawlerUtil.getCq11x5OpenCode(lotteryName, date);
|
||||
// case "ah11x5":
|
||||
// return Kai168CrawlerUtil.getAh11x5OpenCode(lotteryName, date);
|
||||
// case "sh11x5":
|
||||
// return Kai168CrawlerUtil.getSh11x5OpenCode(lotteryName, date);
|
||||
// case "sd11x5":
|
||||
// return Kai168CrawlerUtil.getSd11x5OpenCode(lotteryName, date);
|
||||
// case "jsk3":
|
||||
// return Kai168CrawlerUtil.getJsk3OpenCode(lotteryName, date);
|
||||
// case "ahk3":
|
||||
// return Kai168CrawlerUtil.getAhk3OpenCode(lotteryName, date);
|
||||
// case "jlk3":
|
||||
// return Kai168CrawlerUtil.getJlk3OpenCode(lotteryName, date);
|
||||
// case "bjkl8":
|
||||
// return Kai168CrawlerUtil.getBjkl8OpenCode(lotteryName, date);
|
||||
// case "bjpk10":
|
||||
// return Kai168CrawlerUtil.getBjpk10OpenCode(lotteryName, date);
|
||||
//
|
||||
// default:
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Autowired
|
||||
// @Qualifier(value = "highOpenTimeUtil")
|
||||
// private OpenTimeUtil highOpenTimeUtil;
|
||||
//
|
||||
// /**
|
||||
// * 判断是否是当前期
|
||||
// *
|
||||
// * @param lotteryName
|
||||
// * @return
|
||||
// */
|
||||
// public boolean isCurrExpect(String lotteryName) {
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// String currExpect = highOpenTimeUtil.getCurrExpect(lotteryName,
|
||||
// currTime);
|
||||
// logger.debug("当前期时间:" + currTime + " 期号:" + currExpect);
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao
|
||||
// .get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// logger.debug("当前期:" + currExpect + " 最后一期:" + lastExpect);
|
||||
// return lastExpect.equals(currExpect);
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,108 @@
|
||||
// package lottery.domains.capture.sites.kj168;
|
||||
//
|
||||
// import javautils.date.DateUtil;
|
||||
// import lottery.domains.capture.utils.open.OpenTimeUtil;
|
||||
// import lottery.domains.content.dao.LotteryCrawlerStatusDao;
|
||||
// import lottery.domains.content.dao.LotteryOpenCodeDao;
|
||||
// import lottery.domains.content.entity.LotteryCrawlerStatus;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import lottery.domains.content.pool.LotteryDataFactory;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
// import org.springframework.beans.factory.annotation.Autowired;
|
||||
// import org.springframework.beans.factory.annotation.Qualifier;
|
||||
// import org.springframework.stereotype.Component;
|
||||
//
|
||||
// import java.util.List;
|
||||
//
|
||||
// /**
|
||||
// * 福彩3d
|
||||
// * 排列3
|
||||
// */
|
||||
// @Component
|
||||
// public class Kai168L {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(Kai168L.class);
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryCrawlerStatusDao lotteryCrawlerStatusDao;
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryOpenCodeDao lotteryOpenCodeDao;
|
||||
//
|
||||
// public boolean start(String lotteryName) {
|
||||
// logger.debug("开始抓取" + lotteryName + "数据>>>>>>>>>>>>>>>>");
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao.get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// String expect = getNextExpect(lotteryName, lastExpect);
|
||||
// logger.debug("目标期数:" + expect);
|
||||
// List<LotteryOpenCode> list = getOpenCode(lotteryName);
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// for (LotteryOpenCode lotteryOpenCode : list) {
|
||||
// // 设置数据来源
|
||||
// lotteryOpenCode.setRemarks("kai168.com");
|
||||
// String currExpect = lotteryOpenCode.getExpect();
|
||||
// // 如果大于上一期,插入
|
||||
// if(currExpect.compareTo(lastExpect) > 0) {
|
||||
// boolean isSucc = lotteryOpenCodeDao.add(lotteryOpenCode);
|
||||
// if(isSucc) {
|
||||
// // 更新状态,最后成功抓到数据的期数、时间
|
||||
// lotteryCrawlerStatusDao.update(lotteryName, currExpect, currTime);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // 如果不是当前期,那么就继续抓吧
|
||||
// if(!isCurrExpect(lotteryName)) {
|
||||
// logger.debug("不是最新的数据,1分钟后继续抓...");
|
||||
// return false;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取开奖数据
|
||||
// * @param lotteryName
|
||||
// * @param date
|
||||
// * @return
|
||||
// */
|
||||
// private List<LotteryOpenCode> getOpenCode(String lotteryName) {
|
||||
// switch (lotteryName) {
|
||||
// case "fc3d":
|
||||
// return Kai168CrawlerUtil.getFc3dOpenCode(lotteryName);
|
||||
// case "pl3":
|
||||
// return Kai168CrawlerUtil.getPl3OpenCode(lotteryName);
|
||||
// default:
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Autowired
|
||||
// @Qualifier(value = "lowOpenTimeUtil")
|
||||
// private OpenTimeUtil lowOpenTimeUtil;
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryDataFactory df;
|
||||
//
|
||||
// /**
|
||||
// * 判断是否是当前期
|
||||
// * @param lotteryName
|
||||
// * @return
|
||||
// */
|
||||
// public boolean isCurrExpect(String lotteryName) {
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// String currExpect = lowOpenTimeUtil.getCurrExpect(lotteryName, currTime);
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao.get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// logger.debug("当前期:" + currExpect + " 最后一期:" + lastExpect);
|
||||
// return lastExpect.equals(currExpect);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取下一期
|
||||
// * @param lastExpect
|
||||
// * @return
|
||||
// */
|
||||
// public String getNextExpect(String lotteryName, String lastExpect) {
|
||||
// return lowOpenTimeUtil.getNextExpect(lotteryName, lastExpect);
|
||||
// }
|
||||
// }
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
// package lottery.domains.capture.sites.lecai;
|
||||
//
|
||||
// import javautils.date.DateUtil;
|
||||
// import lottery.domains.capture.utils.open.OpenTimeTransUtil;
|
||||
// import lottery.domains.capture.utils.open.OpenTimeUtil;
|
||||
// import lottery.domains.content.dao.LotteryCrawlerStatusDao;
|
||||
// import lottery.domains.content.dao.LotteryOpenCodeDao;
|
||||
// import lottery.domains.content.entity.LotteryCrawlerStatus;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import lottery.domains.content.entity.LotteryOpenTime;
|
||||
// import lottery.domains.content.pool.LotteryDataFactory;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
// import org.springframework.beans.factory.annotation.Autowired;
|
||||
// import org.springframework.beans.factory.annotation.Qualifier;
|
||||
// import org.springframework.stereotype.Component;
|
||||
//
|
||||
// import java.util.List;
|
||||
//
|
||||
// @Component
|
||||
// public class LecaiCrawlerBJC {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(LecaiCrawlerBJC.class);
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryCrawlerStatusDao lotteryCrawlerStatusDao;
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryOpenCodeDao lotteryOpenCodeDao;
|
||||
//
|
||||
// public boolean start(String lotteryName) {
|
||||
// logger.debug("开始抓取" + lotteryName + "数据>>>>>>>>>>>>>>>>");
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao.get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// String expect = getNextExpect(lotteryName, lastExpect, lotteryCrawlerStatus.getTimes());
|
||||
// String date = DateUtil.formatTime(expect.substring(0, 8), "yyyyMMdd", "yyyy-MM-dd");
|
||||
// logger.debug("目标期数:" + expect);
|
||||
// List<LotteryOpenCode> list = getOpenCode(lotteryName, date);
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// for (LotteryOpenCode lotteryOpenCode : list) {
|
||||
// // 设置数据来源
|
||||
// lotteryOpenCode.setRemarks("baidu.lecai.com");
|
||||
// String currExpect = lotteryOpenCode.getExpect();
|
||||
// // 如果大于上一期,插入
|
||||
// if(currExpect.compareTo(lastExpect) > 0) {
|
||||
// boolean isSucc = lotteryOpenCodeDao.add(lotteryOpenCode);
|
||||
// if(isSucc) {
|
||||
// // 更新状态,最后成功抓到数据的期数、时间
|
||||
// lotteryCrawlerStatusDao.update(lotteryName, currExpect, currTime);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // 如果不是当前期,那么就继续抓吧
|
||||
// if(!isCurrExpect(lotteryName, lotteryCrawlerStatus.getTimes())) {
|
||||
// logger.debug("不是最新的数据,10秒后继续抓...");
|
||||
// return false;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取开奖数据
|
||||
// * @param lotteryName
|
||||
// * @param date
|
||||
// * @return
|
||||
// */
|
||||
// private List<LotteryOpenCode> getOpenCode(String lotteryName, String date) {
|
||||
// switch (lotteryName) {
|
||||
// case "bjkl8":
|
||||
// return LecaiCrawlerUtil.getBjkl8OpenCode(lotteryName, date);
|
||||
// case "bjpk10":
|
||||
// return LecaiCrawlerUtil.getBjpk10OpenCode(lotteryName, date);
|
||||
// default:
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Autowired
|
||||
// @Qualifier(value = "highOpenTimeUtil")
|
||||
// private OpenTimeUtil highOpenTimeUtil;
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryDataFactory df;
|
||||
//
|
||||
// /**
|
||||
// * 判断是否是当前期
|
||||
// * @param lotteryName
|
||||
// * @return
|
||||
// */
|
||||
// public boolean isCurrExpect(String lotteryName, int times) {
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// String expect = highOpenTimeUtil.getCurrExpect(lotteryName, currTime);
|
||||
// String refLotteryName = lotteryName + "_ref"; // 获取坐标名称
|
||||
// List<LotteryOpenTime> opList = df.listLotteryOpenTime(refLotteryName);
|
||||
// LotteryOpenTime lotteryOpenTime = opList.get(0);
|
||||
// String refDate = lotteryOpenTime.getOpenTime();
|
||||
// int refExpect = Integer.parseInt(lotteryOpenTime.getExpect());
|
||||
// String currExpect = OpenTimeTransUtil.trans(expect, refDate, refExpect, times);
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao.get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// logger.debug("当前期:" + currExpect + " 最后一期:" + lastExpect);
|
||||
// return lastExpect.equals(currExpect);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取下一期
|
||||
// * @param lastExpect
|
||||
// * @return
|
||||
// */
|
||||
// public String getNextExpect(String lotteryName, String lastExpect, int times) {
|
||||
// int nextExpect = Integer.parseInt(lastExpect) + 1;
|
||||
// logger.debug("获取下一期:" + nextExpect);
|
||||
// String refLotteryName = lotteryName + "_ref"; // 获取坐标名称
|
||||
// List<LotteryOpenTime> opList = df.listLotteryOpenTime(refLotteryName);
|
||||
// LotteryOpenTime lotteryOpenTime = opList.get(0);
|
||||
// String refDate = lotteryOpenTime.getOpenTime();
|
||||
// int refExpect = Integer.parseInt(lotteryOpenTime.getExpect());
|
||||
// return OpenTimeTransUtil.trans(nextExpect, refDate, refExpect, times);
|
||||
// }
|
||||
// }
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
// package lottery.domains.capture.sites.lecai;
|
||||
//
|
||||
// import javautils.date.DateUtil;
|
||||
// import lottery.domains.capture.utils.open.OpenTimeUtil;
|
||||
// import lottery.domains.content.dao.LotteryCrawlerStatusDao;
|
||||
// import lottery.domains.content.dao.LotteryOpenCodeDao;
|
||||
// import lottery.domains.content.entity.LotteryCrawlerStatus;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import lottery.domains.content.pool.LotteryDataFactory;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
// import org.springframework.beans.factory.annotation.Autowired;
|
||||
// import org.springframework.beans.factory.annotation.Qualifier;
|
||||
// import org.springframework.stereotype.Component;
|
||||
//
|
||||
// import java.util.List;
|
||||
//
|
||||
// @Component
|
||||
// public class LecaiCrawlerPl3 {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(LecaiCrawlerPl3.class);
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryCrawlerStatusDao lotteryCrawlerStatusDao;
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryOpenCodeDao lotteryOpenCodeDao;
|
||||
//
|
||||
// public boolean start(String lotteryName) {
|
||||
// logger.debug("开始抓取" + lotteryName + "数据>>>>>>>>>>>>>>>>");
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao.get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// String expect = getNextExpect(lotteryName, lastExpect);
|
||||
// logger.debug("目标期数:" + expect);
|
||||
// List<LotteryOpenCode> list = getOpenCode(lotteryName, expect);
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// for (LotteryOpenCode lotteryOpenCode : list) {
|
||||
// // 设置数据来源
|
||||
// lotteryOpenCode.setRemarks("baidu.lecai.com");
|
||||
// String currExpect = lotteryOpenCode.getExpect();
|
||||
// // 如果大于上一期,插入
|
||||
// if(currExpect.compareTo(lastExpect) > 0) {
|
||||
// boolean isSucc = lotteryOpenCodeDao.add(lotteryOpenCode);
|
||||
// if(isSucc) {
|
||||
// // 更新状态,最后成功抓到数据的期数、时间
|
||||
// lotteryCrawlerStatusDao.update(lotteryName, currExpect, currTime);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // 如果不是当前期,那么就继续抓吧
|
||||
// if(!isCurrExpect(lotteryName)) {
|
||||
// logger.debug("不是最新的数据,1分钟后继续抓...");
|
||||
// return false;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取开奖数据
|
||||
// * @param lotteryName
|
||||
// * @param date
|
||||
// * @return
|
||||
// */
|
||||
// private List<LotteryOpenCode> getOpenCode(String lotteryName, String expect) {
|
||||
// switch (lotteryName) {
|
||||
// case "pl3":
|
||||
// return LecaiCrawlerUtil.getPl3OpenCode(lotteryName);
|
||||
// default:
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Autowired
|
||||
// @Qualifier(value = "lowOpenTimeUtil")
|
||||
// private OpenTimeUtil lowOpenTimeUtil;
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryDataFactory df;
|
||||
//
|
||||
// /**
|
||||
// * 判断是否是当前期
|
||||
// * @param lotteryName
|
||||
// * @return
|
||||
// */
|
||||
// public boolean isCurrExpect(String lotteryName) {
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// String currExpect = lowOpenTimeUtil.getCurrExpect(lotteryName, currTime);
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao.get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// logger.debug("当前期:" + currExpect + " 最后一期:" + lastExpect);
|
||||
// return lastExpect.equals(currExpect);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取下一期
|
||||
// * @param lastExpect
|
||||
// * @return
|
||||
// */
|
||||
// public String getNextExpect(String lotteryName, String lastExpect) {
|
||||
// return lowOpenTimeUtil.getNextExpect(lotteryName, lastExpect);
|
||||
// }
|
||||
// }
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
// package lottery.domains.capture.sites.lecai;
|
||||
//
|
||||
// import javautils.StringUtil;
|
||||
// import javautils.date.DateUtil;
|
||||
// import javautils.html.HtmlUtils;
|
||||
// import javautils.http.EasyHttpClient;
|
||||
// import lottery.domains.capture.utils.CodeValidate;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import net.sf.json.JSONObject;
|
||||
// import org.htmlparser.Node;
|
||||
// import org.htmlparser.util.NodeList;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
//
|
||||
// import java.util.*;
|
||||
//
|
||||
// public class LecaiCrawlerUtil {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(LecaiCrawlerUtil.class);
|
||||
// private static Map<String, EasyHttpClient> httpClientPool = new HashMap<>();
|
||||
//
|
||||
// private static EasyHttpClient getHttpClient(String lotteryName) {
|
||||
// if (!httpClientPool.containsKey(lotteryName)) {
|
||||
// httpClientPool.put(lotteryName, new EasyHttpClient());
|
||||
// }
|
||||
// return httpClientPool.get(lotteryName);
|
||||
// }
|
||||
//
|
||||
// private LecaiCrawlerUtil() {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getPl3OpenCode(String lotteryName) {
|
||||
// logger.info("获取排列3开奖号码....");
|
||||
// String url = "http://baidu.lecai.com/lottery/draw/list/3";
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// List<LotteryOpenCode> tmplist = getFc3dAndPl3(lotteryName, data);
|
||||
// List<LotteryOpenCode> list = new ArrayList<>();
|
||||
// for (LotteryOpenCode tmpBean : tmplist) {
|
||||
// if(CodeValidate.is3d(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getBjkl8OpenCode(String lotteryName, String date) {
|
||||
// logger.info("获取北京快乐8开奖号码....");
|
||||
// String url = "http://baidu.lecai.com/lottery/draw/list/543?d=" + date;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// List<LotteryOpenCode> tmplist = getFc3dAndPl3(lotteryName, data);
|
||||
// List<LotteryOpenCode> list = new ArrayList<>();
|
||||
// for (LotteryOpenCode tmpBean : tmplist) {
|
||||
// if(CodeValidate.isBjkl8(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getBjpk10OpenCode(String lotteryName, String date) {
|
||||
// logger.info("获取北京PK拾开奖号码....");
|
||||
// String url = "http://baidu.lecai.com/lottery/draw/list/557?d=" + date;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// List<LotteryOpenCode> tmplist = getFc3dAndPl3(lotteryName, data);
|
||||
// List<LotteryOpenCode> list = new ArrayList<>();
|
||||
// for (LotteryOpenCode tmpBean : tmplist) {
|
||||
// if(CodeValidate.isBjpk10(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
//
|
||||
// public static void main(String[] args) {
|
||||
// //List<LotteryOpenCode> lists = getBjkl8OpenCode("bjkl8", "2015-04-06");
|
||||
// //List<LotteryOpenCode> lists = getBjpk10OpenCode("bjpk10", "2015-04-06");
|
||||
// List<LotteryOpenCode> lists = getPl3OpenCode("pl3");
|
||||
// for (LotteryOpenCode lotteryOpenCode : lists) {
|
||||
// System.out.println(JSONObject.fromObject(lotteryOpenCode));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private static List<LotteryOpenCode> getFc3dAndPl3(String lotteryName, String data) {
|
||||
// int expectIndex = 1, ballsIndex = 2, codeLength = 1;
|
||||
// if("bjkl8".equals(lotteryName)) {
|
||||
// ballsIndex = 3;
|
||||
// codeLength = 2;
|
||||
// }
|
||||
// if("bjpk10".equals(lotteryName)) {
|
||||
// codeLength = 2;
|
||||
// }
|
||||
// List<LotteryOpenCode> list = new ArrayList<>();
|
||||
// Map<String, LotteryOpenCode> map = new HashMap<>();
|
||||
// if(StringUtil.isNotNull(data)) {
|
||||
// try {
|
||||
// String resource = data.replaceAll("(\t|\r|\n)", "");
|
||||
// NodeList nList = HtmlUtils.FilterTag(resource, "table", "UTF-8");
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// if(nList.size() > 0) {
|
||||
// NodeList trList = HtmlUtils.FilterTag(nList.elementAt(0).toString(), "tr", "UTF-8");
|
||||
// Node[] nodes = trList.toNodeArray();
|
||||
// for (int i = 1; i < nodes.length; i++) {
|
||||
// LotteryOpenCode bean = new LotteryOpenCode();
|
||||
// bean.setLottery(lotteryName);
|
||||
// bean.setTime(currTime);
|
||||
//
|
||||
// NodeList tds = HtmlUtils.FilterTag(nodes[i].toHtml(), "td", "UTF-8");
|
||||
// String expect = tds.elementAt(expectIndex).toPlainTextString().replaceAll(" ", "");
|
||||
// NodeList balls = HtmlUtils.FilterCss(tds.elementAt(ballsIndex).toHtml(), ".ball_1", "UTF-8");
|
||||
//
|
||||
// String code = transCode(balls.asString(), codeLength);
|
||||
// bean.setExpect(expect);
|
||||
// bean.setCode(code);
|
||||
// map.put(expect, bean);
|
||||
// }
|
||||
// }
|
||||
// Object[] keys = map.keySet().toArray();
|
||||
// Arrays.sort(keys);
|
||||
// for (Object o : keys) {
|
||||
// list.add(map.get(o));
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("解析数据失败...", e);
|
||||
// }
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
//
|
||||
// private static String transCode(String code, int cLen) {
|
||||
// StringBuffer sb = new StringBuffer();
|
||||
// for (int i = 0, j = code.length()/cLen; i < j; i++) {
|
||||
// sb.append(code.substring(i * cLen, (i + 1) * cLen));
|
||||
// if(i < j - 1) {
|
||||
// sb.append(",");
|
||||
// }
|
||||
// }
|
||||
// return sb.toString();
|
||||
// }
|
||||
//
|
||||
// }
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package lottery.domains.capture.sites.opencai;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author ROOT
|
||||
*
|
||||
*/
|
||||
public class OpenCaiBean {
|
||||
|
||||
private String code; // 菜种
|
||||
private String expect;// 期号
|
||||
private String opencode;// 开奖号码
|
||||
private String opentime;// 开奖时间
|
||||
private String opentimestamp;// 时间搓
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getExpect() {
|
||||
return expect;
|
||||
}
|
||||
|
||||
public void setExpect(String expect) {
|
||||
this.expect = expect;
|
||||
}
|
||||
|
||||
public String getOpencode() {
|
||||
return opencode;
|
||||
}
|
||||
|
||||
public void setOpencode(String opencode) {
|
||||
this.opencode = opencode;
|
||||
}
|
||||
|
||||
public String getOpentime() {
|
||||
return opentime;
|
||||
}
|
||||
|
||||
public void setOpentime(String opentime) {
|
||||
this.opentime = opentime;
|
||||
}
|
||||
|
||||
public String getOpentimestamp() {
|
||||
return opentimestamp;
|
||||
}
|
||||
|
||||
public void setOpentimestamp(String opentimestamp) {
|
||||
this.opentimestamp = opentimestamp;
|
||||
}
|
||||
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package lottery.domains.capture.sites.opencai;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Iterator;
|
||||
import net.sf.json.JSONException;
|
||||
import net.sf.json.JSONObject;
|
||||
|
||||
public class OpenCaiUtils {
|
||||
public static void main(String[] args) {
|
||||
// 此处需要传递的3个参数,必填项
|
||||
String name = "";// 彩种
|
||||
String uid = "";// 用户ID
|
||||
String token = "5332B2C452B7E697";// token
|
||||
|
||||
String url = "http://api.caipiaokong.com/lottery/";
|
||||
url += "?name=" + name;
|
||||
url += "&format=json";// 数据格式,此文件仅支持json
|
||||
url += "&uid=" + uid;
|
||||
url += "&token=" + token;
|
||||
|
||||
url = "http://c.apiplus.cn/newly.do?token=5332B2C452B7E697&rows=3&format=json";
|
||||
|
||||
String urlAll = new StringBuffer(url).toString();
|
||||
String charset = "UTF-8";
|
||||
String jsonResult = get(urlAll, charset);// 得到JSON字符串
|
||||
JSONObject object = JSONObject.fromObject(jsonResult);// 转化为JSON类
|
||||
try {
|
||||
Iterator it = object.keys();
|
||||
while (it.hasNext()) {
|
||||
String key = (String) it.next();
|
||||
String value = object.getString(key);
|
||||
JSONObject object1 = JSONObject.fromObject(value);// 转化为JSON类
|
||||
String outputStr = "id:" + key;
|
||||
outputStr += " number:" + object1.getString("number");
|
||||
outputStr += " dateline:" + object1.getString("dateline");
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param urlAll
|
||||
* :请求接口
|
||||
* @param charset
|
||||
* :字符编码
|
||||
* @return 返回json结果
|
||||
*/
|
||||
public static String get(String urlAll, String charset) {
|
||||
BufferedReader reader = null;
|
||||
String result = null;
|
||||
StringBuffer sbf = new StringBuffer();
|
||||
String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";// 模拟浏览器
|
||||
try {
|
||||
URL url = new URL(urlAll);
|
||||
HttpURLConnection connection = (HttpURLConnection) url
|
||||
.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
connection.setReadTimeout(30000);
|
||||
connection.setConnectTimeout(30000);
|
||||
connection.setRequestProperty("User-agent", userAgent);
|
||||
connection.connect();
|
||||
InputStream is = connection.getInputStream();
|
||||
reader = new BufferedReader(new InputStreamReader(is, charset));
|
||||
String strRead = null;
|
||||
while ((strRead = reader.readLine()) != null) {
|
||||
sbf.append(strRead);
|
||||
sbf.append("\r\n");
|
||||
}
|
||||
reader.close();
|
||||
result = sbf.toString();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package lottery.domains.capture.sites.pcqq;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import javautils.date.Moment;
|
||||
|
||||
/**
|
||||
* Created by Nick on 2017-07-28.
|
||||
*/
|
||||
public class PCQQBean {
|
||||
@JSONField(deserialize = false)
|
||||
private String onlinetime = new Moment().toSimpleTime();
|
||||
@JSONField(name = "c")
|
||||
private int onlinenumber;
|
||||
@JSONField(deserialize = false)
|
||||
private int onlinechange;
|
||||
|
||||
public String getOnlinetime() {
|
||||
return onlinetime;
|
||||
}
|
||||
|
||||
public void setOnlinetime(String onlinetime) {
|
||||
this.onlinetime = onlinetime;
|
||||
}
|
||||
|
||||
public int getOnlinenumber() {
|
||||
return onlinenumber;
|
||||
}
|
||||
|
||||
public void setOnlinenumber(int onlinenumber) {
|
||||
this.onlinenumber = onlinenumber;
|
||||
}
|
||||
|
||||
public int getOnlinechange() {
|
||||
return onlinechange;
|
||||
}
|
||||
|
||||
public void setOnlinechange(int onlinechange) {
|
||||
this.onlinechange = onlinechange;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package lottery.domains.capture.sites.qqtj;
|
||||
|
||||
/**
|
||||
* Created by Nick on 2017-07-28.
|
||||
*/
|
||||
public class QQTJBean {
|
||||
private String onlinetime;
|
||||
private int onlinenumber;
|
||||
private int onlinechange;
|
||||
|
||||
public String getOnlinetime() {
|
||||
return onlinetime;
|
||||
}
|
||||
|
||||
public void setOnlinetime(String onlinetime) {
|
||||
this.onlinetime = onlinetime;
|
||||
}
|
||||
|
||||
public int getOnlinenumber() {
|
||||
return onlinenumber;
|
||||
}
|
||||
|
||||
public void setOnlinenumber(int onlinenumber) {
|
||||
this.onlinenumber = onlinenumber;
|
||||
}
|
||||
|
||||
public int getOnlinechange() {
|
||||
return onlinechange;
|
||||
}
|
||||
|
||||
public void setOnlinechange(int onlinechange) {
|
||||
this.onlinechange = onlinechange;
|
||||
}
|
||||
}
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
// package lottery.domains.capture.sites.shishicai;
|
||||
//
|
||||
// import javautils.date.DateUtil;
|
||||
// import lottery.domains.capture.utils.open.OpenTimeUtil;
|
||||
// import lottery.domains.content.dao.LotteryCrawlerStatusDao;
|
||||
// import lottery.domains.content.dao.LotteryOpenCodeDao;
|
||||
// import lottery.domains.content.entity.LotteryCrawlerStatus;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
// import org.springframework.beans.factory.annotation.Autowired;
|
||||
// import org.springframework.beans.factory.annotation.Qualifier;
|
||||
// import org.springframework.stereotype.Component;
|
||||
//
|
||||
// import java.util.List;
|
||||
//
|
||||
// @Component
|
||||
// public class ShiShiCaiCrawler {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(ShiShiCaiCrawler.class);
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryCrawlerStatusDao lotteryCrawlerStatusDao;
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryOpenCodeDao lotteryOpenCodeDao;
|
||||
//
|
||||
// public boolean start(String lotteryName) {
|
||||
// logger.debug("开始抓取" + lotteryName + "数据>>>>>>>>>>>>>>>>");
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao.get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// String date = DateUtil.formatTime(lastExpect.substring(0, 8), "yyyyMMdd", "yyyy-MM-dd");
|
||||
// String expect = lastExpect.substring(9);
|
||||
// // 如果是最后一期,那么就抓明天的
|
||||
// if(lotteryCrawlerStatus.getTimes() == Integer.parseInt(expect)) {
|
||||
// date = DateUtil.calcNextDay(date);
|
||||
// }
|
||||
// logger.debug("目标日期:" + date);
|
||||
// List<LotteryOpenCode> list = getOpenCode(lotteryName, date);
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// for (LotteryOpenCode lotteryOpenCode : list) {
|
||||
// // 设置数据来源
|
||||
// lotteryOpenCode.setRemarks("shishicai.cn");
|
||||
// String currExpect = lotteryOpenCode.getExpect();
|
||||
// // 如果大于上一期,插入
|
||||
// if(currExpect.compareTo(lastExpect) > 0) {
|
||||
// boolean isSucc = lotteryOpenCodeDao.add(lotteryOpenCode);
|
||||
// if(isSucc) {
|
||||
// // 更新状态,最后成功抓到数据的期数、时间
|
||||
// lotteryCrawlerStatusDao.update(lotteryName, currExpect, currTime);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // 如果不是当前期,那么就继续抓吧
|
||||
// if(!isCurrExpect(lotteryName)) {
|
||||
// logger.debug("不是最新的数据,10秒后继续抓...");
|
||||
// return false;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取开奖数据
|
||||
// * @param lotteryName
|
||||
// * @param date
|
||||
// * @return
|
||||
// */
|
||||
// private List<LotteryOpenCode> getOpenCode(String lotteryName, String date) {
|
||||
// switch (lotteryName) {
|
||||
// case "cqssc":
|
||||
// return ShiShiCaiCrawlerUtil.getCqsscOpenCode(lotteryName, date);
|
||||
// // case "jxssc":
|
||||
// // return ShiShiCaiCrawlerUtil.getJxsscOpenCode(lotteryName, date);
|
||||
// case "gd11x5":
|
||||
// return ShiShiCaiCrawlerUtil.getGd11x5OpenCode(lotteryName, date);
|
||||
// case "jx11x5":
|
||||
// return ShiShiCaiCrawlerUtil.getJx11x5OpenCode(lotteryName, date);
|
||||
// case "cq11x5":
|
||||
// return ShiShiCaiCrawlerUtil.getCq11x5OpenCode(lotteryName, date);
|
||||
// case "ah11x5":
|
||||
// return ShiShiCaiCrawlerUtil.getAh11x5OpenCode(lotteryName, date);
|
||||
// case "sh11x5":
|
||||
// return ShiShiCaiCrawlerUtil.getSh11x5OpenCode(lotteryName, date);
|
||||
// case "sd11x5":
|
||||
// return ShiShiCaiCrawlerUtil.getSd11x5OpenCode(lotteryName, date);
|
||||
// case "jsk3":
|
||||
// return ShiShiCaiCrawlerUtil.getJsk3OpenCode(lotteryName, date);
|
||||
// case "ahk3":
|
||||
// return ShiShiCaiCrawlerUtil.getAhk3OpenCode(lotteryName, date);
|
||||
// case "hbk3":
|
||||
// return ShiShiCaiCrawlerUtil.getHbk3OpenCode(lotteryName, date);
|
||||
// case "jlk3":
|
||||
// return ShiShiCaiCrawlerUtil.getJlk3OpenCode(lotteryName, date);
|
||||
// default:
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Autowired
|
||||
// @Qualifier(value = "highOpenTimeUtil")
|
||||
// private OpenTimeUtil highOpenTimeUtil;
|
||||
//
|
||||
// /**
|
||||
// * 判断是否是当前期
|
||||
// * @param lotteryName
|
||||
// * @return
|
||||
// */
|
||||
// public boolean isCurrExpect(String lotteryName) {
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// String currExpect = highOpenTimeUtil.getCurrExpect(lotteryName, currTime);
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao.get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// logger.debug("当前期:" + currExpect + " 最后一期:" + lastExpect);
|
||||
// return lastExpect.equals(currExpect);
|
||||
// }
|
||||
// }
|
||||
+289
@@ -0,0 +1,289 @@
|
||||
// package lottery.domains.capture.sites.shishicai;
|
||||
//
|
||||
// import javautils.StringUtil;
|
||||
// import javautils.date.DateUtil;
|
||||
// import lottery.domains.capture.utils.CodeValidate;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import net.sf.json.JSONArray;
|
||||
// import net.sf.json.JSONObject;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
//
|
||||
// import java.util.*;
|
||||
//
|
||||
// public class ShiShiCaiCrawlerUtil {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(ShiShiCaiCrawlerUtil.class);
|
||||
// private static Map<String, ShiShiCaiHttpClient> httpClientPool = new HashMap<>();
|
||||
//
|
||||
// private static ShiShiCaiHttpClient getHttpClient(String lotteryName) {
|
||||
// if (!httpClientPool.containsKey(lotteryName)) {
|
||||
// httpClientPool.put(lotteryName,
|
||||
// new ShiShiCaiHttpClient(lotteryName));
|
||||
// }
|
||||
// return httpClientPool.get(lotteryName);
|
||||
// }
|
||||
//
|
||||
// private ShiShiCaiCrawlerUtil() {
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getCqsscOpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.debug("获取重庆时时彩开奖号码,日期为:" + date);
|
||||
// String getUrl = "http://data.shishicai.cn/cqssc/haoma/";
|
||||
// String postUrl = "http://data.shishicai.cn/handler/kuaikai/data.ashx";
|
||||
// String lottery = "4";
|
||||
// String type = "ssc";
|
||||
// ShiShiCaiHttpClient client = getHttpClient(lotteryName);
|
||||
// String data = client.handleData(getUrl, postUrl, lottery, date);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getJxsscOpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.debug("获取江西时时彩开奖号码....");
|
||||
// String getUrl = "http://data.shishicai.cn/jxssc/haoma/";
|
||||
// String postUrl = "http://data.shishicai.cn/handler/kuaikai/data.ashx";
|
||||
// String lottery = "5";
|
||||
// String type = "ssc";
|
||||
// ShiShiCaiHttpClient client = getHttpClient(lotteryName);
|
||||
// String data = client.handleData(getUrl, postUrl, lottery, date);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getSd11x5OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.debug("获取山东11选5开奖号码....");
|
||||
// String getUrl = "http://data.shishicai.cn/sd11x5/haoma/";
|
||||
// String postUrl = "http://data.shishicai.cn/handler/kuaikai/data.ashx";
|
||||
// String lottery = "16";
|
||||
// String type = "11x5";
|
||||
// ShiShiCaiHttpClient client = getHttpClient(lotteryName);
|
||||
// String data = client.handleData(getUrl, postUrl, lottery, date);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getGd11x5OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.debug("获取广东11选5开奖号码....");
|
||||
// String getUrl = "http://data.shishicai.cn/gd11x5/haoma/";
|
||||
// String postUrl = "http://data.shishicai.cn/handler/kuaikai/data.ashx";
|
||||
// String lottery = "24";
|
||||
// String type = "11x5";
|
||||
// ShiShiCaiHttpClient client = getHttpClient(lotteryName);
|
||||
// String data = client.handleData(getUrl, postUrl, lottery, date);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getJx11x5OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.debug("获取江西11选5开奖号码....");
|
||||
// String getUrl = "http://data.shishicai.cn/jx11x5/haoma/";
|
||||
// String postUrl = "http://data.shishicai.cn/handler/kuaikai/data.ashx";
|
||||
// String lottery = "23";
|
||||
// String type = "11x5";
|
||||
// ShiShiCaiHttpClient client = getHttpClient(lotteryName);
|
||||
// String data = client.handleData(getUrl, postUrl, lottery, date);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getCq11x5OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.debug("获取重庆11选5开奖号码....");
|
||||
// String getUrl = "http://data.shishicai.cn/cq11x5/haoma/";
|
||||
// String postUrl = "http://data.shishicai.cn/handler/kuaikai/data.ashx";
|
||||
// String lottery = "36";
|
||||
// String type = "11x5";
|
||||
// ShiShiCaiHttpClient client = getHttpClient(lotteryName);
|
||||
// String data = client.handleData(getUrl, postUrl, lottery, date);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getAh11x5OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.debug("获取安徽11选5开奖号码....");
|
||||
// String getUrl = "http://data.shishicai.cn/ah11x5/haoma/";
|
||||
// String postUrl = "http://data.shishicai.cn/handler/kuaikai/data.ashx";
|
||||
// String lottery = "38";
|
||||
// String type = "11x5";
|
||||
// ShiShiCaiHttpClient client = getHttpClient(lotteryName);
|
||||
// String data = client.handleData(getUrl, postUrl, lottery, date);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getSh11x5OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.debug("获取上海11选5开奖号码....");
|
||||
// String getUrl = "http://data.shishicai.cn/sh11x5/haoma/";
|
||||
// String postUrl = "http://data.shishicai.cn/handler/kuaikai/data.ashx";
|
||||
// String lottery = "37";
|
||||
// String type = "11x5";
|
||||
// ShiShiCaiHttpClient client = getHttpClient(lotteryName);
|
||||
// String data = client.handleData(getUrl, postUrl, lottery, date);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getJsk3OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.debug("获取江苏快3开奖号码....");
|
||||
// String getUrl = "http://data.shishicai.cn/jsk3/haoma/";
|
||||
// String postUrl = "http://data.shishicai.cn/handler/kuaikai/data.ashx";
|
||||
// String lottery = "3";
|
||||
// String type = "k3";
|
||||
// ShiShiCaiHttpClient client = getHttpClient(lotteryName);
|
||||
// String data = client.handleData(getUrl, postUrl, lottery, date);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getAhk3OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.debug("获取安徽快3开奖号码....");
|
||||
// String getUrl = "http://data.shishicai.cn/ahk3/haoma/";
|
||||
// String postUrl = "http://data.shishicai.cn/handler/kuaikai/data.ashx";
|
||||
// String lottery = "32";
|
||||
// String type = "k3";
|
||||
// ShiShiCaiHttpClient client = getHttpClient(lotteryName);
|
||||
// String data = client.handleData(getUrl, postUrl, lottery, date);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getHbk3OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.debug("获取湖北快3开奖号码....");
|
||||
// String getUrl = "http://data.shishicai.cn/hbk3/haoma/";
|
||||
// String postUrl = "http://data.shishicai.cn/handler/kuaikai/data.ashx";
|
||||
// String lottery = "31";
|
||||
// String type = "k3";
|
||||
// ShiShiCaiHttpClient client = getHttpClient(lotteryName);
|
||||
// String data = client.handleData(getUrl, postUrl, lottery, date);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getJlk3OpenCode(
|
||||
// String lotteryName, String date) {
|
||||
// logger.debug("获取吉林快3开奖号码....");
|
||||
// String getUrl = "http://data.shishicai.cn/jlk3/haoma/";
|
||||
// String postUrl = "http://data.shishicai.cn/handler/kuaikai/data.ashx";
|
||||
// String lottery = "39";
|
||||
// String type = "k3";
|
||||
// ShiShiCaiHttpClient client = getHttpClient(lotteryName);
|
||||
// String data = client.handleData(getUrl, postUrl, lottery, date);
|
||||
// return handleData(lotteryName, type, data);
|
||||
// }
|
||||
//
|
||||
// public static void main(String[] args) throws InterruptedException {
|
||||
// List<LotteryOpenCode> list = getCq11x5OpenCode("cq11x5", "2015-03-18");
|
||||
// for (LotteryOpenCode lotteryOpenCode : list) {
|
||||
// System.out.println(JSONObject.fromObject(lotteryOpenCode));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private static List<LotteryOpenCode> handleData(String lotteryName,
|
||||
// String type, String data) {
|
||||
// List<LotteryOpenCode> list = new ArrayList<>();
|
||||
// if (StringUtil.isNotNull(data)) {
|
||||
// try {
|
||||
// Map<String, LotteryOpenCode> codeMap = new HashMap<>();
|
||||
// JSONArray jsonList = JSONArray.fromObject(data);
|
||||
// int length = jsonList.size();
|
||||
// String decodeStr = (String) jsonList.get(length - 1);
|
||||
// Map<String, String> decodeMap = getDecodeMap(decodeStr, type);
|
||||
// for (int i = 0; i < length - 1; i++) {
|
||||
// String[] s = String.valueOf(jsonList.get(i)).split(";");
|
||||
// String expect = s[0];
|
||||
// String code = decodeNumber(s[1], decodeMap, type);
|
||||
// String time = DateUtil.getCurrentTime();
|
||||
// LotteryOpenCode bean = new LotteryOpenCode(lotteryName,
|
||||
// expect, code, time, 0);
|
||||
// codeMap.put(expect, bean);
|
||||
// }
|
||||
// Object[] keys = codeMap.keySet().toArray();
|
||||
// Arrays.sort(keys);
|
||||
// for (Object o : keys) {
|
||||
// LotteryOpenCode tmpBean = codeMap.get(o);
|
||||
// switch (type) {
|
||||
// case "ssc":
|
||||
// if (CodeValidate.isSsc(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// break;
|
||||
// case "11x5":
|
||||
// if (CodeValidate.is11x5(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// break;
|
||||
// case "k3":
|
||||
// if (CodeValidate.isK3(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// break;
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("解析数据失败...", e);
|
||||
// }
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
//
|
||||
// private static Map<String, String> getDecodeMap(String s, String t) {
|
||||
// Map<String, String> map = new HashMap<>();
|
||||
// try {
|
||||
// String[] a = s.substring(0, s.indexOf("_")).split(",");
|
||||
// switch (t) {
|
||||
// case "11x5":
|
||||
// for (int i = 0; i < a.length; i++) {
|
||||
// String key = String.format("%02d", Integer.parseInt(a[i]));
|
||||
// String value = String.format("%02d", i + 1);
|
||||
// map.put(key, value);
|
||||
// }
|
||||
// break;
|
||||
// case "ssc":
|
||||
// for (int i = 0; i < a.length; i++) {
|
||||
// String key = String.valueOf(a[i]);
|
||||
// String val = String.valueOf(i);
|
||||
// map.put(key, val);
|
||||
// }
|
||||
// break;
|
||||
// case "k3":
|
||||
// for (int i = 0; i < a.length; i++) {
|
||||
// String key = String.valueOf(a[i]);
|
||||
// String val = String.valueOf(i + 1);
|
||||
// map.put(key, val);
|
||||
// }
|
||||
// break;
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// }
|
||||
// return map;
|
||||
// }
|
||||
//
|
||||
// private static String decodeNumber(String s, Map<String, String> m, String t) {
|
||||
// StringBuffer sb = new StringBuffer();
|
||||
// switch (t) {
|
||||
// case "11x5":
|
||||
// String[] a = s.split(",");
|
||||
// for (int i = 0, l = a.length; i < l; i++) {
|
||||
// sb.append(m.get(a[i]));
|
||||
// if (i != l - 1)
|
||||
// sb.append(",");
|
||||
// }
|
||||
// break;
|
||||
// case "ssc":
|
||||
// case "k3":
|
||||
// for (int i = 0, l = s.length(); i < l; i++) {
|
||||
// sb.append(m.get(s.substring(i, i + 1)));
|
||||
// if (i != l - 1)
|
||||
// sb.append(",");
|
||||
// }
|
||||
// break;
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
// return sb.toString();
|
||||
// }
|
||||
// }
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
// package lottery.domains.capture.sites.shishicai;
|
||||
//
|
||||
// import javautils.StringUtil;
|
||||
// import org.apache.commons.httpclient.*;
|
||||
// import org.apache.commons.httpclient.methods.GetMethod;
|
||||
// import org.apache.commons.httpclient.methods.PostMethod;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
//
|
||||
// import java.net.SocketTimeoutException;
|
||||
//
|
||||
// public class ShiShiCaiHttpClient {
|
||||
//
|
||||
// private Logger logger;
|
||||
//
|
||||
// private HttpClient httpClient;
|
||||
// private final int TIMEOUT = 10 * 1000;
|
||||
// private final int SO_TIMEOUT = 10 * 1000;
|
||||
// private final int REPEAT_TIMES = 3;
|
||||
//
|
||||
// // 刷新Cookie用
|
||||
// private final int COOKIE_TIMEOUT = 1 * 60 * 60 * 1000;
|
||||
// private long lastUpdate;
|
||||
//
|
||||
// public ShiShiCaiHttpClient(String name) {
|
||||
// logger = LoggerFactory.getLogger(ShiShiCaiHttpClient.class.getSimpleName() + "-" + name.toUpperCase());
|
||||
// httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
|
||||
// httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(TIMEOUT);
|
||||
// httpClient.getHttpConnectionManager().getParams().setSoTimeout(SO_TIMEOUT);
|
||||
// }
|
||||
//
|
||||
// private boolean handleCookie(String getUrl) {
|
||||
// GetMethod get = new GetMethod(getUrl);
|
||||
// for (int i = 0; i < REPEAT_TIMES; i++) {
|
||||
// logger.debug("正在请求Cookie,当前第" + (i + 1) + "次...");
|
||||
// try {
|
||||
// int state = httpClient.executeMethod(get);
|
||||
// logger.debug("请求状态" + state);
|
||||
// if(state == HttpStatus.SC_OK) {
|
||||
// Cookie[] cookies = httpClient.getState().getCookies();
|
||||
// for (Cookie cookie : cookies) {
|
||||
// if(cookie.getName().indexOf("Html_v") != -1) {
|
||||
// logger.debug("请求Cookie成功了...");
|
||||
// lastUpdate = System.currentTimeMillis();
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// } catch(ConnectTimeoutException e) {
|
||||
// logger.error("请求时时彩数据超时了...Connect Timeout");
|
||||
// } catch(SocketTimeoutException e) {
|
||||
// logger.error("请求时时彩数据超时了...Socket Timeout");
|
||||
// } catch (Exception e) {
|
||||
// logger.error("请求Cookie出错了...", e);
|
||||
// } finally {
|
||||
// closeConnection(get);
|
||||
// }
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// private String getCookie(String getUrl, String date) {
|
||||
// logger.debug("获取Cookie数据...");
|
||||
// if(System.currentTimeMillis() > lastUpdate + COOKIE_TIMEOUT) {
|
||||
// logger.debug("Cookie过期了,重新获取Cookie...");
|
||||
// boolean flag = handleCookie(getUrl);
|
||||
// if(flag) {
|
||||
// return getCookie(getUrl, date);
|
||||
// }
|
||||
// }
|
||||
// StringBuffer sb = new StringBuffer();
|
||||
// Cookie[] cookies = httpClient.getState().getCookies();
|
||||
// for (Cookie cookie : cookies) {
|
||||
// if(cookie.getName().indexOf("Html_v") != -1) {
|
||||
// sb.append(cookie.toString());
|
||||
// }
|
||||
// }
|
||||
// if(!StringUtil.isNotNull(sb.toString())) {
|
||||
// logger.debug("没有Cookie数据...");
|
||||
// boolean flag = handleCookie(getUrl);
|
||||
// if(flag) {
|
||||
// return getCookie(getUrl, date);
|
||||
// }
|
||||
// }
|
||||
// sb.append(";ssc_user_LandingPage=" + getUrl + date + "/");
|
||||
// sb.append(";ssc_user_RegEnterPage=" + getUrl + date + "/");
|
||||
// return sb.toString();
|
||||
// }
|
||||
//
|
||||
// public String handleData(String getUrl, String postUrl, String lottery, String date) {
|
||||
// StringBuffer sb = new StringBuffer();
|
||||
// String cookie = getCookie(getUrl, date);
|
||||
// if(StringUtil.isNotNull(cookie)) {
|
||||
// PostMethod post = new PostMethod(postUrl);
|
||||
// post.setRequestHeader("Referer", getUrl);
|
||||
// post.setRequestHeader("Cookie", cookie);
|
||||
// NameValuePair[] params = { new NameValuePair("lottery", lottery), new NameValuePair("date", date) };
|
||||
// post.setRequestBody(params);
|
||||
// for (int i = 0; i < REPEAT_TIMES; i++) {
|
||||
// logger.debug("正在请求开奖数据,当前第" + (i + 1) + "次...");
|
||||
// try {
|
||||
// int state = httpClient.executeMethod(post);
|
||||
// logger.debug("请求状态" + state);
|
||||
// if(state == HttpStatus.SC_OK) {
|
||||
// sb.append(post.getResponseBodyAsString());
|
||||
// logger.debug("成功获取到开奖数据,长度为" + sb.length());
|
||||
// return sb.toString();
|
||||
// }
|
||||
// } catch(ConnectTimeoutException e) {
|
||||
// logger.error("请求时时彩数据超时了...Connect Timeout");
|
||||
// } catch(SocketTimeoutException e) {
|
||||
// logger.error("请求时时彩数据超时了...Socket Timeout");
|
||||
// } catch (Exception e) {
|
||||
// logger.error("请求开奖数据出错了...", e);
|
||||
// } finally {
|
||||
// closeConnection(post);
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// logger.debug("没有Cookie...无法获取数据...");
|
||||
// }
|
||||
// return sb.toString();
|
||||
// }
|
||||
//
|
||||
// private void closeConnection(HttpMethod method) {
|
||||
// method.releaseConnection();
|
||||
// MultiThreadedHttpConnectionManager manager = (MultiThreadedHttpConnectionManager) httpClient.getHttpConnectionManager();
|
||||
// manager.closeIdleConnections(0);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
// package lottery.domains.capture.sites.tjflcpw;
|
||||
//
|
||||
// import javautils.date.DateUtil;
|
||||
// import lottery.domains.capture.utils.open.OpenTimeUtil;
|
||||
// import lottery.domains.content.dao.LotteryCrawlerStatusDao;
|
||||
// import lottery.domains.content.dao.LotteryOpenCodeDao;
|
||||
// import lottery.domains.content.entity.LotteryCrawlerStatus;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
// import org.springframework.beans.factory.annotation.Autowired;
|
||||
// import org.springframework.beans.factory.annotation.Qualifier;
|
||||
// import org.springframework.stereotype.Component;
|
||||
//
|
||||
// import java.util.List;
|
||||
//
|
||||
// @Component
|
||||
// public class TjflcpwCrawler {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(TjflcpwCrawler.class);
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryCrawlerStatusDao lotteryCrawlerStatusDao;
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryOpenCodeDao lotteryOpenCodeDao;
|
||||
//
|
||||
// public boolean start(String lotteryName) {
|
||||
// logger.debug("开始抓取" + lotteryName + "数据>>>>>>>>>>>>>>>>");
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao.get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// String nextExpect = getNextExpect(lastExpect, lotteryCrawlerStatus.getTimes());
|
||||
// List<LotteryOpenCode> list = TjflcpwCrawlerUtil.getTjsscOpenCode(lotteryName, nextExpect);
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// for (LotteryOpenCode lotteryOpenCode : list) {
|
||||
// // 设置数据来源
|
||||
// lotteryOpenCode.setRemarks("www.tjflcpw.com");
|
||||
// String currExpect = lotteryOpenCode.getExpect();
|
||||
// // 如果大于上一期,插入
|
||||
// if(currExpect.compareTo(lastExpect) > 0) {
|
||||
// boolean isSucc = lotteryOpenCodeDao.add(lotteryOpenCode);
|
||||
// if(isSucc) {
|
||||
// // 更新状态,最后成功抓到数据的期数、时间
|
||||
// lotteryCrawlerStatusDao.update(lotteryName, currExpect, currTime);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // 如果不是当前期,那么就继续抓吧
|
||||
// if(!isCurrExpect(lotteryName)) {
|
||||
// logger.debug("不是最新的数据,10秒后继续抓...");
|
||||
// return false;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// @Autowired
|
||||
// @Qualifier(value = "highOpenTimeUtil")
|
||||
// private OpenTimeUtil highOpenTimeUtil;
|
||||
//
|
||||
// /**
|
||||
// * 判断是否是当前期
|
||||
// * @param lotteryName
|
||||
// * @return
|
||||
// */
|
||||
// public boolean isCurrExpect(String lotteryName) {
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// String currExpect = highOpenTimeUtil.getCurrExpect(lotteryName, currTime);
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao.get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// logger.debug("当前期:" + currExpect + " 最后一期:" + lastExpect);
|
||||
// return lastExpect.equals(currExpect);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取下一期期号
|
||||
// * @param expect
|
||||
// * @param times
|
||||
// * @return
|
||||
// */
|
||||
// public String getNextExpect(String expect, int times) {
|
||||
// String date = expect.split("-")[0];
|
||||
// int num = Integer.parseInt(expect.split("-")[1]);
|
||||
// if(num == times) {
|
||||
// num = 1;
|
||||
// String d = DateUtil.formatTime(date, "yyyyMMdd", "yyyy-MM-dd");
|
||||
// date = DateUtil.calcNextDay(d);
|
||||
// date = DateUtil.formatTime(date, "yyyy-MM-dd", "yyyyMMdd");
|
||||
// } else {
|
||||
// num++;
|
||||
// }
|
||||
// return date + "-" + String.format("%03d", num);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
// package lottery.domains.capture.sites.tjflcpw;
|
||||
//
|
||||
// import javautils.StringUtil;
|
||||
// import javautils.date.DateUtil;
|
||||
// import javautils.http.EasyHttpClient;
|
||||
// import lottery.domains.capture.utils.CodeValidate;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import net.sf.json.JSONArray;
|
||||
// import net.sf.json.JSONObject;
|
||||
// import org.apache.commons.httpclient.NameValuePair;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
//
|
||||
// import java.util.ArrayList;
|
||||
// import java.util.HashMap;
|
||||
// import java.util.List;
|
||||
// import java.util.Map;
|
||||
//
|
||||
// public class TjflcpwCrawlerUtil {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(TjflcpwCrawlerUtil.class);
|
||||
// private static Map<String, EasyHttpClient> httpClientPool = new HashMap<>();
|
||||
//
|
||||
// private static EasyHttpClient getHttpClient(String lotteryName) {
|
||||
// if (!httpClientPool.containsKey(lotteryName)) {
|
||||
// httpClientPool.put(lotteryName, new EasyHttpClient());
|
||||
// }
|
||||
// return httpClientPool.get(lotteryName);
|
||||
// }
|
||||
//
|
||||
// private TjflcpwCrawlerUtil() {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getTjsscOpenCode(
|
||||
// String lotteryName, String expect) {
|
||||
// logger.info("获取天津时时彩开奖号码....");
|
||||
// String url = "http://www.tjflcpw.com/Handlers/WinNumHandler.ashx";
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String _expect = expect.substring(2).replace("-", "");
|
||||
// System.out.println(_expect);
|
||||
// NameValuePair[] params = { new NameValuePair("termCode", _expect),
|
||||
// new NameValuePair("playid", "4") };
|
||||
// String data = httpClient.post(url, params);
|
||||
// List<LotteryOpenCode> tmplist = getOpenCode(lotteryName, expect, data);
|
||||
// List<LotteryOpenCode> list = new ArrayList<>();
|
||||
// for (LotteryOpenCode tmpBean : tmplist) {
|
||||
// if (CodeValidate.isSsc(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
//
|
||||
// public static void main(String[] args) {
|
||||
// System.out.println(JSONArray.fromObject(getTjsscOpenCode("tjssc",
|
||||
// "20150317-001")));
|
||||
// System.out.println(JSONArray.fromObject(getTjsscOpenCode("tjssc",
|
||||
// "20150317-001")));
|
||||
// }
|
||||
//
|
||||
// private static List<LotteryOpenCode> getOpenCode(String lotteryName,
|
||||
// String expect, String data) {
|
||||
// List<LotteryOpenCode> list = new ArrayList<>();
|
||||
// if (StringUtil.isNotNull(data)) {
|
||||
// JSONObject json = JSONObject.fromObject(data);
|
||||
// if (!json.get("Data").equals("null")) {
|
||||
// String code = json.getJSONObject("Data").getString("WinNum");
|
||||
// code = transCode(code);
|
||||
// String time = DateUtil.getCurrentTime();
|
||||
// LotteryOpenCode lotteryOpenCode = new LotteryOpenCode(
|
||||
// lotteryName, expect, code, time, 0);
|
||||
// list.add(lotteryOpenCode);
|
||||
// }
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
//
|
||||
// private static String transCode(String code) {
|
||||
// code = code.replaceAll("<li>|</li>", "");
|
||||
// StringBuffer sb = new StringBuffer();
|
||||
// for (int i = 0, j = code.length(); i < j; i++) {
|
||||
// sb.append(code.substring(i, i + 1));
|
||||
// if (i < j - 1) {
|
||||
// sb.append(",");
|
||||
// }
|
||||
// }
|
||||
// return sb.toString();
|
||||
// }
|
||||
//
|
||||
// }
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
// package lottery.domains.capture.sites.xjflcp;
|
||||
//
|
||||
// import javautils.date.DateUtil;
|
||||
// import lottery.domains.capture.utils.open.OpenTimeUtil;
|
||||
// import lottery.domains.content.dao.LotteryCrawlerStatusDao;
|
||||
// import lottery.domains.content.dao.LotteryOpenCodeDao;
|
||||
// import lottery.domains.content.entity.LotteryCrawlerStatus;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
// import org.springframework.beans.factory.annotation.Autowired;
|
||||
// import org.springframework.beans.factory.annotation.Qualifier;
|
||||
// import org.springframework.stereotype.Component;
|
||||
//
|
||||
// import java.util.List;
|
||||
//
|
||||
// @Component
|
||||
// public class XjflcpCrawler {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(XjflcpCrawler.class);
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryCrawlerStatusDao lotteryCrawlerStatusDao;
|
||||
//
|
||||
// @Autowired
|
||||
// private LotteryOpenCodeDao lotteryOpenCodeDao;
|
||||
//
|
||||
// public boolean start(String lotteryName) {
|
||||
// logger.debug("开始抓取" + lotteryName + "数据>>>>>>>>>>>>>>>>");
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao.get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// String expect = getNextExpect(lastExpect, lotteryCrawlerStatus.getTimes());
|
||||
// logger.debug("目标期数:" + expect);
|
||||
// List<LotteryOpenCode> list = XjflcpCrawlerUtil.getXjsscOpenCode(lotteryName, expect);
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// for (LotteryOpenCode lotteryOpenCode : list) {
|
||||
// // 设置数据来源
|
||||
// lotteryOpenCode.setRemarks("www.xjflcp.com");
|
||||
// String currExpect = lotteryOpenCode.getExpect();
|
||||
// // 如果大于上一期,插入
|
||||
// if(currExpect.compareTo(lastExpect) > 0) {
|
||||
// boolean isSucc = lotteryOpenCodeDao.add(lotteryOpenCode);
|
||||
// if(isSucc) {
|
||||
// // 更新状态,最后成功抓到数据的期数、时间
|
||||
// lotteryCrawlerStatusDao.update(lotteryName, currExpect, currTime);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // 如果不是当前期,那么就继续抓吧
|
||||
// if(!isCurrExpect(lotteryName)) {
|
||||
// logger.debug("不是最新的数据,10秒后继续抓...");
|
||||
// return false;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// @Autowired
|
||||
// @Qualifier(value = "highOpenTimeUtil")
|
||||
// private OpenTimeUtil highOpenTimeUtil;
|
||||
//
|
||||
// /**
|
||||
// * 判断是否是当前期
|
||||
// * @param lotteryName
|
||||
// * @return
|
||||
// */
|
||||
// public boolean isCurrExpect(String lotteryName) {
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// String currExpect = highOpenTimeUtil.getCurrExpect(lotteryName, currTime);
|
||||
// LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao.get(lotteryName);
|
||||
// String lastExpect = lotteryCrawlerStatus.getLastExpect();
|
||||
// logger.debug("当前期:" + currExpect + " 最后一期:" + lastExpect);
|
||||
// return lastExpect.equals(currExpect);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取下一期
|
||||
// * @param lotteryName
|
||||
// * @return
|
||||
// */
|
||||
// public String getNextExpect(String lastExpect, int times) {
|
||||
// String[] tmpArray = lastExpect.split("-");
|
||||
// String expectDate = DateUtil.formatTime(tmpArray[0], "yyyyMMdd", "yyyy-MM-dd");
|
||||
// int expectNumber = Integer.parseInt(tmpArray[1]);
|
||||
// if(expectNumber == times) {
|
||||
// expectDate = DateUtil.calcNextDay(expectDate);
|
||||
// expectNumber = 1;
|
||||
// } else {
|
||||
// expectNumber++;
|
||||
// }
|
||||
// return expectDate.replace("-", "") + "-" + String.format("%03d", expectNumber);
|
||||
// }
|
||||
// }
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
// package lottery.domains.capture.sites.xjflcp;
|
||||
//
|
||||
// import javautils.StringUtil;
|
||||
// import javautils.date.DateUtil;
|
||||
// import javautils.html.HtmlUtils;
|
||||
// import javautils.http.EasyHttpClient;
|
||||
// import lottery.domains.capture.utils.CodeValidate;
|
||||
// import lottery.domains.content.entity.LotteryOpenCode;
|
||||
// import org.htmlparser.Node;
|
||||
// import org.htmlparser.util.NodeList;
|
||||
// import org.slf4j.Logger;
|
||||
// import org.slf4j.LoggerFactory;
|
||||
//
|
||||
// import java.util.ArrayList;
|
||||
// import java.util.HashMap;
|
||||
// import java.util.List;
|
||||
// import java.util.Map;
|
||||
//
|
||||
// public class XjflcpCrawlerUtil {
|
||||
//
|
||||
// private static final Logger logger = LoggerFactory.getLogger(XjflcpCrawlerUtil.class);
|
||||
// private static Map<String, EasyHttpClient> httpClientPool = new HashMap<>();
|
||||
//
|
||||
// private static EasyHttpClient getHttpClient(String lotteryName) {
|
||||
// if (!httpClientPool.containsKey(lotteryName)) {
|
||||
// httpClientPool.put(lotteryName, new EasyHttpClient());
|
||||
// }
|
||||
// return httpClientPool.get(lotteryName);
|
||||
// }
|
||||
//
|
||||
// private XjflcpCrawlerUtil() {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public static synchronized List<LotteryOpenCode> getXjsscOpenCode(String lotteryName, String expect) {
|
||||
// logger.info("获取新疆时时彩开奖号码....");
|
||||
// String[] tmpArray = expect.split("-");
|
||||
// String expectDate = tmpArray[0];
|
||||
// String expectNumber = String.format("%02d", Integer.parseInt(tmpArray[1]));
|
||||
// String targetExpect = expectDate + expectNumber;
|
||||
// String url = "http://www.xjflcp.com/video/prizeDetail.do?operator=detailssc&lotterydraw=" + targetExpect;
|
||||
// EasyHttpClient httpClient = getHttpClient(lotteryName);
|
||||
// String data = httpClient.get(url);
|
||||
// List<LotteryOpenCode> tmplist = getOpenCode(lotteryName, data);
|
||||
// List<LotteryOpenCode> list = new ArrayList<>();
|
||||
// for (LotteryOpenCode tmpBean : tmplist) {
|
||||
// if(CodeValidate.isSsc(tmpBean.getCode())) {
|
||||
// list.add(tmpBean);
|
||||
// }
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
//
|
||||
// private static List<LotteryOpenCode> getOpenCode(String lotteryName, String data) {
|
||||
// List<LotteryOpenCode> list = new ArrayList<>();
|
||||
// if(StringUtil.isNotNull(data)) {
|
||||
// try {
|
||||
// String resource = data.replaceAll("(\t|\r|\n)", "");
|
||||
// NodeList nList = HtmlUtils.FilterCss(resource, ".r5", "UTF-8");
|
||||
// String currTime = DateUtil.getCurrentTime();
|
||||
// if(nList.size() > 0) {
|
||||
// NodeList tdList = HtmlUtils.FilterTag(nList.elementAt(0).toHtml(), "td", "UTF-8");
|
||||
// Node[] nodes = tdList.toNodeArray();
|
||||
// LotteryOpenCode lotteryOpenCode = new LotteryOpenCode();
|
||||
// lotteryOpenCode.setLottery(lotteryName);
|
||||
// lotteryOpenCode.setTime(currTime);
|
||||
// for (int i = 0; i < nodes.length; i++) {
|
||||
// if(nodes[i].getChildren() != null) {
|
||||
// for (Node node : nodes[i].getChildren().toNodeArray()) {
|
||||
// String html = node.toHtml();
|
||||
// if(StringUtil.isNotNull(html)) {
|
||||
// if(i == 1) {
|
||||
// String expectDate = html.substring(0, 8);
|
||||
// String expectNumber = String.format("%03d", Integer.parseInt(html.substring(8)));
|
||||
// String expect = expectDate + "-" + expectNumber;
|
||||
// lotteryOpenCode.setExpect(expect);
|
||||
// }
|
||||
// if(i == 5) {
|
||||
// String code = html.replace(" ", ",");
|
||||
// if(!",,,,".equals(code)) {
|
||||
// lotteryOpenCode.setCode(code);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if(lotteryOpenCode.getCode() != null) {
|
||||
// list.add(lotteryOpenCode);
|
||||
// }
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("解析数据失败...", e);
|
||||
// }
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,200 @@
|
||||
package lottery.domains.capture.utils;
|
||||
|
||||
import javautils.StringUtil;
|
||||
|
||||
public class CodeValidate {
|
||||
public static boolean validate(String lottery, String code) {
|
||||
switch (lottery) {
|
||||
case "cqssc":
|
||||
case "xjssc":
|
||||
case "tjssc":
|
||||
case "tw5fc":
|
||||
case "bj5fc":
|
||||
case "jnd3d5fc":
|
||||
case "txffc":
|
||||
return isSsc(code);
|
||||
case "sd11x5":
|
||||
case "gd11x5":
|
||||
case "jx11x5":
|
||||
case "ah11x5":
|
||||
return is11x5(code);
|
||||
case "jsk3":
|
||||
case "ahk3":
|
||||
case "jlk3":
|
||||
case "hbk3":
|
||||
case "shk3":
|
||||
return isK3(code);
|
||||
case "fc3d":
|
||||
case "pl3":
|
||||
return is3d(code);
|
||||
case "bjkl8":
|
||||
return isBjkl8(code);
|
||||
case "bjpk10":
|
||||
return isBjpk10(code);
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isSsc(String s) {
|
||||
// 是否为空
|
||||
if (!StringUtil.isNotNull(s))
|
||||
return false;
|
||||
String[] codes = s.split(",");
|
||||
// 数字长度
|
||||
if (codes.length != 5)
|
||||
return false;
|
||||
for (String tmpS : codes) {
|
||||
// 是否是数字
|
||||
if (!StringUtil.isInteger(tmpS))
|
||||
return false;
|
||||
// 数字位数对不对
|
||||
if (tmpS.length() != 1)
|
||||
return false;
|
||||
int tmpC = Integer.parseInt(tmpS);
|
||||
// 数字的有效范围
|
||||
if (!(tmpC >= 0 && tmpC <= 9))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean is11x5(String s) {
|
||||
// 是否为空
|
||||
if (!StringUtil.isNotNull(s))
|
||||
return false;
|
||||
String[] codes = s.split(",");
|
||||
// 数字长度
|
||||
if (codes.length != 5)
|
||||
return false;
|
||||
for (String tmpS : codes) {
|
||||
// 是否是数字
|
||||
if (!StringUtil.isInteger(tmpS))
|
||||
return false;
|
||||
// 数字位数对不对
|
||||
if (tmpS.length() != 2)
|
||||
return false;
|
||||
int tmpC = Integer.parseInt(tmpS);
|
||||
// 数字的有效范围
|
||||
if (!(tmpC >= 1 && tmpC <= 11))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isK3(String s) {
|
||||
// 是否为空
|
||||
if (!StringUtil.isNotNull(s))
|
||||
return false;
|
||||
String[] codes = s.split(",");
|
||||
// 数字长度
|
||||
if (codes.length != 3)
|
||||
return false;
|
||||
for (String tmpS : codes) {
|
||||
// 是否是数字
|
||||
if (!StringUtil.isInteger(tmpS))
|
||||
return false;
|
||||
// 数字位数对不对
|
||||
if (tmpS.length() != 1)
|
||||
return false;
|
||||
int tmpC = Integer.parseInt(tmpS);
|
||||
// 数字的有效范围
|
||||
if (!(tmpC >= 1 && tmpC <= 6))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean is3d(String s) {
|
||||
// 是否为空
|
||||
if (!StringUtil.isNotNull(s))
|
||||
return false;
|
||||
String[] codes = s.split(",");
|
||||
// 数字长度
|
||||
if (codes.length != 3)
|
||||
return false;
|
||||
for (String tmpS : codes) {
|
||||
// 是否是数字
|
||||
if (!StringUtil.isInteger(tmpS))
|
||||
return false;
|
||||
// 数字位数对不对
|
||||
if (tmpS.length() != 1)
|
||||
return false;
|
||||
int tmpC = Integer.parseInt(tmpS);
|
||||
// 数字的有效范围
|
||||
if (!(tmpC >= 0 && tmpC <= 9))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isBjkl8(String s) {
|
||||
// 是否为空
|
||||
if (!StringUtil.isNotNull(s))
|
||||
return false;
|
||||
String[] codes = s.split(",");
|
||||
// 数字长度
|
||||
if (codes.length != 20)
|
||||
return false;
|
||||
for (String tmpS : codes) {
|
||||
// 是否是数字
|
||||
if (!StringUtil.isInteger(tmpS))
|
||||
return false;
|
||||
// 数字位数对不对
|
||||
if (tmpS.length() != 2)
|
||||
return false;
|
||||
int tmpC = Integer.parseInt(tmpS);
|
||||
// 数字的有效范围
|
||||
if (!(tmpC >= 1 && tmpC <= 80))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isBjpk10(String s) {
|
||||
// 是否为空
|
||||
if (!StringUtil.isNotNull(s))
|
||||
return false;
|
||||
String[] codes = s.split(",");
|
||||
// 数字长度
|
||||
if (codes.length != 10)
|
||||
return false;
|
||||
for (String tmpS : codes) {
|
||||
// 是否是数字
|
||||
if (!StringUtil.isInteger(tmpS))
|
||||
return false;
|
||||
// 数字位数对不对
|
||||
if (tmpS.length() != 2)
|
||||
return false;
|
||||
int tmpC = Integer.parseInt(tmpS);
|
||||
// 数字的有效范围
|
||||
if (!(tmpC >= 1 && tmpC <= 10))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isKy481(String s) {
|
||||
// 是否为空
|
||||
if (!StringUtil.isNotNull(s))
|
||||
return false;
|
||||
String[] codes = s.split(",");
|
||||
// 数字长度
|
||||
if (codes.length != 4)
|
||||
return false;
|
||||
for (String tmpS : codes) {
|
||||
// 是否是数字
|
||||
if (!StringUtil.isInteger(tmpS))
|
||||
return false;
|
||||
// 数字位数对不对
|
||||
if (tmpS.length() != 1)
|
||||
return false;
|
||||
int tmpC = Integer.parseInt(tmpS);
|
||||
// 数字的有效范围
|
||||
if (!(tmpC >= 1 && tmpC <= 8))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
package lottery.domains.capture.utils;
|
||||
|
||||
import javautils.StringUtil;
|
||||
|
||||
/**
|
||||
* Created by Nick on 2017/1/6.
|
||||
*/
|
||||
public class ExpectValidate {
|
||||
public static boolean validate(String lottery, String expect) {
|
||||
switch (lottery) {
|
||||
case "cqssc":
|
||||
case "xjssc":
|
||||
case "tjssc":
|
||||
return isSsc(expect);
|
||||
case "tw5fc":
|
||||
return isTw5fc(expect);
|
||||
case "sd11x5":
|
||||
case "gd11x5":
|
||||
case "jx11x5":
|
||||
case "ah11x5":
|
||||
return is11x5(expect);
|
||||
case "jsk3":
|
||||
case "ahk3":
|
||||
case "jlk3":
|
||||
case "hbk3":
|
||||
case "shk3":
|
||||
return isK3(expect);
|
||||
case "fc3d":
|
||||
case "pl3":
|
||||
return is3d(expect);
|
||||
case "bjkl8":
|
||||
case "bj5fc":
|
||||
return isBjkl8(expect);
|
||||
case "bjpk10":
|
||||
return isBjpk10(expect);
|
||||
case "jnd3d5fc":
|
||||
return isJnd3d5fc(expect);
|
||||
case "txffc":
|
||||
return isTxffc(expect);
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isSsc(String expect) {
|
||||
// 是否为空
|
||||
if (!StringUtil.isNotNull(expect))
|
||||
return false;
|
||||
if (expect.length() != 12)
|
||||
return false;
|
||||
String[] expects = expect.split("-");
|
||||
// 长度
|
||||
if (expects.length != 2)
|
||||
return false;
|
||||
|
||||
String date = expects[0];
|
||||
String exp = expects[1];
|
||||
|
||||
if (!StringUtil.isInteger(date))
|
||||
return false;
|
||||
if (!StringUtil.isInteger(exp))
|
||||
return false;
|
||||
if (date.length() != 8)
|
||||
return false;
|
||||
if (exp.length() != 3)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isTxffc(String expect) {
|
||||
// 是否为空
|
||||
if (!StringUtil.isNotNull(expect))
|
||||
return false;
|
||||
if (expect.length() != 13)
|
||||
return false;
|
||||
String[] expects = expect.split("-");
|
||||
// 长度
|
||||
if (expects.length != 2)
|
||||
return false;
|
||||
|
||||
String date = expects[0];
|
||||
String exp = expects[1];
|
||||
|
||||
if (!StringUtil.isInteger(date))
|
||||
return false;
|
||||
if (!StringUtil.isInteger(exp))
|
||||
return false;
|
||||
if (date.length() != 8)
|
||||
return false;
|
||||
if (exp.length() != 4)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isTw5fc(String expect) {
|
||||
// 是否为空
|
||||
if (!StringUtil.isNotNull(expect))
|
||||
return false;
|
||||
if (!StringUtil.isInteger(expect))
|
||||
return false;
|
||||
if (expect.length() != 9)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean is11x5(String expect) {
|
||||
// 是否为空
|
||||
if (!StringUtil.isNotNull(expect))
|
||||
return false;
|
||||
if (expect.length() != 12)
|
||||
return false;
|
||||
String[] expects = expect.split("-");
|
||||
// 长度
|
||||
if (expects.length != 2)
|
||||
return false;
|
||||
|
||||
String date = expects[0];
|
||||
String exp = expects[1];
|
||||
|
||||
if (!StringUtil.isInteger(date))
|
||||
return false;
|
||||
if (!StringUtil.isInteger(exp))
|
||||
return false;
|
||||
if (date.length() != 8)
|
||||
return false;
|
||||
if (exp.length() != 3)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isK3(String expect) {
|
||||
// 是否为空
|
||||
if (!StringUtil.isNotNull(expect))
|
||||
return false;
|
||||
if (expect.length() != 12)
|
||||
return false;
|
||||
String[] expects = expect.split("-");
|
||||
// 长度
|
||||
if (expects.length != 2)
|
||||
return false;
|
||||
|
||||
String date = expects[0];
|
||||
String exp = expects[1];
|
||||
|
||||
if (!StringUtil.isInteger(date))
|
||||
return false;
|
||||
if (!StringUtil.isInteger(exp))
|
||||
return false;
|
||||
if (date.length() != 8)
|
||||
return false;
|
||||
if (exp.length() != 3)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean is3d(String expect) {
|
||||
// 是否为空
|
||||
if (!StringUtil.isNotNull(expect))
|
||||
return false;
|
||||
if (!StringUtil.isInteger(expect))
|
||||
return false;
|
||||
if (expect.length() != 5)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isBjkl8(String expect) {
|
||||
// 是否为空
|
||||
if (!StringUtil.isNotNull(expect))
|
||||
return false;
|
||||
if (!StringUtil.isInteger(expect))
|
||||
return false;
|
||||
if (expect.length() != 6)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isBjpk10(String expect) {
|
||||
// 是否为空
|
||||
if (!StringUtil.isNotNull(expect))
|
||||
return false;
|
||||
if (!StringUtil.isInteger(expect))
|
||||
return false;
|
||||
if (expect.length() != 6)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isJnd3d5fc(String expect) {
|
||||
// 是否为空
|
||||
if (!StringUtil.isNotNull(expect))
|
||||
return false;
|
||||
if (!StringUtil.isInteger(expect))
|
||||
return false;
|
||||
if (expect.length() != 7)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package lottery.domains.capture.utils;
|
||||
|
||||
public class LotteryCrons {
|
||||
|
||||
/**
|
||||
* 格式: [秒] [分] [小时] [日] [月] [周] [年]
|
||||
*/
|
||||
private LotteryCrons() {}
|
||||
|
||||
public static final String TEST = "0/10 * * * * *";
|
||||
// 重庆时时彩5分钟档
|
||||
public static final String cqssc_5 = "36 0/5 0-1,22-23 * * *";
|
||||
// 重庆时时彩10分钟档
|
||||
public static final String cqssc_10 = "6 0/10 10-21 * * *";
|
||||
|
||||
// 江西时时彩时时彩9点
|
||||
public static final String jxssc_9 = "0 10,20,30,40,50 9 * * *";
|
||||
// 江西时时彩时时彩10点
|
||||
public static final String jxssc_10 = "0 0,10,21,31,41,51 10 * * *";
|
||||
// 江西时时彩11点
|
||||
public static final String jxssc_11 = "0 1,11,21,32,42,52 11 * * *";
|
||||
// 江西时时彩12点
|
||||
public static final String jxssc_12 = "0 2,12,22,32,43,53 12 * * *";
|
||||
// 江西时时彩13点
|
||||
public static final String jxssc_13 = "0 3,13,23,33,43,54 13 * * *";
|
||||
// 江西时时彩14点
|
||||
public static final String jxssc_14 = "0 4,14,24,34,44,55 14 * * *";
|
||||
// 江西时时彩15点
|
||||
public static final String jxssc_15 = "0 5,15,25,35,45,55 15 * * *";
|
||||
// 江西时时彩16点
|
||||
public static final String jxssc_16 = "0 6,16,26,36,46,56 16 * * *";
|
||||
// 江西时时彩17点
|
||||
public static final String jxssc_17 = "0 6,17,27,37,47,57 17 * * *";
|
||||
// 江西时时彩18点
|
||||
public static final String jxssc_18 = "0 7,18,28,38,48,58 18 * * *";
|
||||
// 江西时时彩19点
|
||||
public static final String jxssc_19 = "0 8,18,29,39,49,59 19 * * *";
|
||||
// 江西时时彩20点
|
||||
public static final String jxssc_20 = "0 9,19,29,40,50 20 * * *";
|
||||
// 江西时时彩21点
|
||||
public static final String jxssc_21 = "0 0,10,20,30,40,51 21 * * *";
|
||||
// 江西时时彩22点
|
||||
public static final String jxssc_22 = "0 1,11,21,31,41,51 22 * * *";
|
||||
// 江西时时彩23点
|
||||
public static final String jxssc_23 = "0 2,12 23 * * *";
|
||||
|
||||
// 新疆时时彩10:10~10:50
|
||||
public static final String xjssc_10 = "0 10/10 10 * * *";
|
||||
// 新疆时时彩11~23点
|
||||
public static final String xjssc_11_23 = "0 0/10 11-23 * * *";
|
||||
// 新疆时时彩0~1点
|
||||
public static final String xjssc_0_1 = "0 0/10 0-1 * * *";
|
||||
// 新疆时时彩2:00点
|
||||
public static final String xjssc_2 = "0 0 2 * * *";
|
||||
|
||||
// 天津时时彩9~22点
|
||||
public static final String tjssc = "0 9/10 9-22 * * *";
|
||||
|
||||
// 广东11选5 09:10~09:50
|
||||
public static final String gd11x5_9 = "0 9/10 10 * * *";
|
||||
// 广东11选5 10~22点
|
||||
public static final String gd11x5_10_22 = "0 0/10 10-22 * * *";
|
||||
// 广东11选5 23点00
|
||||
public static final String gd11x5_23 = "0 0 23 * * *";
|
||||
|
||||
|
||||
// 江西11选5 9:10~10:50
|
||||
public static final String jx11x5_9 = "0 10/10 10 * * *";
|
||||
// 江西11选5 10~21点
|
||||
public static final String jx11x5_10_21 = "0 0/10 10-21 * * *";
|
||||
// 江西11选5 22点
|
||||
public static final String jx11x5_22 = "0 0 22 * * *";
|
||||
|
||||
// 重庆11选5 9~22点
|
||||
public static final String cq11x5_9_22 = "0 0/10 9-22 * * *";
|
||||
// 重庆11选5 23点
|
||||
public static final String cq11x5_23 = "0 0 23 * * *";
|
||||
|
||||
// 安徽11选5 8点
|
||||
public static final String ah11x5_8 = "0 40,50 8 * * *";
|
||||
// 安徽11选5 9~21点
|
||||
public static final String ah11x5_9_21 = "0 0/10 9-21 * * *";
|
||||
// 安徽11选5 22点
|
||||
public static final String ah11x5_22 = "0 0 22 * * *";
|
||||
|
||||
// 上海11选5 9~23点
|
||||
public static final String sh11x5_9_23 = "0 0/10 9-23 * * *";
|
||||
|
||||
// 山东11选5 9~23点
|
||||
public static final String sd11x5_9_21 = "0 5/10 9-21 * * *";
|
||||
|
||||
// 江苏快3 8点
|
||||
public static final String jsk3_8 = "0 40,50 8 * * *";
|
||||
// 江苏快3 9~15点
|
||||
public static final String jsk3_9_15 = "0 0/10 9-15 * * *";
|
||||
// 江苏快3 16~20点
|
||||
public static final String jsk3_16_20 = "0 1/10 16-20 * * *";
|
||||
// 江苏快3 21点
|
||||
public static final String jsk3_21 = "0 1,11,21,32,42,52 21 * * *";
|
||||
// 江苏快3 22点
|
||||
public static final String jsk3_22 = "0 2,12 22 * * *";
|
||||
|
||||
// 安徽快3 8点
|
||||
public static final String ahk3_8 = "0 50 8 * * *";
|
||||
// 安徽快3 9~21点
|
||||
public static final String ahk3_9_21 = "0 0/10 9-21 * * *";
|
||||
// 安徽快3 22点
|
||||
public static final String ahk3_22 = "0 0 22 * * *";
|
||||
|
||||
// 湖北快3 9~21点
|
||||
public static final String hbk3_9_21 = "0 0/10 9-21 * * *";
|
||||
// 湖北快3 22点
|
||||
public static final String hbk3_22 = "0 0 22 * * *";
|
||||
|
||||
// 吉林快3 8点
|
||||
public static final String jlk3_8 = "0 29,39,49 8 * * *";
|
||||
// 吉林快3 9~11点
|
||||
public static final String jlk3_9_11 = "0 0/10 9-11 * * *";
|
||||
// 吉林快3 12点
|
||||
public static final String jlk3_12 = "0 0,10,21,31,41,51 12 * * *";
|
||||
// 吉林快3 13~14点
|
||||
public static final String jlk3_13_14 = "0 1/10 13-14 * * *";
|
||||
// 吉林快3 15点
|
||||
public static final String jlk3_15 = "0 1,11,21,32,42,52 15 * * *";
|
||||
// 吉林快3 16~17点
|
||||
public static final String jlk3_16_17 = "0 2/10 16-17 * * *";
|
||||
// 吉林快3 18点
|
||||
public static final String jlk3_18 = "0 2,12,23,33,43,53 18 * * *";
|
||||
// 吉林快3 19~20点
|
||||
public static final String jlk3_19_20 = "0 3/10 19-20 * * *";
|
||||
// 吉林快3 21点
|
||||
public static final String jlk3_21 = "0 3,14,24,34 21 * * *";
|
||||
|
||||
// 北京快乐8
|
||||
public static final String bjkl8 = "0 0/5 9-23 * * *";
|
||||
|
||||
// 北京PK拾
|
||||
public static final String bjpk10 = "0 7/5 9-23 * * *";
|
||||
|
||||
// 福彩3d
|
||||
public static final String fc3d = "0 30 20 * * *";
|
||||
|
||||
// 排列三
|
||||
public static final String pl3 = "0 50 20 * * *";
|
||||
|
||||
// 河南481 9点
|
||||
public static final String hn481_9 = "0 10,20,30,40,50 9 * * *";
|
||||
// 河南481 10~21点
|
||||
public static final String hn481_10_21 = "0 0/10 10-21 * * *";
|
||||
// 河南481 22点
|
||||
public static final String hn481_22 = "0 0 22 * * *";
|
||||
|
||||
}
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
package lottery.domains.capture.utils.open;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javautils.date.DateUtil;
|
||||
import lottery.domains.content.dao.LotteryCrawlerStatusDao;
|
||||
import lottery.domains.content.entity.LotteryCrawlerStatus;
|
||||
import lottery.domains.content.entity.LotteryOpenTime;
|
||||
import lottery.domains.content.pool.LotteryDataFactory;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 高频彩开奖信息
|
||||
* 时时彩、十一选五、快3、北京快乐8、北京PK拾
|
||||
*/
|
||||
@Component
|
||||
public class HighOpenTimeUtil implements OpenTimeUtil {
|
||||
|
||||
@Autowired
|
||||
private LotteryDataFactory df;
|
||||
|
||||
@Autowired
|
||||
private LotteryCrawlerStatusDao lotteryCrawlerStatusDao;
|
||||
|
||||
public String getExpect(String lotteryName, String currTime) {
|
||||
List<LotteryOpenTime> list = df.listLotteryOpenTime(lotteryName);
|
||||
String currDate = currTime.substring(0, 10);
|
||||
String nextDate = DateUtil.calcNextDay(currDate);
|
||||
String lastDate = DateUtil.calcLastDay(currDate);
|
||||
for (int i = 0, j = list.size(); i < j; i++) {
|
||||
LotteryOpenTime tmpBean = list.get(i);
|
||||
String startDate = currDate; // 开始日期
|
||||
String stopDate = currDate; // 结束日期
|
||||
String openDate = currDate; // 开奖日期
|
||||
String expectDate = currDate; // 期号日期
|
||||
String startTime = tmpBean.getStartTime(); // 开始时间
|
||||
String stopTime = tmpBean.getStopTime(); // 结束时间
|
||||
String openTime = tmpBean.getOpenTime(); // 开奖时间
|
||||
String expect = tmpBean.getExpect();
|
||||
if(i == 0) {
|
||||
if(startTime.compareTo(stopTime) > 0) {
|
||||
startDate = lastDate;
|
||||
}
|
||||
} else if(i == j - 1) {
|
||||
if(startTime.compareTo(stopTime) > 0) {
|
||||
stopDate = nextDate;
|
||||
}
|
||||
if(startTime.compareTo(openTime) > 0) {
|
||||
openDate = nextDate;
|
||||
}
|
||||
if(currTime.compareTo(stopDate + " " + stopTime) >= 0) {
|
||||
tmpBean = list.get(0);
|
||||
startDate = nextDate; // 开始日期
|
||||
stopDate = nextDate; // 结束日期
|
||||
openDate = nextDate; // 开奖日期
|
||||
expectDate = nextDate; // 期号日期
|
||||
startTime = tmpBean.getStartTime(); // 开始时间
|
||||
stopTime = tmpBean.getStopTime(); // 结束时间
|
||||
openTime = tmpBean.getOpenTime(); // 开奖时间
|
||||
expect = tmpBean.getExpect();
|
||||
if(startTime.compareTo(stopTime) > 0) {
|
||||
startDate = currDate;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(startTime.compareTo(stopTime) > 0) {
|
||||
stopDate = nextDate;
|
||||
}
|
||||
if(startTime.compareTo(openTime) > 0) {
|
||||
openDate = nextDate;
|
||||
}
|
||||
}
|
||||
if(!tmpBean.getIsTodayExpect()) {
|
||||
expectDate = lastDate;
|
||||
}
|
||||
startTime = startDate + " " + startTime;
|
||||
stopTime = stopDate + " " + stopTime;
|
||||
openTime = openDate + " " + openTime;
|
||||
expect = expectDate.replace("-", "") + "-" + expect;
|
||||
if(currTime.compareTo(startTime) >= 0 && currTime.compareTo(stopTime) < 0) {
|
||||
return expect;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCurrExpect(String lotteryName, String currTime) {
|
||||
LotteryCrawlerStatus lotteryCrawlerStatus = lotteryCrawlerStatusDao.get(lotteryName);
|
||||
// 先得到当前销售期
|
||||
String tmpExpect = getExpect(lotteryName, currTime);
|
||||
String tmpDate = tmpExpect.substring(0, 8);
|
||||
String currDate = DateUtil.formatTime(tmpDate, "yyyyMMdd", "yyyy-MM-dd");
|
||||
String currExpect = tmpExpect.substring(9);
|
||||
String lastDate = currDate;
|
||||
int lastExpect = Integer.parseInt(currExpect);
|
||||
int times = lotteryCrawlerStatus.getTimes();
|
||||
// 如果是第1期,那么上一期的就是昨天最后一期
|
||||
if(lastExpect == 1) {
|
||||
lastDate = DateUtil.calcLastDay(currDate);
|
||||
lastExpect = times;
|
||||
} else {
|
||||
lastExpect--;
|
||||
}
|
||||
|
||||
int formatCount = 3;
|
||||
if ("fgffc".equals(lotteryName)) {
|
||||
formatCount = 4;
|
||||
}
|
||||
|
||||
String expect = lastDate.replaceAll("-", "") + "-" + String.format("%0"+formatCount+"d", lastExpect);
|
||||
return expect;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNextExpect(String lotteryName, String lastExpect) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package lottery.domains.capture.utils.open;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javautils.date.DateUtil;
|
||||
import javautils.date.Moment;
|
||||
import lottery.domains.content.entity.LotteryOpenTime;
|
||||
import lottery.domains.content.pool.LotteryDataFactory;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class LotteryOpenTimeUtils {
|
||||
|
||||
@Autowired
|
||||
private LotteryDataFactory df;
|
||||
|
||||
public String getCurrExpect(String lotteryName) {
|
||||
Moment m = new Moment();
|
||||
String curTime = m.toCurTime();
|
||||
curTime = "00:00:00";
|
||||
String toDay = m.toSimpleDate();
|
||||
List<LotteryOpenTime> list = df.listLotteryOpenTime(lotteryName);
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (i < list.size() - 1) {
|
||||
LotteryOpenTime ot1 = list.get(i);
|
||||
LotteryOpenTime ot2 = list.get(i + 1);
|
||||
if (curTime.compareTo(ot1.getOpenTime()) >= 0
|
||||
&& curTime.compareTo(ot2.getOpenTime()) < 0) {
|
||||
String expect = toDay.replace("-", "") + "-"
|
||||
+ ot1.getExpect();
|
||||
return expect;
|
||||
} else if (curTime.compareTo(ot1.getOpenTime()) >= 0
|
||||
&& ot1.getOpenTime().compareTo(ot2.getOpenTime()) > 0) {
|
||||
String yesterDay = m.subtract(1, "d").toSimpleDate();
|
||||
String expect = yesterDay.replace("-", "") + "-"
|
||||
+ ot1.getExpect();
|
||||
return expect;
|
||||
}
|
||||
} else {
|
||||
LotteryOpenTime lotteryOpenTime = list.get(list.size() - 1);
|
||||
if (lotteryOpenTime.getOpenTime().compareTo(
|
||||
lotteryOpenTime.getStartTime()) < 0) {
|
||||
String yesterDay = m.subtract(1, "d").toSimpleDate();
|
||||
String expect = yesterDay.replace("-", "") + "-"
|
||||
+ lotteryOpenTime.getExpect();
|
||||
return expect;
|
||||
} else {
|
||||
String expect = toDay.replace("-", "") + "-"
|
||||
+ lotteryOpenTime.getExpect();
|
||||
return expect;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package lottery.domains.capture.utils.open;
|
||||
|
||||
import javautils.date.Moment;
|
||||
import lottery.domains.content.entity.Lottery;
|
||||
import lottery.domains.content.pool.LotteryDataFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class LotteryOpenUtil {
|
||||
|
||||
@Autowired
|
||||
private LotteryDataFactory dataFactory;
|
||||
|
||||
/**
|
||||
* 物理计算指定期号的上一期开奖号码
|
||||
* @param lotteryShortName 彩票
|
||||
* @param expect 期号
|
||||
* @return
|
||||
*/
|
||||
public String subtractExpect(String lotteryShortName, String expect) {
|
||||
Lottery lottery = dataFactory.getLottery(lotteryShortName);
|
||||
if (lottery == null) return null;
|
||||
|
||||
String subExpect;
|
||||
if (expect.indexOf("-") <= -1) {
|
||||
Integer integerExpect = Integer.valueOf(expect);
|
||||
integerExpect -= 1;
|
||||
|
||||
if (integerExpect.toString().length() >= expect.length()) {
|
||||
subExpect = integerExpect.toString();
|
||||
}
|
||||
else {
|
||||
subExpect = String.format("%0"+expect.length()+"d", integerExpect);
|
||||
}
|
||||
}
|
||||
else {
|
||||
String[] split = expect.split("-");
|
||||
int formatCount = split[1].length();
|
||||
String date = split[0];
|
||||
String currExpect = split[1];
|
||||
|
||||
if (currExpect.equals("001") || currExpect.equals("0001")) {
|
||||
date = new Moment().fromDate(date).subtract(1, "days").format("yyyyMMdd");
|
||||
subExpect = String.format("%0"+formatCount+"d", lottery.getTimes());
|
||||
}
|
||||
else {
|
||||
Integer integer = Integer.valueOf(currExpect);
|
||||
integer -= 1;
|
||||
if (integer.toString().length() >= formatCount) {
|
||||
subExpect = integer.toString();
|
||||
}
|
||||
else {
|
||||
subExpect = String.format("%0"+formatCount+"d", integer);
|
||||
}
|
||||
|
||||
}
|
||||
subExpect = date + "-" + subExpect;
|
||||
}
|
||||
|
||||
return subExpect;
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package lottery.domains.capture.utils.open;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import lottery.domains.content.entity.LotteryOpenTime;
|
||||
import lottery.domains.content.pool.LotteryDataFactory;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 低频彩开奖信息
|
||||
* 福彩3D、排列三
|
||||
*/
|
||||
@Component
|
||||
public class LowOpenTimeUtil implements OpenTimeUtil {
|
||||
|
||||
@Autowired
|
||||
private LotteryDataFactory df;
|
||||
|
||||
@Override
|
||||
public String getCurrExpect(String lotteryName, String currTime) {
|
||||
List<LotteryOpenTime> list = df.listLotteryOpenTime(lotteryName);
|
||||
for (int i = 0, j = list.size(); i < j; i++) {
|
||||
LotteryOpenTime tmpBean = list.get(i);
|
||||
String startTime = tmpBean.getStartTime();
|
||||
String stopTime = tmpBean.getStopTime();
|
||||
if(currTime.compareTo(startTime) >= 0 && currTime.compareTo(stopTime) < 0) {
|
||||
if(i == 0) {
|
||||
return null;
|
||||
}
|
||||
tmpBean = list.get(i - 1);
|
||||
return tmpBean.getExpect();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNextExpect(String lotteryName, String lastExpect) {
|
||||
List<LotteryOpenTime> list = df.listLotteryOpenTime(lotteryName);
|
||||
for (int i = 0, j = list.size(); i < j; i++) {
|
||||
LotteryOpenTime tmpBean = list.get(i);
|
||||
String expect = tmpBean.getExpect();
|
||||
if(lastExpect.compareTo(expect) < 0) {
|
||||
return expect;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package lottery.domains.capture.utils.open;
|
||||
|
||||
import javautils.date.DateUtil;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class OpenTimeTransUtil {
|
||||
|
||||
/**
|
||||
* <p>根据时间转换成对应期数,refDate和refExpect必须按规则配置</p>
|
||||
* <p>韩国1.5分彩某天第1期在这里看:http://www.jlotto.kr/keno.aspx?method=kenoWinNoList</p>
|
||||
* <p>北京快乐8某天第1期在这里看:http://www.bwlc.net/bulletin/prevkeno.html?dates=2016-09-16&page=3</p>
|
||||
* <p>北京PK10某天第1期在这里看:http://www.bwlc.net/bulletin/prevtrax.html?dates=2016-09-16&page=2</p>
|
||||
* <br/>
|
||||
*
|
||||
* <ul>
|
||||
* <li>韩国1.5分彩 2016-09-16第1期号1638731</li>
|
||||
* <li>北京快乐8 2016-09-16第1期号781437</li>
|
||||
* <li>北京PK10 2016-09-16第1期号576009</li>
|
||||
* </ul>
|
||||
* @param expect 当前开奖时间,配置在数据库的,未经过计算的
|
||||
* @param refDate 引用日期,用来计算的日期,随便配置,引用日期必须要大于要转换的日期
|
||||
* @param refExpect 引用期数,引用期数必须是引用日期当天的第1期期号
|
||||
* @param times 每天多少期
|
||||
* @return 返回计算后的对象
|
||||
*/
|
||||
public static String trans(String expect, String refDate, int refExpect, int times) {
|
||||
String calcExpect = expect;
|
||||
|
||||
// 当前日期
|
||||
Date currentDate = DateUtil.stringToDate(calcExpect.substring(0, 8), "yyyyMMdd");
|
||||
// 当天第多少期
|
||||
int currentTimes = Integer.valueOf(calcExpect.substring(9));
|
||||
|
||||
// 引用日期时间对象
|
||||
Date refDateDate = DateUtil.stringToDate(refDate, "yyyy-MM-dd");
|
||||
|
||||
// 相差天数=当前日期-引用日期
|
||||
int disDays = DateUtil.calcDays(currentDate, refDateDate);
|
||||
|
||||
// 相差期数 = 相差天数 * 每天期数 + 当天第多少期
|
||||
int disTimes = disDays * times + currentTimes;
|
||||
|
||||
// 最终期数 = 引用期数+相差期数
|
||||
int finalExpect = refExpect + disTimes;
|
||||
|
||||
return String.valueOf(finalExpect);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换期数,例如659897,转换后20141024-001
|
||||
*
|
||||
* @param realExpect 计算期数
|
||||
* @param refDate 引用日期
|
||||
* @param refExpect 引用期数
|
||||
* @param times 每天多少期
|
||||
* @return
|
||||
*/
|
||||
public static String trans(int realExpect, String refDate, int refExpect, int times) {
|
||||
// 相差期数 = 当前期数 - 引用期数
|
||||
int disTimes = realExpect - refExpect;
|
||||
|
||||
// 相差天数 = 相差期数 / 每天期数
|
||||
int disDays = disTimes / times;
|
||||
|
||||
// 剩余期数 = 相差期数 % 每天期数
|
||||
int remainTimes = disTimes % times;
|
||||
|
||||
// 计算后的日期
|
||||
String currentDate = DateUtil.calcNewDay(refDate, disDays);
|
||||
|
||||
return currentDate.replace("-", "") + "-" + String.format("%03d", remainTimes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package lottery.domains.capture.utils.open;
|
||||
|
||||
public interface OpenTimeUtil {
|
||||
|
||||
String getCurrExpect(String lotteryName, String currTime);
|
||||
|
||||
String getNextExpect(String lotteryName, String lastExpect);
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package lottery.domains.content.biz;
|
||||
|
||||
import lottery.domains.content.entity.LotteryOpenCode;
|
||||
|
||||
/**
|
||||
* Created by Nick on 2017/2/25.
|
||||
*/
|
||||
public interface LotteryOpenCodeService {
|
||||
/**
|
||||
* 初始化开奖号码到redis中
|
||||
*/
|
||||
void initLotteryOpenCode();
|
||||
|
||||
/**
|
||||
* 查看某个彩种彩期是否已经抓取过了
|
||||
*/
|
||||
boolean hasCaptured(String lotteryName, String expect);
|
||||
|
||||
/**
|
||||
* 新增开奖号码,并放入到redis中
|
||||
*/
|
||||
boolean add(LotteryOpenCode entity, boolean txffcAssistLast);
|
||||
|
||||
LotteryOpenCode get(String lottery, String expect);
|
||||
}
|
||||
+184
@@ -0,0 +1,184 @@
|
||||
package lottery.domains.content.biz.impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javautils.redis.JedisTemplate;
|
||||
import lottery.domains.capture.utils.open.LotteryOpenUtil;
|
||||
import lottery.domains.content.biz.LotteryOpenCodeService;
|
||||
import lottery.domains.content.dao.LotteryDao;
|
||||
import lottery.domains.content.dao.LotteryOpenCodeDao;
|
||||
import lottery.domains.content.entity.Lottery;
|
||||
import lottery.domains.content.entity.LotteryOpenCode;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.Pipeline;
|
||||
|
||||
/**
|
||||
* Created by Nick on 2017/2/25.
|
||||
*/
|
||||
@Service
|
||||
public class LotteryOpenCodeServiceImpl implements LotteryOpenCodeService {
|
||||
private static final Logger log = LoggerFactory.getLogger(LotteryOpenCodeServiceImpl.class);
|
||||
private static final String OPEN_CODE_KEY = "OPEN_CODE:%s";
|
||||
private static final int OPEN_CODE_MOST_EXPECT = 50; // 保留最近50期开奖号码
|
||||
|
||||
@Autowired
|
||||
private LotteryOpenCodeDao lotteryOpenCodeDao;
|
||||
@Autowired
|
||||
private LotteryDao lotteryDao;
|
||||
@Autowired
|
||||
private JedisTemplate jedisTemplate;
|
||||
@Autowired
|
||||
private LotteryOpenUtil lotteryOpenUtil;
|
||||
|
||||
private static ConcurrentHashMap<String, Boolean> CODE_CAPTURE_CACHE = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
@Override
|
||||
public void initLotteryOpenCode() {
|
||||
// 加载所有彩票
|
||||
List<Lottery> lotteries = lotteryDao.listAll();
|
||||
for (Lottery lottery : lotteries) {
|
||||
if (lottery.getId() == 117) continue; // 急速秒秒彩不设置
|
||||
|
||||
initLotteryOpenCodeByLottery(lottery);
|
||||
}
|
||||
}
|
||||
|
||||
private void initLotteryOpenCodeByLottery(final Lottery lottery) {
|
||||
final List<LotteryOpenCode> openCodes = lotteryOpenCodeDao.getLatest(lottery.getShortName(), OPEN_CODE_MOST_EXPECT);
|
||||
|
||||
final String key = String.format(OPEN_CODE_KEY, lottery.getShortName());
|
||||
|
||||
// 批量设置值
|
||||
final TreeMap<String, String> values = new TreeMap<>();
|
||||
for (LotteryOpenCode openCode : openCodes) {
|
||||
values.put(openCode.getExpect(), openCode.getCode());
|
||||
}
|
||||
//
|
||||
// // 倒序
|
||||
// final NavigableMap<String, String> descendingMap = values.descendingMap();
|
||||
|
||||
// 执行pipeline是为保证最大程度减少redis中没有值的情况,比如删除后重新设置前,那么redis此时处于真空状态
|
||||
jedisTemplate.execute(new JedisTemplate.JedisActionNoResult() {
|
||||
@Override
|
||||
public void action(Jedis jedis) {
|
||||
Pipeline pipelined = jedis.pipelined();
|
||||
|
||||
// 删除原开奖号码
|
||||
pipelined.del(key);
|
||||
|
||||
// 重新设置到redis
|
||||
for (String expect : values.keySet()) {
|
||||
pipelined.hset(key, expect, values.get(expect));
|
||||
}
|
||||
|
||||
// 执行
|
||||
pipelined.sync();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCaptured(String lotteryName, String expect) {
|
||||
LotteryOpenCode openCode = lotteryOpenCodeDao.get(lotteryName, expect);
|
||||
return openCode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean add(LotteryOpenCode entity, boolean txffcAssistLast) {
|
||||
String cacheKey = entity.getLottery() + "_" + entity.getExpect();
|
||||
if (CODE_CAPTURE_CACHE.containsKey(cacheKey)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean hasCaptured = hasCaptured(entity.getLottery(), entity.getExpect());
|
||||
if (hasCaptured) {
|
||||
CODE_CAPTURE_CACHE.put(cacheKey, true);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (txffcAssistLast && "txffc".equals(entity.getLottery())) {
|
||||
String lastExpect = lotteryOpenUtil.subtractExpect(entity.getLottery(), entity.getExpect());
|
||||
LotteryOpenCode lastExpectCode = lotteryOpenCodeDao.get(entity.getLottery(), lastExpect);
|
||||
if (lastExpectCode == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (entity.getCode().equals(lastExpectCode.getCode())) {
|
||||
entity.setOpenStatus(2);
|
||||
}
|
||||
}
|
||||
|
||||
boolean added = lotteryOpenCodeDao.add(entity);
|
||||
if (!added) {
|
||||
return false;
|
||||
}
|
||||
|
||||
addedToRedis(entity);
|
||||
CODE_CAPTURE_CACHE.put(cacheKey, true);
|
||||
|
||||
return added;
|
||||
}
|
||||
|
||||
private void addedToRedis(LotteryOpenCode entity) {
|
||||
// 操作redis
|
||||
String key = String.format(OPEN_CODE_KEY, entity.getLottery());
|
||||
|
||||
// 删除redis中多余的期数,始终保持50期
|
||||
Set<String> hkeys = jedisTemplate.hkeys(key);
|
||||
if (CollectionUtils.isEmpty(hkeys)) {
|
||||
// 最新的设置进去
|
||||
jedisTemplate.hset(key, entity.getExpect(), entity.getCode());
|
||||
}
|
||||
else {
|
||||
TreeSet<String> sortHKeys = new TreeSet<>(hkeys);
|
||||
|
||||
// 先设置进去
|
||||
String[] expects = sortHKeys.toArray(new String[]{});
|
||||
String firstExpect = expects[0];
|
||||
if (entity.getExpect().compareTo(firstExpect) > 0) {
|
||||
jedisTemplate.hset(key, entity.getExpect(), entity.getCode());
|
||||
sortHKeys.add(entity.getExpect());
|
||||
}
|
||||
|
||||
if (CollectionUtils.isNotEmpty(sortHKeys) && sortHKeys.size() > OPEN_CODE_MOST_EXPECT) {
|
||||
int exceedSize = sortHKeys.size() - OPEN_CODE_MOST_EXPECT;
|
||||
|
||||
Iterator<String> iterator = sortHKeys.iterator();
|
||||
int count = 0;
|
||||
List<String> delFields = new ArrayList<>();
|
||||
while (iterator.hasNext()) {
|
||||
if (count >= exceedSize) {
|
||||
break;
|
||||
}
|
||||
|
||||
delFields.add(iterator.next());
|
||||
iterator.remove();
|
||||
count++;
|
||||
}
|
||||
|
||||
// 删除多余的
|
||||
jedisTemplate.hdel(key, delFields.toArray(new String[]{}));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LotteryOpenCode get(String lottery, String expect) {
|
||||
return lotteryOpenCodeDao.get(lottery, expect);
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package lottery.domains.content.dao;
|
||||
|
||||
import lottery.domains.content.entity.LotteryCrawlerStatus;
|
||||
|
||||
public interface LotteryCrawlerStatusDao {
|
||||
|
||||
LotteryCrawlerStatus get(String lottery);
|
||||
|
||||
boolean update(String shortName, String lastExpect, String lastUpdate);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package lottery.domains.content.dao;
|
||||
|
||||
import lottery.domains.content.entity.Lottery;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface LotteryDao {
|
||||
|
||||
List<Lottery> listAll();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package lottery.domains.content.dao;
|
||||
|
||||
import lottery.domains.content.entity.LotteryOpenCode;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface LotteryOpenCodeDao {
|
||||
|
||||
LotteryOpenCode get(String lottery, String expect);
|
||||
|
||||
boolean add(LotteryOpenCode entity);
|
||||
|
||||
List<LotteryOpenCode> getLatest(String lotteryName, int count);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package lottery.domains.content.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import lottery.domains.content.entity.LotteryOpenTime;
|
||||
|
||||
public interface LotteryOpenTimeDao {
|
||||
|
||||
List<LotteryOpenTime> listAll();
|
||||
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package lottery.domains.content.dao.impl;
|
||||
|
||||
import javautils.jdbc.hibernate.HibernateSuperDao;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import lottery.domains.content.dao.LotteryCrawlerStatusDao;
|
||||
import lottery.domains.content.entity.LotteryCrawlerStatus;
|
||||
|
||||
@Repository
|
||||
public class LotteryCrawlerStatusDaoImpl implements LotteryCrawlerStatusDao {
|
||||
|
||||
private final String tab = LotteryCrawlerStatus.class.getSimpleName();
|
||||
|
||||
@Autowired
|
||||
private HibernateSuperDao<LotteryCrawlerStatus> superDao;
|
||||
|
||||
@Override
|
||||
public LotteryCrawlerStatus get(String shortName) {
|
||||
String hql = "from " + tab + " where shortName = ?0";
|
||||
Object[] values = {shortName};
|
||||
return (LotteryCrawlerStatus) superDao.unique(hql, values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean update(String shortName, String lastExpect, String lastUpdate) {
|
||||
String hql = "update " + tab + " set lastExpect = ?1, lastUpdate = ?2 where shortName = ?0";
|
||||
Object[] values = {shortName, lastExpect, lastUpdate};
|
||||
return superDao.update(hql, values);
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user