【代码优化】IoT: 插件管理

This commit is contained in:
安浩浩
2024-12-29 22:31:58 +08:00
parent c9904f0994
commit 0e20ca342f
10 changed files with 258 additions and 127 deletions

View File

@@ -0,0 +1,91 @@
/*
* Copyright (C) 2012-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.iocoder.yudao.module.iot.plugin;
import cn.iocoder.yudao.module.iot.api.Greeting;
import org.apache.commons.lang.StringUtils;
import org.pf4j.Extension;
import org.pf4j.Plugin;
import org.pf4j.PluginWrapper;
import org.pf4j.RuntimeMode;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
/**
* 打招呼 测试用例
*/
public class WelcomePlugin extends Plugin {
private HttpServer server;
public WelcomePlugin(PluginWrapper wrapper) {
super(wrapper);
}
@Override
public void start() {
System.out.println("WelcomePlugin.start()");
// for testing the development mode
if (RuntimeMode.DEVELOPMENT.equals(wrapper.getRuntimeMode())) {
System.out.println(StringUtils.upperCase("WelcomePlugin"));
}
startHttpServer();
}
@Override
public void stop() {
System.out.println("WelcomePlugin.stop()");
stopHttpServer();
}
private void startHttpServer() {
try {
server = HttpServer.create(new InetSocketAddress(9081), 0);
server.createContext("/", exchange -> {
String response = "Welcome to PF4J HTTP Server";
exchange.sendResponseHeaders(200, response.getBytes().length);
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
});
server.setExecutor(null);
server.start();
System.out.println("HTTP server started on port 9081");
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopHttpServer() {
if (server != null) {
server.stop(0);
System.out.println("HTTP server stopped");
}
}
@Extension
public static class WelcomeGreeting implements Greeting {
@Override
public String getGreeting() {
return "Welcome to PF4J";
}
}
}

View File

@@ -24,6 +24,7 @@
<plugin.class>cn.iocoder.yudao.module.iot.plugin.HttpPlugin</plugin.class>
<plugin.version>0.0.1</plugin.version>
<plugin.provider>ahh</plugin.provider>
<plugin.description>http-plugin-0.0.1</plugin.description>
<plugin.dependencies/>
</properties>
@@ -104,6 +105,7 @@
<Plugin-Class>${plugin.class}</Plugin-Class>
<Plugin-Version>${plugin.version}</Plugin-Version>
<Plugin-Provider>${plugin.provider}</Plugin-Provider>
<Plugin-Description>${plugin.description}</Plugin-Description>
<Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>
</manifestEntries>
</archive>