From 62722e859590685699118f1b8ba55fdafde84e43 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=A9=AC=E5=BA=9C=E5=BC=BA?=
<8569561+fuqiangma@user.noreply.gitee.com>
Date: Thu, 11 May 2023 00:17:08 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A0=86=E5=A4=96=E5=86=85=E5=AD=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
ohcdemo/ohcdemo/.gitignore | 54 +++++++++++
ohcdemo/ohcdemo/README.md | 86 +++++++++++++++++
ohcdemo/ohcdemo/pom.xml | 96 +++++++++++++++++++
.../executor/core/HashMapCacheExample.java | 30 ++++++
.../xxl/job/plus/executor/core/OhcDemo.java | 83 ++++++++++++++++
.../main/resources/META-INF/spring.factories | 2 +
6 files changed, 351 insertions(+)
create mode 100644 ohcdemo/ohcdemo/.gitignore
create mode 100644 ohcdemo/ohcdemo/README.md
create mode 100644 ohcdemo/ohcdemo/pom.xml
create mode 100644 ohcdemo/ohcdemo/src/main/java/com/xxl/job/plus/executor/core/HashMapCacheExample.java
create mode 100644 ohcdemo/ohcdemo/src/main/java/com/xxl/job/plus/executor/core/OhcDemo.java
create mode 100644 ohcdemo/ohcdemo/src/main/resources/META-INF/spring.factories
diff --git a/ohcdemo/ohcdemo/.gitignore b/ohcdemo/ohcdemo/.gitignore
new file mode 100644
index 0000000..8b761f3
--- /dev/null
+++ b/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
\ No newline at end of file
diff --git a/ohcdemo/ohcdemo/README.md b/ohcdemo/ohcdemo/README.md
new file mode 100644
index 0000000..bc17e90
--- /dev/null
+++ b/ohcdemo/ohcdemo/README.md
@@ -0,0 +1,86 @@
+## xxljob-autoregister-spring-boot-starter
+
+**********************************
+
+自动注册xxl-job执行器以及任务
+
+## 1、打包
+
+```
+mvn clean install
+```
+
+## 2、项目中引入
+
+```xml
+
+ com.cn.woniu
+ xxljob-autoregister-spring-boot-starter
+ 0.0.1
+
+```
+
+## 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");
+ }
+}
+```
diff --git a/ohcdemo/ohcdemo/pom.xml b/ohcdemo/ohcdemo/pom.xml
new file mode 100644
index 0000000..3080386
--- /dev/null
+++ b/ohcdemo/ohcdemo/pom.xml
@@ -0,0 +1,96 @@
+
+
+ 4.0.0
+
+ com.cn.woniu
+ xxljob-autoregister-spring-boot-starter
+ 0.0.1
+
+
+ 8
+ 2.3.0
+ 2.6.7
+ 5.4.2
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ ${spring-boot.version}
+ pom
+ import
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+ org.springframework.boot
+ spring-boot-autoconfigure
+
+
+
+ org.caffinitas.ohc
+ ohc-core
+ 0.7.4
+
+
+
+
+
+
+ com.xuxueli
+ xxl-job-core
+ ${xxl-job.version}
+
+
+
+ cn.hutool
+ hutool-all
+ ${hutool.version}
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+
+ ${java.version}
+ UTF-8
+
+
+
+
+
+ maven-source-plugin
+ 3.0.1
+
+ true
+
+
+
+ compile
+
+ jar
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/ohcdemo/ohcdemo/src/main/java/com/xxl/job/plus/executor/core/HashMapCacheExample.java b/ohcdemo/ohcdemo/src/main/java/com/xxl/job/plus/executor/core/HashMapCacheExample.java
new file mode 100644
index 0000000..59584ce
--- /dev/null
+++ b/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 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++;
+ }
+
+ }
+
+
+}
diff --git a/ohcdemo/ohcdemo/src/main/java/com/xxl/job/plus/executor/core/OhcDemo.java b/ohcdemo/ohcdemo/src/main/java/com/xxl/job/plus/executor/core/OhcDemo.java
new file mode 100644
index 0000000..4780dab
--- /dev/null
+++ b/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.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 stringSerializer = new CacheSerializer() {
+ 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;
+ }
+
+}
\ No newline at end of file
diff --git a/ohcdemo/ohcdemo/src/main/resources/META-INF/spring.factories b/ohcdemo/ohcdemo/src/main/resources/META-INF/spring.factories
new file mode 100644
index 0000000..c712ad3
--- /dev/null
+++ b/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
\ No newline at end of file