From 26d2eb946fc0922cb0596053df3ff77b6143a0c4 Mon Sep 17 00:00:00 2001 From: "michele.artini" Date: Thu, 4 Feb 2021 09:45:46 +0100 Subject: [PATCH] messages sender --- .../eu/dnetlib/{ => dhp}/message/Message.java | 17 +++--------- .../{ => dhp}/message/MessageSender.java | 26 +++++++++++++++++-- .../java/eu/dnetlib/message/MessageType.java | 6 ----- 3 files changed, 27 insertions(+), 22 deletions(-) rename dhp-common/src/main/java/eu/dnetlib/{ => dhp}/message/Message.java (68%) rename dhp-common/src/main/java/eu/dnetlib/{ => dhp}/message/MessageSender.java (59%) delete mode 100644 dhp-common/src/main/java/eu/dnetlib/message/MessageType.java diff --git a/dhp-common/src/main/java/eu/dnetlib/message/Message.java b/dhp-common/src/main/java/eu/dnetlib/dhp/message/Message.java similarity index 68% rename from dhp-common/src/main/java/eu/dnetlib/message/Message.java rename to dhp-common/src/main/java/eu/dnetlib/dhp/message/Message.java index 2de8ead42..57844d490 100644 --- a/dhp-common/src/main/java/eu/dnetlib/message/Message.java +++ b/dhp-common/src/main/java/eu/dnetlib/dhp/message/Message.java @@ -1,5 +1,5 @@ -package eu.dnetlib.message; +package eu.dnetlib.dhp.message; import java.io.Serializable; import java.util.Map; @@ -15,18 +15,15 @@ public class Message implements Serializable { private String jobName; - private MessageType type; - private Map body; public Message() { } - public Message(final String workflowId, final String jobName, final MessageType type, + public Message(final String workflowId, final String jobName, final Map body) { this.workflowId = workflowId; this.jobName = jobName; - this.type = type; this.body = body; } @@ -46,14 +43,6 @@ public class Message implements Serializable { this.jobName = jobName; } - public MessageType getType() { - return type; - } - - public void setType(final MessageType type) { - this.type = type; - } - public Map getBody() { return body; } @@ -64,6 +53,6 @@ public class Message implements Serializable { @Override public String toString() { - return String.format("Message [workflowId=%s, jobName=%s, type=%s, body=%s]", workflowId, jobName, type, body); + return String.format("Message [workflowId=%s, jobName=%s, body=%s]", workflowId, jobName, body); } } diff --git a/dhp-common/src/main/java/eu/dnetlib/message/MessageSender.java b/dhp-common/src/main/java/eu/dnetlib/dhp/message/MessageSender.java similarity index 59% rename from dhp-common/src/main/java/eu/dnetlib/message/MessageSender.java rename to dhp-common/src/main/java/eu/dnetlib/dhp/message/MessageSender.java index 020d6087f..70eb594f8 100644 --- a/dhp-common/src/main/java/eu/dnetlib/message/MessageSender.java +++ b/dhp-common/src/main/java/eu/dnetlib/dhp/message/MessageSender.java @@ -1,6 +1,7 @@ -package eu.dnetlib.message; +package eu.dnetlib.dhp.message; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.SerializableEntity; @@ -13,6 +14,12 @@ public class MessageSender { private static final Logger log = LoggerFactory.getLogger(MessageSender.class); + private static final int SOCKET_TIMEOUT_MS = 2000; + + private static final int CONNECTION_REQUEST_TIMEOUT_MS = 2000; + + private static final int CONNTECTION_TIMEOUT_MS = 2000; + private final String dnetMessageEndpoint; public MessageSender(final String dnetMessageEndpoint) { @@ -20,10 +27,25 @@ public class MessageSender { } public void sendMessage(final Message message) { + new Thread(() -> _sendMessage(message)).start(); + } + + private void _sendMessage(final Message message) { final HttpPut req = new HttpPut(dnetMessageEndpoint); req.setEntity(new SerializableEntity(message)); - try (final CloseableHttpClient client = HttpClients.createDefault(); + final RequestConfig requestConfig = RequestConfig + .custom() + .setConnectTimeout(CONNTECTION_TIMEOUT_MS) + .setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT_MS) + .setSocketTimeout(SOCKET_TIMEOUT_MS) + .build(); + ; + + try (final CloseableHttpClient client = HttpClients + .custom() + .setDefaultRequestConfig(requestConfig) + .build(); final CloseableHttpResponse response = client.execute(req)) { log.debug("Sent Message to " + dnetMessageEndpoint); log.debug("MESSAGE:" + message); diff --git a/dhp-common/src/main/java/eu/dnetlib/message/MessageType.java b/dhp-common/src/main/java/eu/dnetlib/message/MessageType.java deleted file mode 100644 index 72cbda252..000000000 --- a/dhp-common/src/main/java/eu/dnetlib/message/MessageType.java +++ /dev/null @@ -1,6 +0,0 @@ - -package eu.dnetlib.message; - -public enum MessageType { - ONGOING, REPORT -}