Share messages type among two project in Akka

流过昼夜 提交于 2020-01-07 04:44:55

问题


I am developing a reactive library that internally is implemented using Akka actors. Let's call this library server. The library exposes a single type of Actor as its API. This Actor accepts different types of messages, that define its public interface. Let's say that these messages are defined as the followings.

sealed trait Request
case class Creation(name: String) extends Request
sealed trait Response
case class CreationAck(name: String) extends Response

Now, I have to implement also a program that uses the above library. Let's call this program client. This program will also use Akka Actors to integrate with the library, for example using the ask pattern (the rest of the application is not developed using actors).

implicit val timeout = Timeout(5 seconds)
def create(): Future[CreationAck] = (mainActor ? Creation).mapTo[CreationAck]
// And so on...

Clearly, to let compiling to the above code, types Creation and CreationAck must be available both to server and to client.

My question is: Which is the best approach to share the public messages like Creation and CreationAck among the two projects? To build each program I am using sbt.


回答1:


Package the messages with the server library, because the messages represent the library's public API. Any program that wishes to use this library could then import the necessary server packages and classes.

Creating a separate project for the messages could make sense if the messages were an interface that both server and client (and potentially others) implemented. But this is not the case, based on your description. Rather, the messages, and the implementation for which the messages are the API, are specifically tied to server. client doesn't implement these messages in any way; it's simply a user of the server library.

To reiterate the point, the messages are the public API of server and are tightly coupled to that library. The messages should be bundled with server.




回答2:


I would recommend you to create another project (ex: messages) and implement these kinds of common objects and classes inside it.

Afterwards, import this library in which project you want to use. In your case, add it to your build.sbt files in both server and client projects.

By doing so, you will be able to use same classes in different projects.



来源:https://stackoverflow.com/questions/46418817/share-messages-type-among-two-project-in-akka

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!