Simple email notification micro service using Eclipse Vert.x

How service does work

Listen for requests (HttpVerticle)

  • Router = this class handles all stuff with routing and paths
  • HttpServer = this class works with HTTP requests
@Override
public void start(Promise<Void> startPromise) throws Exception {
HttpServer server = vertx.createHttpServer();
Router router = Router.router(vertx);
EventBus eventBus = vertx.eventBus();

router.route("/*").handler(BodyHandler.create());
router.post("/message").handler(context -> {

// here comes handler
context.response().end("Hello!");
});

server.requestHandler(router::accept);
server.listen(config().getInteger("app.port"), result -> {
if (result.succeeded()) {
startPromise.complete();
} else {
startPromise.fail(result.cause());
}
});
}
//...
router.post("/message").handler(context -> {
JsonObject payload = context.getBodyAsJson();
eventBus.request("smtp.send", payload, result -> {
if (result.succeeded()) {
JsonObject response = JsonObject.mapFrom(result.result().body());
context.response().end(response.encodePrettily());
} else {
String cause = result.cause().getLocalizedMessage();
JsonObject response = new JsonObject();
response.put("status", cause);
context.response().setStatusCode(500).end(response.encodePrettily());
}
});
});

Send emails (SmtpVerticle)

  1. Prepare MailConfig object with configuration options
  2. Create MailClient instance = this is actual SMTP client
  3. Create MailMessage = this entity represents a mail message that can be sent via the MailClient
  4. Call MailClient.sendMail() method to trigger actual sending. This method provides asynchronous handler, that we will use to get a result of operation
@Override
public void start(Promise<Void> startPromise) throws Exception {
EventBus eventBus = vertx.eventBus();
eventBus.consumer("smtp.send", this::sendMessage);
MailConfig mailConfig = new MailConfig();
mailConfig.setHostname(config().getString("mail.hostname"));
mailConfig.setPort(config().getInteger("mail.port"));
mailConfig.setUsername(config().getString("mail.username"));
mailConfig.setPassword(config().getString("mail.password"));
this.mailClient = MailClient.create(vertx, mailConfig);
startPromise.complete();
}
private void sendMessage (Message<Object> message) {
JsonObject payload = JsonObject.mapFrom(message.body());
String to = payload.getString("to");
String from = payload.getString("from");
String subject = payload.getString("subject");
String body = payload.getString("body");
MailMessage mailMessage = new MailMessage();
mailMessage.setFrom(from);
mailMessage.setHtml(body);
mailMessage.setSubject(subject);
mailMessage.setTo(to);
mailClient.sendMail(mailMessage, result ->{
if (result.succeeded()){
JsonObject response = new JsonObject();
response.put("status", true);
message.reply(response);
} else {
message.fail(0, result.cause().getLocalizedMessage());
}
});
}

Configuration questions

public static void main(String[] args) {

//..get config retriever options

Vertx vertx = Vertx.vertx();
ConfigRetriever configRetriever =
ConfigRetriever.create(vertx, configRetrieverOptions);

WebVerticle web = new WebVerticle();
SmtpVerticle smtp = new SmtpVerticle();

configRetriever.getConfig(config -> {
if (config.succeeded()) {
DeploymentOptions options = new DeploymentOptions();
options.setConfig(config.result());
vertx.deployVerticle(smtp, options, result -> {
if (result.succeeded()) {
vertx.deployVerticle(web, options, result2 -> {
if (result2.succeeded()) {
System.out.println("App deployed");
} else {
System.out.println(result2.cause().getLocalizedMessage());
vertx.close();
}
});
} else {
System.out.println(result.cause().getLocalizedMessage());
vertx.close();
}
});
} else {
config.cause().printStackTrace();
vertx.close();
}
});
}
ConfigStoreOptions configStoreOptions = new ConfigStoreOptions();
configStoreOptions.setType("file");
configStoreOptions.setFormat("properties");
configStoreOptions.setConfig(new JsonObject()
.put("path", "config.properties"));
ConfigRetrieverOptions configRetrieverOptions = new ConfigRetrieverOptions();
configRetrieverOptions.addStore(configStoreOptions);

///...

--

--

--

I pay my bills by telling computers what to do

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Java 9 : What’s new ?

Golang Rest Api by Docker

AWS EC2 (latest)

All You Want To Know About Algorithm & What Is The Part Of It In Programming ……

Houseplant CTF

Leo Language: Overview and Installation (Part-2)

Speaking at iOSCon in London

Lobster Daily #64 — Daily Recap — May 3:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yuri Mednikov

Yuri Mednikov

I pay my bills by telling computers what to do

More from Medium

RabbitMQ or Kafka

Logging in Microservices

Bind Istio with API Gateway — IaaC

What exactly is Apache Maven?