Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest form of process communication in Scala

What I want to do: I want to add communication capabilities to a couple of applications (soon to be jar libraries for Java) in Scala, and I want to do it in the most painless way, with no Tomcat, wars, paths for GET requests, RPC servers, etc.

What I have done: I've been checking a number of libraries, like Jetty, JAX-RS, Jackson, etc. But then I see the examples and they usually involve many different folders for configuration, WSDL files, etc. Most of the examples lack a main method and I don't have a clear picture about how many additional requirements may they have (e.g. Tomcat).

What I am planning to do: I'm considering to simply open a socket on the "server" to listen, then connect with the "client" and transfer some JSON, in both directions. This should be fairly standard so that I can use other programming languages in compatible ways (e.g. Python).

What I am asking: I would like to know whether there is some library that makes this easier. Not necessarily using raw sockets, but setting up some process communication in just a few lines, maybe not as simple as Node.js, but something similar.

Bonus: It would be cool to

  • be able to use other programming languages (e.g. Python) by using open standards
  • have authentication

But I don't really need any of those at this point.

like image 739
jmora Avatar asked Oct 31 '14 18:10

jmora


People also ask

What is the example of Scala example?

Scala Example: Hello Scala. The following code example is a simple scala program. In the above code, we have created an object ScalaExample. It contains a main method and display message using println method. This file is saved with the name ScalaExample.scala. Command to compile this code is: scalac ScalaExample.scala.

What is processio in Scala?

This package presents an alternative model: the I/O of a running process is controlled by a scala.sys.process.ProcessIO object, which can be passed to the code that runs the external process. A ProcessIO will have direct access to the java streams associated with the process I/O.

How does the Scala compiler work?

The Scala compiler compiles your Scala code into Java Byte Code, which can then be executed by the ' scala ' command. The ' scala ' command is similar to the java command, in that it executes your compiled Scala code.

What is the best way to execute Scala code?

You can also use IDE (Integrated Development Environment) for executing scala code. The above example is written using object oriented approach. You can also use functional approach to write code in scala.


2 Answers

I think you need RPC client/server system, I would suggest to take one of these two:

  1. Finagle - super flexible and powerful RPC client/server from Finagle. You can define your service with Thrift, and it will generate stubs for client/server in scala. With Thrift it should be straightforward to add Python support.

  2. Spray - much smaller library, focused on creating REST services. It's not so powerful as Finagle, however much easier. And REST allows you to use any other clients

  3. Remotely - an elegant RPC system for reasonable people. Interesting and very promising project, however maybe difficult to start with because of extensive Scalaz+Shapeless+Macro usage

like image 165
Eugene Zhulenev Avatar answered Oct 15 '22 03:10

Eugene Zhulenev


RabbitMQ provides one easy way to do what you want without writing a server and implementing your own persistence, flow control, authentication, etc. You can brew or apt-get install it.

  • You start up a broker daemon process (i.e. manages message queues)
  • In the Scala producer, you can use Maven-provided Java API to send JSON strings without any fuss (e.g. no definition languages) to specified queues
  • Then in your other Scala program, connect to the broker, and listen for messages on the queue, and parse the incoming JSON

Because it is so popular, there are many tutorials online for different patterns you may want to use to distribute the messages, e.g. pub/sub, one-to-one, exactly-once delivery, etc.

like image 20
EthanP Avatar answered Oct 15 '22 03:10

EthanP