在Akka的两个项目之间共享消息类型 [英] Share messages type among two project in Akka
问题描述
我正在开发一个反应式库,该库在内部使用Akka actor实现。我们将此库称为服务器
。该库公开一种类型的Actor作为其API。该Actor接受不同类型的消息,这些消息定义了其公共接口。假设这些消息定义如下。
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
<现在,我还必须实现一个使用上述库的程序。我们将此程序称为 client
。该程序还将使用Akka Actors与库进行集成,例如使用 ask模式(应用程序的其余部分不是使用actors开发的。)。
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...
很显然,要编译为上述代码,请键入 Creation
和 CreationAck
必须对 server
和 client
。
Clearly, to let compiling to the above code, types Creation
and CreationAck
must be available both to server
and to client
.
我的问题是:这是共享公共消息(如 Creation)的最佳方法
和 CreationAck
这两个项目?要构建每个程序,我正在使用 sbt 。
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.
推荐答案
将消息与<$ c $打包在一起c> server 库,因为消息代表该库的公共API。任何希望使用该库的程序都可以导入必要的 server
包和类。
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.
创建一个单独的项目如果消息是同时实现了 server
和 client
(可能还有其他)的接口的消息,则可能有意义。但是,根据您的描述,情况并非如此。而是将消息以及消息作为API的实现专门绑定到 server
。 客户端
不会以任何方式实现这些消息;只是服务器
库的用户。
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.
重申这一点,消息是 server
并与该库紧密耦合。邮件应与服务器
捆绑在一起。
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
.
这篇关于在Akka的两个项目之间共享消息类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!