49 lines
1.5 KiB
Java
49 lines
1.5 KiB
Java
package cryptosky.me.helpers;
|
|
|
|
import java.time.LocalDate;
|
|
import java.time.LocalDateTime;
|
|
import java.time.ZoneId;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.Map;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.function.Function;
|
|
import java.util.function.Predicate;
|
|
|
|
import javax.jms.Message;
|
|
|
|
import static java.util.UUID.randomUUID;
|
|
import static org.apache.commons.lang3.StringUtils.isBlank;
|
|
|
|
public class Utils {
|
|
|
|
private static final String CORRELATION_ID_KEY = "CS-Correlation-ID";
|
|
|
|
public static String getCorrelationId(Message message) {
|
|
String correlationId = null;
|
|
try {
|
|
correlationId = message.getJMSCorrelationID();
|
|
if (isBlank(correlationId)) {
|
|
correlationId = message.getStringProperty(CORRELATION_ID_KEY);
|
|
}
|
|
} catch (Throwable e) {
|
|
// NOOP
|
|
}
|
|
if (isBlank(correlationId)) {
|
|
correlationId = randomUUID().toString();
|
|
}
|
|
return correlationId;
|
|
}
|
|
|
|
// Utility Function to be able to get one of all types
|
|
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
|
|
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
|
|
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
|
|
}
|
|
|
|
public static LocalDateTime format(String T) {
|
|
DateTimeFormatter F = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX")
|
|
.withZone(ZoneId.of("UTC"));
|
|
return LocalDateTime.parse(T, F);
|
|
}
|
|
}
|