akka 使用

1.AKKA 是一款基於actor模型實現的 併發處理框架。基於事件驅動的併發處理模型,每個actor擁有本身的屬性和操做,這樣就避免了一般狀況下由於多個線程之間要共享屬性(數據)而是用鎖機制的處理。這種機制在scala,cloure 語言中應用的很好,將操做和屬性放在一個獨立的單元中進行處理,從而提升併發處理的能力java

2.pom文件apache

     

<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.study</groupId>
	<artifactId>akka</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>akka</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>com.typesafe.akka</groupId>
			<artifactId>akka-actor_2.12</artifactId>
			<version>2.5.11</version>
		</dependency>

		<dependency>
			<groupId>com.typesafe.akka</groupId>
			<artifactId>akka-remote_2.12</artifactId>
			<version>2.5.11</version>
		</dependency>

	</dependencies>
</project>

3.客戶端 client.java併發

     

package com.study.akka;

import java.util.Properties;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import akka.actor.Props;

public class Client extends AbstractActor {
	@Override
	public Receive createReceive() {
		return receiveBuilder().match(String.class, msg -> {
			System.out.println(self() + " 收到消息 from " + sender() + " :" + msg);
//格式:akka.<protocol>://<actor system name>@<hostname>:<port>/<actor path>
			ActorSelection as = getContext().actorSelection("akka.tcp://test-server@127.0.0.1:5555/user/server");
			as.tell("client ..............." + System.currentTimeMillis(), self());
		}).matchAny(msg -> System.out.println(msg)).build();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Properties p = new Properties();
		p.setProperty("akka.actor.provider", "akka.remote.RemoteActorRefProvider");
		p.setProperty("akka.remote.netty.tcp.hostname", "127.0.0.1");
		p.setProperty("akka.remote.netty.tcp.port", "6666");
		Config conf = ConfigFactory.parseProperties(p);
		ActorSystem system = ActorSystem.create("test-client", conf);
		ActorRef ref = system.actorOf(Props.create(Client.class), "client");
		ref.tell("client ...............", ActorRef.noSender());
	}
}

 

4.服務端 server.java框架

package com.study.akka;

import java.util.Properties;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import akka.actor.Props;

public class Server extends AbstractActor {

	@Override
	public Receive createReceive() {
		return receiveBuilder().matchAny(msg -> {
			System.out.println(self() + " 收到消息 from " + sender() + " :" + msg);
//格式:akka.<protocol>://<actor system name>@<hostname>:<port>/<actor path>
			ActorSelection as = getContext().actorSelection("akka.tcp://test-client@127.0.0.1:6666/user/client");
			as.tell("server >>>>>>>>>>>>>>>>> " + System.currentTimeMillis(), self());
		}).build();
	}

	public static void main(String[] args) {
		Properties p = new Properties();
		p.setProperty("akka.actor.provider", "akka.remote.RemoteActorRefProvider");
		p.setProperty("akka.remote.netty.tcp.hostname", "127.0.0.1");
		p.setProperty("akka.remote.netty.tcp.port", "5555");
		Config conf = ConfigFactory.parseProperties(p);
		ActorSystem system = ActorSystem.create("test-server", conf);
		// ActorSystem system = ActorSystem.create("test-server");
		ActorRef ref = system.actorOf(Props.create(Server.class), "server");
		ref.tell("server ....................", ActorRef.noSender());
	}

}

5.運行結果:maven

客戶端輸出:tcp

服務端輸出:ide

相關文章
相關標籤/搜索