This commit is contained in:
2019-01-21 11:10:50 +08:00
commit bcab770043
14 changed files with 378 additions and 0 deletions

12
.gitignore vendored Normal file
View File

@@ -0,0 +1,12 @@
/target/
.project
.classpath
/.settings/
/.externalToolBuilders/
/bin/
*.iml
.idea/
/.idea/
/out/
/tmp/
/libs/

12
qtask/.gitignore vendored Normal file
View File

@@ -0,0 +1,12 @@
/target/
.project
.classpath
/.settings/
/.externalToolBuilders/
/bin/
*.iml
.idea/
/.idea/
/out/
/tmp/
/libs/

34
qtask/pom.xml Normal file
View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.tccn</groupId>
<artifactId>qtask</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,43 @@
package net.tccn;
import net.tccn.base.Kv;
/**
* cate:mysql
* queryId: select * from user where userid=#(userid)
* comment: 查询用户列表
* para: {userid:1}
*
* cate:method
* queryId:com.test.abc.take
* comment:调用Java函数take
* para:{name:xxx,age:12}
*
* cate:http
* query:http://127.0.0.1/meta/db_plat_list
* comment:查询数据平台列表
* para:{platToken:3421432}
*
*
*
*/
public class E {
public String cate;//MYSQL,ES,METHOD,HTTP
public String queryId;
public String dbPlatId;// MYSQL, ES,METHOD,http
public String comment;
public Kv para;
public String restType;//List, Map
public E(String cate, String queryId, String comment, Kv para) {
this.cate = cate;
this.queryId = queryId;
this.comment = comment;
this.para = para;
}
}

View File

@@ -0,0 +1,20 @@
package net.tccn;
import net.tccn.qtask.QTaskHttp;
import net.tccn.qtask.QTaskMethod;
import net.tccn.qtask.QTaskMysql;
public class QRuner {
public static Object query(E e) {
switch (e.cate.toLowerCase()) {
case "mysql":
return new QTaskMysql(e).execute();
case "method":
return new QTaskMethod(e).execute();
case "http":
return new QTaskHttp(e).execute();
}
return null;
}
}

View File

@@ -0,0 +1,21 @@
package net.tccn.base;
import java.util.LinkedHashMap;
/**
* Created by liangxianyou@eversec.cn at 2018/3/12 14:17.
*/
public class Kv<K,V> extends LinkedHashMap {
public static Kv of(){
return new Kv();
}
public static Kv of(Object k, Object v){
return new Kv().set(k,v);
}
public Kv<K, V> set(K k, V v){
put(k, v);
return this;
}
}

View File

@@ -0,0 +1,9 @@
package net.tccn.qtask;
import net.tccn.E;
public interface QTask {
E getE();
Object execute();
}

View File

@@ -0,0 +1,17 @@
package net.tccn.qtask;
import net.tccn.E;
public abstract class QTaskAbs implements QTask {
private E e;
public QTaskAbs(E e) {
this.e = e;
}
@Override
public E getE() {
return e;
}
}

View File

@@ -0,0 +1,16 @@
package net.tccn.qtask;
import net.tccn.E;
public class QTaskEs extends QTaskAbs {
public QTaskEs(E e) {
super(e);
}
@Override
public Object execute() {
//find result by es api todo:
return null;
}
}

View File

@@ -0,0 +1,71 @@
package net.tccn.qtask;
import net.tccn.E;
import java.io.*;
import java.net.*;
/**
* @author: liangxianyou at 2019/1/20 11:07.
*/
public class QTaskHttp extends QTaskAbs implements QTask {
public QTaskHttp(E e) {
super(e);
}
private URLConnection getConnection() {
try {
URI uri = URI.create(getE().queryId);
URL url = uri.toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
StringBuilder content = new StringBuilder();
getE().para.forEach((k,v) -> {
content.append(k).append("=").append(v).append("&");
});
if (content.length() > 0) {
content.deleteCharAt(content.length() - 1);
}
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", String.valueOf(content.length()));
connection.getOutputStream().write(content.toString().getBytes());
return connection;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
public Object execute() {
try (
InputStream is = getConnection().getInputStream();
InputStreamReader reader = new InputStreamReader(is);
BufferedReader br = new BufferedReader(reader);
){
String content = "";
String line;
while ((line = br.readLine()) != null) {
content += line + System.lineSeparator();
}
return content;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

View File

@@ -0,0 +1,52 @@
package net.tccn.qtask;
import net.tccn.E;
import net.tccn.base.Kv;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class QTaskMethod extends QTaskAbs implements QTask {
public QTaskMethod(E e) {
super(e);
}
public String getClazz(){
String queryId = getE().queryId;
return queryId.substring(0, queryId.lastIndexOf("."));
}
public String getMethod(){
String queryId = getE().queryId;
return queryId.substring(queryId.lastIndexOf(".") + 1);
}
public Kv getPara() {
return getE().para;
}
// execute JAVA method by CLASS AND METHOD
@Override
public Object execute() {
try {
Class<?> type = Class.forName(getClazz());
Object obj = type.newInstance();
Method method = type.getMethod(getMethod(), Kv.class);
return method.invoke(obj, getPara());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
}

View File

@@ -0,0 +1,30 @@
package net.tccn.qtask;
import net.tccn.E;
public class QTaskMysql extends QTaskAbs implements QTask {
private Object mapper;
public QTaskMysql(E e) {
super(e);
}
public String getSQL(){
String sql = getE().para.get("sql")+"";
return sql;
}
@Override
public Object execute() {
System.out.println("QTaskMysql.execute: " + getE().queryId);
/*if ("Map".equalsIgnoreCase(getE().queryType)){
// return mapper.findFirst(getSQL()); todo:
} else if ("list".equalsIgnoreCase(getE().queryType)){
// return mapper.findList(getSQL()); todo:
}*/
return null;
}
}

View File

@@ -0,0 +1,27 @@
import net.tccn.E;
import net.tccn.QRuner;
import net.tccn.base.Kv;
import org.junit.Test;
/**
* @author: liangxianyou at 2019/1/20 12:43.
*/
public class RunTest {
public static E A = new E("mysql", "select * from user where userid=#(userid)", "查询用户列表", Kv.of("userid", 1));
public static E B = new E("method", "User.say", "user调用", Kv.of("name", "张三").set("age", 13));
public static E C = new E("http", "http://127.0.0.1/meta/db_plat_list?platToken=3421432", "查询数据平台列表", Kv.of("abx", "abx111"));
@Test
public void t() {
long start = System.currentTimeMillis();
Object query = QRuner.query(C);
System.out.printf("耗时:%s MS" ,System.currentTimeMillis() - start);
System.out.println();
System.out.println(query);
//System.out.println(query.getClass());
}
}

View File

@@ -0,0 +1,14 @@
import net.tccn.base.Kv;
/**
* @author: liangxianyou at 2019/1/20 12:52.
*/
public class User {
public String say(Kv kv) {
String s = String.format("我叫:%s, 今年:%s岁", kv.get("name"), kv.get("age"));
System.out.println(s);
return s;
}
}