|
@@ -25,6 +25,7 @@ import org.thingsboard.server.common.data.id.EntityIdFactory; |
|
@@ -25,6 +25,7 @@ import org.thingsboard.server.common.data.id.EntityIdFactory; |
25
|
import org.thingsboard.server.common.data.plugin.ComponentType;
|
25
|
import org.thingsboard.server.common.data.plugin.ComponentType;
|
26
|
import org.thingsboard.server.common.msg.TbMsg;
|
26
|
import org.thingsboard.server.common.msg.TbMsg;
|
27
|
import org.thingsboard.server.common.msg.TbMsgMetaData;
|
27
|
import org.thingsboard.server.common.msg.TbMsgMetaData;
|
|
|
28
|
+import org.thingsboard.server.common.msg.cluster.ClusterEventMsg;
|
28
|
|
29
|
|
29
|
import java.util.UUID;
|
30
|
import java.util.UUID;
|
30
|
import java.util.concurrent.TimeUnit;
|
31
|
import java.util.concurrent.TimeUnit;
|
|
@@ -56,6 +57,7 @@ public class TbMsgGeneratorNode implements TbNode { |
|
@@ -56,6 +57,7 @@ public class TbMsgGeneratorNode implements TbNode { |
56
|
private EntityId originatorId;
|
57
|
private EntityId originatorId;
|
57
|
private UUID nextTickId;
|
58
|
private UUID nextTickId;
|
58
|
private TbMsg prevMsg;
|
59
|
private TbMsg prevMsg;
|
|
|
60
|
+ private volatile boolean initialized;
|
59
|
|
61
|
|
60
|
@Override
|
62
|
@Override
|
61
|
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
|
63
|
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
|
|
@@ -66,21 +68,42 @@ public class TbMsgGeneratorNode implements TbNode { |
|
@@ -66,21 +68,42 @@ public class TbMsgGeneratorNode implements TbNode { |
66
|
} else {
|
68
|
} else {
|
67
|
originatorId = ctx.getSelfId();
|
69
|
originatorId = ctx.getSelfId();
|
68
|
}
|
70
|
}
|
69
|
- this.jsEngine = ctx.createJsScriptEngine(config.getJsScript(), "prevMsg", "prevMetadata", "prevMsgType");
|
|
|
70
|
- scheduleTickMsg(ctx);
|
71
|
+ updateGeneratorState(ctx);
|
|
|
72
|
+ }
|
|
|
73
|
+
|
|
|
74
|
+ @Override
|
|
|
75
|
+ public void onClusterEventMsg(TbContext ctx, ClusterEventMsg msg) {
|
|
|
76
|
+ updateGeneratorState(ctx);
|
|
|
77
|
+ }
|
|
|
78
|
+
|
|
|
79
|
+ private void updateGeneratorState(TbContext ctx) {
|
|
|
80
|
+ if (ctx.isLocalEntity(originatorId)) {
|
|
|
81
|
+ if (!initialized) {
|
|
|
82
|
+ initialized = true;
|
|
|
83
|
+ this.jsEngine = ctx.createJsScriptEngine(config.getJsScript(), "prevMsg", "prevMetadata", "prevMsgType");
|
|
|
84
|
+ scheduleTickMsg(ctx);
|
|
|
85
|
+ }
|
|
|
86
|
+ } else if (initialized) {
|
|
|
87
|
+ initialized = false;
|
|
|
88
|
+ destroy();
|
|
|
89
|
+ }
|
71
|
}
|
90
|
}
|
72
|
|
91
|
|
73
|
@Override
|
92
|
@Override
|
74
|
public void onMsg(TbContext ctx, TbMsg msg) {
|
93
|
public void onMsg(TbContext ctx, TbMsg msg) {
|
75
|
- if (msg.getType().equals(TB_MSG_GENERATOR_NODE_MSG) && msg.getId().equals(nextTickId)) {
|
94
|
+ if (initialized && msg.getType().equals(TB_MSG_GENERATOR_NODE_MSG) && msg.getId().equals(nextTickId)) {
|
76
|
withCallback(generate(ctx),
|
95
|
withCallback(generate(ctx),
|
77
|
m -> {
|
96
|
m -> {
|
78
|
- ctx.tellNext(m, SUCCESS);
|
|
|
79
|
- scheduleTickMsg(ctx);
|
97
|
+ if (initialized) {
|
|
|
98
|
+ ctx.tellNext(m, SUCCESS);
|
|
|
99
|
+ scheduleTickMsg(ctx);
|
|
|
100
|
+ }
|
80
|
},
|
101
|
},
|
81
|
t -> {
|
102
|
t -> {
|
82
|
- ctx.tellFailure(msg, t);
|
|
|
83
|
- scheduleTickMsg(ctx);
|
103
|
+ if (initialized) {
|
|
|
104
|
+ ctx.tellFailure(msg, t);
|
|
|
105
|
+ scheduleTickMsg(ctx);
|
|
|
106
|
+ }
|
84
|
});
|
107
|
});
|
85
|
}
|
108
|
}
|
86
|
}
|
109
|
}
|
|
@@ -102,8 +125,10 @@ public class TbMsgGeneratorNode implements TbNode { |
|
@@ -102,8 +125,10 @@ public class TbMsgGeneratorNode implements TbNode { |
102
|
if (prevMsg == null) {
|
125
|
if (prevMsg == null) {
|
103
|
prevMsg = ctx.newMsg("", originatorId, new TbMsgMetaData(), "{}");
|
126
|
prevMsg = ctx.newMsg("", originatorId, new TbMsgMetaData(), "{}");
|
104
|
}
|
127
|
}
|
105
|
- TbMsg generated = jsEngine.executeGenerate(prevMsg);
|
|
|
106
|
- prevMsg = ctx.newMsg(generated.getType(), originatorId, generated.getMetaData(), generated.getData());
|
128
|
+ if (initialized) {
|
|
|
129
|
+ TbMsg generated = jsEngine.executeGenerate(prevMsg);
|
|
|
130
|
+ prevMsg = ctx.newMsg(generated.getType(), originatorId, generated.getMetaData(), generated.getData());
|
|
|
131
|
+ }
|
107
|
return prevMsg;
|
132
|
return prevMsg;
|
108
|
});
|
133
|
});
|
109
|
}
|
134
|
}
|
|
@@ -113,6 +138,7 @@ public class TbMsgGeneratorNode implements TbNode { |
|
@@ -113,6 +138,7 @@ public class TbMsgGeneratorNode implements TbNode { |
113
|
prevMsg = null;
|
138
|
prevMsg = null;
|
114
|
if (jsEngine != null) {
|
139
|
if (jsEngine != null) {
|
115
|
jsEngine.destroy();
|
140
|
jsEngine.destroy();
|
|
|
141
|
+ jsEngine = null;
|
116
|
}
|
142
|
}
|
117
|
}
|
143
|
}
|
118
|
} |
144
|
} |