[关闭]
@zhanjindong 2015-02-12T07:36:11.000000Z 字数 15190 阅读 2702

iFlytek

[ossp-framework][v1.0.1][RELEASE LOG][2014-09-17]


Release Log

ossp-framework-common

  1. XML和Properties配置文件读取功能,支持动态监听。
  2. HTTP请求,支持URL负载均衡。
  3. 提供基本的IO和流相关的操作。
  4. 提供邮件和短信发送相关类。
  5. 提供资源寻址和路径相关的工具类。
  6. 提供常用加密解密相关操作的类。
  7. XML和JSON序列化和反序列化操作。
  8. Tomcat端口和服务器IP检测。
  9. 提供用于文本处理相关的类和接口,封装基本的正则操作。
  10. 其他工具类:gzip压缩,FTP帮助类,类型转换的try-parse工具类。

ossp-framework-usdl

  1. MongoDB连接池管理。
  2. Mysql连接池管理。
  3. Redis连接池管理。
  4. Solr连接池管理。
  5. Tfs连接池管理。

ossp-framework-dt

  1. 基准测试基类。
  2. JMeter测试基类。
  3. Testng单元测试基类。

POM Dependency

Nexus私服

ossp-framework-common

  1. <dependency>
  2. <groupId>com.iflytek</groupId>
  3. <artifactId>ossp-framework-common</artifactId>
  4. <version>1.0.1</version>
  5. </dependency>

ossp-framework-usdl

  1. <dependency>
  2. <groupId>com.iflytek</groupId>
  3. <artifactId>ossp-framework-usdl</artifactId>
  4. <version>1.0.1</version>
  5. </dependency>

ossp-framework-dt

  1. <dependency>
  2. <groupId>com.iflytek</groupId>
  3. <artifactId>ossp-framework-dt</artifactId>
  4. <version>1.0.1</version>
  5. </dependency>

Javadoc

点击查看
使用示例,请查看Example Packages的内容。


Benchmark(基准测试)

点击查看


性能测试


Change Log

详细的修改日志,请点击查看


Example

引入ossp-framework-example可以查看示例代码:

  1. <dependency>
  2. <groupId>com.iflytek</groupId>
  3. <artifactId>ossp-framework-example</artifactId>
  4. <version>1.0.2-SNAPSHOT</version>
  5. </dependency>

MyqlClientExample

  1. package com.iflytek.ossp.framework.example.usdl.mysql;
  2. import java.beans.PropertyVetoException;
  3. import java.io.IOException;
  4. import java.net.URISyntaxException;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.Iterator;
  8. import java.util.Random;
  9. import com.iflytek.ossp.framework.usdl.mysql.ConnectionCallback;
  10. import com.iflytek.ossp.framework.usdl.mysql.MySqlClient;
  11. import com.iflytek.ossp.framework.usdl.mysql.MySqlConnectionManagement;
  12. /**
  13. *
  14. * {@link MySqlClient} API使用示例。
  15. *
  16. * @author jdzhan,2014-9-22
  17. *
  18. */
  19. public class MyqlClientExample {
  20. static Random random = new Random();
  21. public static void main(String[] args) throws PropertyVetoException, IOException, URISyntaxException, SQLException,
  22. IllegalAccessException {
  23. MySqlConnectionManagement.load();
  24. System.out.println("执行存储过程,只能处理第一个结果集:");
  25. execProcedureExample();
  26. System.out.println("执行存储过程,可以处理多结果集:");
  27. execMutilResultProcedureExample();
  28. System.out.println("执行存储过程,也可以不指定回调接口,返回null:");
  29. execProcedureWithNullCallback();
  30. MySqlConnectionManagement.destroy();
  31. }
  32. // 执行存储过程,只返回第一个结果集。
  33. private static void execProcedureExample() throws PropertyVetoException, IOException, SQLException,
  34. URISyntaxException, IllegalAccessException {
  35. MySqlClient<Object> client = MySqlConnectionManagement.sharedClient("example-2");
  36. int i = random.nextInt(100);
  37. client.execProc("test_query_proc4", new ConnectionCallback<String>() {
  38. @Override
  39. public String callback(ResultSet resultSet) throws SQLException {
  40. while (resultSet.next()) {
  41. System.out.println(resultSet.getString(1));
  42. }
  43. return "";
  44. }
  45. }, "framework-" + i, i);
  46. }
  47. // 执行多结果集的存储过程。
  48. // begin
  49. // insert into test(name,age,description,height)
  50. // values('zdd',11,'dede',122);
  51. // select 1;
  52. // select 2;
  53. // select 3;
  54. // select 4;
  55. // end;
  56. private static void execMutilResultProcedureExample() throws PropertyVetoException, IOException,
  57. URISyntaxException, IllegalAccessException, SQLException {
  58. MySqlClient<Object> client = MySqlConnectionManagement.sharedClient("example-2");
  59. int i = random.nextInt(100);
  60. client.execMutilProc("test_query_proc4",
  61. new com.iflytek.ossp.framework.usdl.mysql.ConnectionMutilCallback<String>() {
  62. @Override
  63. public String callback(Iterable<ResultSet> resultSets) throws SQLException {
  64. Iterator<ResultSet> iterator = resultSets.iterator();
  65. for (ResultSet resultSet : resultSets) {
  66. while (resultSet.next()) {
  67. System.out.println(resultSet.getString(1));
  68. }
  69. }
  70. return "";
  71. }
  72. }, "framework-" + i, i);
  73. }
  74. /** 可以不知道回调接口,返回结果为null */
  75. private static void execProcedureWithNullCallback() throws PropertyVetoException, IOException, URISyntaxException,
  76. IllegalAccessException, SQLException {
  77. MySqlClient<Object> client = MySqlConnectionManagement.sharedClient("example-2");
  78. int i = random.nextInt(100);
  79. Object result = client.execMutilProc("test_query_proc4", null, "framework-" + i, i);
  80. System.out.println("result:" + result);
  81. result = client.execProc("test_query_proc4", null, "framework-" + i, i);
  82. System.out.println("result:" + result);
  83. }
  84. }

