Browse Source

堆外内存

pull/2/head
马府强 2 years ago
parent
commit
62722e8595
  1. 54
      ohcdemo/ohcdemo/.gitignore
  2. 86
      ohcdemo/ohcdemo/README.md
  3. 96
      ohcdemo/ohcdemo/pom.xml
  4. 30
      ohcdemo/ohcdemo/src/main/java/com/xxl/job/plus/executor/core/HashMapCacheExample.java
  5. 83
      ohcdemo/ohcdemo/src/main/java/com/xxl/job/plus/executor/core/OhcDemo.java
  6. 2
      ohcdemo/ohcdemo/src/main/resources/META-INF/spring.factories

54
ohcdemo/ohcdemo/.gitignore

@ -0,0 +1,54 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
# Log file
*.log
/logs*
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar
*.cmd
/data

86
ohcdemo/ohcdemo/README.md

@ -0,0 +1,86 @@
## xxljob-autoregister-spring-boot-starter
**********************************
自动注册xxl-job执行器以及任务
## 1、打包
```
mvn clean install
```
## 2、项目中引入
```xml
<dependency>
<groupId>com.cn.woniu</groupId>
<artifactId>xxljob-autoregister-spring-boot-starter</artifactId>
<version>0.0.1</version>
</dependency>
```
## 3、配置
springboot项目配置文件application.properties:
```properties
server.port=8082
# 原生xxl-job配置
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
xxl.job.accessToken=default_token
xxl.job.executor.appname=xxl-job-executor-test
xxl.job.executor.address=
xxl.job.executor.ip=127.0.0.1
xxl.job.executor.port=9999
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
xxl.job.executor.logretentiondays=30
# 新增配置项,必须项
# admin用户名
xxl.job.admin.username=admin
# admin 密码
xxl.job.admin.password=123456
# 执行器名称
xxl.job.executor.title=Exe-Titl
# 新增配置项,可选项
# 执行器地址类型:0=自动注册、1=手动录入,默认为0
xxl.job.executor.addressType=1
# 在上面为1的情况下,手动录入执行器地址列表,多地址逗号分隔
xxl.job.executor.addressList=http://127.0.0.1:9999
```
`XxlJobSpringExecutor`参数配置与之前相同
## 4、添加注解
需要自动注册的方法添加注解`@XxlRegister`,不加则不会自动注册
```java
@Service
public class TestService {
@XxlJob(value = "testJob")
@XxlRegister(cron = "0 0 0 * * ? *",
author = "woniu",
jobDesc = "测试job")
public void testJob(){
System.out.println("#公众号:程序员蜗牛g");
}
@XxlJob(value = "testJob222")
@XxlRegister(cron = "59 1-2 0 * * ?",
triggerStatus = 1)
public void testJob2(){
System.out.println("#作者:woniu");
}
@XxlJob(value = "testJob444")
@XxlRegister(cron = "59 59 23 * * ?")
public void testJob4(){
System.out.println("hello xxl job");
}
}
```

96
ohcdemo/ohcdemo/pom.xml

@ -0,0 +1,96 @@
<?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>com.cn.woniu</groupId>
<artifactId>xxljob-autoregister-spring-boot-starter</artifactId>
<version>0.0.1</version>
<properties>
<java.version>8</java.version>
<xxl-job.version>2.3.0</xxl-job.version>
<spring-boot.version>2.6.7</spring-boot.version>
<hutool.version>5.4.2</hutool.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot (依赖管理:继承一些默认的依赖,工程需要依赖的jar包的管理,申明其他dependency的时候就不需要version) -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.caffinitas.ohc</groupId>
<artifactId>ohc-core</artifactId>
<version>0.7.4</version>
</dependency>
<!-- xxl-job-core -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>${xxl-job.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 要将源码放上去,需要加入这个插件 -->
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

30
ohcdemo/ohcdemo/src/main/java/com/xxl/job/plus/executor/core/HashMapCacheExample.java

@ -0,0 +1,30 @@
package com.xxl.job.plus.executor.core;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
/**
* -Xms100m -Xmx100m
*/
public class HashMapCacheExample {
private static HashMap<String, String> HASHMAP = new HashMap<>();
public static void main(String[] args) throws InterruptedException {
hashMapOOM();
}
private static void hashMapOOM() throws InterruptedException {
//准备时间,方便观察
TimeUnit.SECONDS.sleep(10);
int num = 0;
while (true) {
//往 map 中存放 1M 大小的字符串
String big = new String(new byte[1024 * 1024]);
HASHMAP.put(num + "", big);
num++;
}
}
}

83
ohcdemo/ohcdemo/src/main/java/com/xxl/job/plus/executor/core/OhcDemo.java

@ -0,0 +1,83 @@
package com.xxl.job.plus.executor.core;
import com.google.common.base.Charsets;
import org.caffinitas.ohc.CacheSerializer;
import org.caffinitas.ohc.OHCache;
import org.caffinitas.ohc.OHCacheBuilder;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
/**
* 基于堆外内存的缓存框架 OHC
* 物理内存=堆外内存+堆内内存
* 堆外内存不受堆内内存大小的限制只受服务器物理内存的大小限制
*/
public class OhcDemo {
public static void main(String[] args) throws InterruptedException {
//准备时间,方便观察
TimeUnit.SECONDS.sleep(10);
OHCache ohCache = OHCacheBuilder.<String, String>newBuilder()
.keySerializer(OhcDemo.stringSerializer)
.valueSerializer(OhcDemo.stringSerializer)
.capacity(1024L * 1024 * 1024 * 2)
.build();
int num = 0;
while (true) {
String big = new String(new byte[1024 * 1024]);
ohCache.put(num + "", big);
num++;
}
}
public static final CacheSerializer<String> stringSerializer = new CacheSerializer<String>() {
public void serialize(String s, ByteBuffer buf) {
byte[] bytes = s.getBytes(Charsets.UTF_8);
buf.put((byte) ((bytes.length >>> 8) & 0xFF));
buf.put((byte) ((bytes.length >>> 0) & 0xFF));
buf.put(bytes);
}
public String deserialize(ByteBuffer buf) {
int length = (((buf.get() & 0xff) << 8) + ((buf.get() & 0xff) << 0));
byte[] bytes = new byte[length];
buf.get(bytes);
return new String(bytes, Charsets.UTF_8);
}
public int serializedSize(String s) {
byte[] bytes = s.getBytes(Charsets.UTF_8);
// 设置字符串长度限制,2^16 = 65536
// if (bytes.length > 65535){
// throw new RuntimeException("encoded string too long: " + bytes.length + " bytes");
// }
return bytes.length + 2;
}
};
static int writeUTFLen(String str) {
int strlen = str.length();
int utflen = 0;
int c;
for (int i = 0; i < strlen; i++) {
c = str.charAt(i);
if ((c >= 0x0001) && (c <= 0x007F))
utflen++;
else if (c > 0x07FF)
utflen += 3;
else
utflen += 2;
}
if (utflen > 65535)
throw new RuntimeException("encoded string too long: " + utflen + " bytes");
return utflen + 2;
}
}

2
ohcdemo/ohcdemo/src/main/resources/META-INF/spring.factories

@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xxl.job.plus.executor.config.XxlJobPlusConfig
Loading…
Cancel
Save