How to run akka actors in IntelliJ IDEA

2019-04-06 01:41发布

From Akka' site docs:

This main method will then create the infrastructure needed for running the actors, start the given main actor and arrange for the whole application to shut down once the main actor terminates. Thus you will be able to run the above code with a command similar to the following:

java -classpath akka.Main example.two.HelloWorld

So, how can I launch it from IntelliJ IDEA? I have not found good/proper Window for that.

Dependecy to AKKA is already in the project:

 <dependencies>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-actor_2.10</artifactId>
            <version>2.2-M3</version>
        </dependency>
    </dependencies>

The code itself (as you can see there is no main(...) ) :

public class HelloWorld extends UntypedActor {

    @Override
    public void preStart() {
        // create the greeter actor
        final ActorRef greeter =
                getContext().actorOf(Props.create(Greeter.class), "greeter");
        // tell it to perform the greeting
        greeter.tell(Greeter.Msg.GREET, getSelf());
    }

    @Override
    public void onReceive(Object msg) {
        if (msg == Greeter.Msg.DONE) {
            // when the greeter is done, stop this actor and with it the application
            getContext().stop(getSelf());
        } else unhandled(msg);
    }
}

2条回答
何必那么认真
2楼-- · 2019-04-06 02:08
//You can use the following code to start your actor:

public class Main {

  public static void main(String[] args) {
      akka.Main.main(new String[] { HelloWorld.class.getName() });
  }
}
查看更多
仙女界的扛把子
3楼-- · 2019-04-06 02:18

It seems like the documentation and the binary distributions are not in sync.

As you can see here on Github the Main.scala was added just 16 days ago.

To fix this you can change the dependency version to the SNAPSHOT instead. Add the snapshot repository to the pom and change version to 2.2-SNAPSHOT:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.stackoverflow</groupId>
    <artifactId>akka-app</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>${project.artifactId}-${project.version}</name>

    <repositories>
        <repository>
            <id>akka-snapshots</id>
            <name>Akka Snapshots</name>
            <url>http://repo.akka.io/snapshots/</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-actor_2.10</artifactId>
            <version>2.2-SNAPSHOT</version>
        </dependency>

    </dependencies>

</project>

Now the akka.Main will be available. In order to start the application you will have to point to the correct main class and add your own actor class as an argument.

First chose to create a new run configuration:

enter image description here

Then add a new application:

enter image description here

Give the application a name (Actor or something) and fill in the main class to be akka.Main and add your HelloWorld class as Program Arguments (remember to include the full package):

enter image description here

And now you are ready to run the program, just press the play button in the tool bar:

enter image description here

Voila!

查看更多
登录 后发表回答