MysqlConnectionManagementExample

  1. package com.iflytek.ossp.framework.example.usdl.mysql;
  2. import java.beans.PropertyVetoException;
  3. import java.io.IOException;
  4. import java.net.URISyntaxException;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import com.iflytek.ossp.framework.usdl.mysql.ConnectionCallback;
  8. import com.iflytek.ossp.framework.usdl.mysql.MySqlClient;
  9. import com.iflytek.ossp.framework.usdl.mysql.MySqlConnectionManagement;
  10. /**
  11. * {@link MySqlConnectionManagement}使用示例。
  12. *
  13. */
  14. public class MysqlConnectionManagementExample {
  15. public static void main(String[] args) throws PropertyVetoException, IOException, URISyntaxException, SQLException,
  16. IllegalAccessException {
  17. // 在调用之前需要先调用load方法,默认无参的方法会初始化classpath:config/dbaccess/mysql路径下的所有配置。
  18. // 默认使用c3p0连接池,如果需要使用druid或dbcp,请在配置文件中指定dataSource的值为c3p0.
  19. MySqlConnectionManagement.load();
  20. // 重复调用无效
  21. MySqlConnectionManagement.load("/error/path");
  22. MySqlClient<Object> client = MySqlConnectionManagement.sharedClient("example");
  23. int rst = (Integer) client.query("select count(*) as count from pet", new TestQueryCallback());
  24. System.out.println(rst);
  25. // insert和update操作
  26. rst = client.update("insert into pet(name,psssword)values('zjd3','121313')");
  27. System.out.println(rst);
  28. rst = client.update("update pet set name = 'zs' where name = 'zjd3'");
  29. System.out.println(rst);
  30. // 程序关闭的时候建议调用静态方法来销毁资源。
  31. MySqlConnectionManagement.destroy();
  32. // 如果配置文件不是在默认的classpath路径下,可以调用重载的load方法指定父级路径,
  33. // 但注意配置文件所在的上级目录必须为mysql,如下面的参数则配置文件在classpath:config/mysql路径下
  34. MySqlConnectionManagement.load("classpath:config");
  35. client = MySqlConnectionManagement.sharedClient("example2");
  36. rst = (Integer) client.query("select count(*) as count from pet", new TestQueryCallback());
  37. System.out.println(rst);
  38. // insert和update操作
  39. rst = client.update("insert into pet(name,psssword)values('zjd3','121313')");
  40. System.out.println(rst);
  41. rst = client.update("update pet set name = 'zs' where name = 'zjd3'");
  42. System.out.println(rst);
  43. MySqlConnectionManagement.destroy();
  44. // dbcp example
  45. // 注意dbcp除了基本的配置项 其余的和c3p0以及druid差别较大。
  46. MySqlConnectionManagement.load();
  47. // 重复调用无效
  48. client = MySqlConnectionManagement.sharedClient("example-dbcp");
  49. rst = (Integer) client.query("select count(*) as count from pet", new TestQueryCallback());
  50. System.out.println(rst);
  51. // insert和update操作
  52. rst = client.update("insert into pet(name,psssword)values('zjd3','121313')");
  53. System.out.println(rst);
  54. rst = client.update("update pet set name = 'zs' where name = 'zjd3'");
  55. System.out.println(rst);
  56. // 程序关闭的时候建议调用静态方法来销毁资源。
  57. MySqlConnectionManagement.destroy();
  58. // 父级路径也可以是绝对路径,如:配置文件在D:\example\mysql下。
  59. MySqlConnectionManagement.load("file:D:\\example");
  60. // 或
  61. // MySqlConnectionManagement.load("D:\\example");
  62. // 或
  63. // MySqlConnectionManagement.load("D:/example");
  64. // reload:相当于先destroy再load
  65. MySqlConnectionManagement.reload("file:D:\\example");
  66. MySqlConnectionManagement.destroy();
  67. }
  68. public static class TestQueryCallback implements ConnectionCallback<Integer> {
  69. @Override
  70. public Integer callback(ResultSet resultSet) throws SQLException {
  71. while (resultSet.next()) {
  72. return resultSet.getInt("count");
  73. }
  74. return -1;
  75. }
  76. }
  77. }

