master
WuXianChaoPin 6 years ago
parent 0a94406f82
commit a21dd29e01
  1. 11
      core/src/main/java/core/thrift/ThriftClientPool.java
  2. 3
      web/src/main/java/web/service/BiliService.java
  3. 1
      web/src/main/java/web/util/DynamicTimer.java
  4. 2
      web/src/main/resources/config.properties
  5. 3
      web/src/main/webapp/WEB-INF/jsp/head.jsp
  6. 5
      web/src/main/webapp/WEB-INF/jsp/test.jsp
  7. 4
      web/src/test/java/hibernate/HibernateTest.java
  8. 12
      web/src/test/java/spring/SpringConfig.java
  9. 4
      web/src/test/java/spring/SpringMVCTest.java
  10. 50
      web/src/test/java/spring/UrlTest.java
  11. 28
      web/src/test/java/thrift/ThriftTest.java
  12. 13
      web/src/test/java/web/controller/TestController.java

@ -31,7 +31,9 @@ public class ThriftClientPool {
private static GenericObjectPool<TNonblockingSocket> pool;
private static final Map<Class<? extends TAsyncClient>, Constructor> map = new HashMap<>();
private static final Map<Class<? extends TAsyncClient>, Constructor> asyncClient = new HashMap<>();
private static final Map<Class<? extends TAsyncClient>, Constructor> synchronizeClient = new HashMap<>();
@PostConstruct
public void init() {
@ -47,14 +49,15 @@ public class ThriftClientPool {
}
private static <E extends TAsyncClient> E client(Class<E> c, TNonblockingSocket socket) throws NoSuchMethodException, IOException, IllegalAccessException, InvocationTargetException, InstantiationException {
if (!map.containsKey(c)) {
map.put(c, c.getConstructor(TProtocolFactory.class, TAsyncClientManager.class, TNonblockingTransport.class));
if (!asyncClient.containsKey(c)) {
asyncClient.put(c, c.getConstructor(TProtocolFactory.class, TAsyncClientManager.class, TNonblockingTransport.class));
}
TAsyncClientManager clientManager = new TAsyncClientManager();
TProtocolFactory protocol = new TBinaryProtocol.Factory();
return (E) map.get(c).newInstance(protocol,clientManager,socket);
return (E) asyncClient.get(c).newInstance(protocol,clientManager,socket);
}
public interface Thrift<E extends TAsyncClient> {
void doExecute(E client) throws Exception;
}

@ -2,11 +2,10 @@ package web.service;
import db.annotation.Aliyun;
import db.annotation.Model;
import db.model.AbstractModel;
import org.springframework.stereotype.Service;
@Service
@Model(Aliyun.class)
public class BiliService<T extends AbstractModel> extends BaseService{
public class BiliService extends BaseService{
}

@ -30,6 +30,7 @@ public class DynamicTimer extends ScheduledTaskRegistrar implements ApplicationL
public void addTriggerTask(ScheduledTaskEntity scheduledTaskEntity) {
super.scheduleTriggerTask(new TriggerTask(()->{
},(TriggerContext triggerContext)->{
CronTrigger trigger = new CronTrigger(scheduledTaskEntity.getExpression());
log.debug(triggerContext+"");

@ -9,6 +9,6 @@ hibernate.connection.driver_class=com.mysql.cj.jdbc.Driver
druid.maxActive=20
#thrift\u914D\u7F6E
thrift.ip=sukura.top
thrift.ip=127.0.0.1
thrift.port=2233
thrift.timeout=3000

@ -6,9 +6,6 @@
To change this template use File | Settings | File Templates.
--%>
<%@ page pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="fmt"%> <!--数据格式化标签库-->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="sql"%> <!--数据库相关标签库-->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="fn"%> <!--常用函数标签库-->
<%@ page isELIgnored="false"%> <!--支持EL表达式,不设的话,EL表达式不会解析-->
<meta http-equiv="X-UA-Compatible" content="IE=edge">

@ -8,6 +8,7 @@
<!DOCTYPE html>
<html lang="en">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!--输出,条件,迭代标签库-->
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ page pageEncoding="utf-8" %>
<head>
<c:import url="head.jsp"/>
@ -20,12 +21,14 @@
$(function () {
$("#demo").tmpl([{type: 'primary'}, {type: 'secondary'}, {type: 'success'}, {type: 'danger'}, {type: 'warning'}, {type: 'info'}, {type: 'light'}, {type: 'dark'}]).appendTo('#div_demo');
console.info("${spring:mvcUrl('MC#get#0').build()}");
});
</script>
</head>
<body>
<div id="div_demo">
<div id="div_demo" hidden>
<svg aria-hidden="true" data-prefix="fas" data-icon="spinner" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" class="svg-inline--fa fa-spinner fa-w-16 fa-spin fa-lg"><path fill="currentColor" d="M304 48c0 26.51-21.49 48-48 48s-48-21.49-48-48 21.49-48 48-48 48 21.49 48 48zm-48 368c-26.51 0-48 21.49-48 48s21.49 48 48 48 48-21.49 48-48-21.49-48-48-48zm208-208c-26.51 0-48 21.49-48 48s21.49 48 48 48 48-21.49 48-48-21.49-48-48-48zM96 256c0-26.51-21.49-48-48-48S0 229.49 0 256s21.49 48 48 48 48-21.49 48-48zm12.922 99.078c-26.51 0-48 21.49-48 48s21.49 48 48 48 48-21.49 48-48c0-26.509-21.491-48-48-48zm294.156 0c-26.51 0-48 21.49-48 48s21.49 48 48 48 48-21.49 48-48c0-26.509-21.49-48-48-48zM108.922 60.922c-26.51 0-48 21.49-48 48s21.49 48 48 48 48-21.49 48-48-21.491-48-48-48z" class=""></path></svg>
</div>

@ -1,3 +1,5 @@
package hibernate;
import db.model.TaskModel;
import db.model.bilibili.ScheduledTaskEntity;
import db.util.ExportUtil;
@ -7,8 +9,6 @@ import org.hibernate.criterion.DetachedCriteria;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@ -0,0 +1,12 @@
package spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@ComponentScan({"web.controller","web.service"})
public class SpringConfig implements WebMvcConfigurer {
}

@ -1,3 +1,5 @@
package spring;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Before;
@ -26,7 +28,7 @@ public class SpringMVCTest {
private MockMvc mockMvc;
@Autowired
WebApplicationContext wac;
private WebApplicationContext wac;
@Before
public void setup() {

@ -0,0 +1,50 @@
package spring;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import web.controller.MenuController;
//让测试运行于Spring环境
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitWebConfig
@ContextConfiguration(classes = SpringConfig.class)
public class UrlTest {
private MockMvc mockMvc;
private Logger log = LogManager.getLogger();
@Autowired
private WebApplicationContext wac;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void buildUrl() {
log.info(MvcUriComponentsBuilder.fromController(MenuController.class).build());
}
@Test
public void testJson() throws Exception {
ResultActions resultActions=mockMvc.perform(MockMvcRequestBuilders.get("/test/task"));
resultActions.andDo(MockMvcResultHandlers.print()).andReturn();
}
}

@ -6,31 +6,45 @@ import core.thrift.comment.QueryComment;
import core.thrift.task.TSDM;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.IOException;
import java.util.ArrayList;
//让测试运行于Spring环境
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitWebConfig
@ContextConfiguration(classes = SpringConfig.class)
@ComponentScan("core.thrift")
public class ThriftTest {
private static Logger log = LogManager.getLogger();
@Test
public void commentSum() throws Exception {
int i=100;
while (i-->0) {
MyAsyncMethodCallback<Integer> call = new MyAsyncMethodCallback<>();
ThriftClientPool.doExecute(client -> client.commentSum(49052, call), QueryComment.AsyncClient.class);
log.info(call.getResult());
Thread.sleep(10);
}
}
MyAsyncMethodCallback<Integer> call = new MyAsyncMethodCallback<>();
ThriftClientPool.doExecute(client -> client.commentSum(49052, call), QueryComment.AsyncClient.class);
log.info(call.getResult());
@Test
public void commentSum_() throws IOException, TException {
TTransport transport=new TSocket("sukura.top",2233);
transport.open();
TBinaryProtocol protocol=new TBinaryProtocol(transport);
QueryComment.Client client=new QueryComment.Client(protocol);
log.info(client.commentSum(49052));
}
@Test
@ -45,7 +59,7 @@ public class ThriftTest {
@Test
public void work() throws Exception {
MyAsyncMethodCallback<Boolean> call = new MyAsyncMethodCallback<>();
ThriftClientPool.doExecute(client -> client.qiandao(call), TSDM.AsyncClient.class);
ThriftClientPool.doExecute(client -> client.word(call), TSDM.AsyncClient.class);
log.info(call.getResult());
}

@ -2,15 +2,28 @@ package web.controller;
import db.form.DBAction;
import db.model.TaskModel;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/test")
public class TestController extends BiliController<TaskModel>{
@ResponseBody
@RequestMapping(value = "task",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public TaskModel test(){
TaskModel model=new TaskModel();
model.setId(1);
model.setApi("www.baidu.com");
return model;
}
@Override
public boolean checkAction(DBAction action) {
return true;
}
}

Loading…
Cancel
Save