门禁人脸识别项目,项目会议中讨论终端与后台信息通讯消息中间件,考虑到常用的消息通讯系ActiveMQ(Java编写)、KafKa(Scala编写)、RabbitMq(Erlang编写)、Nats(之前是Ruby编写现已修改为Go)、Redis(C语言编写)、Kestrel(Scala编写不常用)、NSQ(Go语言编写)、Nats(Go)都能满足基本的需求,但公司后台开发人员使用go语言,于是选择了Nats框架。
Nats 官方与社区版本均无对应的客户端,Android 使用 Java 开发,在 Java 上能运行即可移植在 Android 客户端
Java 客户端sample:https://github.com/nats-io/nats.java
Android 终端集成问题
Android 工程中引入 nats 框架,开发过程中遇到时间类文件在 Android 8.0 版本才引入,但门禁终端为 7.0 系统,需要找个折中办法改造 Java 客户端 sdk,万能 Github 已有个人改造nats java sdk,引入时间类 Duration :
Android 客户端sample:https://github.com/SpotOnInc/nats-android
Android 工程中引入改造后 JDK:implementation 'com.spoton:nats-android:2.4.6'
Java 工程中无UI主线程,但 Android 客户端 UI 主线程不能执行耗时任务,客户端发起请求连接服务端为耗时任务,直接在主线程中请求,触发设备 ANR,必须在子线程中去请求连接服务器。
Basic Usage
Connecting「连接」
There are four different ways to connect using the Java library:
-
Connect to a local server on the default port:
Connection nc = Nats.connect();
-
Connect to a server using a URL:「推荐」
Connection nc = Nats.connect("nats://myhost:4222");
-
Connect to one or more servers with a custom configuration:
Options o = new Options.Builder().server("nats://serverone:4222").server("nats://servertwo:4222").maxReconnects(-1).build(); Connection nc = Nats.connect(o);
See the javadoc for a complete list of configuration options.
-
Connect asynchronously, this requires a callback to tell the application when the client is connected:
Options options = new Options.Builder().server(Options.DEFAULT_URL).connectionListener(handler).build(); Nats.connectAsynchronously(options, true);
Publishing「发布」
Once connected, publishing is accomplished via one of three methods:
-
With a subject and message body:「推荐」
nc.publish("subject", "hello world".getBytes(StandardCharsets.UTF_8));
-
With a subject and message body, as well as a subject for the receiver to reply to:
nc.publish("subject", "replyto", "hello world".getBytes(StandardCharsets.UTF_8));
-
As a request that expects a reply. This method uses a Future to allow the application code to wait for the response. Under the covers a request/reply pair is the same as a publish/subscribe only the library manages the subscription for you.「私有发布,只有服务器能接收消息」
Future<Message> incoming = nc.request("subject", "hello world".getBytes(StandardCharsets.UTF_8)); Message msg = incoming.get(500, TimeUnit.MILLISECONDS); String response = new String(msg.getData(), StandardCharsets.UTF_8);
Listening for Incoming Messages
The Java NATS library provides two mechanisms to listen for messages, three if you include the request/reply discussed above.
-
Synchronous subscriptions where the application code manually asks for messages and blocks until they arrive. Each subscription is associated with a single subject, although that subject can be a wildcard.
Subscription sub = nc.subscribe("subject"); Message msg = sub.nextMessage(Duration.ofMillis(500)); String response = new String(msg.getData(), StandardCharsets.UTF_8);
-
A Dispatcher that will call application code in a background thread. Dispatchers can manage multiple subjects with a single thread and single callback.「推荐,Java 8 Lambda表达式,工程中JDK 必选选择为JDK 8,回调监听模式」
Dispatcher d = nc.createDispatcher((msg) -> { String response = new String(msg.getData(), StandardCharsets.UTF_8); ... }); d.subscribe("subject");