FastjsonExample

  1. package com.iflytek.ossp.framework.example.common.serializable;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.alibaba.fastjson.annotation.JSONField;
  5. import com.alibaba.fastjson.serializer.SerializerFeature;
  6. import com.iflytek.ossp.framework.common.serializable.JSONRoot;
  7. import com.iflytek.ossp.framework.common.serializable.SerializationInstance;
  8. import com.iflytek.ossp.framework.common.serializable.SerializationTools;
  9. import com.thoughtworks.xstream.annotations.XStreamAlias;
  10. import com.thoughtworks.xstream.annotations.XStreamImplicit;
  11. import com.thoughtworks.xstream.annotations.XStreamOmitField;
  12. /**
  13. *
  14. * {@link SerializationTools#fastFromJson(Class, String)}<br />
  15. * {@link SerializationTools#fastToJson(Object)}<br />
  16. *
  17. * @author jdzhan,2014-8-21
  18. *
  19. */
  20. public class FastjsonExample {
  21. static SerializationInstance tools = SerializationInstance.sharedInstance();
  22. public static void main(String[] args) throws IllegalStateException, Exception {
  23. // annotationBenchmark();
  24. fastJsonExample();
  25. }
  26. public static void fastJsonExample() throws SecurityException, IllegalArgumentException, NoSuchFieldException,
  27. IllegalAccessException {
  28. A a = new A();
  29. a.setStr1("str1");
  30. a.setStr2("str2");
  31. List<String> list = new ArrayList<String>();
  32. list.add("item1");
  33. list.add("item1");
  34. a.setLists(list);
  35. List<B> barr = new ArrayList<FastjsonExample.B>();
  36. for (int i = 0; i < 2; i++) {
  37. B b = new B();
  38. b.setBstr1("b str1");
  39. b.setBstr2("b str2");
  40. barr.add(b);
  41. }
  42. a.setBarr(barr);
  43. // fastjson
  44. String json = tools.fastToJson(a, true);
  45. System.out.println("fastjson result:\r\n" + json);
  46. A newA = tools.fastFromJson(A.class, json);
  47. System.out.println(newA);
  48. // xstream
  49. json = tools.toJson(a);
  50. System.out.println("xstream result:\r\n" + json);
  51. newA = tools.fromJson(A.class, json);
  52. System.out.println(newA);
  53. }
  54. @XStreamAlias("abc")
  55. @JSONRoot(name = "abc")
  56. public static class A {
  57. /** 忽略 */
  58. @JSONField(serialize = false)
  59. // fastjson
  60. @XStreamOmitField
  61. // xstream
  62. public String str1 = "str1";
  63. /** 指定别名 */
  64. @JSONField(name = "name")
  65. // xtream
  66. @XStreamAlias("name")
  67. // fastjson
  68. public String str2;
  69. /** 未指定默认为0 */
  70. @JSONField(serialzeFeatures = { SerializerFeature.WriteNullNumberAsZero })
  71. // fastjson
  72. public int ivalue;
  73. /** 未指定默认为空字符串 */
  74. @JSONField(serialzeFeatures = { SerializerFeature.WriteNullStringAsEmpty })
  75. // fastjson
  76. public String str3;
  77. @JSONField(name = "items")
  78. @XStreamImplicit(itemFieldName = "key")
  79. public List<String> lists;
  80. @JSONField(name = "bitems")
  81. @XStreamAlias("bitems")
  82. //@XStreamImplicit(itemFieldName = "bitem")
  83. public List<B> barr;
  84. public String getStr1() {
  85. return str1;
  86. }
  87. public void setStr1(String str1) {
  88. this.str1 = str1;
  89. }
  90. public String getStr2() {
  91. return str2;
  92. }
  93. public void setStr2(String str2) {
  94. this.str2 = str2;
  95. }
  96. public int getIvalue() {
  97. return ivalue;
  98. }
  99. public void setIvalue(int ivalue) {
  100. this.ivalue = ivalue;
  101. }
  102. public String getStr3() {
  103. return str3;
  104. }
  105. public void setStr3(String str3) {
  106. this.str3 = str3;
  107. }
  108. public List<String> getLists() {
  109. return lists;
  110. }
  111. public void setLists(List<String> lists) {
  112. this.lists = lists;
  113. }
  114. public List<B> getBarr() {
  115. return barr;
  116. }
  117. public void setBarr(List<B> barr) {
  118. this.barr = barr;
  119. }
  120. @Override
  121. public String toString() {
  122. return "str1:" + str1 + ",str2:" + str2 + ",str3:" + str3 + ",ivalue" + ivalue + ",lists:" + lists
  123. + "B list:" + barr;
  124. }
  125. }
  126. public static class B {
  127. private String bstr1;
  128. private String bstr2;
  129. public String getBstr1() {
  130. return bstr1;
  131. }
  132. public void setBstr1(String bstr1) {
  133. this.bstr1 = bstr1;
  134. }
  135. public String getBstr2() {
  136. return bstr2;
  137. }
  138. public void setBstr2(String bstr2) {
  139. this.bstr2 = bstr2;
  140. }
  141. @Override
  142. public String toString() {
  143. return "bstr1:" + bstr1 + ",bstr2:" + bstr2;
  144. }
  145. }
  146. }

