测试xxjob
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
<module>file-read</module>
|
||||
<module>kafka</module>
|
||||
<module>spring-boot</module>
|
||||
<module>redis</module>
|
||||
</modules>
|
||||
<packaging>pom</packaging>
|
||||
<properties>
|
||||
@@ -27,13 +28,24 @@
|
||||
<artifactId>cglib</artifactId>
|
||||
<version>3.3.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson</artifactId>
|
||||
<version>3.13.4</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
|
||||
<dependency>
|
||||
<groupId>org.apache.kafka</groupId>
|
||||
<artifactId>kafka-clients</artifactId>
|
||||
<version>2.8.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.xuxueli</groupId>
|
||||
<artifactId>xxl-job-core</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@@ -47,6 +59,12 @@
|
||||
<version>5.8.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>5.8.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-core</artifactId>
|
||||
@@ -57,6 +75,5 @@
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.2.3</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,25 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>JavaBasiceDemo</artifactId>
|
||||
<groupId>com.wyl.example</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>redis</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.wyl.demo.redission;
|
||||
|
||||
import com.wyl.demo.redission.conf.RedissonConf;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
|
||||
public class RedisLock extends Thread {
|
||||
|
||||
static RedissonClient redissonClient = RedissonConf.getRedissonClient();
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
lock();
|
||||
}
|
||||
|
||||
public void lock() {
|
||||
RLock lock = redissonClient.getLock("wyl_lock");
|
||||
lock.lock();
|
||||
try {
|
||||
System.out.println("获取到锁了");
|
||||
Thread.sleep(5000000);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
finally {
|
||||
System.out.println("解锁了");
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.wyl.demo.redission;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class RedisObject implements Serializable {
|
||||
String name;
|
||||
LocalDateTime createTime;
|
||||
Long time;
|
||||
List<String> tmp = Arrays.asList("123", "234", "345");
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.wyl.demo.redission.conf;
|
||||
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.config.Config;
|
||||
|
||||
/**
|
||||
*
|
||||
* @ClassName: RedissonConf
|
||||
* @Date: 2022/3/3 22:37
|
||||
* @author wangyl
|
||||
* @version V1.0
|
||||
*/
|
||||
public class RedissonConf {
|
||||
|
||||
private volatile static RedissonClient redissonClient;
|
||||
|
||||
public static RedissonClient getRedissonClient() {
|
||||
if (redissonClient == null) {
|
||||
synchronized (RedissonConf.class) {
|
||||
if (redissonClient == null) {
|
||||
Config config = new Config();
|
||||
config.useSingleServer()
|
||||
.setAddress("redis://192.168.123.102:6379")
|
||||
.setDatabase(0);
|
||||
return Redisson.create(config);
|
||||
}
|
||||
}
|
||||
}
|
||||
return redissonClient;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.wyl.demo.redission;
|
||||
|
||||
import com.wyl.demo.redission.conf.RedissonConf;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.redisson.api.RAtomicDouble;
|
||||
import org.redisson.api.RBloomFilter;
|
||||
import org.redisson.api.RBucket;
|
||||
import org.redisson.api.RedissonClient;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class RedissonTest {
|
||||
@Test
|
||||
public void RedisStringTest() {
|
||||
RedissonClient redissonClient = RedissonConf.getRedissonClient();
|
||||
RAtomicDouble wyl = redissonClient.getAtomicDouble("wyl");
|
||||
double v = wyl.get();
|
||||
System.out.println(v);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void RedisBloomFilterTest() {
|
||||
RedissonClient redissonClient = RedissonConf.getRedissonClient();
|
||||
RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter("bloomFilter");
|
||||
bloomFilter.tryInit(100, 0);
|
||||
bloomFilter.add("123");
|
||||
bloomFilter.add("124");
|
||||
boolean contains = bloomFilter.contains("456");
|
||||
long count = bloomFilter.count();
|
||||
boolean contains1 = bloomFilter.contains("123");
|
||||
System.out.println(count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void RedisObjectTest() {
|
||||
RedissonClient redissonClient = RedissonConf.getRedissonClient();
|
||||
RedisObject redisObject = new RedisObject();
|
||||
redisObject.setName("1230");
|
||||
redisObject.setCreateTime(LocalDateTime.now());
|
||||
redisObject.setTime(123L);
|
||||
RBucket<RedisObject> testObject = redissonClient.getBucket("testObject");
|
||||
if (!testObject.isExists()) {
|
||||
testObject.set(redisObject, 10, TimeUnit.SECONDS);
|
||||
}
|
||||
RedisObject redisObject1 = testObject.get();
|
||||
System.out.println(redisObject1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void RedisLockTest() throws InterruptedException {
|
||||
for (int i = 0; i < 100; i++){
|
||||
new RedisLock().start();
|
||||
}
|
||||
Thread.sleep(1000000);
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -10,11 +10,12 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<modules>
|
||||
<module>spring-boot-drools</module>
|
||||
<module>spring-boot-xxjob</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>spring-boot</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<name>A Camel Spring Route</name>
|
||||
<name>spring-boot</name>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.wyl.example</groupId>
|
||||
<artifactId>spring-boot</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.wyl</groupId>
|
||||
<artifactId>spring-boot-xxjob</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-boot-xxjob</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/com.xuxueli/xxl-job-core -->
|
||||
<dependency>
|
||||
<groupId>com.xuxueli</groupId>
|
||||
<artifactId>xxl-job-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.78</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.wyl.springbootxxjob;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootXxjobApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootXxjobApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.wyl.springbootxxjob.config;
|
||||
|
||||
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@Getter
|
||||
public class JobServerConfig {
|
||||
|
||||
@Value("${job.server.admin.addresses}")
|
||||
private String adminAddresses;
|
||||
|
||||
@Value("${job.server.accessToken}")
|
||||
private String accessToken;
|
||||
|
||||
@Value("${job.server.executor.appname}")
|
||||
private String appname;
|
||||
|
||||
@Value("${job.server.executor.address}")
|
||||
private String address;
|
||||
|
||||
@Value("${job.server.userName}")
|
||||
private String userName;
|
||||
|
||||
@Value("${job.server.password}")
|
||||
private String password;
|
||||
|
||||
@Value("${job.server.jobGroup}")
|
||||
private String jobGroup;
|
||||
|
||||
@Bean
|
||||
public XxlJobSpringExecutor xxlJobExecutor() {
|
||||
log.info(">>>>>>>>>>> job-server config init.");
|
||||
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
|
||||
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
|
||||
xxlJobSpringExecutor.setAppname(appname);
|
||||
xxlJobSpringExecutor.setAddress(address);
|
||||
xxlJobSpringExecutor.setAccessToken(accessToken);
|
||||
return xxlJobSpringExecutor;
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.wyl.springbootxxjob.config;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Configuration
|
||||
public class RestTemplateConfig {
|
||||
|
||||
@ConditionalOnMissingBean(RestTemplate.class)
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());
|
||||
return restTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用OkHttpClient作为底层客户端
|
||||
* @return
|
||||
*/
|
||||
private ClientHttpRequestFactory getClientHttpRequestFactory() {
|
||||
OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(5, TimeUnit.SECONDS)
|
||||
.writeTimeout(5, TimeUnit.SECONDS)
|
||||
.readTimeout(5, TimeUnit.SECONDS)
|
||||
.build();
|
||||
return new OkHttp3ClientHttpRequestFactory(okHttpClient);
|
||||
}
|
||||
}
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
package com.wyl.springbootxxjob.service;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.wyl.springbootxxjob.config.JobServerConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Service
|
||||
public class DynamicXxlJobService {
|
||||
|
||||
@Resource
|
||||
private JobServerConfig jobServerConfig;
|
||||
|
||||
@Autowired
|
||||
RestTemplate restTemplate;
|
||||
|
||||
/**
|
||||
* 创建固定任务
|
||||
*
|
||||
* @param desc 任务描述
|
||||
* @param corn cron 表达式
|
||||
* @param param param
|
||||
* @return jobId
|
||||
*/
|
||||
private int createJob(String cookie, String desc, String corn, String param) {
|
||||
|
||||
int jobId = -1;
|
||||
MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
|
||||
paramMap.add("jobGroup", jobServerConfig.getJobGroup());
|
||||
paramMap.add("jobDesc", desc);
|
||||
paramMap.add("author", "system_patrol");
|
||||
paramMap.add("scheduleType", "CRON");
|
||||
paramMap.add("scheduleConf", corn);
|
||||
paramMap.add("cronGen_display", corn);
|
||||
paramMap.add("glueType", "BEAN");
|
||||
paramMap.add("executorHandler", "patrolGenerateHandler");
|
||||
paramMap.add("executorParam", param);
|
||||
paramMap.add("executorRouteStrategy", "FIRST");
|
||||
paramMap.add("misfireStrategy", "DO_NOTHING");
|
||||
paramMap.add("executorBlockStrategy", "SERIAL_EXECUTION");
|
||||
paramMap.add("executorTimeout", "0");
|
||||
paramMap.add("executorFailRetryCount", "0");
|
||||
paramMap.add("glueRemark", "GLUE代码初始化");
|
||||
paramMap.add("cookie", cookie);
|
||||
String s = post("", paramMap);
|
||||
JSONObject jsonObject = JSON.parseObject(s);
|
||||
int code = jsonObject.getIntValue("code");
|
||||
if (code == 200) {
|
||||
jobId = jsonObject.getIntValue("content");
|
||||
}
|
||||
|
||||
return jobId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 启动固定任务
|
||||
* @param cookie cookie
|
||||
* @param jobId jobId
|
||||
* @return 启动固定任务
|
||||
*/
|
||||
private boolean start(String cookie, int jobId) {
|
||||
MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
|
||||
paramMap.add("id", String.valueOf(jobId));
|
||||
paramMap.add("cookie", "cookie");
|
||||
String s = post("", paramMap);
|
||||
JSONObject jsonObject = JSON.parseObject(s);
|
||||
int code = jsonObject.getIntValue("code");
|
||||
if (code == 200) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除固定任务
|
||||
* @param cookie cookie
|
||||
* @param jobId jobId
|
||||
* @return 启动固定任务
|
||||
*/
|
||||
public boolean remove(String cookie, int jobId) {
|
||||
MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
|
||||
paramMap.add("id", String.valueOf(jobId));
|
||||
String s = post("", paramMap);
|
||||
JSONObject jsonObject = JSON.parseObject(s);
|
||||
int code = jsonObject.getInteger("code");
|
||||
if (code == 200) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取cookie
|
||||
* @return 返回cookie值
|
||||
*/
|
||||
public String getCookie() {
|
||||
String path = jobServerConfig.getAdminAddresses() + "/login";
|
||||
|
||||
// 请求头设置,x-www-form-urlencoded格式的数据
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||
|
||||
//提交参数设置
|
||||
MultiValueMap<String, String> hashMap = new LinkedMultiValueMap<>();
|
||||
// 组装请求体
|
||||
hashMap.add("userName", jobServerConfig.getUserName());
|
||||
hashMap.add("password", jobServerConfig.getPassword());
|
||||
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(hashMap, headers);
|
||||
ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(path, request, String.class);
|
||||
return stringResponseEntity.toString();
|
||||
//
|
||||
// List<HttpCookie> cookies = response.getCookies();
|
||||
// StringBuilder sb = new StringBuilder();
|
||||
// for (HttpCookie cookie : cookies) {
|
||||
// sb.append(cookie.toString());
|
||||
// }
|
||||
// return sb.toString();
|
||||
}
|
||||
|
||||
public String post(String url, MultiValueMap<String, String> hashMap) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(hashMap, headers);
|
||||
ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, request, String.class);
|
||||
return stringResponseEntity.getBody();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
job.server.admin.addresses=http://192.168.123.102:9090/xxl-job-admin
|
||||
job.server.accessToken=1
|
||||
job.server.executor.appname= asd
|
||||
job.server.executor.address= 192.168.123.148
|
||||
job.server.userName=admin
|
||||
job.server.password=123456
|
||||
job.server.jobGroup=1
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.wyl.springbootxxjob;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseTest.Application.class)
|
||||
public class BaseTest {
|
||||
@ComponentScan(value = "com.wyl.springbootxxjob")
|
||||
public static class Application {
|
||||
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.wyl.springbootxxjob;
|
||||
|
||||
import com.wyl.springbootxxjob.service.DynamicXxlJobService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
class SpringBootXxjobApplicationTests extends BaseTest{
|
||||
|
||||
@Autowired
|
||||
DynamicXxlJobService dynamicXxlJobService;
|
||||
@Test
|
||||
void contextLoads() {
|
||||
dynamicXxlJobService.getCookie();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user