DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Fun with Scala and Vert.x

Fun with Scala and Vert.x

Slim Ouertani user avatar by
Slim Ouertani
·
Feb. 14, 13 · Interview
Like (0)
Save
Tweet
Share
9.58K Views

Join the DZone community and get the full member experience.

Join For Free

Vert.x is a polyglot event-driven application framework that runs on the Java Virtual Machine (JAVA 7 is the minimum supported version). Like Node.js, Vert.x is asynchronous and scalable, and lets developers build modern and effective web applications.

Being polyglot, Vert.x can be used in many flavors, among which : JavaScript,CoffeeScript, Ruby, Python,Groovy and Java. In order to enforce asynchronism and scalabity, Vert.x is built upon Netty, leverage the reactor pattern, using a frightening number of handlers.

This article aims at showing the powerful combination of Scala and Vert.x - the Java counterpart being provided as comparison.

Note : the source code are hosted on GitHub as part of lang-scala https://github.com/ouertani/vert.x/tree/master/vertx-lang/vertx-lang-scala

Vert.x supports many components :

  WebSocket
  HttpServer
  Distributed Event Bus
  TCP Server, SockJS ,... not presented here

I - WebSocket

Web Socket are HTML 5 feature providing full-duplex communications. For old browsers that do not support WebSocket, Vert.x provides SockJS as out-of-the-box component.

In order to run the following example check out : https://github.com/ouertani/vert.x/blob/master/vertx-examples/src/main/javascript/websockets/ws.html and save it into the compiled lib directory
Java version

import org.vertx.java.core.Handler;
import org.vertx.java.core.buffer.Buffer;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.http.ServerWebSocket;
import org.vertx.java.deploy.Verticle;
 
public class SampleWebSocket extends Verticle {
 
  public void start() {
    vertx.createHttpServer().websocketHandler(new Handler<ServerWebSocket>() {
      public void handle(final ServerWebSocket ws) {
        if (ws.path.equals("/myapp")) {
          ws.dataHandler(new Handler<Buffer>() {
            public void handle(Buffer data) {
              ws.writeTextFrame(data.toString()); // Echo it back
            }
          });
        } else {
          ws.reject();
        }
      }
    }).requestHandler(new Handler<HttpServerRequest>() {
      public void handle(HttpServerRequest req) {
        if (req.path.equals("/")) req.response.sendFile("ws.html"); // Serve the html
      }
    }).listen(8080);
  }
}



scala version

import org.vertx.java.core.buffer.Buffer;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.http.ServerWebSocket;
import org.vertx.scala.deploy.Verticle
import org.vertx.scala.core._
 
class SampleWebSocket extends Verticle (
  _.getVertx().createHttpServer().websocketHandler{
  ws:ServerWebSocket => ws.path match {
  case "/myapp" => ws.dataHandler{data : Buffer =>  ws.writeTextFrame(data.toString())}
  case _ => ws.reject();
  }}
  .requestHandler{req : HttpServerRequest => req.path match {case ("/") => req.response.sendFile("ws.html")}}
  .listen(8080)
)()



II-HttpWebServer

Vert.x allows you to easily write full featured, highly performant and scalable HTTP and HTTPS servers.

The following example starts up an Http server, listening on port 8080, and logging all received requests.
Java

import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpServer;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.logging.Logger;
import org.vertx.java.deploy.Verticle;
 
 
public class SampleHttpWebServer extends Verticle {
 
  public void start() {
  HttpServer server = vertx.createHttpServer();
  final Logger log = getContainer().getLogger();
  server.requestHandler(new Handler<HttpServerRequest>() {
  public void handle(HttpServerRequest request) {
  log.info("A request has arrived on the server!");
  }
  }).listen(8080, "localhost");
  }
}



Scala

import org.vertx.java.core.http. { HttpServerRequest => JHttpServerRequest}
import org.vertx.scala.deploy.Verticle
import org.vertx.scala.core._
 
class SampleWebServer extends Verticle ( x =>
  x.getVertx().createHttpServer().withRequestHandler{_ : JHttpServerRequest =>
  x.info("A request has arrived on the server!")}
.listen(8080, "localhost")
)()



III-HttpClient

Vert.x also provides an HttpClient API, so as to interact with the server part. The following samples create and send a GET request, then log the server's response.
Java

import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpClient;
import org.vertx.java.core.http.HttpClientRequest;
import org.vertx.java.core.http.HttpClientResponse;
import org.vertx.java.core.logging.Logger;
import org.vertx.java.deploy.Verticle;
 
 
public class SampleHttpClient extends Verticle {
 
  @Override
  public void start() throws Exception {
  HttpClient client = vertx.createHttpClient().setHost("127.0.0.1");
  final Logger log = getContainer().getLogger();
 
  HttpClientRequest request = client.post("/some-path/", new Handler<HttpClientResponse>() {
  public void handle(HttpClientResponse resp) {
  log.info("Got a response: ");
  }
  });
 
  request.end();
  }
}



Scala

import org.vertx.scala.deploy.Verticle
import org.vertx.scala.core._
 
class SampleWebClient extends Verticle (v =>
  v.getVertx.createHttpClient().setHost("127.0.0.1").setPort(8080)
  .andGetNow("/") {_ => v.info("Got a response: " )}
  )()



IV-EventBus

The event bus is like a vertebral spine, it can be used to connect distributed nodes, and to support interaction between different Verticles, even written in different languages.
Java

import org.vertx.java.deploy.Verticle;
import org.vertx.java.core.eventbus.EventBus;
 
public class SampleEventBus extends Verticle {
  @Override
  public void start() throws Exception {
  EventBus eb = vertx.eventBus();
  eb.send("path", "ping1");
  eb.send("path", "ping2");
  }  
}



Scala

import org.vertx.scala.deploy.Verticle
import org.vertx.scala.core._
class SampleEventBus extends Verticle ( x => {
 val  point = x ! ("path")
 point >> ("ping 1")
 point >> ("ping 2")
}
)()



Conclusion

This article introduced the basic of Vert.x using scala language and short examples. Full Scala language support will soon, hopefully, become available.

Stay tuned, a subsequent post will show you more about Vert.x with Scala.
source : http://blog.zenika.com/index.php?post/2013/02/11/fun-with-scala-and-vert-x

Vert.x Scala (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Key Considerations When Implementing Virtual Kubernetes Clusters
  • How To Use Terraform to Provision an AWS EC2 Instance
  • Top Five Tools for AI-based Test Automation
  • Visual Network Mapping Your K8s Clusters To Assess Performance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: