Message Sender

This commit is contained in:
Michele Artini 2021-02-03 16:42:36 +01:00
parent 820d729e99
commit 1b9731632b
2 changed files with 47 additions and 1 deletions

View File

@ -1,9 +1,15 @@
package eu.dnetlib.message;
import java.io.Serializable;
import java.util.Map;
public class Message {
public class Message implements Serializable {
/**
*
*/
private static final long serialVersionUID = 401753881204524893L;
private String workflowId;
@ -55,4 +61,9 @@ public class Message {
public void setBody(final Map<String, String> body) {
this.body = body;
}
@Override
public String toString() {
return String.format("Message [workflowId=%s, jobName=%s, type=%s, body=%s]", workflowId, jobName, type, body);
}
}

View File

@ -0,0 +1,35 @@
package eu.dnetlib.message;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.SerializableEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MessageSender {
private static final Logger log = LoggerFactory.getLogger(MessageSender.class);
private final String dnetMessageEndpoint;
public MessageSender(final String dnetMessageEndpoint) {
this.dnetMessageEndpoint = dnetMessageEndpoint;
}
public void sendMessage(final Message message) {
final HttpPut req = new HttpPut(dnetMessageEndpoint);
req.setEntity(new SerializableEntity(message));
try (final CloseableHttpClient client = HttpClients.createDefault();
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);
}
}
}