Skip to content

Commit 291005a

Browse files
authored
Merge pull request #5 from mobiera/feat/add-support-text-message
feat: Add Support for Text Messages in Stats
2 parents 1dbad69 + 947d3e9 commit 291005a

File tree

1 file changed

+64
-25
lines changed

1 file changed

+64
-25
lines changed

src/main/java/com/mobiera/ms/commons/stats/jms/StatQueueConsumer.java

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package com.mobiera.ms.commons.stats.jms;
22

33

4+
import java.time.Instant;
5+
import java.time.format.DateTimeParseException;
6+
import java.util.ArrayList;
47
import java.util.HashMap;
8+
import java.util.List;
59
import java.util.Map;
610
import java.util.UUID;
711

812
import org.eclipse.microprofile.config.inject.ConfigProperty;
913
import org.jboss.logging.Logger;
1014

1115
import com.fasterxml.jackson.core.JsonProcessingException;
16+
import com.fasterxml.jackson.databind.JsonNode;
17+
import com.fasterxml.jackson.databind.ObjectMapper;
1218
import com.mobiera.commons.util.JsonUtil;
19+
import com.mobiera.ms.commons.stats.api.StatEnum;
1320
import com.mobiera.ms.commons.stats.api.StatEvent;
1421
import com.mobiera.ms.commons.stats.svc.StatBuilderService;
1522

@@ -28,6 +35,8 @@
2835
import jakarta.jms.ObjectMessage;
2936
import jakarta.jms.Queue;
3037
import jakarta.jms.Session;
38+
import jakarta.jms.TextMessage;
39+
import lombok.Data;
3140

3241
@ApplicationScoped
3342
public class StatQueueConsumer {
@@ -237,31 +246,32 @@ Uni<Void> runConsumer(UUID uuid) {
237246

238247
}
239248
}
240-
try {
241-
if (debug)
242-
logger.info("statConsumer: " + queueName + " before stat "+ uuid + " " + (System.currentTimeMillis() - now));
243-
244-
getStatBuilderService().statEvent(event);
245-
if (debug)
246-
logger.info("statConsumer: " + queueName + " after stat, before commit "+ uuid + " " + (System.currentTimeMillis() - now));
247-
248-
context.commit();
249-
if (debug)
250-
logger.info("statConsumer: " + queueName + " after commit "+ uuid + " " + (System.currentTimeMillis() - now));
251-
252-
253-
254-
} catch (Exception e) {
255-
try {
256-
logger.warn("statConsumer: " + queueName + " "+ uuid + " " + (System.currentTimeMillis() - now)+ ": exception " + JsonUtil.serialize(event, false), e);
257-
} catch (JsonProcessingException e1) {
258-
logger.warn("statConsumer: " + queueName + " "+ uuid + " " + (System.currentTimeMillis() - now)+ ": exception", e);
259-
}
260-
context.rollback();
261-
//if (debug)
262-
logger.info("statConsumer: " + queueName + " after rollback "+ uuid + " " + (System.currentTimeMillis() - now));
263-
264-
}
249+
processStatEvent(uuid, now, event, context);
250+
} else if (message instanceof TextMessage) {
251+
ObjectMapper objectMapper = new ObjectMapper();
252+
TextMessage txtMsg = (TextMessage) message;
253+
JsonNode rootNode = objectMapper.readTree(txtMsg.getText());
254+
255+
List<StatEnum> enums = new ArrayList<>();
256+
rootNode.path("enums").forEach(enumNode -> {
257+
try {
258+
enums.add(objectMapper.treeToValue(enumNode, StatEnumImpl.class));
259+
} catch (JsonProcessingException e) {
260+
}
261+
});
262+
event = new StatEvent();
263+
event.setStatClass(rootNode.path("statClass").asText("null"));
264+
event.setEntityId(rootNode.path("entityId").asText("null"));
265+
if (!rootNode.path("ts").isMissingNode()) {
266+
try {
267+
event.setTs(Instant.parse(rootNode.path("ts").asText()));
268+
} catch (DateTimeParseException e) {
269+
}
270+
}
271+
event.setIncrement(rootNode.path("increment").asInt(0));
272+
event.setDoubleIncrement(rootNode.path("doubleIncrement").asDouble(0.0));
273+
event.setEnums(enums);
274+
processStatEvent(uuid, now, event, context);
265275
} else {
266276
if (debug) logger.warn("statConsumer " + queueName + " "+ uuid + " " + (System.currentTimeMillis() - now)+ ": unkown event " + event);
267277
context.commit();
@@ -342,7 +352,36 @@ private StatBuilderService getStatBuilderService() {
342352
}
343353

344354

355+
private void processStatEvent(UUID uuid, long now, StatEvent event, JMSContext context) {
356+
try {
357+
if (debug)
358+
logger.info("statConsumer: " + queueName + " before stat "+ uuid + " " + (System.currentTimeMillis() - now));
359+
360+
getStatBuilderService().statEvent(event);
361+
if (debug)
362+
logger.info("statConsumer: " + queueName + " after stat, before commit "+ uuid + " " + (System.currentTimeMillis() - now));
363+
364+
context.commit();
365+
if (debug)
366+
logger.info("statConsumer: " + queueName + " after commit "+ uuid + " " + (System.currentTimeMillis() - now));
367+
} catch (Exception e) {
368+
try {
369+
logger.warn("statConsumer: " + queueName + " "+ uuid + " " + (System.currentTimeMillis() - now)+ ": exception " + JsonUtil.serialize(event, false), e);
370+
} catch (JsonProcessingException e1) {
371+
logger.warn("statConsumer: " + queueName + " "+ uuid + " " + (System.currentTimeMillis() - now)+ ": exception", e);
372+
}
373+
context.rollback();
374+
logger.info("statConsumer: " + queueName + " after rollback "+ uuid + " " + (System.currentTimeMillis() - now));
375+
}
376+
}
345377

378+
@Data
379+
public static class StatEnumImpl implements StatEnum {
380+
private Integer index;
381+
private String label;
382+
private String value;
383+
private String description;
384+
}
346385

347386

348387

0 commit comments

Comments
 (0)