使用Java的Akka远程路由
我正在尝试实现一个基本的广播路由器,其中路由对象位于远程机器上 代码如下: localApp.conf使用Java的Akka远程路由,java,routing,akka,dead-letter,Java,Routing,Akka,Dead Letter,我正在尝试实现一个基本的广播路由器,其中路由对象位于远程机器上 代码如下: localApp.conf akka { log-dead-letters = 10 log-dead-letters-during-shutdown = off actor { provider = "akka.remote.RemoteActorRefProvider" serialize-messages = on serializers
akka {
log-dead-letters = 10
log-dead-letters-during-shutdown = off
actor {
provider = "akka.remote.RemoteActorRefProvider"
serialize-messages = on
serializers {
java = "akka.serialization.JavaSerializer"
}
serialization-bindings {
"java.lang.String" = java
"test.akkaLocal.LocalWrapper" = java
}
deployment {
/LocalMaster/broadcastRouter {
router = "broadcast"
nr-of-instances = 1
target {
nodes = ["akka.tcp://RemoteApp@127.0.0.1:10175"]
}
}
}
}
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty {
tcp {
hostname = "127.0.0.1"
port = 10174
}
}
}
}
LocalApp.java
public class LocalApp
{
public static void main(String[] args)
{
LocalApp app = new LocalApp();
app.executeLocal();
}
private void executeLocal() {
ActorSystem system = ActorSystem.create("LocalApp", ConfigFactory.load("localApp"));
final ActorRef master = system.actorOf(Props.create(LocalMaster.class), "LocalMaster");
master.tell(new LocalWrapper.Execute(), ActorRef.noSender());
}
public static class LocalMaster extends UntypedActor {
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof LocalWrapper.Execute) {
ActorSelection remoteActor =
getContext().actorSelection("akka.tcp://RemoteApp@127.0.0.1:10175/user/RemoteMaster");
ActorRef remoteRouter = getContext().actorOf(
Props.create(RemoteActor.class).withRouter(new FromConfig()), "broadcastRouter");
String msg = "Hello";
// remoteActor.tell(msg, getSelf());
remoteRouter.tell(msg, getSelf());
} else if (message instanceof String) {
String response = (String) message;
System.out.println(response);
}
}
}
public static class RemoteActor extends UntypedActor {
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof String) {
String msg = (String) message;
System.out.println(msg);
String resp = "World";
getSender().tell(resp, getSelf());
}
}
}
}
public class RemoteApp
{
public static void main(String[] args)
{
RemoteApp app = new RemoteApp();
app.executeRemote();
}
private void executeRemote() {
ActorSystem system = ActorSystem.create("RemoteApp", ConfigFactory.load("remoteApp"));
system.actorOf(Props.create(RemoteMaster.class), "RemoteMaster");
}
public static class RemoteMaster extends UntypedActor {
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof String) {
String msg = (String) message;
System.out.println(msg);
String response = "World";
getSender().tell(response, getSelf());
}
}
}
}
在remoteApp.conf中,端口为10175
RemoteApp.java
public class LocalApp
{
public static void main(String[] args)
{
LocalApp app = new LocalApp();
app.executeLocal();
}
private void executeLocal() {
ActorSystem system = ActorSystem.create("LocalApp", ConfigFactory.load("localApp"));
final ActorRef master = system.actorOf(Props.create(LocalMaster.class), "LocalMaster");
master.tell(new LocalWrapper.Execute(), ActorRef.noSender());
}
public static class LocalMaster extends UntypedActor {
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof LocalWrapper.Execute) {
ActorSelection remoteActor =
getContext().actorSelection("akka.tcp://RemoteApp@127.0.0.1:10175/user/RemoteMaster");
ActorRef remoteRouter = getContext().actorOf(
Props.create(RemoteActor.class).withRouter(new FromConfig()), "broadcastRouter");
String msg = "Hello";
// remoteActor.tell(msg, getSelf());
remoteRouter.tell(msg, getSelf());
} else if (message instanceof String) {
String response = (String) message;
System.out.println(response);
}
}
}
public static class RemoteActor extends UntypedActor {
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof String) {
String msg = (String) message;
System.out.println(msg);
String resp = "World";
getSender().tell(resp, getSelf());
}
}
}
}
public class RemoteApp
{
public static void main(String[] args)
{
RemoteApp app = new RemoteApp();
app.executeRemote();
}
private void executeRemote() {
ActorSystem system = ActorSystem.create("RemoteApp", ConfigFactory.load("remoteApp"));
system.actorOf(Props.create(RemoteMaster.class), "RemoteMaster");
}
public static class RemoteMaster extends UntypedActor {
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof String) {
String msg = (String) message;
System.out.println(msg);
String response = "World";
getSender().tell(response, getSelf());
}
}
}
}
现在我无法理解远程路由的概念。它是在远程机器上部署一个本地参与者然后向他们发送消息,还是连接到远程机器上的远程参与者然后向他们发送消息
使用我的代码,我能够向远程机器发送简单的消息(使用参与者选择)
LocalApp中的remoteActor.tell(msg,getSelf())(注释代码)发送和接收消息,并且不给出任何错误
但是,当我使用LocalActor创建路由器时,会出现死信错误
[INFO] [02/04/2014 16:34:58.408] [RemoteApp-akka.actor.default-dispatcher-4] [akka://RemoteApp/system/endpointManager/reliableEndpointWriter-akka.tcp%3A%2F%2FLocalApp%40127.0.0.1%3A10174-0/endpointWriter/endpointReader-akka.tcp%3A%2F%2FLocalApp%40127.0.0.1%3A10174-0]
Message [akka.remote.transport.AssociationHandle$InboundPayload] from Actor[akka://RemoteApp/deadLetters] to Actor[akka://RemoteApp/system/endpointManager/reliableEndpointWriter-akka.tcp%3A%2F%2FLocalApp%40127.0.0.1%3A10174-0/endpointWriter/endpointReader-akka.tcp%3A%2F%2FLocalApp%40127.0.0.1%3A10174-0#-288427524] was not delivered.
[1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
有人能告诉我我做错了什么吗
------更新------------
我发现了问题。远程类和本地类位于不同的项目中。在本地和远程项目之间的基本通信过程中,字符串是传输的对象的类型,这就是它成功的原因。有没有办法在两个不同的项目之间传递自定义类的对象?我尝试实现Serializable并将其添加到conf文件中,但这没有什么区别默认情况下,akka将对自定义消息类使用java序列化。如果类定义在系统的两侧(发送端和接收端)都可用(即在类路径中),那么您应该能够将其用于远程通信。我的建议是使用一个jar文件来表示系统两侧的类路径中可用的消息类
Akka还允许您对不同的消息类类型使用不同的序列化程序,因此您不必拘泥于java序列化,但如果您愿意,我建议您在尝试其他序列化程序之前先使用这种方式。默认情况下,Akka将对自定义消息类使用java序列化。如果类定义在系统的两侧(发送端和接收端)都可用(即在类路径中),那么您应该能够将其用于远程通信。Akka还允许您对不同的消息类类型使用不同的序列化程序,因此您不必拘泥于java序列化,但我建议您在尝试其他序列化程序之前,先使用这种方式,如果您愿意的话。谢谢您的回复。成功了。我必须在两个项目中使用相同的包创建相同的类。把它写下来作为一个答案,这样我就可以接受它。作为一个答案添加。。。