feat(新增): 🚀 执行普通任务,执行定时任务

This commit is contained in:
bunny 2024-05-23 13:13:22 +08:00
parent be5b656a70
commit d0fecee98d
12 changed files with 136 additions and 32 deletions

View File

@ -13,6 +13,7 @@
<module name="service" /> <module name="service" />
<module name="service-utils" /> <module name="service-utils" />
<module name="module-rabbitMQ" /> <module name="module-rabbitMQ" />
<module name="spring-security" />
<module name="common-utils" /> <module name="common-utils" />
<module name="model" /> <module name="model" />
<module name="module-mail" /> <module name="module-mail" />
@ -33,6 +34,7 @@
<module name="module-websocket" options="-parameters" /> <module name="module-websocket" options="-parameters" />
<module name="service" options="-parameters" /> <module name="service" options="-parameters" />
<module name="service-utils" options="-parameters" /> <module name="service-utils" options="-parameters" />
<module name="spring-security" options="-parameters" />
</option> </option>
</component> </component>
</project> </project>

View File

@ -15,6 +15,7 @@
<file url="file://$PROJECT_DIR$/netty/module/module-rabbitMQ/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/netty/module/module-rabbitMQ/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/netty/module/module-task/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/netty/module/module-task/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/netty/module/module-websocket/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/netty/module/module-websocket/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/netty/module/spring-security/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/netty/module/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/netty/module/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/netty/module/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/netty/module/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/netty/service/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/netty/service/src/main/java" charset="UTF-8" />

View File

@ -6,6 +6,7 @@
<list> <list>
<option value="$PROJECT_DIR$/demo1/pom.xml" /> <option value="$PROJECT_DIR$/demo1/pom.xml" />
<option value="$PROJECT_DIR$/netty/pom.xml" /> <option value="$PROJECT_DIR$/netty/pom.xml" />
<option value="$PROJECT_DIR$/netty/module/spring-security/pom.xml" />
</list> </list>
</option> </option>
</component> </component>

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>cn.bunny</groupId> <groupId>cn.bunny</groupId>
<artifactId>bunny-template</artifactId> <artifactId>netty</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
</parent> </parent>

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>cn.bunny</groupId> <groupId>cn.bunny</groupId>
<artifactId>bunny-template</artifactId> <artifactId>netty</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
</parent> </parent>

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>cn.bunny</groupId> <groupId>cn.bunny</groupId>
<artifactId>bunny-template</artifactId> <artifactId>netty</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
</parent> </parent>

View File

@ -9,7 +9,7 @@
<relativePath/> <!-- lookup parent from repository --> <relativePath/> <!-- lookup parent from repository -->
</parent> </parent>
<groupId>cn.bunny</groupId> <groupId>cn.bunny</groupId>
<artifactId>bunny-template</artifactId> <artifactId>netty</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<name>bunny-template</name> <name>bunny-template</name>

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>cn.bunny</groupId> <groupId>cn.bunny</groupId>
<artifactId>bunny-template</artifactId> <artifactId>netty</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
</parent> </parent>

View File

@ -1,27 +0,0 @@
package cn.bunny;
import cn.bunny.security.custom.CustomPasswordEncoder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.password.PasswordEncoder;
@SpringBootTest(classes = CustomPasswordEncoder.class)
class CustomPasswordEncoderTest {
@Autowired
private CustomPasswordEncoder customPasswordEncoder;
@Autowired
private PasswordEncoder passwordEncoder;
@Test
void testCustomPasswordEncoder() {
String encode = customPasswordEncoder.encode("111111");
System.out.println(encode);
}
@Test
void testPasswordEncoder() {
String encode = passwordEncoder.encode("111111");
System.out.println(encode);
}
}

View File

@ -0,0 +1,47 @@
package cn.bunny.demo1;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import java.util.Date;
public class TestDemoClient {
public static void main(String[] args) throws InterruptedException {
// 启动类
new Bootstrap()
// 添加EventLoop
.group(new NioEventLoopGroup())
// 选择客户端 Channelt
.handler(new ChannelInitializer<NioSocketChannel>() {
@Override// 在连接建立后调用
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
nioSocketChannel.pipeline().addLast(new StringEncoder());
}
})
.connect("localhost", 8080)
.sync()
.channel()
// 向服务器发送数据
.writeAndFlush("你好啊啊啊");
}
private static void m1() throws InterruptedException {
new Bootstrap()
.group(new NioEventLoopGroup()) // 1
.channel(NioSocketChannel.class) // 2
.handler(new ChannelInitializer<NioSocketChannel>() { // 3
@Override
protected void initChannel(NioSocketChannel ch) {
ch.pipeline().addLast(new StringEncoder()); // 8
}
})
.connect("127.0.0.1", 8080) // 4
.sync() // 5
.channel() // 6
.writeAndFlush(new Date() + ": hello world!"); // 7
}
}

View File

@ -0,0 +1,36 @@
package cn.bunny.demo1;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
public class TestDemoServer {
public static void main(String[] args) {
// 服务器端启动器负责组装 netty 组件启动服务器
new ServerBootstrap()
// 2. BossEventLoop,WorkEventLoop group组
.group(new NioEventLoopGroup())
// 3. 选择服务器ServerSocketChannel 实现
.channel(NioServerSocketChannel.class)
// 4. boss 负责处理连接决定work可以做哪些事情
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
// 将By特Buffer转为字符串
nioSocketChannel.pipeline().addLast(new StringDecoder());
nioSocketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() {// 自定义handler
@Override// 读事件
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// 消息字符串
System.out.println(msg);
}
});
}
}).bind(8080);
}
}

View File

@ -0,0 +1,44 @@
package cn.bunny.demo2;
import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.NettyRuntime;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.TimeUnit;
@Slf4j
public class TestGroupLoop {
public static void main(String[] args) {
// io 事件普通任务定时任务
NioEventLoopGroup group = new NioEventLoopGroup();
// 获取下一个事件循环对象
System.out.println(group.next());
System.out.println(group.next());
System.out.println(group.next());
System.out.println(group.next());
// 执行普通任务
group.next().submit(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("执行完成");
});
// 执行定时任务
group.next().scheduleAtFixedRate(() -> {
log.debug("定时任务的执行");
}, 0, 1, TimeUnit.SECONDS);
}
private static void test1() {
// io 事件普通任务定时任务
NioEventLoopGroup group = new NioEventLoopGroup();
// 普通任务定时任务
DefaultEventLoopGroup eventLoopGroup = new DefaultEventLoopGroup();
// 查看系统是几核
System.out.println(NettyRuntime.availableProcessors());
}
}