Skip to content

Commit 1422de0

Browse files
rewrite waitListWorker to be more fail safe
rewrite waitListWorker to be more fail safe fix dependency conflicts
1 parent c91f652 commit 1422de0

File tree

6 files changed

+78
-53
lines changed

6 files changed

+78
-53
lines changed

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@
7676
<groupId>org.springframework.boot</groupId>
7777
<artifactId>spring-boot-starter-test</artifactId>
7878
<scope>test</scope>
79+
<exclusions>
80+
<exclusion>
81+
<groupId>com.vaadin.external.google</groupId>
82+
<artifactId>android-json</artifactId>
83+
</exclusion>
84+
</exclusions>
7985
</dependency>
8086

8187
<dependency>
@@ -153,7 +159,7 @@
153159
<dependency>
154160
<groupId>org.json</groupId>
155161
<artifactId>json</artifactId>
156-
<version>20160810</version>
162+
<version>20180130</version>
157163
</dependency>
158164

159165
<!-- https://mvnrepository.com/artifact/com.github.wnameless/json-flattener -->

src/functionaltests/java/com/ericsson/ei/subscriptions/trigger/SubscriptionTriggerSteps.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ private boolean requestBodyContainsStatedValues(String endpoint) throws JSONExce
256256
JSONArray jsonArray = new JSONArray(restBodyData);
257257

