2021-02-03 16:42:36 +01:00
|
|
|
|
2021-02-04 09:45:46 +01:00
|
|
|
package eu.dnetlib.dhp.message;
|
2021-02-03 16:42:36 +01:00
|
|
|
|
2021-02-12 12:31:02 +01:00
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
2021-02-04 09:45:46 +01:00
|
|
|
import org.apache.http.client.config.RequestConfig;
|
2021-02-03 16:42:36 +01:00
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
|
import org.apache.http.client.methods.HttpPut;
|
2021-02-05 09:45:39 +01:00
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
|
import org.apache.http.entity.StringEntity;
|
2021-02-03 16:42:36 +01:00
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2021-02-05 09:45:39 +01:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2021-02-04 15:51:15 +01:00
|
|
|
|
2021-02-03 16:42:36 +01:00
|
|
|
public class MessageSender {
|
|
|
|
|
2021-02-05 09:45:39 +01:00
|
|
|
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 ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
|
|
private final String dnetMessageEndpoint;
|
|
|
|
|
|
|
|
private final String workflowId;
|
|
|
|
|
2021-02-12 12:31:02 +01:00
|
|
|
private ExecutorService executorService = Executors.newCachedThreadPool();
|
|
|
|
|
2021-02-05 09:45:39 +01:00
|
|
|
public MessageSender(final String dnetMessageEndpoint, final String workflowId) {
|
|
|
|
this.workflowId = workflowId;
|
|
|
|
this.dnetMessageEndpoint = dnetMessageEndpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendMessage(final Message message) {
|
2021-02-12 12:31:02 +01:00
|
|
|
executorService.submit(() -> _sendMessage(message));
|
2021-02-05 09:45:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void sendMessage(final Long current, final Long total) {
|
|
|
|
sendMessage(createMessage(current, total));
|
|
|
|
}
|
|
|
|
|
|
|
|
private Message createMessage(final Long current, final Long total) {
|
|
|
|
|
|
|
|
final Message m = new Message();
|
|
|
|
m.setWorkflowId(workflowId);
|
|
|
|
m.getBody().put(Message.CURRENT_PARAM, current.toString());
|
|
|
|
if (total != null) {
|
|
|
|
m.getBody().put(Message.TOTAL_PARAM, total.toString());
|
|
|
|
}
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void _sendMessage(final Message message) {
|
|
|
|
try {
|
|
|
|
final String json = objectMapper.writeValueAsString(message);
|
|
|
|
|
|
|
|
final HttpPut req = new HttpPut(dnetMessageEndpoint);
|
|
|
|
req.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
|
|
|
|
|
|
|
|
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);
|
|
|
|
} catch (final Throwable e) {
|
|
|
|
log.error("Error sending message to " + dnetMessageEndpoint + ", message content: " + message, e);
|
|
|
|
}
|
|
|
|
} catch (final JsonProcessingException e) {
|
|
|
|
log.error("Error sending message to " + dnetMessageEndpoint + ", message content: " + message, e);
|
|
|
|
}
|
|
|
|
}
|
2021-02-03 16:42:36 +01:00
|
|
|
|
|
|
|
}
|