Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
D
data-sending
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Matrix
data-sending
Commits
69781500
提交
69781500
authored
3月 31, 2023
作者:
Matrix
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
代码优化重构
上级
30f0edb1
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
213 行增加
和
85 行删除
+213
-85
pom.xml
pom.xml
+12
-5
ApplicationController.java
...n/java/com/example/datasending/ApplicationController.java
+127
-0
SendController.java
src/main/java/com/example/datasending/SendController.java
+0
-79
TerminalData.java
src/main/java/com/example/datasending/TerminalData.java
+30
-0
DataCollectorScheduler.java
...xample/datasending/collectors/DataCollectorScheduler.java
+1
-1
ApplicationControllerTest.java
...va/com/example/datasending/ApplicationControllerTest.java
+43
-0
没有找到文件。
pom.xml
浏览文件 @
69781500
...
@@ -29,9 +29,6 @@
...
@@ -29,9 +29,6 @@
<version>
3.5.3.1
</version>
<version>
3.5.3.1
</version>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter
</artifactId>
<artifactId>
spring-boot-starter
</artifactId>
...
@@ -60,13 +57,23 @@
...
@@ -60,13 +57,23 @@
<version>
20210307
</version>
<version>
20210307
</version>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
<scope>
test
</scope>
</dependency>
</dependency>
<dependency>
<groupId>
com.squareup.okhttp
</groupId>
<artifactId>
okhttp
</artifactId>
<version>
2.7.5
</version>
</dependency>
<dependency>
<groupId>
io.github.cweijan
</groupId>
<artifactId>
http-test
</artifactId>
<version>
RELEASE
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
...
...
src/main/java/com/example/datasending/ApplicationController.java
0 → 100644
浏览文件 @
69781500
package
com
.
example
.
datasending
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.squareup.okhttp.*
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.io.IOException
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.function.Supplier
;
@RestController
@SpringBootApplication
@RequestMapping
(
"/simc"
)
public
class
ApplicationController
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
ApplicationController
.
class
,
args
);
}
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
ApplicationController
.
class
);
private
static
final
String
IP
=
"http://localhost:82/schedule/terminal"
;
private
static
final
MediaType
JSON
=
MediaType
.
parse
(
"application/json; charset=utf-8"
);
private
static
final
int
THREAD_POOL_SIZE
=
2
;
private
final
OkHttpClient
httpClient
=
new
OkHttpClient
();
private
ThreadPoolTaskExecutor
taskExecutor
;
private
Alamofire
alamofire
;
private
ObjectMapper
objectMapper
;
private
Map
<
String
,
Boolean
>
tasks
;
public
ApplicationController
()
{
taskExecutor
=
new
ThreadPoolTaskExecutor
();
taskExecutor
.
setCorePoolSize
(
THREAD_POOL_SIZE
);
taskExecutor
.
setMaxPoolSize
(
THREAD_POOL_SIZE
);
taskExecutor
.
setQueueCapacity
(
1
);
taskExecutor
.
initialize
();
objectMapper
=
new
ObjectMapper
();
tasks
=
new
ConcurrentHashMap
<>();
alamofire
=
new
Alamofire
();
}
@GetMapping
(
"/start/{taskName}/{interval}"
)
public
ResponseEntity
<
String
>
startTask
(
@PathVariable
String
taskName
,
@PathVariable
long
interval
)
{
if
(
tasks
.
containsKey
(
taskName
))
{
return
ResponseEntity
.
badRequest
().
body
(
"该任务已在运行。"
);
}
tasks
.
put
(
taskName
,
true
);
Runnable
task
;
switch
(
taskName
)
{
case
"task1"
:
task
=
()
->
sendTerminalDataTask
(
tasks
,
taskName
,
()
->
new
TerminalData
().
initTerminalData
(),
interval
);
break
;
case
"task2"
:
task
=
()
->
sendTerminalDataTask
(
tasks
,
taskName
,
()
->
new
TerminalData
().
initTerminalData1
(),
interval
);
break
;
default
:
tasks
.
remove
(
taskName
);
return
ResponseEntity
.
badRequest
().
body
(
"无效的任务名称。"
);
}
taskExecutor
.
submit
(
task
);
return
ResponseEntity
.
ok
(
taskName
+
"已启动。"
);
}
@GetMapping
(
"/stop/{taskName}"
)
public
ResponseEntity
<
String
>
stopTask
(
@PathVariable
String
taskName
)
{
if
(!
tasks
.
containsKey
(
taskName
))
{
return
ResponseEntity
.
badRequest
().
body
(
"任务不存在。"
);
}
tasks
.
put
(
taskName
,
false
);
return
ResponseEntity
.
ok
(
taskName
+
"已停止。"
);
}
@GetMapping
(
"/tasks"
)
public
ResponseEntity
<
String
>
getRunningTasks
()
{
StringBuilder
tasksStatus
=
new
StringBuilder
();
tasks
.
forEach
((
taskName
,
isRunning
)
->
tasksStatus
.
append
(
taskName
)
.
append
(
":"
)
.
append
(
isRunning
?
"正在运行"
:
"已停止"
)
.
append
(
"\n"
)
);
return
ResponseEntity
.
ok
(
tasksStatus
.
toString
());
}
private
void
sendTerminalDataTask
(
Map
<
String
,
Boolean
>
tasks
,
String
taskName
,
Supplier
<
TerminalData
>
terminalDataSupplier
,
long
interval
)
{
while
(
tasks
.
get
(
taskName
))
{
TerminalData
terminalData
=
terminalDataSupplier
.
get
();
String
json
=
null
;
try
{
json
=
objectMapper
.
writeValueAsString
(
terminalData
);
}
catch
(
JsonProcessingException
e
)
{
e
.
printStackTrace
();
}
try
{
alamofire
.
POST
(
IP
,
json
,
httpClient
);
Thread
.
sleep
(
interval
);
// 添加间隔
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
private
static
class
Alamofire
{
private
String
POST
(
final
String
url
,
final
String
json
,
OkHttpClient
httpClient
)
throws
IOException
{
RequestBody
body
=
RequestBody
.
create
(
JSON
,
json
);
Request
request
=
new
Request
.
Builder
()
.
url
(
url
)
.
post
(
body
)
.
build
();
Response
response
=
httpClient
.
newCall
(
request
).
execute
();
return
response
.
body
().
string
();
}
}
}
src/main/java/com/example/datasending/SendController.java
deleted
100644 → 0
浏览文件 @
30f0edb1
package
com
.
example
.
datasending
;
import
cn.hutool.http.HttpUtil
;
import
cn.hutool.json.JSONUtil
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.client.RestTemplate
;
/**
* SendController.
*
* @author Matrix <xhyrzldf@gmail.com>
* @since 2023/2/21 at 15:02
* Suffering is the most powerful teacher of life.
*/
@Slf4j
@RestController
@RequestMapping
(
"/sending"
)
public
class
SendController
{
private
boolean
flag
=
false
;
private
String
ip
=
"http://192.168.102.68:82/schedule/terminal"
;
// private String ip = "http://127.0.0.1:82/schedule/terminal";
/**
* 开始循环发送数据,每30秒发送一次
*/
@GetMapping
(
"/start"
)
public
ResponseEntity
sendTerminal
()
{
flag
=
true
;
log
.
info
(
"[模拟消息发送] 开始循环发送消息"
);
//使用hutool 向 http://192.168.102.68:82/schedule/terminal 发送Post TerminalData数据
TerminalData
terminalData
=
new
TerminalData
().
initTerminalData
();
// 开一个新的线程 循环发送数据
new
Thread
(()
->
{
while
(
true
)
{
try
{
if
(
flag
)
{
RestTemplate
restTemplate
=
new
RestTemplate
();
//创建请求头
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
MediaType
.
APPLICATION_JSON
);
HttpEntity
<
TerminalData
>
entity
=
new
HttpEntity
<
TerminalData
>(
terminalData
,
headers
);
ResponseEntity
<
String
>
responseEntity
=
restTemplate
.
postForEntity
(
ip
,
entity
,
String
.
class
);
// HttpUtil.post(ip, );
log
.
info
(
"[模拟消息发送] 发送地址 {},模拟数据 {}"
,
ip
,
new
ObjectMapper
().
writeValueAsString
(
terminalData
));
Thread
.
sleep
(
30000
);
}
}
catch
(
InterruptedException
|
JsonProcessingException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}).
start
();
return
ResponseEntity
.
ok
(
"发送成功"
);
}
/**
* 停止循环发送数据
*/
@GetMapping
(
"/stop"
)
public
ResponseEntity
stopSendTerminal
()
{
flag
=
false
;
return
ResponseEntity
.
ok
(
"停止发送"
);
}
}
src/main/java/com/example/datasending/TerminalData.java
浏览文件 @
69781500
...
@@ -28,6 +28,12 @@ public class TerminalData {
...
@@ -28,6 +28,12 @@ public class TerminalData {
return
this
;
return
this
;
}
}
public
TerminalData
initTerminalData1
()
{
this
.
hardwareDetection
=
new
HardwareDetectionDTO
().
initHardware1
();
this
.
taskManager
=
new
TaskManagerDTO
().
initTask
();
return
this
;
}
@NoArgsConstructor
@NoArgsConstructor
@Data
@Data
@AllArgsConstructor
@AllArgsConstructor
...
@@ -62,6 +68,19 @@ public class TerminalData {
...
@@ -62,6 +68,19 @@ public class TerminalData {
return
this
;
return
this
;
}
}
public
HardwareDetectionDTO
initHardware1
()
{
HardwareDetectionDTO
mockData
=
new
HardwareDetectionDTO
();
mockData
.
setComputerModel
(
"MacBook Pro"
);
mockData
.
setOperatingSystem
(
"macOS Big Sur"
);
mockData
.
setComputerManufacture
(
"Apple"
);
mockData
.
setCpuModel
(
"Intel Core i7"
);
mockData
.
setComputerBaseBoardModel
(
"Mac-9AE82516C7C6B903"
);
mockData
.
setBiosModel
(
"MBA81.88Z.0167.B00.2008272126"
);
mockData
.
setUuid
(
"6ED07C24-893D-4423-AE11-45670DE54321"
);
mockData
.
setAgent
(
new
AgentDTO
().
initAgent1
());
return
mockData
;
}
@NoArgsConstructor
@NoArgsConstructor
@Data
@Data
...
@@ -91,6 +110,17 @@ public class TerminalData {
...
@@ -91,6 +110,17 @@ public class TerminalData {
return
this
;
return
this
;
}
}
public
AgentDTO
initAgent1
()
{
AgentDTO
mockData
=
new
AgentDTO
();
mockData
.
setId
(
RandomUtil
.
randomString
(
10
));
mockData
.
setType
(
"server"
);
mockData
.
setStartTime
(
"2022-01-01 00:00:00"
);
mockData
.
setVersion
(
"2.0.0"
);
mockData
.
setMacAddress
(
"1A:2B:3C:4D:5E:6F"
);
mockData
.
setIpv4Address
(
"192.168.100.100"
);
return
mockData
;
}
}
}
}
}
...
...
src/main/java/com/example/datasending/collectors/DataCollectorScheduler.java
浏览文件 @
69781500
...
@@ -18,7 +18,7 @@ public class DataCollectorScheduler {
...
@@ -18,7 +18,7 @@ public class DataCollectorScheduler {
@Resource
@Resource
private
DataCollector
dataCollector
;
private
DataCollector
dataCollector
;
@Scheduled
(
fixedRate
=
5000
)
//
@Scheduled(fixedRate = 5000)
public
void
collectData
()
{
public
void
collectData
()
{
dataCollector
.
collectData
();
dataCollector
.
collectData
();
}
}
...
...
src/test/java/com/example/datasending/ApplicationControllerTest.java
0 → 100644
浏览文件 @
69781500
package
com
.
example
.
datasending
;
import
static
io
.
github
.
cweijan
.
mock
.
Mocker
.*;
import
io.github.cweijan.mock.Asserter
;
import
io.github.cweijan.mock.jupiter.HttpTest
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.condition.EnabledOnOs
;
import
org.junit.jupiter.api.condition.OS
;
import
org.springframework.http.ResponseEntity
;
import
javax.annotation.Resource
;
@HttpTest
class
ApplicationControllerTest
{
@Resource
private
ApplicationController
applicationController
;
@Test
@EnabledOnOs
({
OS
.
WINDOWS
,
OS
.
MAC
})
void
startTask
()
{
String
string
=
mock
(
String
.
class
);
ResponseEntity
<
String
>
startTask
=
applicationController
.
startTask
(
string
);
Asserter
.
assertNotNull
(
startTask
);
}
@Test
@EnabledOnOs
({
OS
.
WINDOWS
,
OS
.
MAC
})
void
stopTask
()
{
String
string
=
mock
(
String
.
class
);
ResponseEntity
<
String
>
stopTask
=
applicationController
.
stopTask
(
string
);
Asserter
.
assertNotNull
(
stopTask
);
}
@Test
@EnabledOnOs
({
OS
.
WINDOWS
,
OS
.
MAC
})
void
getRunningTasks
()
{
ResponseEntity
<
String
>
getRunningTasks
=
applicationController
.
getRunningTasks
();
Asserter
.
assertNotNull
(
getRunningTasks
);
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论