258258
for (int i = 0; i < jsonArray.length(); i++) {
259-
String requestBody = jsonArray.getString(i);
259+
String requestBody = jsonArray.get(i).toString();
260260
if (requestBody.contains("TC5")) {
261261
tc5++;
262262
}

src/main/java/com/ericsson/ei/jmespath/JmesPathInterface.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
*/
1717
package com.ericsson.ei.jmespath;
1818

19-
import com.fasterxml.jackson.databind.JsonNode;
20-
import com.fasterxml.jackson.databind.ObjectMapper;
21-
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
22-
2319
import org.slf4j.Logger;
2420
import org.slf4j.LoggerFactory;
2521
import org.springframework.stereotype.Component;
2622

23+
import com.fasterxml.jackson.databind.JsonNode;
24+
import com.fasterxml.jackson.databind.ObjectMapper;
25+
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
26+
2727
import io.burt.jmespath.Expression;
2828
import io.burt.jmespath.JmesPath;
2929
import io.burt.jmespath.function.FunctionRegistry;
@@ -64,7 +64,10 @@ public JsonNode runRuleOnEvent(String rule, String event) {
6464
JsonNode eventJson = objectMapper.readValue(event, JsonNode.class);
6565
result = expression.search(eventJson);
6666
} catch (Exception e) {
67-
log.info(e.getMessage(), e);
67+
String msg = "runRuleOnEvent failed for given arguments:\n";
68+
msg += "rule was: " + rule + "\n";
69+
msg += "event was: " + event + "\n";
70+
log.error(msg, e);
6871
}
6972

7073
return result;

src/main/java/com/ericsson/ei/jsonmerge/MergePrepare.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@
1616
*/
1717
package com.ericsson.ei.jsonmerge;
1818

19-
import com.ericsson.ei.jmespath.JmesPathInterface;
20-
import com.fasterxml.jackson.databind.JsonNode;
21-
import com.fasterxml.jackson.databind.ObjectMapper;
22-
import com.fasterxml.jackson.databind.node.ArrayNode;
23-
import com.fasterxml.jackson.databind.node.NullNode;
24-
import com.github.wnameless.json.flattener.JsonFlattener;
25-
2619
import java.util.ArrayList;
2720
import java.util.Arrays;
2821
import java.util.Map;
@@ -36,6 +29,13 @@
3629
import org.springframework.beans.factory.annotation.Autowired;
3730
import org.springframework.stereotype.Component;
3831

32+
import com.ericsson.ei.jmespath.JmesPathInterface;
33+
import com.fasterxml.jackson.databind.JsonNode;
34+
import com.fasterxml.jackson.databind.ObjectMapper;
35+
import com.fasterxml.jackson.databind.node.ArrayNode;
36+
import com.fasterxml.jackson.databind.node.NullNode;
37+
import com.github.wnameless.json.flattener.JsonFlattener;
38+
3939
import lombok.Setter;
4040

4141
@Component
@@ -64,7 +64,7 @@ else if (value.getClass() == JSONArray.class) {
6464
} catch (JSONException e) {
6565
try {
6666
JSONArray ruleJSONArray = new JSONArray(mergeRule);
67-
return getValueFromRule(ruleJSONArray.getString(1));
67+
return getValueFromRule(ruleJSONArray.get(1).toString());
6868
} catch (Exception ne) {
6969
log.info(ne.getMessage(), ne);
7070
}
@@ -135,8 +135,8 @@ public String getMergePathFromArrayMergeRules(String originObject, String mergeR
135135
log.debug(" originObject is : " + originObject);
136136
try {
137137
JSONArray ruleJSONArray = new JSONArray(mergeRule);
138-
String firstRule = ruleJSONArray.getString(0);
139-
String secondRule = ruleJSONArray.getString(1);
138+
String firstRule = ruleJSONArray.get(0).toString();
139+
String secondRule = ruleJSONArray.get(1).toString();
140140
String firstPath = getMergePath(originObject, firstRule, false);
141141
String firstPathTrimmed = trimLastInPath(firstPath, ".");
142142

@@ -441,7 +441,12 @@ public String addMissingLevels(String originObject, String objectToMerge, String
441441
}
442442
}
443443
} catch (Exception e) {
444-
log.error(e.getMessage(), e);
444+
String msg = "addMissingLevels failed for arguments:\n";
445+
msg += "originObject was : " + originObject + "\n";
446+
msg += "objectTomerge was: " + objectToMerge + "\n";
447+
msg += "mergeRule was: " + mergeRule + "\n";
448+
msg += "mergePath was: " + mergePath + "\n";
449+
log.error(msg, e);
445450
}
446451
return newObject.toString();
447452
}
@@ -457,7 +462,7 @@ private int getOriginObjectArraySize(String originObject, JSONArray mergePathArr
457462
JSONObject originJSONObject = new JSONObject(originObject);
458463
Object valueForKey = null;
459464
for (int i = 0; i < mergePathIndex; i++) {
460-
String key = mergePathArray.getString(i);
465+
String key = mergePathArray.get(i).toString();
461466
if (valueForKey == null && originJSONObject.has(key)) {
462467
valueForKey = originJSONObject.get(key);
463468
} else {

src/main/java/com/ericsson/ei/waitlist/WaitListWorker.java

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,9 @@
1616
*/
1717
package com.ericsson.ei.waitlist;
1818

19-
import com.ericsson.ei.handlers.EventToObjectMapHandler;
20-
import com.ericsson.ei.handlers.MatchIdRulesHandler;
21-
import com.ericsson.ei.jmespath.JmesPathInterface;
22-
import com.ericsson.ei.rmqhandler.RmqHandler;
23-
import com.ericsson.ei.rules.RulesHandler;
24-
import com.ericsson.ei.rules.RulesObject;
25-
import com.fasterxml.jackson.databind.JsonNode;
19+
import java.util.Collection;
20+
import java.util.List;
2621

27-
import org.json.JSONException;
28-
import org.json.JSONObject;
2922
import org.slf4j.Logger;
3023
import org.slf4j.LoggerFactory;
3124
import org.springframework.beans.factory.annotation.Autowired;
@@ -35,8 +28,14 @@
3528
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
3629
import org.springframework.stereotype.Component;
3730

38-
import java.util.Collection;
39-
import java.util.List;
31+
import com.ericsson.ei.handlers.EventToObjectMapHandler;
32+
import com.ericsson.ei.handlers.MatchIdRulesHandler;
33+
import com.ericsson.ei.jmespath.JmesPathInterface;
34+
import com.ericsson.ei.rmqhandler.RmqHandler;
35+
import com.ericsson.ei.rules.RulesHandler;
36+
import com.ericsson.ei.rules.RulesObject;
37+
import com.fasterxml.jackson.databind.JsonNode;
38+
import com.fasterxml.jackson.databind.ObjectMapper;
4039

4140
@Component
4241
public class WaitListWorker {
@@ -66,30 +65,41 @@ public TaskScheduler taskScheduler() {
6665
}
6766

6867
@Scheduled(initialDelayString = "${waitlist.initialDelayResend}", fixedRateString = "${waitlist.fixedRateResend}")
69-
public void run() throws JSONException {
70-
RulesObject rulesObject;
68+
public void run() {
7169
List<String> documents = waitListStorageHandler.getWaitList();
7270
for (String document : documents) {
73-
JSONObject eventObject = new JSONObject(document);
74-
if (eventToObjectMapHandler.isEventInEventObjectMap(eventObject.getString("_id"))) {
75-
waitListStorageHandler.dropDocumentFromWaitList(document);
76-
} else {
77-
String event = eventObject.getString("Event");
78-
rulesObject = rulesHandler.getRulesForEvent(event);
79-
String idRule = rulesObject.getIdentifyRules();
80-
81-
if (idRule != null && !idRule.isEmpty()) {
82-
JsonNode ids = jmesPathInterface.runRuleOnEvent(idRule, event);
83-
if (ids.isArray()) {
84-
LOGGER.debug("[EIFFEL EVENT RESENT] id:" + eventObject.getString("_id") + " time:"
85-
+ eventObject.getString("Time"));
86-
for (final JsonNode idJsonObj : ids) {
87-
Collection<String> objects = matchIdRulesHandler.fetchObjectsById(rulesObject,
88-
idJsonObj.textValue());
89-
if (!objects.isEmpty()) {
90-
rmqHandler.publishObjectToWaitlistQueue(event);
91-
}
92-
}
71+
try {
72+
ObjectMapper objectMapper = new ObjectMapper();
73+
JsonNode eventJson = objectMapper.readTree(document);
74+
String id = eventJson.get("_id").asText();
75+
if (eventToObjectMapHandler.isEventInEventObjectMap(id)) {
76+
waitListStorageHandler.dropDocumentFromWaitList(document);
77+
} else {
78+
checkTargetAggregationsExistAndRepublishEvent(eventJson);
79+
}
80+
} catch (Exception e) {
81+
LOGGER.error("Exception occured while trying to resend event: " + document, e);
82+
}
83+
}
84+
}
85+
86+
public void checkTargetAggregationsExistAndRepublishEvent(JsonNode eventJson) {
87+
JsonNode event = eventJson.get("Event");
88+
String eventStr = event.asText();
89+
RulesObject rulesObject = rulesHandler.getRulesForEvent(eventStr);
90+
String idRule = rulesObject.getIdentifyRules();
91+
92+
if (idRule != null && !idRule.isEmpty()) {
93+
JsonNode ids = jmesPathInterface.runRuleOnEvent(idRule, eventStr);
94+
if (ids.isArray()) {
95+
JsonNode idNode = eventJson.get("_id");
96+
JsonNode timeNode = eventJson.get("Time");
97+
LOGGER.debug("[EIFFEL EVENT RESENT] id:" + idNode.textValue() + " time:" + timeNode);
98+
for (final JsonNode idJsonObj : ids) {
99+
Collection<String> objects = matchIdRulesHandler.fetchObjectsById(rulesObject,
100+
idJsonObj.textValue());
101+
if (!objects.isEmpty()) {
102+
rmqHandler.publishObjectToWaitlistQueue(eventStr);
93103
}
94104
}
95105
}

src/test/java/com/ericsson/ei/rules/test/TestRulesRestAPI.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ public void testJmesPathRestApi() throws Exception {
105105

106106
assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus());
107107
assertEquals("e90daae3-bf3f-4b0a-b899-67834fd5ebd0", obj.getString("id"));
108-
assertEquals("1484061386383", obj.getString("time"));
108+
long timeV = 1484061386383L;
109+
assertEquals(timeV, obj.getLong("time"));
109110

110111
}
111112

0 commit comments

Comments
 (0)