|
21 | 21 | import org.apache.shenyu.common.enums.PluginEnum; |
22 | 22 | import org.apache.shenyu.common.utils.GsonUtils; |
23 | 23 | import org.apache.shenyu.plugin.base.handler.PluginDataHandler; |
24 | | -import org.apache.shenyu.protocol.mqtt.BootstrapServer; |
25 | 24 | import org.apache.shenyu.protocol.mqtt.MqttBootstrapServer; |
26 | 25 | import org.apache.shenyu.protocol.mqtt.MqttServerConfiguration; |
27 | 26 |
|
28 | | -import java.net.InetAddress; |
29 | | -import java.net.Socket; |
| 27 | +import java.util.Objects; |
30 | 28 |
|
31 | 29 | /** |
32 | 30 | * The type Mqtt plugin data handler. |
33 | 31 | */ |
34 | 32 | public class MqttPluginDataHandler implements PluginDataHandler { |
35 | 33 |
|
36 | | - private final BootstrapServer server = new MqttBootstrapServer(); |
| 34 | + private final MqttBootstrapServer server = new MqttBootstrapServer(); |
| 35 | + |
| 36 | + private MqttServerConfiguration runningConfiguration; |
37 | 37 |
|
38 | 38 | @Override |
39 | | - public void handlerPlugin(final PluginData pluginData) { |
| 39 | + public synchronized void handlerPlugin(final PluginData pluginData) { |
| 40 | + if (!pluginData.getEnabled()) { |
| 41 | + server.shutdown(); |
| 42 | + runningConfiguration = null; |
| 43 | + return; |
| 44 | + } |
40 | 45 | MqttServerConfiguration configuration = GsonUtils.getInstance().fromJson(pluginData.getConfig(), MqttServerConfiguration.class); |
41 | | - configuration.afterPropertiesSet(); |
42 | | - if (pluginData.getEnabled()) { |
43 | | - server.init(); |
44 | | - server.start(); |
45 | | - } else { |
46 | | - if (isPortUsing(configuration.getPort())) { |
47 | | - server.shutdown(); |
48 | | - } |
| 46 | + if (configuration.hasSameRuntimeSettings(runningConfiguration) && server.isRunning()) { |
| 47 | + return; |
49 | 48 | } |
| 49 | + if (Objects.nonNull(runningConfiguration)) { |
| 50 | + server.shutdown(); |
| 51 | + runningConfiguration = null; |
| 52 | + } |
| 53 | + configuration.afterPropertiesSet(); |
| 54 | + server.init(); |
| 55 | + server.start(); |
| 56 | + runningConfiguration = configuration; |
50 | 57 | } |
51 | 58 |
|
52 | 59 | @Override |
53 | 60 | public String pluginNamed() { |
54 | 61 | return PluginEnum.MQTT.getName(); |
55 | 62 | } |
56 | | - |
57 | | - /** |
58 | | - * Ture is use else false. |
59 | | - * |
60 | | - * @param port server port |
61 | | - * @return boolean |
62 | | - */ |
63 | | - private boolean isPortUsing(final int port) { |
64 | | - boolean flag = false; |
65 | | - try (Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), port)) { |
66 | | - flag = true; |
67 | | - } catch (Exception ignored) { |
68 | | - |
69 | | - } |
70 | | - return flag; |
71 | | - } |
72 | 63 | } |
0 commit comments