├── .gitignore ├── .project ├── .settings ├── org.eclipse.core.resources.prefs └── org.eclipse.m2e.core.prefs ├── LICENSE ├── README.md ├── jsoup_demo ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── pom.xml └── src │ ├── main │ └── java │ │ └── org │ │ ├── jsoup │ │ └── entity │ │ │ ├── Flower.java │ │ │ └── FlowerCategory.java │ │ └── jsoup_demo │ │ └── MyJsoup.java │ └── test │ └── java │ └── org │ └── jsoup_demo │ ├── AppTest.java │ └── TestJsoup.java ├── mina_demo ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── pom.xml └── src │ ├── main │ └── java │ │ └── org │ │ └── mina_demo │ │ ├── MimaTimeClient.java │ │ ├── MinaTimeServer.java │ │ ├── TimeClientHander.java │ │ └── TimeServerHandler.java │ └── test │ └── java │ └── org │ └── mina_demo │ └── AppTest.java ├── pom.xml ├── quartz_demo ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── .jsdtscope │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ ├── org.eclipse.m2e.core.prefs │ ├── org.eclipse.wst.common.component │ ├── org.eclipse.wst.common.project.facet.core.xml │ ├── org.eclipse.wst.jsdt.ui.superType.container │ ├── org.eclipse.wst.jsdt.ui.superType.name │ └── org.eclipse.wst.validation.prefs ├── pom.xml ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── quartz │ │ │ │ ├── base │ │ │ │ └── task │ │ │ │ │ ├── Handx.java │ │ │ │ │ └── QuartzJobFactory.java │ │ │ │ ├── controller │ │ │ │ └── ScheduleJobController.java │ │ │ │ ├── dao │ │ │ │ └── ScheduleJobDao.java │ │ │ │ ├── entity │ │ │ │ └── ScheduleJob.java │ │ │ │ ├── model │ │ │ │ └── RetObj.java │ │ │ │ ├── service │ │ │ │ └── ScheduleJobService.java │ │ │ │ └── utils │ │ │ │ ├── SpringUtils.java │ │ │ │ └── TaskUtils.java │ │ ├── resources │ │ │ ├── config.properties │ │ │ ├── log4j.properties │ │ │ └── spring.xml │ │ └── webapp │ │ │ ├── WEB-INF │ │ │ ├── spring-mvc.xml │ │ │ ├── views │ │ │ │ └── jobList.jsp │ │ │ └── web.xml │ │ │ ├── js │ │ │ └── jquery-1.9.1.min.js │ │ │ └── sql │ │ │ └── schedule_job.sql │ └── test │ │ ├── java │ │ └── com │ │ │ └── quartz │ │ │ └── service │ │ │ ├── TaskMain.java │ │ │ └── TaskService.java │ │ └── resources │ │ └── spring-quartz.xml └── target │ ├── classes │ ├── config.properties │ ├── log4j.properties │ └── spring.xml │ ├── m2e-wtp │ └── web-resources │ │ └── META-INF │ │ ├── MANIFEST.MF │ │ └── maven │ │ └── com.framework │ │ └── quartz_demo │ │ ├── pom.properties │ │ └── pom.xml │ └── test-classes │ └── spring-quartz.xml ├── restful_demo ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── .jsdtscope │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ ├── org.eclipse.m2e.core.prefs │ ├── org.eclipse.wst.common.component │ ├── org.eclipse.wst.common.project.facet.core.xml │ ├── org.eclipse.wst.jsdt.ui.superType.container │ ├── org.eclipse.wst.jsdt.ui.superType.name │ └── org.eclipse.wst.validation.prefs ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── framework │ │ ├── comm │ │ └── RestResourceConfig.java │ │ ├── model │ │ └── User.java │ │ └── rest │ │ ├── filter │ │ ├── RestAuthRequestFilter.java │ │ └── RestResponseFilter.java │ │ └── service │ │ └── UserRestService.java │ └── webapp │ ├── WEB-INF │ └── web.xml │ └── index.jsp └── spring_demo ├── .classpath ├── .gitignore ├── .project ├── .settings ├── org.eclipse.core.resources.prefs ├── org.eclipse.jdt.core.prefs └── org.eclipse.m2e.core.prefs ├── README.md ├── pom.xml └── src └── main ├── java └── org │ └── handx │ ├── constructor │ ├── ioc │ ├── IocBeanConfigSimple.java │ └── SequenceGenerator.java │ └── scanning │ ├── Main.java │ ├── Sequence.java │ ├── SequenceDao.java │ ├── SequenceDaoImpl.java │ └── SequenceService.java └── resources ├── log4j.properties ├── spring-constructor.xml ├── spring-ioc.xml └── spring-scanning.xml /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | frameworkAggregate 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.m2e.core.maven2Builder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.m2e.core.maven2Nature 16 | 17 | 18 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding/=UTF-8 3 | -------------------------------------------------------------------------------- /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 框架学习 2 | 3 | 4 | > 觉得不错的朋友可以点下star,watch,fork也算是对我的鼓励了。 5 | 6 | ## 一、quartz框架的使用 7 | - 简单的定时任务配置 8 | - 动态定时任务配置 9 | - 定时任务的添加,修改cron,开启、停止等功能。 10 | 11 | ## 二、使用Jsoup爬取数据 12 | 13 | - 语法学习 [jsoup中文教程](http://www.open-open.com/jsoup/) 14 | - 实战爬取数据(简单的静态网页解析) 15 | - 爬去京东数据实战 [爬去京东数据](https://github.com/handexing/JdBee) 16 | 17 | ## 三、spring框架学习 18 | 19 | ## 四、Apache mina 20 | 21 | ## 五、restFul 案例 22 | - JAX-RS (JSR-311) 是为 Java EE 环境下的 RESTful 服务能力提供的一种规范。它能提供对传统的基于 SOAP 的 Web 服务的一种可行替代。 23 | 24 | ### 注解说明 25 | - @Path:注释提供了一个 value属性,用来表明此资源所在的路径。(可用于类头或方法头上) 26 | - @GET、@POST、@PUT、@DELETE 以及 @HEAD 均是 HTTP 请求方法指示符注释。 27 | 28 | > HTTP GET:获取/列出/检索单个资源或资源集合。 29 | > HTTP POST:新建资源。 30 | > HTTP PUT:更新现有资源或资源集合。 31 | > HTTP DELETE:删除资源或资源集合。 32 | 33 | - @Produces:注释代表的是一个资源可以返回的 MIME 类型。 34 | - @Consumes:注释代表的是一个资源可以接受的 MIME 类型。 35 | - @PathParam("userId"):该注释将参数注入方法参数的路径,在本例中就是用户 id。其他可用的注释有 @FormParam、@QueryParam 等。 36 | - @Context: 使用该注释注入上下文对象,比如 Request、Response、UriInfo、ServletContext 等。 37 | 38 | > 运行访问 `http://127.0.0.1:8888/restful_demo/rs/user/1` 39 | 40 | > 会不定时学习框架,持续学习。觉得不错的朋友可以点下star,watch,fork也算是对我的鼓励了。QQ群:爱码仕 - 49422384 -------------------------------------------------------------------------------- /jsoup_demo/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /jsoup_demo/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /jsoup_demo/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | jsoup_demo 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /jsoup_demo/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/test/java=UTF-8 4 | encoding/=UTF-8 5 | -------------------------------------------------------------------------------- /jsoup_demo/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.5 6 | -------------------------------------------------------------------------------- /jsoup_demo/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /jsoup_demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | com.framework 8 | frameworkAggregate 9 | 0.0.1-SNAPSHOT 10 | ../pom.xml 11 | 12 | 13 | jsoup_demo 14 | jsoup_demo 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | junit 23 | junit 24 | 3.8.1 25 | test 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /jsoup_demo/src/main/java/org/jsoup/entity/Flower.java: -------------------------------------------------------------------------------- 1 | package org.jsoup.entity; 2 | 3 | /** 4 | * @ClassName: Flower 5 | * @Description: 花实体类 6 | * @author handx 908716835@qq.com 7 | * @date 2017年5月14日 下午2:19:56 8 | * 9 | */ 10 | 11 | public class Flower { 12 | 13 | private Long id; 14 | private String name; 15 | private String url; 16 | private String imgPath; 17 | private String nick; 18 | private String eng; 19 | private String type; 20 | private String genus; 21 | private String blooming; 22 | 23 | private String introduce;// 介绍 24 | private String morphological;// 形态特征 25 | private String ecologicalHabit;// 生态习性 26 | private String cultivationTechniques;// 栽培技术 27 | private String reproductiveMode;// 繁殖方式 28 | private String cutFlower;// 切花采收 29 | private String diseaseControl;// 病害防治 30 | private String distributionArea;// 分布区域 31 | private String foodTherapy;// 食疗或药用价值 32 | private String gardenUse;// 园林用途 33 | private String functionUse;// 作用用途 34 | private String culturalBackground;// 文化背景 35 | 36 | // 描述实在是太多了... 37 | 38 | public String getBlooming() { 39 | return blooming; 40 | } 41 | 42 | public String getCultivationTechniques() { 43 | return cultivationTechniques; 44 | } 45 | 46 | public String getCulturalBackground() { 47 | return culturalBackground; 48 | } 49 | 50 | public String getCutFlower() { 51 | return cutFlower; 52 | } 53 | 54 | public String getDiseaseControl() { 55 | return diseaseControl; 56 | } 57 | 58 | public String getDistributionArea() { 59 | return distributionArea; 60 | } 61 | 62 | public String getEcologicalHabit() { 63 | return ecologicalHabit; 64 | } 65 | 66 | public String getEng() { 67 | return eng; 68 | } 69 | 70 | public String getFoodTherapy() { 71 | return foodTherapy; 72 | } 73 | 74 | public String getFunctionUse() { 75 | return functionUse; 76 | } 77 | 78 | public String getGardenUse() { 79 | return gardenUse; 80 | } 81 | 82 | public String getGenus() { 83 | return genus; 84 | } 85 | 86 | public Long getId() { 87 | return id; 88 | } 89 | 90 | public String getImgPath() { 91 | return imgPath; 92 | } 93 | 94 | public String getIntroduce() { 95 | return introduce; 96 | } 97 | 98 | public String getMorphological() { 99 | return morphological; 100 | } 101 | 102 | public String getName() { 103 | return name; 104 | } 105 | 106 | public String getNick() { 107 | return nick; 108 | } 109 | 110 | public String getReproductiveMode() { 111 | return reproductiveMode; 112 | } 113 | 114 | public String getType() { 115 | return type; 116 | } 117 | 118 | public String getUrl() { 119 | return url; 120 | } 121 | 122 | public void setBlooming(String blooming) { 123 | this.blooming = blooming; 124 | } 125 | 126 | public void setCultivationTechniques(String cultivationTechniques) { 127 | this.cultivationTechniques = cultivationTechniques; 128 | } 129 | 130 | public void setCulturalBackground(String culturalBackground) { 131 | this.culturalBackground = culturalBackground; 132 | } 133 | 134 | public void setCutFlower(String cutFlower) { 135 | this.cutFlower = cutFlower; 136 | } 137 | 138 | public void setDiseaseControl(String diseaseControl) { 139 | this.diseaseControl = diseaseControl; 140 | } 141 | 142 | public void setDistributionArea(String distributionArea) { 143 | this.distributionArea = distributionArea; 144 | } 145 | 146 | public void setEcologicalHabit(String ecologicalHabit) { 147 | this.ecologicalHabit = ecologicalHabit; 148 | } 149 | 150 | public void setEng(String eng) { 151 | this.eng = eng; 152 | } 153 | 154 | public void setFoodTherapy(String foodTherapy) { 155 | this.foodTherapy = foodTherapy; 156 | } 157 | 158 | public void setFunctionUse(String functionUse) { 159 | this.functionUse = functionUse; 160 | } 161 | 162 | public void setGardenUse(String gardenUse) { 163 | this.gardenUse = gardenUse; 164 | } 165 | 166 | public void setGenus(String genus) { 167 | this.genus = genus; 168 | } 169 | 170 | public void setId(Long id) { 171 | this.id = id; 172 | } 173 | 174 | public void setImgPath(String imgPath) { 175 | this.imgPath = imgPath; 176 | } 177 | 178 | public void setIntroduce(String introduce) { 179 | this.introduce = introduce; 180 | } 181 | 182 | public void setMorphological(String morphological) { 183 | this.morphological = morphological; 184 | } 185 | 186 | public void setName(String name) { 187 | this.name = name; 188 | } 189 | 190 | public void setNick(String nick) { 191 | this.nick = nick; 192 | } 193 | 194 | public void setReproductiveMode(String reproductiveMode) { 195 | this.reproductiveMode = reproductiveMode; 196 | } 197 | 198 | public void setType(String type) { 199 | this.type = type; 200 | } 201 | 202 | public void setUrl(String url) { 203 | this.url = url; 204 | } 205 | 206 | @Override 207 | public String toString() { 208 | return "Flower [id=" + id + ", name=" + name + ", url=" + url + ", imgPath=" + imgPath + ", nick=" + nick 209 | + ", eng=" + eng + ", type=" + type + ", genus=" + genus + ", blooming=" + blooming + "]"; 210 | } 211 | 212 | 213 | } 214 | -------------------------------------------------------------------------------- /jsoup_demo/src/main/java/org/jsoup/entity/FlowerCategory.java: -------------------------------------------------------------------------------- 1 | package org.jsoup.entity; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * @ClassName: FlowerCategory 7 | * @Description: 花的品类 8 | * @author handx 908716835@qq.com 9 | * @date 2017年5月14日 下午2:20:33 10 | * 11 | */ 12 | 13 | public class FlowerCategory { 14 | 15 | private Integer id; 16 | private String name; 17 | private String url; 18 | private String imgPath; 19 | private Date createTime; 20 | 21 | public Date getCreateTime() { 22 | return createTime; 23 | } 24 | 25 | public Integer getId() { 26 | return id; 27 | } 28 | 29 | public String getImgPath() { 30 | return imgPath; 31 | } 32 | 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | public String getUrl() { 38 | return url; 39 | } 40 | 41 | public void setCreateTime(Date createTime) { 42 | this.createTime = createTime; 43 | } 44 | 45 | public void setId(Integer id) { 46 | this.id = id; 47 | } 48 | 49 | public void setImgPath(String imgPath) { 50 | this.imgPath = imgPath; 51 | } 52 | 53 | public void setName(String name) { 54 | this.name = name; 55 | } 56 | 57 | public void setUrl(String url) { 58 | this.url = url; 59 | } 60 | 61 | @Override 62 | public String toString() { 63 | return "FlowerCategory [id=" + id + ", name=" + name + ", url=" + url + ", imgPath=" + imgPath + ", createTime=" 64 | + createTime + "]"; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /jsoup_demo/src/main/java/org/jsoup_demo/MyJsoup.java: -------------------------------------------------------------------------------- 1 | package org.jsoup_demo; 2 | 3 | import org.jsoup.Jsoup; 4 | import org.jsoup.entity.Flower; 5 | import org.jsoup.entity.FlowerCategory; 6 | import org.jsoup.nodes.Document; 7 | import org.jsoup.nodes.Element; 8 | import org.jsoup.nodes.Node; 9 | import org.jsoup.select.Elements; 10 | 11 | import java.io.IOException; 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | /** 16 | * @Description: jsoup联系爬取花朵 17 | * @author handx 908716835@qq.com 18 | * @date 2017年5月14日 下午2:18:45 19 | * 20 | */ 21 | 22 | public class MyJsoup { 23 | 24 | private static List getCategoryList() { 25 | 26 | List categories = new ArrayList(); 27 | 28 | try { 29 | Document doc = Jsoup.connect("http://www.aihuhua.com/baike/").get(); 30 | Elements catelist = doc.getElementsByClass("catelist"); 31 | Element cates = catelist.first(); 32 | List childNodes = cates.childNodes(); 33 | for (int i = 0; i < childNodes.size(); i++) { 34 | Node node = childNodes.get(i); 35 | List childs = node.childNodes(); 36 | if (childs != null && childs.size() > 0) { 37 | FlowerCategory category = new FlowerCategory(); 38 | for (int j = 0; j < childs.size(); j++) { 39 | Node child = childs.get(j); 40 | if ("a".equals(child.nodeName())) { 41 | category.setUrl(child.attr("href")); 42 | category.setImgPath(child.childNode(1).attr("src")); 43 | } else if ("h2".equals(child.nodeName())) { 44 | category.setName(child.attr("title")); 45 | } 46 | } 47 | categories.add(category); 48 | } 49 | } 50 | } catch (IOException e) { 51 | e.printStackTrace(); 52 | } 53 | 54 | return categories; 55 | } 56 | 57 | public static void main(String[] args) throws InterruptedException { 58 | 59 | long startTime = System.currentTimeMillis(); // 获取开始时间 60 | 61 | System.out.println("开始爬取类别...."); 62 | List categoryList = getCategoryList(); 63 | long endTime = System.currentTimeMillis(); // 获取结束时间 64 | System.out.println("类别爬取完毕....爬取时间:" + (endTime - startTime)); 65 | 66 | List flowers = new ArrayList(); 67 | 68 | startTime = System.currentTimeMillis(); // 获取开始时间 69 | for (FlowerCategory cates : categoryList) { 70 | System.out.println("正在爬取....路径为:" + cates.getUrl()); 71 | 72 | try { 73 | Document doc = Jsoup.connect(cates.getUrl()).get(); 74 | Elements elementsByClass = doc.getElementsByClass("cate_list"); 75 | 76 | // 要给【FlowerCategory】修改,添加简介说明 77 | String cateName = elementsByClass.select("h1").text(); 78 | // System.out.println("简介:" + 79 | // elementsByClass.select(".cont").text()); 80 | 81 | Elements firstList = elementsByClass.select(".list"); 82 | Elements elements = firstList.select("li"); 83 | for (Element element : elements) { 84 | Flower flower = new Flower(); 85 | flower.setName(element.select("a").attr("title")); 86 | flower.setNick(element.select("label").last().text()); 87 | flower.setUrl(element.select("a").attr("href")); 88 | flower.setImgPath(element.select("img").attr("src")); 89 | flowers.add(flower); 90 | } 91 | 92 | System.out.println("获取成功,正在解析类别为【" + cateName + "】"); 93 | 94 | // 如果有分页,循环解析 95 | int isPagination = elementsByClass.select(".pagination a").size(); 96 | if (isPagination > 0) { 97 | 98 | String paginationText = elementsByClass.select(".pagination a") 99 | .eq(elementsByClass.select(".pagination a").size() - 2).text(); 100 | 101 | int paginationTotal; 102 | if (isPagination >= 7) { 103 | paginationTotal = Integer.parseInt(paginationText.substring(2, paginationText.length())); 104 | } else { 105 | paginationTotal = Integer.parseInt(paginationText); 106 | } 107 | 108 | String urlCate = elementsByClass.select(".pagination a").eq(1).attr("href").split("/")[2]; 109 | 110 | List urls = new ArrayList(); 111 | 112 | System.out.println("开始解析分页路径...."); 113 | for (int i = 1; i < paginationTotal; i++) { 114 | String url = "http://www.aihuhua.com/baike/" + urlCate + "/page-" + i + ".html"; 115 | urls.add(url); 116 | } 117 | 118 | System.out.println("路径解析完毕,共" + paginationTotal + "页,开始爬取..."); 119 | 120 | for (String url : urls) { 121 | 122 | doc = Jsoup.connect(url).get(); 123 | elementsByClass = doc.getElementsByClass("cate_list"); 124 | firstList = elementsByClass.select(".list"); 125 | elements = firstList.select("li"); 126 | for (Element element : elements) { 127 | Flower flower = new Flower(); 128 | flower.setName(element.select("a").attr("title")); 129 | flower.setNick(element.select("label").last().text()); 130 | flower.setUrl(element.select("a").attr("href")); 131 | flower.setImgPath(element.select("img").attr("src")); 132 | flowers.add(flower); 133 | } 134 | } 135 | 136 | System.out.println(cateName + "类别解析完毕!"); 137 | } 138 | } catch (IOException e) { 139 | e.printStackTrace(); 140 | } 141 | } 142 | 143 | endTime = System.currentTimeMillis(); // 获取结束时间 144 | System.out.println("解析完毕,共解析数据:" + flowers.size() + "条。" + "运行时间:" + (endTime - startTime)); 145 | } 146 | 147 | 148 | } 149 | -------------------------------------------------------------------------------- /jsoup_demo/src/test/java/org/jsoup_demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package org.jsoup_demo; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest extends TestCase { 11 | /** 12 | * @return the suite of tests being tested 13 | */ 14 | public static Test suite() { 15 | return new TestSuite(AppTest.class); 16 | } 17 | 18 | /** 19 | * Create the test case 20 | * 21 | * @param testName 22 | * name of the test case 23 | */ 24 | public AppTest(String testName) { 25 | super(testName); 26 | } 27 | 28 | /** 29 | * Rigourous Test :-) 30 | */ 31 | public void testApp() { 32 | assertTrue(true); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /jsoup_demo/src/test/java/org/jsoup_demo/TestJsoup.java: -------------------------------------------------------------------------------- 1 | package org.jsoup_demo; 2 | 3 | import org.jsoup.Jsoup; 4 | import org.jsoup.entity.Flower; 5 | import org.jsoup.entity.FlowerCategory; 6 | import org.jsoup.nodes.Document; 7 | import org.jsoup.nodes.Element; 8 | import org.jsoup.nodes.Node; 9 | import org.jsoup.select.Elements; 10 | 11 | import java.io.IOException; 12 | import java.util.ArrayList; 13 | import java.util.HashMap; 14 | import java.util.Iterator; 15 | import java.util.List; 16 | import java.util.Map; 17 | 18 | public class TestJsoup { 19 | 20 | // 爬取类别 21 | private static List getCategoryList() { 22 | 23 | List categories = new ArrayList(); 24 | 25 | try { 26 | Document doc = Jsoup.connect("http://www.aihuhua.com/baike/").get(); 27 | Elements catelist = doc.getElementsByClass("catelist"); 28 | Element cates = catelist.first(); 29 | List childNodes = cates.childNodes(); 30 | for (int i = 0; i < childNodes.size(); i++) { 31 | Node node = childNodes.get(i); 32 | List childs = node.childNodes(); 33 | if (childs != null && childs.size() > 0) { 34 | FlowerCategory category = new FlowerCategory(); 35 | for (int j = 0; j < childs.size(); j++) { 36 | Node child = childs.get(j); 37 | if ("a".equals(child.nodeName())) { 38 | category.setUrl(child.attr("href")); 39 | category.setImgPath(child.childNode(1).attr("src")); 40 | } else if ("h2".equals(child.nodeName())) { 41 | category.setName(child.attr("title")); 42 | } 43 | } 44 | categories.add(category); 45 | } 46 | } 47 | } catch (IOException e) { 48 | e.printStackTrace(); 49 | } 50 | 51 | return categories; 52 | } 53 | 54 | // 爬取类别下具体花的url 55 | private static List getFlowerList(List categoryList) { 56 | 57 | List flowers = new ArrayList(); 58 | 59 | long startTime = System.currentTimeMillis(); // 获取开始时间 60 | 61 | for (FlowerCategory cates : categoryList) { 62 | System.out.println("正在爬取....路径为:" + cates.getUrl()); 63 | 64 | try { 65 | Document doc = Jsoup.connect(cates.getUrl()).get(); 66 | Elements elementsByClass = doc.getElementsByClass("cate_list"); 67 | 68 | // 要给【FlowerCategory】修改,添加简介说明 69 | String cateName = elementsByClass.select("h1").text(); 70 | // System.out.println("简介:" + 71 | // elementsByClass.select(".cont").text()); 72 | 73 | Elements firstList = elementsByClass.select(".list"); 74 | Elements elements = firstList.select("li"); 75 | for (Element element : elements) { 76 | flowers.add(element.select("a").attr("href")); 77 | } 78 | 79 | System.out.println("获取成功,正在解析类别为【" + cateName + "】"); 80 | 81 | // 如果有分页,循环解析 82 | int isPagination = elementsByClass.select(".pagination a").size(); 83 | if (isPagination > 0) { 84 | 85 | String paginationText = elementsByClass.select(".pagination a") 86 | .eq(elementsByClass.select(".pagination a").size() - 2).text(); 87 | 88 | int paginationTotal; 89 | if (isPagination >= 7) { 90 | paginationTotal = Integer.parseInt(paginationText.substring(2, paginationText.length())); 91 | } else { 92 | paginationTotal = Integer.parseInt(paginationText); 93 | } 94 | 95 | String urlCate = elementsByClass.select(".pagination a").eq(1).attr("href").split("/")[2]; 96 | 97 | List urls = new ArrayList(); 98 | 99 | System.out.println("开始解析分页路径...."); 100 | for (int i = 1; i < paginationTotal; i++) { 101 | String url = "http://www.aihuhua.com/baike/" + urlCate + "/page-" + i + ".html"; 102 | urls.add(url); 103 | } 104 | 105 | System.out.println("路径解析完毕,共" + paginationTotal + "页,开始爬取..."); 106 | 107 | for (String url : urls) { 108 | 109 | doc = Jsoup.connect(url).get(); 110 | elementsByClass = doc.getElementsByClass("cate_list"); 111 | firstList = elementsByClass.select(".list"); 112 | elements = firstList.select("li"); 113 | for (Element element : elements) { 114 | flowers.add(element.select("a").attr("href")); 115 | } 116 | } 117 | 118 | System.out.println(cateName + "类别解析完毕!"); 119 | } 120 | } catch (IOException e) { 121 | e.printStackTrace(); 122 | } 123 | } 124 | 125 | long endTime = System.currentTimeMillis(); // 获取结束时间 126 | System.out.println("解析完毕,共解析花朵:" + flowers.size() + "条。" + "运行时间:" + (endTime - startTime)); 127 | 128 | return flowers; 129 | } 130 | 131 | public static void main(String[] args) throws InterruptedException { 132 | 133 | long startTime = System.currentTimeMillis(); // 获取开始时间 134 | 135 | System.out.println("开始爬取类别...."); 136 | List categoryList = getCategoryList(); 137 | long endTime = System.currentTimeMillis(); // 获取结束时间 138 | System.out.println("类别爬取完毕....爬取时间:" + (endTime - startTime)); 139 | 140 | List flowerList = getFlowerList(categoryList); 141 | 142 | List Flowers = new ArrayList(); 143 | try { 144 | for (String url : flowerList) { 145 | 146 | System.out.println("开始爬取花朵详细信息..."); 147 | Document doc = Jsoup.connect(url).get(); 148 | Element contDoc = doc.getElementsByClass("cont").first(); 149 | 150 | Flower flower = new Flower(); 151 | 152 | String name = contDoc.getElementsByClass("name").attr("title"); 153 | 154 | flower.setImgPath(doc.getElementsByClass("img").select("img").attr("src")); 155 | flower.setName(name); 156 | flower.setEng(contDoc.getElementsByClass("eng").text()); 157 | 158 | Elements labels = contDoc.getElementsByTag("label"); 159 | flower.setNick(labels.get(0).text()); 160 | flower.setType(labels.get(1).text()); 161 | flower.setGenus(labels.get(2).text()); 162 | flower.setBlooming(labels.get(3).text()); 163 | 164 | Elements sideList = doc.getElementById("sideBox_listbox").getElementsByTag("a"); 165 | 166 | Map catalogs = new HashMap(); 167 | 168 | for (Element element : sideList) { 169 | String title = element.attr("title"); 170 | String catalogId = element.attr("href"); 171 | catalogs.put(title, catalogId.substring(1, catalogId.length())); 172 | } 173 | 174 | Iterator iter = catalogs.keySet().iterator(); 175 | while (iter.hasNext()) { 176 | String title = iter.next(); 177 | String id = catalogs.get(title); 178 | 179 | Element element = doc.getElementById(id); 180 | title = title.substring(name.length(), title.length()); 181 | 182 | if ("介绍".equals(title)) { 183 | flower.setIntroduce(element.text()); 184 | } else if ("形态特征".equals(title)) { 185 | flower.setMorphological(element.text()); 186 | } else if ("生态习性".equals(title)) { 187 | flower.setEcologicalHabit(element.text()); 188 | } else if ("栽培技术".equals(title)) { 189 | flower.setCultivationTechniques(element.text()); 190 | } else if ("繁殖方式".equals(title)) { 191 | flower.setReproductiveMode(element.text()); 192 | } else if ("切花采收".equals(title)) { 193 | flower.setCutFlower(element.text()); 194 | } else if ("病害防治".equals(title)) { 195 | flower.setDiseaseControl(element.text()); 196 | } else if ("分布区域".equals(title)) { 197 | flower.setDistributionArea(element.text()); 198 | } else if ("食疗或药用价值".equals(title)) { 199 | flower.setFoodTherapy(element.text()); 200 | } else if ("园林用途".equals(title)) { 201 | flower.setGardenUse(element.text()); 202 | } else if ("作用用途".equals(title)) { 203 | flower.setFunctionUse(element.text()); 204 | } else if ("文化背景".equals(title)) {// 205 | flower.setCulturalBackground(element.text()); 206 | } else { 207 | System.out.println(element.getElementsByTag("h3").attr("title") + "===title" + title); 208 | } /* 209 | * else if ("化学成分".equals(title)) { 210 | * flower.setCulturalBackground(element.text()); } else 211 | * if ("养殖方法".equals(title)) { 212 | * flower.setCulturalBackground(element.text()); } else 213 | * if ("品种分类".equals(title)) { 214 | * flower.setCulturalBackground(element.text()); } else 215 | * if ("神奇的草".equals(title)) { 216 | * flower.setCulturalBackground(element.text()); } else 217 | * if ("花语".equals(title)) { 218 | * flower.setCulturalBackground(element.text()); } else 219 | * if ("经济价值".equals(title)) { 220 | * flower.setCulturalBackground(element.text()); } else 221 | * if ("毒性结构".equals(title)) { 222 | * flower.setCulturalBackground(element.text()); } else 223 | * if ("盆栽技术".equals(title)) { 224 | * flower.setCulturalBackground(element.text()); } else 225 | * if ("鉴别方法".equals(title)) { 226 | * flower.setCulturalBackground(element.text()); } else 227 | * if ("药材相关".equals(title)) { 228 | * flower.setCulturalBackground(element.text()); } else 229 | * if ("采收储藏".equals(title)) { 230 | * flower.setCulturalBackground(element.text()); } else 231 | * if ("现代研究".equals(title)) { 232 | * flower.setCulturalBackground(element.text()); } else 233 | * if ("药材鉴别".equals(title)) { 234 | * flower.setCulturalBackground(element.text()); } else 235 | * if ("采收及储藏".equals(title)) { 236 | * flower.setCulturalBackground(element.text()); } else 237 | * if ("性状鉴别".equals(title)) { 238 | * flower.setCulturalBackground(element.text()); } else 239 | * if ("所属科属简介".equals(title)) { 240 | * flower.setCulturalBackground(element.text()); } else 241 | * if ("采收及储藏".equals(title)) { 242 | * flower.setCulturalBackground(element.text()); } else 243 | * if ("植物危害".equals(title)) { 244 | * flower.setCulturalBackground(element.text()); } else 245 | * if ("相关比较".equals(title)) { 246 | * flower.setCulturalBackground(element.text()); } else 247 | * if ("系统发育".equals(title)) { 248 | * flower.setCulturalBackground(element.text()); } else 249 | * if ("常用方法".equals(title)) { 250 | * flower.setCulturalBackground(element.text()); } else 251 | * if ("综合治理".equals(title)) { 252 | * flower.setCulturalBackground(element.text()); } else 253 | * if ("捕虫方式".equals(title)) { 254 | * flower.setCulturalBackground(element.text()); } else 255 | * if ("辨别方法".equals(title)) { 256 | * flower.setCulturalBackground(element.text()); } else 257 | * if ("相关研究".equals(title)) { 258 | * flower.setCulturalBackground(element.text()); } else 259 | * if ("相关知识".equals(title)) { 260 | * flower.setCulturalBackground(element.text()); } else 261 | * if ("科学鉴定".equals(title)) { 262 | * flower.setCulturalBackground(element.text()); } else 263 | * if ("相关研究".equals(title)) { 264 | * flower.setCulturalBackground(element.text()); } else 265 | * if ("科属分类".equals(title)) { 266 | * flower.setCulturalBackground(element.text()); } else 267 | * if ("参考文献".equals(title)) { 268 | * flower.setCulturalBackground(element.text()); } else 269 | * if ("毒性毒理".equals(title)) { 270 | * flower.setCulturalBackground(element.text()); } else 271 | * if ("保护状况".equals(title)) { 272 | * flower.setCulturalBackground(element.text()); } else 273 | * if ("研究成果".equals(title)) { 274 | * flower.setCulturalBackground(element.text()); } else 275 | * if ("杂草属性".equals(title)) { 276 | * flower.setCulturalBackground(element.text()); } else 277 | * if ("国花市花".equals(title)) { 278 | * flower.setCulturalBackground(element.text()); } else 279 | * if ("危害".equals(title)) { 280 | * flower.setCulturalBackground(element.text()); } else 281 | * if ("名称由来".equals(title)) { 282 | * flower.setCulturalBackground(element.text()); } else 283 | * if ("药用价值".equals(title)) { 284 | * flower.setCulturalBackground(element.text()); } 285 | */ 286 | } 287 | 288 | Flowers.add(flower); 289 | } 290 | 291 | System.out.println("爬取完毕!共" + Flowers.size() + "朵花!"); 292 | } catch (IOException e) { 293 | System.out.println("爬取异常!共" + Flowers.size() + "朵花!"); 294 | e.printStackTrace(); 295 | } 296 | 297 | } 298 | 299 | } 300 | -------------------------------------------------------------------------------- /mina_demo/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /mina_demo/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /mina_demo/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | mina_demo 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /mina_demo/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/test/java=UTF-8 4 | encoding/=UTF-8 5 | -------------------------------------------------------------------------------- /mina_demo/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.5 6 | -------------------------------------------------------------------------------- /mina_demo/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /mina_demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | com.framework 8 | frameworkAggregate 9 | 0.0.1-SNAPSHOT 10 | ../pom.xml 11 | 12 | 13 | mina_demo 14 | mina_demo 15 | 16 | 17 | mina_demo 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /mina_demo/src/main/java/org/mina_demo/MimaTimeClient.java: -------------------------------------------------------------------------------- 1 | package org.mina_demo; 2 | 3 | import org.apache.mina.core.future.ConnectFuture; 4 | import org.apache.mina.core.service.IoConnector; 5 | import org.apache.mina.core.session.IoSession; 6 | import org.apache.mina.filter.codec.ProtocolCodecFilter; 7 | import org.apache.mina.filter.codec.prefixedstring.PrefixedStringCodecFactory; 8 | import org.apache.mina.filter.logging.LoggingFilter; 9 | import org.apache.mina.transport.socket.nio.NioSocketConnector; 10 | 11 | import java.net.InetSocketAddress; 12 | import java.nio.charset.Charset; 13 | import java.util.Scanner; 14 | 15 | public class MimaTimeClient { 16 | 17 | private static final int PORT = 9123; 18 | 19 | public static void main(String[] args) { 20 | IoConnector connector = new NioSocketConnector(); 21 | connector.getFilterChain().addLast("logger", new LoggingFilter()); 22 | connector.getFilterChain().addLast("codec", 23 | new ProtocolCodecFilter(new PrefixedStringCodecFactory(Charset.forName("UTF-8")))); 24 | connector.setHandler(new TimeClientHander()); 25 | ConnectFuture connectFuture = connector.connect(new InetSocketAddress("127.0.0.1", PORT)); 26 | // 等待建立连接 27 | connectFuture.awaitUninterruptibly(); 28 | System.out.println("连接成功"); 29 | IoSession session = connectFuture.getSession(); 30 | Scanner sc = new Scanner(System.in); 31 | boolean quit = false; 32 | while (!quit) { 33 | String str = sc.next(); 34 | if (str.equalsIgnoreCase("quit")) { 35 | quit = true; 36 | } 37 | session.write(str); 38 | } 39 | 40 | // 关闭 41 | if (session != null) { 42 | if (session.isConnected()) { 43 | session.getCloseFuture().awaitUninterruptibly(); 44 | } 45 | connector.dispose(true); 46 | } 47 | 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /mina_demo/src/main/java/org/mina_demo/MinaTimeServer.java: -------------------------------------------------------------------------------- 1 | package org.mina_demo; 2 | 3 | import org.apache.mina.core.service.IoAcceptor; 4 | import org.apache.mina.core.session.IdleStatus; 5 | import org.apache.mina.filter.codec.ProtocolCodecFilter; 6 | import org.apache.mina.filter.codec.textline.TextLineCodecFactory; 7 | import org.apache.mina.filter.logging.LoggingFilter; 8 | import org.apache.mina.transport.socket.nio.NioSocketAcceptor; 9 | 10 | import java.io.IOException; 11 | import java.net.InetSocketAddress; 12 | import java.nio.charset.Charset; 13 | 14 | /** 15 | * @author Pinky Lam 908716835@qq.com 16 | * @date 2017年6月14日 下午5:52:15 17 | */ 18 | public class MinaTimeServer { 19 | 20 | private static final int PORT = 9123; 21 | 22 | public static void main(String[] args) throws IOException { 23 | 24 | IoAcceptor acceptor = new NioSocketAcceptor(); 25 | // 这个过滤器用来记录所有的信息,比如创建session(会话),接收消息,发送消息,关闭会话等 26 | acceptor.getFilterChain().addLast("logger", new LoggingFilter()); 27 | // 用来转换二进制或协议的专用数据到消息对象中 28 | acceptor.getFilterChain().addLast("codec", 29 | new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))); 30 | 31 | // 实时处理客户端的连接和请求 32 | acceptor.setHandler(new TimeServerHandler()); 33 | acceptor.getSessionConfig().setReadBufferSize(2048); 34 | // 方法将定时调用一次会话,保持空闲状态。来设定时间间隔。 35 | acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10); 36 | acceptor.bind(new InetSocketAddress(PORT)); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /mina_demo/src/main/java/org/mina_demo/TimeClientHander.java: -------------------------------------------------------------------------------- 1 | package org.mina_demo; 2 | 3 | import org.apache.mina.core.service.IoHandler; 4 | import org.apache.mina.core.session.IdleStatus; 5 | import org.apache.mina.core.session.IoSession; 6 | 7 | public class TimeClientHander implements IoHandler { 8 | 9 | public void exceptionCaught(IoSession arg0, Throwable arg1) throws Exception { 10 | arg1.printStackTrace(); 11 | } 12 | 13 | public void inputClosed(IoSession arg0) throws Exception { 14 | 15 | } 16 | 17 | public void messageReceived(IoSession arg0, Object message) throws Exception { 18 | System.out.println("client接受信息:" + message.toString()); 19 | } 20 | 21 | public void messageSent(IoSession arg0, Object message) throws Exception { 22 | System.out.println("client发送信息" + message.toString()); 23 | } 24 | 25 | public void sessionClosed(IoSession session) throws Exception { 26 | System.out.println("client与:" + session.getRemoteAddress().toString() + "断开连接"); 27 | } 28 | 29 | public void sessionCreated(IoSession session) throws Exception { 30 | System.out.println("client与:" + session.getRemoteAddress().toString() + "建立连接"); 31 | } 32 | 33 | public void sessionIdle(IoSession session, IdleStatus status) throws Exception { 34 | System.out.println("IDLE " + session.getIdleCount(status)); 35 | } 36 | 37 | public void sessionOpened(IoSession arg0) throws Exception { 38 | System.out.println("打开连接"); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /mina_demo/src/main/java/org/mina_demo/TimeServerHandler.java: -------------------------------------------------------------------------------- 1 | package org.mina_demo; 2 | 3 | import org.apache.mina.core.service.IoHandlerAdapter; 4 | import org.apache.mina.core.session.IdleStatus; 5 | import org.apache.mina.core.session.IoSession; 6 | 7 | import java.util.Date; 8 | 9 | public class TimeServerHandler extends IoHandlerAdapter { 10 | @Override 11 | public void exceptionCaught(IoSession arg0, Throwable arg1) throws Exception { 12 | arg1.printStackTrace(); 13 | } 14 | 15 | @Override 16 | public void messageReceived(IoSession session, Object message) throws Exception { 17 | 18 | String str = message.toString(); 19 | System.out.println("接受到的消息:" + str); 20 | if (str.trim().equalsIgnoreCase("quit")) { 21 | session.close(true); 22 | return; 23 | } 24 | Date date = new Date(); 25 | session.write(date.toString()); 26 | System.out.println("Message written..."); 27 | } 28 | 29 | @Override 30 | public void messageSent(IoSession arg0, Object arg1) throws Exception { 31 | System.out.println("发送信息:" + arg1.toString()); 32 | } 33 | 34 | @Override 35 | public void sessionClosed(IoSession session) throws Exception { 36 | System.out.println("sessionClosed IP:" + session.getRemoteAddress().toString() + "断开连接"); 37 | } 38 | 39 | @Override 40 | public void sessionCreated(IoSession session) throws Exception { 41 | System.out.println("sessionCreated IP:" + session.getRemoteAddress().toString()); 42 | } 43 | 44 | @Override 45 | public void sessionIdle(IoSession session, IdleStatus status) throws Exception { 46 | System.out.println("IDLE " + session.getIdleCount(status)); 47 | } 48 | 49 | @Override 50 | public void sessionOpened(IoSession arg0) throws Exception { 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /mina_demo/src/test/java/org/mina_demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package org.mina_demo; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 4.0.0 5 | com.framework 6 | frameworkAggregate 7 | 0.0.1-SNAPSHOT 8 | pom 9 | 10 | 11 | 4.2.6.RELEASE 12 | 2.4.3 13 | 1.0.20 14 | 1.6.1 15 | 3.0.1 16 | 1.7.2 17 | 4.10 18 | 2.2.3 19 | 5.22.0 20 | UTF-8 21 | 22 | 23 | 24 | 25 | 26 | 27 | org.springframework 28 | spring-test 29 | ${spring.version} 30 | compile 31 | 32 | 33 | org.springframework 34 | spring-core 35 | ${spring.version} 36 | 37 | 38 | org.springframework 39 | spring-beans 40 | ${spring.version} 41 | 42 | 43 | org.springframework 44 | spring-jdbc 45 | ${spring.version} 46 | 47 | 48 | org.springframework 49 | spring-context 50 | ${spring.version} 51 | 52 | 53 | org.springframework 54 | spring-aop 55 | ${spring.version} 56 | 57 | 58 | org.springframework 59 | spring-orm 60 | ${spring.version} 61 | 62 | 63 | org.springframework 64 | spring-context-support 65 | ${spring.version} 66 | 67 | 68 | org.springframework 69 | spring-expression 70 | ${spring.version} 71 | 72 | 73 | com.fasterxml.jackson.core 74 | jackson-core 75 | ${jackson.version} 76 | 77 | 78 | com.fasterxml.jackson.core 79 | jackson-databind 80 | ${jackson.version} 81 | 82 | 83 | org.springframework 84 | spring-tx 85 | ${spring.version} 86 | 87 | 88 | junit 89 | junit 90 | ${junit.version} 91 | test 92 | 93 | 94 | org.springframework 95 | spring-aspects 96 | ${spring.version} 97 | 98 | 99 | 100 | com.jayway.jsonpath 101 | json-path 102 | 0.9.1 103 | 104 | 105 | com.alibaba 106 | druid 107 | ${druid.version} 108 | 109 | 110 | com.alibaba 111 | fastjson 112 | 1.2.4 113 | 114 | 115 | 116 | org.apache.commons 117 | commons-lang3 118 | 3.3.2 119 | 120 | 121 | commons-lang 122 | commons-lang 123 | 2.6 124 | 125 | 126 | commons-fileupload 127 | commons-fileupload 128 | 1.3 129 | 130 | 131 | org.slf4j 132 | slf4j-log4j12 133 | ${slf4j.version} 134 | 135 | 136 | commons-beanutils 137 | commons-beanutils 138 | 1.8.3 139 | 140 | 141 | dom4j 142 | dom4j 143 | ${dom4j.version} 144 | 145 | 146 | org.springframework 147 | spring-web 148 | ${spring.version} 149 | 150 | 151 | org.springframework 152 | spring-webmvc 153 | ${spring.version} 154 | 155 | 156 | 157 | javax.servlet 158 | javax.servlet-api 159 | ${servlet.version} 160 | provided 161 | 162 | 163 | javax.servlet 164 | jstl 165 | 1.2 166 | 167 | 168 | 169 | 170 | org.springframework.data 171 | spring-data-jpa 172 | 1.9.4.RELEASE 173 | 174 | 175 | 176 | org.hibernate 177 | hibernate-entitymanager 178 | 4.3.8.Final 179 | 180 | 181 | 182 | mysql 183 | mysql-connector-java 184 | 5.1.35 185 | 186 | 187 | 188 | 189 | org.quartz-scheduler 190 | quartz 191 | 2.2.3 192 | 193 | 194 | 195 | 196 | org.quartz-scheduler 197 | quartz 198 | ${quartz.version} 199 | 200 | 201 | org.quartz-scheduler 202 | quartz-jobs 203 | ${quartz.version} 204 | 205 | 206 | 207 | 208 | org.jsoup 209 | jsoup 210 | 1.10.2 211 | 212 | 213 | 214 | 215 | org.activiti 216 | activiti-engine 217 | ${activiti-version} 218 | 219 | 220 | org.activiti 221 | activiti-spring 222 | ${activiti-version} 223 | 224 | 225 | 226 | 227 | org.apache.mina 228 | mina-core 229 | 2.0.16 230 | 231 | 232 | 233 | 234 | 235 | quartz_demo 236 | jsoup_demo 237 | spring_demo 238 | mina_demo 239 | restful_demo 240 | 241 | 242 | -------------------------------------------------------------------------------- /quartz_demo/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /quartz_demo/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /quartz_demo/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | quartz_demo 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.wst.jsdt.core.javascriptValidator 10 | 11 | 12 | 13 | 14 | org.eclipse.jdt.core.javabuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.wst.common.project.facet.core.builder 20 | 21 | 22 | 23 | 24 | org.eclipse.wst.validation.validationbuilder 25 | 26 | 27 | 28 | 29 | org.eclipse.m2e.core.maven2Builder 30 | 31 | 32 | 33 | 34 | 35 | org.eclipse.jem.workbench.JavaEMFNature 36 | org.eclipse.wst.common.modulecore.ModuleCoreNature 37 | org.eclipse.jdt.core.javanature 38 | org.eclipse.m2e.core.maven2Nature 39 | org.eclipse.wst.common.project.facet.core.nature 40 | org.eclipse.wst.jsdt.core.jsNature 41 | 42 | 43 | -------------------------------------------------------------------------------- /quartz_demo/.settings/.jsdtscope: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /quartz_demo/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/main/resources=UTF-8 4 | encoding//src/test/java=UTF-8 5 | encoding//src/test/resources=UTF-8 6 | encoding/=UTF-8 7 | -------------------------------------------------------------------------------- /quartz_demo/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 4 | org.eclipse.jdt.core.compiler.compliance=1.5 5 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 6 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 7 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 8 | org.eclipse.jdt.core.compiler.source=1.5 9 | -------------------------------------------------------------------------------- /quartz_demo/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /quartz_demo/.settings/org.eclipse.wst.common.component: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /quartz_demo/.settings/org.eclipse.wst.common.project.facet.core.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /quartz_demo/.settings/org.eclipse.wst.jsdt.ui.superType.container: -------------------------------------------------------------------------------- 1 | org.eclipse.wst.jsdt.launching.baseBrowserLibrary -------------------------------------------------------------------------------- /quartz_demo/.settings/org.eclipse.wst.jsdt.ui.superType.name: -------------------------------------------------------------------------------- 1 | Window -------------------------------------------------------------------------------- /quartz_demo/.settings/org.eclipse.wst.validation.prefs: -------------------------------------------------------------------------------- 1 | disabled=06target 2 | eclipse.preferences.version=1 3 | -------------------------------------------------------------------------------- /quartz_demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | com.framework 8 | frameworkAggregate 9 | 0.0.1-SNAPSHOT 10 | ../pom.xml 11 | 12 | 13 | quartz_demo 14 | war 15 | 16 | 17 | quartz_demo 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /quartz_demo/src/main/java/com/quartz/base/task/Handx.java: -------------------------------------------------------------------------------- 1 | package com.quartz.base.task; 2 | 3 | import org.apache.log4j.Logger; 4 | 5 | public class Handx { 6 | 7 | public final Logger log = Logger.getLogger(this.getClass()); 8 | 9 | public void main() { 10 | log.debug("main========================="); 11 | } 12 | 13 | public void run() { 14 | log.debug("run========================="); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /quartz_demo/src/main/java/com/quartz/base/task/QuartzJobFactory.java: -------------------------------------------------------------------------------- 1 | package com.quartz.base.task; 2 | 3 | import com.quartz.entity.ScheduleJob; 4 | import com.quartz.utils.TaskUtils; 5 | 6 | import org.apache.log4j.Logger; 7 | import org.quartz.Job; 8 | import org.quartz.JobExecutionContext; 9 | import org.quartz.JobExecutionException; 10 | 11 | /** 12 | * @Description: 计划任务执行处 无状态 13 | * @author handx 908716835@qq.com 14 | * @date 2017年5月7日 下午10:04:55 15 | * 16 | */ 17 | public class QuartzJobFactory implements Job { 18 | 19 | public final Logger log = Logger.getLogger(this.getClass()); 20 | 21 | public void execute(JobExecutionContext context) throws JobExecutionException { 22 | ScheduleJob scheduleJob = (ScheduleJob) context.getMergedJobDataMap().get("scheduleJob"); 23 | TaskUtils.invokMethod(scheduleJob); 24 | } 25 | } -------------------------------------------------------------------------------- /quartz_demo/src/main/java/com/quartz/controller/ScheduleJobController.java: -------------------------------------------------------------------------------- 1 | package com.quartz.controller; 2 | 3 | import com.quartz.dao.ScheduleJobDao; 4 | import com.quartz.entity.ScheduleJob; 5 | import com.quartz.model.RetObj; 6 | import com.quartz.service.ScheduleJobService; 7 | import com.quartz.utils.SpringUtils; 8 | 9 | import org.apache.commons.lang.StringUtils; 10 | import org.quartz.CronScheduleBuilder; 11 | import org.quartz.SchedulerException; 12 | import org.slf4j.Logger; 13 | import org.slf4j.LoggerFactory; 14 | import org.springframework.beans.factory.annotation.Autowired; 15 | import org.springframework.web.bind.annotation.RequestMapping; 16 | import org.springframework.web.bind.annotation.ResponseBody; 17 | import org.springframework.web.bind.annotation.RestController; 18 | import org.springframework.web.servlet.ModelAndView; 19 | 20 | import java.lang.reflect.Method; 21 | import java.util.List; 22 | 23 | import javax.servlet.http.HttpServletRequest; 24 | 25 | @RestController 26 | @RequestMapping("job") 27 | public class ScheduleJobController { 28 | 29 | Logger logger = LoggerFactory.getLogger(this.getClass()); 30 | 31 | @Autowired 32 | ScheduleJobDao scheduleJobDao; 33 | @Autowired 34 | ScheduleJobService scheduleJobService; 35 | 36 | @SuppressWarnings({ "unchecked", "rawtypes" }) 37 | @RequestMapping("add") 38 | @ResponseBody 39 | public RetObj add(HttpServletRequest request, ScheduleJob scheduleJob) { 40 | RetObj retObj = new RetObj(); 41 | retObj.setFlag(false); 42 | 43 | try { 44 | CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()); 45 | } catch (Exception e) { 46 | retObj.setMsg("cron表达式有误,不能被解析!"); 47 | return retObj; 48 | } 49 | 50 | Object obj = null; 51 | try { 52 | if (StringUtils.isNotBlank(scheduleJob.getSpringId())) { 53 | obj = SpringUtils.getBean(scheduleJob.getSpringId()); 54 | } else { 55 | Class clazz = Class.forName(scheduleJob.getBeanClass()); 56 | obj = clazz.newInstance(); 57 | } 58 | } catch (Exception e) { 59 | e.printStackTrace(); 60 | } 61 | 62 | if (obj == null) { 63 | retObj.setMsg("未找到目标类!"); 64 | return retObj; 65 | } else { 66 | Class clazz = obj.getClass(); 67 | Method method = null; 68 | try { 69 | method = clazz.getMethod(scheduleJob.getMethodName(), null); 70 | } catch (Exception e) { 71 | e.printStackTrace(); 72 | } 73 | if (method == null) { 74 | retObj.setMsg("未找到目标方法!"); 75 | return retObj; 76 | } 77 | } 78 | 79 | try { 80 | scheduleJobService.addTask(scheduleJob); 81 | } catch (Exception e) { 82 | e.printStackTrace(); 83 | retObj.setFlag(false); 84 | retObj.setMsg("保存失败,检查 name group 组合是否有重复!"); 85 | return retObj; 86 | } 87 | 88 | retObj.setFlag(true); 89 | return retObj; 90 | } 91 | 92 | @RequestMapping("changeJobStatus") 93 | @ResponseBody 94 | public RetObj changeJobStatus(HttpServletRequest request, Long jobId, String cmd) { 95 | RetObj retObj = new RetObj(); 96 | retObj.setFlag(false); 97 | try { 98 | scheduleJobService.changeStatus(jobId, cmd); 99 | } catch (SchedulerException e) { 100 | logger.error(e.getMessage(), e); 101 | retObj.setMsg("任务状态改变失败!"); 102 | return retObj; 103 | } 104 | retObj.setFlag(true); 105 | return retObj; 106 | } 107 | 108 | @RequestMapping("jobList") 109 | public ModelAndView jobList(HttpServletRequest request) { 110 | List jobList = scheduleJobDao.findAll(); 111 | try { 112 | for (ScheduleJob job : jobList) { 113 | if ("1".equals(job.getJobStatus())) { 114 | scheduleJobService.addJob(job); 115 | } 116 | } 117 | } catch (SchedulerException e) { 118 | e.printStackTrace(); 119 | } 120 | request.setAttribute("jobList", jobList); 121 | return new ModelAndView("/jobList"); 122 | } 123 | 124 | @RequestMapping("updateCron") 125 | @ResponseBody 126 | public RetObj updateCron(HttpServletRequest request, Long jobId, String cron) { 127 | RetObj retObj = new RetObj(); 128 | retObj.setFlag(false); 129 | 130 | try { 131 | CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron); 132 | } catch (Exception e) { 133 | retObj.setMsg("cron表达式有误,不能被解析!"); 134 | return retObj; 135 | } 136 | 137 | try { 138 | scheduleJobService.updateCron(jobId, cron); 139 | } catch (SchedulerException e) { 140 | retObj.setMsg("cron更新失败!"); 141 | return retObj; 142 | } 143 | retObj.setFlag(true); 144 | return retObj; 145 | } 146 | 147 | } 148 | -------------------------------------------------------------------------------- /quartz_demo/src/main/java/com/quartz/dao/ScheduleJobDao.java: -------------------------------------------------------------------------------- 1 | package com.quartz.dao; 2 | 3 | import com.quartz.entity.ScheduleJob; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.stereotype.Repository; 7 | 8 | /** 9 | * @author handx 908716835@qq.com 10 | * @date 2017年5月7日 下午5:44:29 11 | */ 12 | 13 | @Repository 14 | public interface ScheduleJobDao extends JpaRepository { 15 | 16 | } 17 | -------------------------------------------------------------------------------- /quartz_demo/src/main/java/com/quartz/entity/ScheduleJob.java: -------------------------------------------------------------------------------- 1 | package com.quartz.entity; 2 | 3 | import com.fasterxml.jackson.annotation.JsonFormat; 4 | 5 | import org.springframework.format.annotation.DateTimeFormat; 6 | 7 | import java.util.Date; 8 | 9 | import javax.persistence.Column; 10 | import javax.persistence.Entity; 11 | import javax.persistence.GeneratedValue; 12 | import javax.persistence.GenerationType; 13 | import javax.persistence.Id; 14 | import javax.persistence.Table; 15 | 16 | /** 17 | * @author handx 908716835@qq.com 18 | * @date 2017年5月7日 下午5:29:45 19 | */ 20 | 21 | @Entity 22 | @Table(name = "SCHEDULE_JOB") 23 | public class ScheduleJob { 24 | 25 | public static final String STATUS_RUNNING = "1"; 26 | public static final String STOP_RUNNING = "0"; 27 | 28 | @Id 29 | @GeneratedValue(strategy = GenerationType.AUTO) 30 | @Column(name = "JOB_ID") 31 | private Long jobId; 32 | @Column(name = "JOB_NAME") 33 | private String jobName;// 任务名称 34 | @Column(name = "JOB_GROUP") 35 | private String jobGroup;// 任务分组 36 | @Column(name = "JOB_STATUS") 37 | private String jobStatus;// 任务状态 是否启动任务 38 | @Column(name = "CRON_EXPRESSION") 39 | private String cronExpression;// cron表达式 40 | @Column(name = "DESCRIPTION") 41 | private String description;// 描述 42 | @Column(name = "BEAN_CLASS") 43 | private String beanClass;// 任务执行时调用哪个类的方法 包名+类名 44 | @Column(name = "SPRING_ID") 45 | private String springId;// spring bean 46 | @Column(name = "METHOD_NAME") 47 | private String methodName;// 任务调用的方法名 48 | @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") 49 | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 50 | @Column(name = "CREATE_TIME") 51 | private Date createTime; 52 | @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") 53 | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 54 | @Column(name = "UPDATE_TIME") 55 | private Date updateTime; 56 | 57 | public String getBeanClass() { 58 | return beanClass; 59 | } 60 | 61 | public Date getCreateTime() { 62 | return createTime; 63 | } 64 | 65 | public String getCronExpression() { 66 | return cronExpression; 67 | } 68 | 69 | public String getDescription() { 70 | return description; 71 | } 72 | 73 | public String getJobGroup() { 74 | return jobGroup; 75 | } 76 | 77 | public Long getJobId() { 78 | return jobId; 79 | } 80 | 81 | public String getJobName() { 82 | return jobName; 83 | } 84 | 85 | public String getJobStatus() { 86 | return jobStatus; 87 | } 88 | 89 | public String getMethodName() { 90 | return methodName; 91 | } 92 | 93 | public String getSpringId() { 94 | return springId; 95 | } 96 | 97 | public Date getUpdateTime() { 98 | return updateTime; 99 | } 100 | 101 | public void setBeanClass(String beanClass) { 102 | this.beanClass = beanClass; 103 | } 104 | 105 | public void setCreateTime(Date createTime) { 106 | this.createTime = createTime; 107 | } 108 | 109 | public void setCronExpression(String cronExpression) { 110 | this.cronExpression = cronExpression; 111 | } 112 | 113 | public void setDescription(String description) { 114 | this.description = description; 115 | } 116 | 117 | public void setJobGroup(String jobGroup) { 118 | this.jobGroup = jobGroup; 119 | } 120 | 121 | public void setJobId(Long jobId) { 122 | this.jobId = jobId; 123 | } 124 | 125 | public void setJobName(String jobName) { 126 | this.jobName = jobName; 127 | } 128 | 129 | public void setJobStatus(String jobStatus) { 130 | this.jobStatus = jobStatus; 131 | } 132 | 133 | public void setMethodName(String methodName) { 134 | this.methodName = methodName; 135 | } 136 | 137 | public void setSpringId(String springId) { 138 | this.springId = springId; 139 | } 140 | 141 | public void setUpdateTime(Date updateTime) { 142 | this.updateTime = updateTime; 143 | } 144 | 145 | @Override 146 | public String toString() { 147 | return "ScheduleJob [jobId=" + jobId + ", jobName=" + jobName + ", jobGroup=" + jobGroup + ", jobStatus=" 148 | + jobStatus + ", cronExpression=" + cronExpression + ", description=" + description + ", beanClass=" 149 | + beanClass + ", springId=" + springId + ", methodName=" + methodName 150 | + ", createTime=" + createTime + ", updateTime=" + updateTime + "]"; 151 | } 152 | 153 | } -------------------------------------------------------------------------------- /quartz_demo/src/main/java/com/quartz/model/RetObj.java: -------------------------------------------------------------------------------- 1 | package com.quartz.model; 2 | 3 | public class RetObj { 4 | 5 | private boolean flag = true; 6 | private String msg; 7 | private Object obj; 8 | 9 | public RetObj() { 10 | 11 | } 12 | 13 | public RetObj(boolean flag, String msg) { 14 | super(); 15 | this.flag = flag; 16 | this.msg = msg; 17 | } 18 | 19 | public RetObj(boolean flag, String msg, Object obj) { 20 | super(); 21 | this.flag = flag; 22 | this.msg = msg; 23 | this.obj = obj; 24 | } 25 | 26 | public String getMsg() { 27 | return msg; 28 | } 29 | 30 | public Object getObj() { 31 | return obj; 32 | } 33 | 34 | public boolean isFlag() { 35 | return flag; 36 | } 37 | 38 | public void setFlag(boolean flag) { 39 | this.flag = flag; 40 | } 41 | 42 | public void setMsg(String msg) { 43 | this.msg = msg; 44 | } 45 | 46 | public void setObj(Object obj) { 47 | this.obj = obj; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /quartz_demo/src/main/java/com/quartz/service/ScheduleJobService.java: -------------------------------------------------------------------------------- 1 | package com.quartz.service; 2 | 3 | import com.quartz.base.task.QuartzJobFactory; 4 | import com.quartz.dao.ScheduleJobDao; 5 | import com.quartz.entity.ScheduleJob; 6 | 7 | import org.quartz.CronScheduleBuilder; 8 | import org.quartz.CronTrigger; 9 | import org.quartz.JobBuilder; 10 | import org.quartz.JobDetail; 11 | import org.quartz.JobKey; 12 | import org.quartz.Scheduler; 13 | import org.quartz.SchedulerException; 14 | import org.quartz.TriggerBuilder; 15 | import org.quartz.TriggerKey; 16 | import org.slf4j.Logger; 17 | import org.slf4j.LoggerFactory; 18 | import org.springframework.beans.factory.annotation.Autowired; 19 | import org.springframework.scheduling.quartz.SchedulerFactoryBean; 20 | import org.springframework.stereotype.Service; 21 | 22 | import java.util.Date; 23 | 24 | import javax.transaction.Transactional; 25 | 26 | @Service 27 | public class ScheduleJobService { 28 | 29 | @Autowired 30 | ScheduleJobDao scheduleJobDao; 31 | @Autowired 32 | SchedulerFactoryBean schedulerFactoryBean; 33 | 34 | Logger logger = LoggerFactory.getLogger(this.getClass()); 35 | 36 | /** 37 | * 添加任务 38 | * 39 | * @param scheduleJob 40 | * @throws SchedulerException 41 | */ 42 | public void addJob(ScheduleJob job) throws SchedulerException { 43 | if (job == null || !ScheduleJob.STATUS_RUNNING.equals(job.getJobStatus())) { 44 | return; 45 | } 46 | 47 | Scheduler scheduler = schedulerFactoryBean.getScheduler(); 48 | //在这里把它设计成一个Job对应一个trigger,两者的分组及名称相同,方便管理,条理也比较清晰,在创建任务时如果不存在新建一个,如果已经存在则更新任务 49 | TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup()); 50 | CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); 51 | 52 | // 不存在,创建一个 53 | if (null == trigger) { 54 | JobDetail jobDetail = JobBuilder.newJob(QuartzJobFactory.class) 55 | .withIdentity(job.getJobName(), job.getJobGroup()).build(); 56 | jobDetail.getJobDataMap().put("scheduleJob", job); 57 | CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()); 58 | trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup()).withSchedule(scheduleBuilder).build(); 59 | scheduler.scheduleJob(jobDetail, trigger); 60 | } else { 61 | // Trigger已存在,那么更新相应的定时设置 62 | CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()); 63 | // 按新的cronExpression表达式重新构建trigger 64 | trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build(); 65 | // 按新的trigger重新设置job执行 66 | scheduler.rescheduleJob(triggerKey, trigger); 67 | } 68 | } 69 | 70 | public void addTask(ScheduleJob job) { 71 | job.setCreateTime(new Date()); 72 | scheduleJobDao.save(job); 73 | } 74 | 75 | /** 76 | * 更改任务状态 77 | * 78 | * @throws SchedulerException 79 | */ 80 | @Transactional 81 | public void changeStatus(Long jobId, String cmd) throws SchedulerException { 82 | 83 | ScheduleJob job = scheduleJobDao.findOne(jobId); 84 | if (job == null) { 85 | return; 86 | } 87 | if ("stop".equals(cmd)) { 88 | deleteJob(job); 89 | job.setJobStatus(ScheduleJob.STOP_RUNNING); 90 | } else if ("start".equals(cmd)) { 91 | job.setJobStatus(ScheduleJob.STATUS_RUNNING); 92 | addJob(job); 93 | } 94 | scheduleJobDao.save(job); 95 | } 96 | 97 | /** 98 | * 删除一个job 99 | * 100 | * @param scheduleJob 101 | * @throws SchedulerException 102 | */ 103 | public void deleteJob(ScheduleJob scheduleJob) throws SchedulerException { 104 | Scheduler scheduler = schedulerFactoryBean.getScheduler(); 105 | JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); 106 | scheduler.deleteJob(jobKey); 107 | } 108 | 109 | /** 110 | * 更改任务 cron表达式 111 | * 112 | * @throws SchedulerException 113 | */ 114 | public void updateCron(Long jobId, String cron) throws SchedulerException { 115 | ScheduleJob job = scheduleJobDao.findOne(jobId); 116 | if (job == null) { 117 | return; 118 | } 119 | job.setCronExpression(cron); 120 | if (ScheduleJob.STATUS_RUNNING.equals(job.getJobStatus())) { 121 | updateJobCron(job); 122 | } 123 | scheduleJobDao.save(job); 124 | } 125 | 126 | /** 127 | * 更新job时间表达式 128 | * 129 | * @param scheduleJob 130 | * @throws SchedulerException 131 | */ 132 | public void updateJobCron(ScheduleJob scheduleJob) throws SchedulerException { 133 | Scheduler scheduler = schedulerFactoryBean.getScheduler(); 134 | TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); 135 | CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); 136 | CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()); 137 | trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build(); 138 | scheduler.rescheduleJob(triggerKey, trigger); 139 | } 140 | 141 | } 142 | -------------------------------------------------------------------------------- /quartz_demo/src/main/java/com/quartz/utils/SpringUtils.java: -------------------------------------------------------------------------------- 1 | package com.quartz.utils; 2 | 3 | import org.springframework.beans.BeansException; 4 | import org.springframework.beans.factory.NoSuchBeanDefinitionException; 5 | import org.springframework.beans.factory.config.BeanFactoryPostProcessor; 6 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 7 | 8 | public final class SpringUtils implements BeanFactoryPostProcessor { 9 | 10 | private static ConfigurableListableBeanFactory beanFactory; // Spring应用上下文环境 11 | 12 | /** 13 | * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true 14 | * 15 | * @param name 16 | * @return boolean 17 | */ 18 | public static boolean containsBean(String name) { 19 | return beanFactory.containsBean(name); 20 | } 21 | 22 | /** 23 | * 如果给定的bean名字在bean定义中有别名,则返回这些别名 24 | * 25 | * @param name 26 | * @return 27 | * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException 28 | * 29 | */ 30 | public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { 31 | return beanFactory.getAliases(name); 32 | } 33 | 34 | /** 35 | * 获取类型为requiredType的对象 36 | * 37 | * @param clz 38 | * @return 39 | * @throws org.springframework.beans.BeansException 40 | * 41 | */ 42 | public static T getBean(Class clz) throws BeansException { 43 | T result = beanFactory.getBean(clz); 44 | return result; 45 | } 46 | 47 | /** 48 | * 获取对象 49 | * 50 | * @param name 51 | * @return Object 一个以所给名字注册的bean的实例 52 | * @throws org.springframework.beans.BeansException 53 | * 54 | */ 55 | @SuppressWarnings("unchecked") 56 | public static T getBean(String name) throws BeansException { 57 | return (T) beanFactory.getBean(name); 58 | } 59 | 60 | /** 61 | * @param name 62 | * @return Class 注册对象的类型 63 | * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException 64 | * 65 | */ 66 | public static Class getType(String name) throws NoSuchBeanDefinitionException { 67 | return beanFactory.getType(name); 68 | } 69 | 70 | /** 71 | * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 72 | * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException) 73 | * 74 | * @param name 75 | * @return boolean 76 | * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException 77 | * 78 | */ 79 | public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { 80 | return beanFactory.isSingleton(name); 81 | } 82 | 83 | public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { 84 | SpringUtils.beanFactory = beanFactory; 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /quartz_demo/src/main/java/com/quartz/utils/TaskUtils.java: -------------------------------------------------------------------------------- 1 | package com.quartz.utils; 2 | 3 | import com.quartz.entity.ScheduleJob; 4 | 5 | import org.apache.commons.lang.StringUtils; 6 | import org.apache.log4j.Logger; 7 | 8 | import java.lang.reflect.InvocationTargetException; 9 | import java.lang.reflect.Method; 10 | 11 | public class TaskUtils { 12 | 13 | public final static Logger log = Logger.getLogger(TaskUtils.class); 14 | 15 | /** 16 | * 通过反射调用scheduleJob中定义的方法 17 | * 18 | * @param scheduleJob 19 | */ 20 | @SuppressWarnings({ "rawtypes", "unchecked" }) 21 | public static void invokMethod(ScheduleJob scheduleJob) { 22 | Object object = null; 23 | Class clazz = null; 24 | if (StringUtils.isNotBlank(scheduleJob.getSpringId())) { 25 | object = SpringUtils.getBean(scheduleJob.getSpringId()); 26 | } else if (StringUtils.isNotBlank(scheduleJob.getBeanClass())) { 27 | try { 28 | clazz = Class.forName(scheduleJob.getBeanClass()); 29 | object = clazz.newInstance(); 30 | } catch (Exception e) { 31 | e.printStackTrace(); 32 | } 33 | 34 | } 35 | if (object == null) { 36 | log.error("未启动成功,请检查是否配置正确!任务名称 = [" + scheduleJob.getJobName() + "]"); 37 | return; 38 | } 39 | clazz = object.getClass(); 40 | Method method = null; 41 | try { 42 | method = clazz.getDeclaredMethod(scheduleJob.getMethodName()); 43 | } catch (NoSuchMethodException e) { 44 | log.error("未启动成功,方法名设置错误!任务名称 = [" + scheduleJob.getJobName() + "]"); 45 | } catch (SecurityException e) { 46 | e.printStackTrace(); 47 | } 48 | if (method != null) { 49 | try { 50 | method.invoke(object); 51 | } catch (IllegalAccessException e) { 52 | e.printStackTrace(); 53 | } catch (IllegalArgumentException e) { 54 | e.printStackTrace(); 55 | } catch (InvocationTargetException e) { 56 | e.printStackTrace(); 57 | } 58 | } 59 | System.out.println("启动成功,任务名称 = [" + scheduleJob.getJobName() + "]"); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /quartz_demo/src/main/resources/config.properties: -------------------------------------------------------------------------------- 1 | mysql_url=jdbc:mysql://localhost:3306/wish?characterEncoding=utf-8 2 | mysql_username=root 3 | mysql_password=root 4 | mysql_driverClassName=com.mysql.jdbc.Driver 5 | maxActive=10 6 | #maxActive=50 7 | initialSize=10 8 | maxWait=30000 9 | minIdle=10 10 | maxIdle=10 11 | #Hibernate Configuration 12 | hibernate.dialect=org.hibernate.dialect.MySQL5Dialect 13 | hibernate.hbm2ddl.auto=none 14 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 15 | hibernate.show_sql=true 16 | hibernate.format_sql=true 17 | -------------------------------------------------------------------------------- /quartz_demo/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootCategory=debug,info,stdout,logfile 2 | 3 | #stdout configure 4 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 5 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 6 | log4j.appender.stdout.layout.ConversionPattern= %d %p [%c] - <%m>%n 7 | 8 | #logfile configure 9 | log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender 10 | log4j.appender.logfile.File=logs/framework.log 11 | log4j.appender.logfile.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.logfile.layout.ConversionPattern= %d %p [%c] - <%m>%n 13 | 14 | log4j.logger.org.apache.zookeeper.ClientCnxn=info 15 | log4j.logger.org.vista.mall.web.ui.utils.ModelHolder=info 16 | 17 | #log4j.appender.logfile=org.apache.log4j.RollingFileAppender 18 | #log4j.appender.logfile.Append=false 19 | #log4j.appender.logfile.File=./logs/server1/SystemOut.log 20 | # Pattern to output: date priority [category] - message 21 | #log4j.appender.logfile.layout=org.apache.log4j.PatternLayout 22 | #log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n -------------------------------------------------------------------------------- /quartz_demo/src/main/resources/spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 24 | 25 | 26 | 27 | 28 | 29 | classpath:config.properties 30 | 31 | 32 | 33 | 34 | 35 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | false 53 | 54 | 55 | false 56 | 57 | 58 | true 59 | 60 | 61 | 1800000 62 | 63 | 64 | 90000 65 | 66 | 67 | SELECT 1 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | com.quartz 79 | 80 | 81 | 82 | 83 | ${hibernate.dialect} 84 | ${hibernate.ejb.naming_strategy} 85 | ${hibernate.show_sql} 86 | ${hibernate.format_sql} 87 | ${hibernate.hbm2ddl.auto} 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | -------------------------------------------------------------------------------- /quartz_demo/src/main/webapp/WEB-INF/spring-mvc.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /quartz_demo/src/main/webapp/WEB-INF/views/jobList.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" import="java.util.*" pageEncoding="utf8"%> 2 | 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 5 | <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 任务列表 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 71 | 72 | 76 | 77 | 78 | 79 | 80 | 81 | 82 |
id   namegroup状态  cron表达式描述同步否类路径spring id方法名操作
${job.jobId }${job.jobName }${job.jobGroup }${job.jobStatus } 45 | 46 | 停止  48 | 49 | 50 | 开启  52 | 53 | 54 | ${job.cronExpression }${job.description }${job.isConcurrent }${job.beanClass }${job.springId }${job.methodName }更新cron
n0
83 |
84 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | -------------------------------------------------------------------------------- /quartz_demo/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | quartz_demo 6 | 7 | log4jConfigLocation 8 | classpath:log4j.properties 9 | 10 | 11 | encodingFilter 12 | org.springframework.web.filter.CharacterEncodingFilter 13 | 14 | encoding 15 | utf8 16 | 17 | 18 | 19 | encodingFilter 20 | /* 21 | 22 | 23 | 24 | 25 | springmvc 26 | org.springframework.web.servlet.DispatcherServlet 27 | 28 | contextConfigLocation 29 | /WEB-INF/spring-mvc.xml 30 | 31 | 1 32 | 33 | 34 | springmvc 35 | / 36 | 37 | 38 | default 39 | /js/* 40 | /css/* 41 | /images/* 42 | *.js 43 | *.css 44 | *.png 45 | *.jpg 46 | *.gif 47 | *.json 48 | *.html 49 | *.swf 50 | 51 | 52 | 60 53 | 54 | 55 | 56 | index.jsp 57 | 58 | 59 | -------------------------------------------------------------------------------- /quartz_demo/src/main/webapp/sql/schedule_job.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat MySQL Data Transfer 3 | 4 | Source Server : vipsnacks 5 | Source Server Version : 50711 6 | Source Host : localhost:3306 7 | Source Database : wish 8 | 9 | Target Server Type : MYSQL 10 | Target Server Version : 50711 11 | File Encoding : 65001 12 | 13 | Date: 2017-05-08 10:07:38 14 | */ 15 | 16 | SET FOREIGN_KEY_CHECKS=0; 17 | 18 | -- ---------------------------- 19 | -- Table structure for schedule_job 20 | -- ---------------------------- 21 | DROP TABLE IF EXISTS `schedule_job`; 22 | CREATE TABLE `schedule_job` ( 23 | `JOB_ID` bigint(20) NOT NULL AUTO_INCREMENT, 24 | `JOB_NAME` varchar(255) DEFAULT NULL COMMENT '任务名称', 25 | `JOB_GROUP` varchar(255) DEFAULT NULL COMMENT '任务分组', 26 | `JOB_STATUS` varchar(255) DEFAULT NULL COMMENT '任务状态 1开启:0停止', 27 | `CRON_EXPRESSION` varchar(255) DEFAULT NULL COMMENT 'cron表达式', 28 | `DESCRIPTION` varchar(255) DEFAULT NULL COMMENT '描述', 29 | `BEAN_CLASS` varchar(255) DEFAULT NULL COMMENT '调用类(包名加类名)', 30 | `SPRING_ID` varchar(255) DEFAULT NULL COMMENT 'spring bean 名称', 31 | `METHOD_NAME` varchar(255) DEFAULT NULL COMMENT '调用方法名称', 32 | `CREATE_TIME` datetime DEFAULT NULL, 33 | `UPDATE_TIME` datetime DEFAULT NULL, 34 | PRIMARY KEY (`JOB_ID`) 35 | ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; 36 | 37 | -- ---------------------------- 38 | -- Records of schedule_job 39 | -- ---------------------------- 40 | INSERT INTO `schedule_job` VALUES ('1', 'handx', 'handx', '0', '0/5 * * * * ?', 'this is test', 'com.quartz.base.task.Handx', null, 'run', '2017-05-07 21:20:53', null); 41 | INSERT INTO `schedule_job` VALUES ('2', 'main', 'main', '0', '0/1 * * * * ?', ' this is test', 'com.quartz.base.task.Handx', '', 'main', '2017-05-08 09:48:23', null); 42 | -------------------------------------------------------------------------------- /quartz_demo/src/test/java/com/quartz/service/TaskMain.java: -------------------------------------------------------------------------------- 1 | package com.quartz.service; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.context.support.ClassPathXmlApplicationContext; 5 | 6 | /** 7 | * @Description: 测试定时任务 TaskService 8 | * @author handx 908716835@qq.com 9 | * @date 2017年5月7日 下午5:28:38 10 | * 11 | */ 12 | 13 | public class TaskMain { 14 | 15 | public static void main(String[] args) { 16 | ApplicationContext context = new ClassPathXmlApplicationContext("spring-quartz.xml"); 17 | context.getBean("scheduler"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /quartz_demo/src/test/java/com/quartz/service/TaskService.java: -------------------------------------------------------------------------------- 1 | package com.quartz.service; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | /** 7 | * @Description: 简单的定时任务service 8 | * @author handx 908716835@qq.com 9 | * @date 2017年5月7日 下午5:28:12 10 | * 11 | */ 12 | 13 | public class TaskService { 14 | 15 | Logger logger = LoggerFactory.getLogger(this.getClass()); 16 | 17 | public void run() { 18 | logger.info("==========run==========="); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /quartz_demo/src/test/resources/spring-quartz.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /quartz_demo/target/classes/config.properties: -------------------------------------------------------------------------------- 1 | mysql_url=jdbc:mysql://localhost:3306/wish?characterEncoding=utf-8 2 | mysql_username=root 3 | mysql_password=root 4 | mysql_driverClassName=com.mysql.jdbc.Driver 5 | maxActive=10 6 | #maxActive=50 7 | initialSize=10 8 | maxWait=30000 9 | minIdle=10 10 | maxIdle=10 11 | #Hibernate Configuration 12 | hibernate.dialect=org.hibernate.dialect.MySQL5Dialect 13 | hibernate.hbm2ddl.auto=none 14 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 15 | hibernate.show_sql=true 16 | hibernate.format_sql=true 17 | -------------------------------------------------------------------------------- /quartz_demo/target/classes/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootCategory=debug,info,stdout,logfile 2 | 3 | #stdout configure 4 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 5 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 6 | log4j.appender.stdout.layout.ConversionPattern= %d %p [%c] - <%m>%n 7 | 8 | #logfile configure 9 | log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender 10 | log4j.appender.logfile.File=logs/framework.log 11 | log4j.appender.logfile.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.logfile.layout.ConversionPattern= %d %p [%c] - <%m>%n 13 | 14 | log4j.logger.org.apache.zookeeper.ClientCnxn=info 15 | log4j.logger.org.vista.mall.web.ui.utils.ModelHolder=info 16 | 17 | #log4j.appender.logfile=org.apache.log4j.RollingFileAppender 18 | #log4j.appender.logfile.Append=false 19 | #log4j.appender.logfile.File=./logs/server1/SystemOut.log 20 | # Pattern to output: date priority [category] - message 21 | #log4j.appender.logfile.layout=org.apache.log4j.PatternLayout 22 | #log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n -------------------------------------------------------------------------------- /quartz_demo/target/classes/spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 24 | 25 | 26 | 27 | 28 | 29 | classpath:config.properties 30 | 31 | 32 | 33 | 34 | 35 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | false 53 | 54 | 55 | false 56 | 57 | 58 | true 59 | 60 | 61 | 1800000 62 | 63 | 64 | 90000 65 | 66 | 67 | SELECT 1 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | com.quartz 79 | 80 | 81 | 82 | 83 | ${hibernate.dialect} 84 | ${hibernate.ejb.naming_strategy} 85 | ${hibernate.show_sql} 86 | ${hibernate.format_sql} 87 | ${hibernate.hbm2ddl.auto} 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | -------------------------------------------------------------------------------- /quartz_demo/target/m2e-wtp/web-resources/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Built-By: handx 3 | Build-Jdk: 1.8.0_144 4 | Created-By: Maven Integration for Eclipse 5 | 6 | -------------------------------------------------------------------------------- /quartz_demo/target/m2e-wtp/web-resources/META-INF/maven/com.framework/quartz_demo/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven Integration for Eclipse 2 | #Mon Aug 21 21:44:41 CST 2017 3 | version=0.0.1-SNAPSHOT 4 | groupId=com.framework 5 | m2e.projectName=quartz_demo 6 | m2e.projectLocation=D\:\\work\\other\\frameworkAggregate\\quartz_demo 7 | artifactId=quartz_demo 8 | -------------------------------------------------------------------------------- /quartz_demo/target/m2e-wtp/web-resources/META-INF/maven/com.framework/quartz_demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | com.framework 8 | frameworkAggregate 9 | 0.0.1-SNAPSHOT 10 | ../pom.xml 11 | 12 | 13 | quartz_demo 14 | war 15 | 16 | 17 | quartz_demo 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /quartz_demo/target/test-classes/spring-quartz.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /restful_demo/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /restful_demo/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /restful_demo/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | restful_demo 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.wst.common.project.facet.core.builder 15 | 16 | 17 | 18 | 19 | org.eclipse.m2e.core.maven2Builder 20 | 21 | 22 | 23 | 24 | org.eclipse.wst.validation.validationbuilder 25 | 26 | 27 | 28 | 29 | 30 | org.eclipse.jem.workbench.JavaEMFNature 31 | org.eclipse.wst.common.modulecore.ModuleCoreNature 32 | org.eclipse.jdt.core.javanature 33 | org.eclipse.m2e.core.maven2Nature 34 | org.eclipse.wst.common.project.facet.core.nature 35 | org.eclipse.wst.jsdt.core.jsNature 36 | 37 | 38 | -------------------------------------------------------------------------------- /restful_demo/.settings/.jsdtscope: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /restful_demo/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/resources=UTF-8 3 | encoding/=UTF-8 4 | -------------------------------------------------------------------------------- /restful_demo/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 4 | org.eclipse.jdt.core.compiler.compliance=1.5 5 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 6 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 7 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 8 | org.eclipse.jdt.core.compiler.source=1.5 9 | -------------------------------------------------------------------------------- /restful_demo/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /restful_demo/.settings/org.eclipse.wst.common.component: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /restful_demo/.settings/org.eclipse.wst.common.project.facet.core.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /restful_demo/.settings/org.eclipse.wst.jsdt.ui.superType.container: -------------------------------------------------------------------------------- 1 | org.eclipse.wst.jsdt.launching.baseBrowserLibrary -------------------------------------------------------------------------------- /restful_demo/.settings/org.eclipse.wst.jsdt.ui.superType.name: -------------------------------------------------------------------------------- 1 | Window -------------------------------------------------------------------------------- /restful_demo/.settings/org.eclipse.wst.validation.prefs: -------------------------------------------------------------------------------- 1 | disabled=06target 2 | eclipse.preferences.version=1 3 | -------------------------------------------------------------------------------- /restful_demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | com.framework 9 | frameworkAggregate 10 | 0.0.1-SNAPSHOT 11 | ../pom.xml 12 | 13 | 14 | restful_demo 15 | war 16 | 17 | 18 | UTF-8 19 | 2.7 20 | 21 | 22 | 23 | 24 | 25 | org.glassfish.jersey.containers 26 | jersey-container-servlet-core 27 | ${jersey2.version} 28 | 29 | 30 | 31 | org.glassfish.jersey.containers 32 | jersey-container-servlet 33 | ${jersey2.version} 34 | 35 | 36 | 37 | org.glassfish.jersey.media 38 | jersey-media-json-jackson 39 | ${jersey2.version} 40 | 41 | 42 | 43 | 44 | 45 | restful_demo 46 | 47 | 48 | -------------------------------------------------------------------------------- /restful_demo/src/main/java/com/framework/comm/RestResourceConfig.java: -------------------------------------------------------------------------------- 1 | package com.framework.comm; 2 | 3 | import org.glassfish.jersey.filter.LoggingFilter; 4 | import org.glassfish.jersey.jackson.JacksonFeature; 5 | import org.glassfish.jersey.message.DeflateEncoder; 6 | import org.glassfish.jersey.message.GZipEncoder; 7 | import org.glassfish.jersey.server.ResourceConfig; 8 | import org.glassfish.jersey.server.filter.EncodingFilter; 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | 12 | import com.framework.rest.filter.RestAuthRequestFilter; 13 | import com.framework.rest.filter.RestResponseFilter; 14 | 15 | 16 | /** 17 | * rest register 18 | */ 19 | public class RestResourceConfig extends ResourceConfig { 20 | Logger log = LoggerFactory.getLogger(RestResourceConfig.class); 21 | 22 | public RestResourceConfig() { 23 | log.info("-----------------------------loading JERSEY2 restful---------------------------"); 24 | packages("com.framework.rest.service"); 25 | register(RestAuthRequestFilter.class); 26 | register(RestResponseFilter.class); 27 | register(LoggingFilter.class); 28 | register(JacksonFeature.class); 29 | register(DeflateEncoder.class); 30 | EncodingFilter.enableFor(this, GZipEncoder.class); 31 | } 32 | } -------------------------------------------------------------------------------- /restful_demo/src/main/java/com/framework/model/User.java: -------------------------------------------------------------------------------- 1 | package com.framework.model; 2 | 3 | public class User { 4 | 5 | private String name; 6 | private String sex; 7 | private Integer age; 8 | 9 | public String getName() { 10 | return name; 11 | } 12 | 13 | public void setName(String name) { 14 | this.name = name; 15 | } 16 | 17 | public String getSex() { 18 | return sex; 19 | } 20 | 21 | public void setSex(String sex) { 22 | this.sex = sex; 23 | } 24 | 25 | public Integer getAge() { 26 | return age; 27 | } 28 | 29 | public void setAge(Integer age) { 30 | this.age = age; 31 | } 32 | 33 | public User(String name, String sex, Integer age) { 34 | super(); 35 | this.name = name; 36 | this.sex = sex; 37 | this.age = age; 38 | } 39 | 40 | public User() { 41 | super(); 42 | // TODO Auto-generated constructor stub 43 | } 44 | 45 | @Override 46 | public String toString() { 47 | return "User [name=" + name + ", sex=" + sex + ", age=" + age + "]"; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /restful_demo/src/main/java/com/framework/rest/filter/RestAuthRequestFilter.java: -------------------------------------------------------------------------------- 1 | package com.framework.rest.filter; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.http.HttpServletRequest; 6 | import javax.servlet.http.HttpServletResponse; 7 | import javax.ws.rs.container.ContainerRequestContext; 8 | import javax.ws.rs.container.ContainerRequestFilter; 9 | import javax.ws.rs.core.Context; 10 | import javax.ws.rs.core.HttpHeaders; 11 | import javax.ws.rs.core.MediaType; 12 | import javax.ws.rs.core.SecurityContext; 13 | 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | 17 | public class RestAuthRequestFilter implements ContainerRequestFilter { 18 | Logger log = LoggerFactory.getLogger(RestAuthRequestFilter.class); 19 | 20 | @Context HttpHeaders httpHeaders; 21 | @Context SecurityContext sc; 22 | @Context HttpServletResponse response; 23 | @Context HttpServletRequest request; 24 | public void filter(ContainerRequestContext requestContext) 25 | throws IOException { 26 | 27 | String headerString = requestContext.getHeaderString("content-type"); 28 | if (headerString != null) { 29 | if (headerString.startsWith(MediaType.APPLICATION_FORM_URLENCODED)) 30 | requestContext.getHeaders().putSingle("content-type", MediaType.APPLICATION_FORM_URLENCODED); 31 | } 32 | 33 | log.info("RESTFUL接口请求 RequestURL:{}",request.getRequestURL()); 34 | log.info("[RESTFUL] RequestAddr:{}",request.getRemoteAddr()); 35 | 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /restful_demo/src/main/java/com/framework/rest/filter/RestResponseFilter.java: -------------------------------------------------------------------------------- 1 | package com.framework.rest.filter; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.ws.rs.container.ContainerRequestContext; 6 | import javax.ws.rs.container.ContainerResponseContext; 7 | import javax.ws.rs.container.ContainerResponseFilter; 8 | 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | 12 | public class RestResponseFilter implements ContainerResponseFilter { 13 | 14 | Logger log = LoggerFactory.getLogger(RestResponseFilter.class); 15 | 16 | public void filter(ContainerRequestContext arg0, 17 | ContainerResponseContext arg1) throws IOException { 18 | 19 | log.info("jersey2 Response"); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /restful_demo/src/main/java/com/framework/rest/service/UserRestService.java: -------------------------------------------------------------------------------- 1 | package com.framework.rest.service; 2 | 3 | import javax.ws.rs.Consumes; 4 | import javax.ws.rs.POST; 5 | import javax.ws.rs.Path; 6 | import javax.ws.rs.PathParam; 7 | import javax.ws.rs.Produces; 8 | 9 | import com.framework.model.User; 10 | 11 | @Path("/user") 12 | public class UserRestService { 13 | 14 | @POST 15 | @Path("{userId}") 16 | @Consumes("application/json; charset=UTF-8") 17 | @Produces("application/json; charset=UTF-8") 18 | public User getUser(@PathParam("userId") Integer userId) { 19 | return new User("jack","man",15); 20 | } 21 | 22 | 23 | 24 | } 25 | -------------------------------------------------------------------------------- /restful_demo/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | 9 | jersey-servlet 10 | org.glassfish.jersey.servlet.ServletContainer 11 | 12 | javax.ws.rs.Application 13 | com.framework.comm.RestResourceConfig 14 | 15 | 2 16 | 17 | 18 | jersey-servlet 19 | /rs/* 20 | 21 | 22 | -------------------------------------------------------------------------------- /restful_demo/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /spring_demo/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /spring_demo/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /spring_demo/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | spring_demo 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /spring_demo/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/main/resources=UTF-8 4 | encoding//src/test/java=UTF-8 5 | encoding/=UTF-8 6 | -------------------------------------------------------------------------------- /spring_demo/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.5 6 | -------------------------------------------------------------------------------- /spring_demo/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /spring_demo/README.md: -------------------------------------------------------------------------------- 1 | ## spring框架学习笔记 2 | 3 | ## IOC 4 | 5 | ### IoC是什么? 6 | Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想。Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制。 7 | IoC容器就是具有依赖注入功能的容器,IoC容器负责实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。应用程序无需直接在代码中new相关的对象,应用程序由IoC容器进行组装。 8 | 9 | ### IoC能做什么? 10 | 它能指导我们如何设计出松耦合、更优良的程序。有了IoC容器后,把创建和查找依赖对象的控制权交给了容器,由容器进行注入组合对象,所以对象与对象之间是松散耦合,这样也方便测试,利于功能复用,更重要的是使得程序的整个体系结构变得非常灵活。 11 | 12 | ### IoC和DI 13 | DI—Dependency Injection,即“依赖注入”:是组件之间依赖关系由容器在运行期决定,形象的说,即由容器动态的将某个依赖关系注入到组件之中。 14 | 15 | 理解DI的关键是:“谁依赖谁,为什么需要依赖,谁注入谁,注入了什么? 16 | - 谁依赖于谁:当然是应用程序依赖于IoC容器; 17 | - 为什么需要依赖:应用程序需要IoC容器来提供对象需要的外部资源; 18 | - 谁注入谁:很明显是IoC容器注入应用程序某个对象,应用程序依赖的对象; 19 | - 注入了什么:就是注入某个对象所需要的外部资源(包括对象、资源、常量数据) 20 | 21 | > “依赖注入”,相对IoC 而言,“依赖注入”明确描述了“被注入对象依赖IoC容器配置依赖对象”。 22 | 23 | ## DI依赖注入 24 | 25 | ### 依赖和依赖注入 26 | *依赖一般指“类之间的关系”:* 27 | - 泛化:表示类与类之间的继承关系、接口与接口之间的继承关系; 28 | - 实现:表示类对接口的实现; 29 | - 依赖:当类与类之间有使用关系时就属于依赖关系,不同于关联关系,依赖不具有“拥有关系”,而是一种“相识关系”,只在某个特定地方(比如某个方法体内)才有关系。 30 | - 关联:表示类与类或类与接口之间的依赖关系,表现为“拥有关系”;具体到代码可以用实例变量来表示; 31 | - 聚合:属于是关联的特殊情况,体现部分-整体关系,是一种弱拥有关系;整体和部分可以有不一样的生命周期;是一种弱关联; 32 | - 组合:属于是关联的特殊情况,也体现了体现部分-整体关系,是一种强“拥有关系”;整体与部分有相同的生命周期,是一种强关联; 33 | 34 | *依赖注入,应用依赖注入的好处:* 35 | - **动态替换Bean依赖对象,程序更灵活**:替换Bean依赖对象,无需修改源文件:应用依赖注入后,由于可以采用配置文件方式实现,从而能随时动态的替换Bean的依赖对象,无需修改java源文件; 36 | - **更好实践面向接口编程,代码更清晰**:在Bean中只需指定依赖对象的接口,接口定义依赖对象完成的功能,通过容器注入依赖实现; 37 | - **更好实践优先使用对象组合,而不是类继承**:因为IoC容器采用注入依赖,也就是组合对象,从而更好的实践对象组合。 38 | 39 | - 采用对象组合,Bean的功能可能由几个依赖Bean的功能组合而成,其Bean本身可能只提供少许功能或根本无任何功能,全部委托给依赖Bean,对象组合具有动态性,能更方便的替换掉依赖Bean,从而改变Bean功能; 40 | - 而如果采用类继承,Bean没有依赖Bean,而是采用继承方式添加新功能,,而且功能是在编译时就确定了,不具有动态性,而且采用类继承导致Bean与子Bean之间高度耦合,难以复用。 41 | 42 | - **增加Bean可复用性**:依赖于对象组合,Bean更可复用且复用更简单; 43 | - **降低Bean之间耦合**:由于我们完全采用面向接口编程,在代码中没有直接引用Bean依赖实现,全部引用接口,而且不会出现显示的创建依赖对象代码,而且这些依赖是由容器来注入,很容易替换依赖实现类,从而降低Bean与依赖之间耦合; 44 | - **代码结构更清晰**:要应用依赖注入,代码结构要按照规约方式进行书写,从而更好的应用一些最佳实践,因此代码结构更清晰。 45 | 46 | *如何注入bean?* 47 | - **构造器注入**:就是容器实例化Bean时注入那些依赖,通过在在Bean定义中指定构造器参数进行注入依赖,包括实例工厂方法参数注入依赖,但静态工厂方法参数不允许注入依赖; 48 | - **setter注入**:通过setter方法进行注入依赖; 49 | - **方法注入**:能通过配置方式替换掉Bean方法,也就是通过配置改变Bean方法 功能。 50 | 51 | *延迟初始化Bean:* 52 | 53 | - 延迟初始化也叫做惰性初始化,指不提前初始化Bean,而是只有在真正使用时才创建及初始化Bean。配置方式很简单只需在``标签上指定 `lazy-init`属性值为`true`即可延迟初始化Bean。 54 | 55 | 56 | ### DI之 Bean的作用域 57 | - singleton:指“singleton”作用域的Bean只会在每个Spring IoC容器中存在一个实例,而且其完整生命周期完全由Spring容器管理。对于所有获取该Bean的操作Spring容器将只返回同一个Bean。 58 | - prototype:即原型,指每次向Spring容器请求获取Bean都返回一个全新的Bean,相对于“singleton”来说就是不缓存Bean,每次都是一个根据Bean定义创建的全新Bean。 59 | -------------------------------------------------------------------------------- /spring_demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.framework 7 | frameworkAggregate 8 | 0.0.1-SNAPSHOT 9 | ../pom.xml 10 | 11 | 12 | spring_demo 13 | spring_demo 14 | 15 | 16 | UTF-8 17 | 18 | 19 | 20 | spring_demo 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /spring_demo/src/main/java/org/handx/constructor/Battery.java: -------------------------------------------------------------------------------- 1 | package org.handx.constructor; 2 | 3 | public class Battery extends Product { 4 | 5 | private boolean rechargeable; 6 | 7 | public Battery() { 8 | super(); 9 | // TODO Auto-generated constructor stub 10 | } 11 | 12 | public Battery(boolean rechargeable) { 13 | super(); 14 | this.rechargeable = rechargeable; 15 | } 16 | 17 | public Battery(String name, Double price) { 18 | super(name, price); 19 | // TODO Auto-generated constructor stub 20 | } 21 | 22 | public boolean isRechargeable() { 23 | return rechargeable; 24 | } 25 | 26 | public void setRechargeable(boolean rechargeable) { 27 | this.rechargeable = rechargeable; 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return "Battery [rechargeable=" + rechargeable + ", getName()=" + getName() + ", getPrice()=" + getPrice() 33 | + "]"; 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /spring_demo/src/main/java/org/handx/constructor/ConstructorBeanSimple.java: -------------------------------------------------------------------------------- 1 | package org.handx.constructor; 2 | 3 | import org.junit.Test; 4 | import org.springframework.context.ApplicationContext; 5 | import org.springframework.context.support.ClassPathXmlApplicationContext; 6 | 7 | public class ConstructorBeanSimple { 8 | 9 | @Test 10 | public void testConstructorBean() { 11 | ApplicationContext context = new ClassPathXmlApplicationContext("spring-constructor.xml"); 12 | Product battery = (Product) context.getBean("battery"); 13 | Product disc = (Product) context.getBean("disc"); 14 | System.out.println(battery.toString()); 15 | System.out.println(disc.toString()); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /spring_demo/src/main/java/org/handx/constructor/Disc.java: -------------------------------------------------------------------------------- 1 | package org.handx.constructor; 2 | 3 | public class Disc extends Product { 4 | 5 | private int capacity; 6 | 7 | public Disc() { 8 | super(); 9 | // TODO Auto-generated constructor stub 10 | } 11 | 12 | public Disc(int capacity) { 13 | super(); 14 | this.capacity = capacity; 15 | } 16 | 17 | public Disc(String name, Double price) { 18 | super(name, price); 19 | // TODO Auto-generated constructor stub 20 | } 21 | 22 | public int getCapacity() { 23 | return capacity; 24 | } 25 | 26 | public void setCapacity(int capacity) { 27 | this.capacity = capacity; 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return "Disc [capacity=" + capacity + ", getName()=" + getName() + ", getPrice()=" + getPrice() + "]"; 33 | } 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /spring_demo/src/main/java/org/handx/constructor/Product.java: -------------------------------------------------------------------------------- 1 | package org.handx.constructor; 2 | 3 | public class Product { 4 | 5 | private String name; 6 | private Double price; 7 | 8 | public Product() { 9 | super(); 10 | // TODO Auto-generated constructor stub 11 | } 12 | 13 | public Product(String name, Double price) { 14 | super(); 15 | this.name = name; 16 | this.price = price; 17 | } 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | public Double getPrice() { 24 | return price; 25 | } 26 | 27 | public void setName(String name) { 28 | this.name = name; 29 | } 30 | 31 | public void setPrice(Double price) { 32 | this.price = price; 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return "Product [name=" + name + ", price=" + price + "]"; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /spring_demo/src/main/java/org/handx/ioc/IocBeanConfigSimple.java: -------------------------------------------------------------------------------- 1 | package org.handx.ioc; 2 | 3 | import org.junit.Test; 4 | import org.springframework.context.ApplicationContext; 5 | import org.springframework.context.support.ClassPathXmlApplicationContext; 6 | 7 | public class IocBeanConfigSimple { 8 | 9 | @Test 10 | public void testIoc() { 11 | 12 | ApplicationContext context = new ClassPathXmlApplicationContext("spring-ioc.xml"); 13 | SequenceGenerator generator = (SequenceGenerator) context.getBean("sequenceGenerator"); 14 | System.out.println(generator.toString()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /spring_demo/src/main/java/org/handx/ioc/SequenceGenerator.java: -------------------------------------------------------------------------------- 1 | package org.handx.ioc; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | import java.util.Map; 6 | import java.util.Properties; 7 | import java.util.Set; 8 | 9 | public class SequenceGenerator { 10 | 11 | private String prefix; 12 | private String suffix; 13 | private int inital; 14 | 15 | private List lists; 16 | private Object[] arrs; 17 | private Set sets; 18 | private Map maps; 19 | private Properties properties; 20 | 21 | public SequenceGenerator() { 22 | super(); 23 | // TODO Auto-generated constructor stub 24 | } 25 | 26 | public SequenceGenerator(String prefix, String suffix, int inital) { 27 | super(); 28 | this.prefix = prefix; 29 | this.suffix = suffix; 30 | this.inital = inital; 31 | } 32 | 33 | public int getInital() { 34 | return inital; 35 | } 36 | 37 | public String getPrefix() { 38 | return prefix; 39 | } 40 | 41 | public String getSuffix() { 42 | return suffix; 43 | } 44 | 45 | public void setArrs(Object[] arrs) { 46 | this.arrs = arrs; 47 | } 48 | 49 | public void setInital(int inital) { 50 | this.inital = inital; 51 | } 52 | 53 | public void setLists(List lists) { 54 | this.lists = lists; 55 | } 56 | 57 | public void setMaps(Map maps) { 58 | this.maps = maps; 59 | } 60 | 61 | public void setPrefix(String prefix) { 62 | this.prefix = prefix; 63 | } 64 | 65 | public void setProperties(Properties properties) { 66 | this.properties = properties; 67 | } 68 | 69 | public void setSets(Set sets) { 70 | this.sets = sets; 71 | } 72 | 73 | public void setSuffix(String suffix) { 74 | this.suffix = suffix; 75 | } 76 | 77 | @Override 78 | public String toString() { 79 | return "SequenceGenerator [prefix=" + prefix + ", suffix=" + suffix + ", inital=" + inital + ", lists=" + lists 80 | + ", arrs=" + Arrays.toString(arrs) + ", sets=" + sets + ", maps=" + maps + ", properties=" + properties 81 | + "]"; 82 | } 83 | 84 | 85 | 86 | 87 | } 88 | -------------------------------------------------------------------------------- /spring_demo/src/main/java/org/handx/scanning/Main.java: -------------------------------------------------------------------------------- 1 | package org.handx.scanning; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.context.support.ClassPathXmlApplicationContext; 5 | 6 | public class Main { 7 | 8 | public static void main(String[] args) { 9 | ApplicationContext context = new ClassPathXmlApplicationContext("spring-scanning.xml"); 10 | SequenceService sequenceService = (SequenceService) context.getBean("sequenceService"); 11 | System.out.println(sequenceService.generate("IT")); 12 | System.out.println(sequenceService.generate("IT")); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /spring_demo/src/main/java/org/handx/scanning/Sequence.java: -------------------------------------------------------------------------------- 1 | package org.handx.scanning; 2 | 3 | public class Sequence { 4 | 5 | private String id; 6 | private String prefix; 7 | private String suffix; 8 | 9 | public Sequence() { 10 | super(); 11 | // TODO Auto-generated constructor stub 12 | } 13 | 14 | public Sequence(String id, String prefix, String suffix) { 15 | super(); 16 | this.id = id; 17 | this.prefix = prefix; 18 | this.suffix = suffix; 19 | } 20 | 21 | public String getId() { 22 | return id; 23 | } 24 | 25 | public String getPrefix() { 26 | return prefix; 27 | } 28 | 29 | public String getSuffix() { 30 | return suffix; 31 | } 32 | 33 | public void setId(String id) { 34 | this.id = id; 35 | } 36 | 37 | public void setPrefix(String prefix) { 38 | this.prefix = prefix; 39 | } 40 | 41 | public void setSuffix(String suffix) { 42 | this.suffix = suffix; 43 | } 44 | 45 | @Override 46 | public String toString() { 47 | return "Sequence [id=" + id + ", prefix=" + prefix + ", suffix=" + suffix + "]"; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /spring_demo/src/main/java/org/handx/scanning/SequenceDao.java: -------------------------------------------------------------------------------- 1 | package org.handx.scanning; 2 | 3 | public interface SequenceDao { 4 | 5 | public int getNextValue(String id); 6 | 7 | public Sequence getSequence(String id); 8 | } 9 | -------------------------------------------------------------------------------- /spring_demo/src/main/java/org/handx/scanning/SequenceDaoImpl.java: -------------------------------------------------------------------------------- 1 | package org.handx.scanning; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class SequenceDaoImpl implements SequenceDao { 7 | 8 | private Map sequences; 9 | private Map values; 10 | 11 | public SequenceDaoImpl() { 12 | sequences = new HashMap(); 13 | sequences.put("IT", new Sequence("IT", "30", "a")); 14 | values = new HashMap(); 15 | values.put("IT", 1000); 16 | } 17 | 18 | public int getNextValue(String id) { 19 | int value = values.get(id); 20 | values.put(id, value + 1); 21 | return value; 22 | } 23 | 24 | public Sequence getSequence(String id) { 25 | return sequences.get(id); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /spring_demo/src/main/java/org/handx/scanning/SequenceService.java: -------------------------------------------------------------------------------- 1 | package org.handx.scanning; 2 | 3 | public class SequenceService { 4 | 5 | SequenceDao sequenceDaoImpl; 6 | 7 | public String generate(String id) { 8 | Sequence sequence = sequenceDaoImpl.getSequence(id); 9 | int value = sequenceDaoImpl.getNextValue(id); 10 | return sequence.getPrefix() + "::" + value + "::" + sequence.getSuffix(); 11 | } 12 | 13 | public void setSequenceDao(SequenceDao sequenceDao) { 14 | sequenceDaoImpl = sequenceDao; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring_demo/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootCategory=debug,info,stdout,logfile 2 | 3 | #stdout configure 4 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 5 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 6 | log4j.appender.stdout.layout.ConversionPattern= %d %p [%c] - <%m>%n 7 | 8 | #logfile configure 9 | log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender 10 | log4j.appender.logfile.File=logs/framework.log 11 | log4j.appender.logfile.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.logfile.layout.ConversionPattern= %d %p [%c] - <%m>%n 13 | 14 | log4j.logger.org.apache.zookeeper.ClientCnxn=info 15 | log4j.logger.org.vista.mall.web.ui.utils.ModelHolder=info 16 | 17 | #log4j.appender.logfile=org.apache.log4j.RollingFileAppender 18 | #log4j.appender.logfile.Append=false 19 | #log4j.appender.logfile.File=./logs/server1/SystemOut.log 20 | # Pattern to output: date priority [category] - message 21 | #log4j.appender.logfile.layout=org.apache.log4j.PatternLayout 22 | #log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n -------------------------------------------------------------------------------- /spring_demo/src/main/resources/spring-constructor.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /spring_demo/src/main/resources/spring-ioc.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 21 | 22 | 23 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | A 38 | B 39 | C 40 | D 41 | 42 | 43 | 44 | 45 | 46 | 1 47 | 2 48 | 3 49 | 4 50 | 51 | 52 | 53 | 54 | 55 | 1 56 | 2 57 | 3 58 | 4 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | handx 72 | man 73 | 24 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /spring_demo/src/main/resources/spring-scanning.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | 14 | --------------------------------------------------------------------------------