SimpleXmlExample

  1. package com.iflytek.ossp.framework.example.common.serializable;
  2. import java.util.Date;
  3. import org.simpleframework.xml.Attribute;
  4. import org.simpleframework.xml.Element;
  5. import org.simpleframework.xml.ElementList;
  6. import org.simpleframework.xml.Root;
  7. import com.iflytek.ossp.framework.common.serializable.SerializationInstance;
  8. import com.iflytek.ossp.framework.example.ExampleUtils;
  9. import com.iflytek.ossp.framework.example.common.serializable.weather.cmcc.CmccWeatherBaseResponseObject;
  10. import com.iflytek.ossp.framework.example.common.serializable.weather.cmcc.Result;
  11. import com.thoughtworks.xstream.annotations.XStreamAlias;
  12. import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
  13. import com.thoughtworks.xstream.annotations.XStreamImplicit;
  14. import com.thoughtworks.xstream.annotations.XStreamOmitField;
  15. /**
  16. *
  17. * Simple XML API示例:{@link SerializationInstance#simpleToXml(Object)},
  18. * {@link SerializationInstance#simpleFromXml(Class, String)}<br />
  19. * 详细参看:<a href=
  20. * "http://simple.sourceforge.net/download/stream/doc/examples/examples.php"
  21. * >http://simple.sourceforge.net/download/stream/doc/examples/examples.php</a> <br />
  22. * <a href=
  23. * "http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php"
  24. * >http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php</a>
  25. *
  26. * <p>
  27. * XStream的注解,Simple-XML基本都有等价的,{@link CmccWeatherBaseResponseObject}。<br />
  28. * <code>CDATA => @Element(data = true)</code><br/>
  29. * <code>@XStreamAlias("allresult") => @Root(name ="root"),@Element(name="test")</code>
  30. * <br/>
  31. * <code>@XStreamAsAttribute => @Attribute</code><br/>
  32. * <code>@XStreamImplicit(itemFieldName = "result") => @ElementList(name = "resultlist", inline = true)</code>
  33. * <br/>
  34. * <code>@XStreamOmitField => 没有注解</code>
  35. *
  36. * @author jdzhan,2014-9-10
  37. *
  38. */
  39. public class SimpleXmlExample {
  40. static SerializationInstance tools = SerializationInstance.sharedInstance();
  41. public static void main(String[] args) throws Exception {
  42. SimpleObject vo = new SimpleObject();
  43. vo.setUserName("michael");
  44. vo.setRealName("大大");
  45. vo.setWife("小小");
  46. vo.setHeight(173.3d);
  47. vo.setBornDate(new Date());
  48. Configuration conf = new Configuration();
  49. Server server = new Server();
  50. Security security = new Security();
  51. server.setSecurity(security);
  52. conf.setServer(server);
  53. security.setKeyStore("KeyStore");
  54. security.setSsl(false);
  55. server.setHost("172.16.1.1");
  56. server.setPort(8080);
  57. conf.setId(1);
  58. System.out.println("======================================================");
  59. System.out.println("simple object example:");
  60. // simple object
  61. String xml = tools.simpleToXml(vo);
  62. System.out.println(xml);
  63. vo = tools.simpleFromXml(SimpleObject.class, xml);
  64. System.out.println(vo);
  65. System.out.println("======================================================");
  66. System.out.println("nested object example:");
  67. // nested object
  68. xml = tools.simpleToXml(conf);
  69. System.out.println(xml);
  70. conf = tools.simpleFromXml(Configuration.class, xml);
  71. System.out.println(conf.getServer().getSecurity().getKeyStore());
  72. System.out.println("======================================================");
  73. System.out.println("complex object & annotation vs XStream:");
  74. // annotation
  75. xml = ExampleUtils.readFile("classpath:serializable/cmcc.xml");
  76. System.out.println(xml);
  77. // XStream
  78. CmccWeatherBaseResponseObject obj = tools.fromXml(CmccWeatherBaseResponseObject.class, xml);
  79. System.out.println(obj);
  80. // Simple-XML
  81. xml = tools.simpleToXml(obj);
  82. System.out.println(xml);
  83. }
  84. @Root(name = "root")
  85. public static class SimpleObject {
  86. @Element(name = "UserName")
  87. private String userName;
  88. @Attribute(name = "MyWife")
  89. private String wife;
  90. // CDATA
  91. @Element(name = "RealName", data = true)
  92. private String realName;
  93. @Element(name = "BornDate")
  94. private Date bornDate;
  95. @Element(name = "Height")
  96. private Double height;
  97. public String toString() {
  98. return "MyTestVo : [ userName = " + userName + " , wife = " + wife + " , realName = " + realName
  99. + " , height = " + height + " , bornDate = " + bornDate + " ]";
  100. }
  101. public String getUserName() {
  102. return userName;
  103. }
  104. public void setUserName(String userName) {
  105. this.userName = userName;
  106. }
  107. public String getWife() {
  108. return wife;
  109. }
  110. public void setWife(String wife) {
  111. this.wife = wife;
  112. }
  113. public String getRealName() {
  114. return realName;
  115. }
  116. public void setRealName(String realName) {
  117. this.realName = realName;
  118. }
  119. public Date getBornDate() {
  120. return bornDate;
  121. }
  122. public void setBornDate(Date bornDate) {
  123. this.bornDate = bornDate;
  124. }
  125. public Double getHeight() {
  126. return height;
  127. }
  128. public void setHeight(Double height) {
  129. this.height = height;
  130. }
  131. }
  132. @Root(name = "root")
  133. @XStreamAlias("root")
  134. public static class Configuration {
  135. @Element
  136. private Server server;
  137. @Attribute
  138. @XStreamAsAttribute
  139. private int id;
  140. public int getIdentity() {
  141. return id;
  142. }
  143. public Server getServer() {
  144. return server;
  145. }
  146. public int getId() {
  147. return id;
  148. }
  149. public void setId(int id) {
  150. this.id = id;
  151. }
  152. public void setServer(Server server) {
  153. this.server = server;
  154. }
  155. }
  156. public static class Server {
  157. @Attribute
  158. private int port;
  159. @Element
  160. private String host;
  161. @Element
  162. private Security security;
  163. public int getPort() {
  164. return port;
  165. }
  166. public String getHost() {
  167. return host;
  168. }
  169. public Security getSecurity() {
  170. return security;
  171. }
  172. public void setPort(int port) {
  173. this.port = port;
  174. }
  175. public void setHost(String host) {
  176. this.host = host;
  177. }
  178. public void setSecurity(Security security) {
  179. this.security = security;
  180. }
  181. }
  182. public static class Security {
  183. @Attribute
  184. @XStreamAsAttribute
  185. private boolean ssl;
  186. @Element
  187. private String keyStore;
  188. public boolean isSSL() {
  189. return ssl;
  190. }
  191. public String getKeyStore() {
  192. return keyStore;
  193. }
  194. public boolean isSsl() {
  195. return ssl;
  196. }
  197. public void setSsl(boolean ssl) {
  198. this.ssl = ssl;
  199. }
  200. public void setKeyStore(String keyStore) {
  201. this.keyStore = keyStore;
  202. }
  203. }
  204. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注