Hazelcast--Topic数据类型中文版

2024-06-08 01:18

本文主要是介绍Hazelcast--Topic数据类型中文版,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

4.6 Topic

概要:

Hazelcast 提供了发布消息给多个消费者的分布式机制.即众所周知的publish/subscribe(pub/sub)消息模型.

在cluster层面进行生产及消费操作.在topic中,当一个新的member加入后,你需要为其添加一个监听器,实际上是为在cluster中的一些member注册消息的发布机制.

image NOTE: Publish operation is async. It does not wait for operations to run in remote nodes, it works as fire and forget.

 

简单的topic例子

 

import com.hazelcast.core.Topic;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.MessageListener;public class Sample implements MessageListener<MyEvent> {public static void main( String[] args ) {Sample sample = new Sample();HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();ITopic topic = hazelcastInstance.getTopic( "default" );topic.addMessageListener( sample );topic.publish( new MyEvent() );}public void onMessage( Message<MyEvent> message ) {MyEvent myEvent = message.getMessageObject();System.out.println( "Message received = " + myEvent.toString() );if ( myEvent.isHeavyweight() ) {messageExecutor.execute( new Runnable() {public void run() {doHeavyweightStuff( myEvent );}} );}}// ...private final Executor messageExecutor = Executors.newSingleThreadExecutor();
}

 

 

  4.6.2 统计 Statistics

  在Topic中有两种统计变量可以进行访问查询操作.这些值是由本地成员负责维护的,一般为增加.

 

HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
ITopic<Object> myTopic = hazelcastInstance.getTopic( "myTopicName" );myTopic.getLocalTopicStats().getPublishOperationCount();
myTopic.getLocalTopicStats().getReceiveOperationCount();

 getPublishOperationCount() and getReceiveOperationCount ( ) 方法会分别返回在此节点启动后生产的消息数量以及接收到的消息数量.请注意,这些值是不会被备份的,此节点关闭后,这些值也会丢失.

 

关于Topic的设置特性请参照 Topic Configuration.

image NOTE: These statistics values can be also viewed in Management Center. Please see Topics.

 

  4.6.3 内部构件 Internals

  每一个节点拥有一个本cluster中所有节点的注册列表.当Topic注册一个新的节点时,它将给本cluster中的所有成员发送一条注册信息.同样的,当一个新节点加入cluster中时,它将会收到截止到它注册时,本cluster中所有节点的注册信息.

  配置globalOrderEnabled可改变topic中的节点行为.

  • If globalOrderEnabled is disabled:

  消息将是有序的,举个例子,如果消费者发布一条消息时,它会将一条消息推送到order队列.假如cluster成员M发布了很多消息 m1, m2, m3,...,mn到topic T,接下来Hazelcast会确保所有topic T中的消费者接收到的信息顺序也为m1, m2, m3,...,mn.

  这就是它的工作原理.比如说我们现在有三个节点(node1,node2,node3),node1和node2注册到名为news的topic中.请注意,所有的三个节点都会知道node1和node2注册到了topic news中喔.

  在这个例子中node1推送了两条消息:a1和a2.node3推送了两条消息:c1和c2.当node1和node3发布消息的时候,他们会检查他们各自本地的注册节点清单.他们发现node1和node2在这个列表中.于是,他们将消息发送到这些在列表中的节点.下面是他们可能受到的消息顺序:

  Node1 -> c1, a1, a2, c2

  Node2 -> c1, c2, a1, a2

  • If globalOrderEnabled is enabled:

  当globalOrderEnabled设置为可用时,它将保证所有监听相同topic的节点,收到的消息顺序相同.

我们再看看这次会怎么样.现在我们还是有三个节点(node1,node2,node3),node1和node2也注册到名为news的topic中.注意,所有的节点都知道node1和node2注册到了news中.

在这个例子里,node1发布两天消息:a1,a2.node3发布两天消息:c1,c2.当一个节点通过topic news发布消息时,它首先会计算news的id与哪一个分区相符.接下来给该分区的拥有者发送一个操作,于是该节点发布消息.我们假设news与node2拥有的分区一致.接下来node1

和node3首先将发送给node2所有信息.假设将用以下顺序发送信息:

  Node1 -> a1, c1, a2, c2

  此时,node2将把这些小心发送给本地存储的节点注册列表中的各个节点.它实际上会将这些消息发送给node1和node2(它将为它本身建立一个分配机制).

  Node1 -> a1, c1, a2, c2

  Node2 -> a1, c1, a2, c2

  使用这种方式时,会保证所有节点接收到消息事件的顺序相同.

在这两种情况下, 都会使用EventService的StripedExecutor来分配接收到的信息.对于Hazelcast中的所有事件,都通过StripedExecutor来处理,以保证它们是有序的.

  In StripedExecutor, there are as much threads specified in the property hazelcast.event.thread.count(default is 5). For a specific event source (for topic, for a particular topic name), hash of that source's name % 5gives the ID of responsible thread. Note that, there can be another event source (entry listener of a map, item listener of a collection, etc.) corresponding to the same thread. In order not to make other messages to block, heavy process should not be done in this thread. If there is a time consuming work needs to be done, the work should be handed over to another thread. Please see Sample Topic Code.

 

  4.6.4 Topic配置 Topic Configuration

  注解式配置:

 

<hazelcast>...<topic name="yourTopicName"><global-ordering-enabled>true</global-ordering-enabled><statistics-enabled>true</statistics-enabled><message-listeners><message-listener>MessageListenerImpl</message-listener></message-listeners></topic>...
</hazelcast>

   在程序中配置:

  

TopicConfig topicConfig = new TopicConfig();
topicConfig.setGlobalOrderingEnabled( true );
topicConfig.setStatisticsEnabled( true );
topicConfig.setName( "yourTopicName" );
MessageListener<String> implementation = new MessageListener<String>() {@Overridepublic void onMessage( Message<String> message ) {// process the message}
};
topicConfig.addMessageListenerConfig( new ListenerConfig( implementation ) );
HazelcastInstance instance = Hazelcast.newHazelcastInstance()

  缺省值:

  • Global ordering is false, meaning there is no global order guarantee by default.

  • Statistics are true, meaning statistics are calculated by default.

  Topic有关的设置,但不是topic特有的配置变量:

 

  • hazelcast.event.queue.capacity: default value is 1,000,000
  • hazelcast.event.queue.timeout.millis: default value is 250
  • hazelcast.event.thread.count: default value is 5


  RELATED INFORMATION

 

  For description of these parameters, please see Global Event Configuration

 

这篇关于Hazelcast--Topic数据类型中文版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1040841

相关文章

JS六大数据类型

js的六大数据类型

使用不同数据类型实例化Stack泛型类的代码

package 泛型;import java.math.BigDecimal;import java.math.MathContext;public class StackDemoApp {public static void main(String[] args) {//长类型栈System.out.println("创建'Long'类型栈");//创建用于保存Long数据类型的Stack对象

【Java】Hashmap不能用基本的数据类型 Dimensions expected after this token

http://moto0421.iteye.com/blog/1143777 今天试了一下HahsMap, 采用如下形似定义 (这个下面是用了csdn的一位同仁的文章,仅作为讲解参考,请见谅) HashMap<int,String> map=new HashMap<int,String>();  map.put(1,"a");  map.put(2,"b");  map.pu

Guitar Pro 8.2中文版图文安装激活使用指南

吉他谱曲软件Guitar Pro 8中文版是Arobas Music公司历时5年的一个全新之作,作为专业的吉他软件,能够创建不同的音轨完成不同乐器乐谱的编排和制作,这次在最新版本中新增了音频轨道、效果器视图、音阶示意图和音频音符微调等功能,优化了乐谱的编辑流程,支持批量调整音量。小哥聊软件为您提供Guitar Pro 8破解版下载,附有详细的安装教程。 软件详情 Guitar Pro 8.2是

66Uptime – 网站服务器 Cronjob 监控工具 v35.0.0扩展中文版安装

66Uptime是一款自托管、易于使用、轻量级且高性能的网站服务器和Cronjob监控工具。以其丰富的功能和便捷的管理方式,为用户提供了全方位的网站服务器和Cronjob监控解决方案: 主要功能: 监控网站服务器和Cronjob的运行状态,确保它们持续稳定运行。提供从多个位置检查显示器的功能,支持自定义HTTP请求和响应。提供正常运行时间和响应时间的监控,以及有关事件的电子邮件通知。支持Sl

Oracle2-数据类型之to_char to_date 函数

http://www.cnblogs.com/ajian/archive/2009/03/25/1421063.html TO_DATE格式(以时间:2007-11-02   13:45:25为例)             Year:               yy two digits 两位年                显示值:07         yyy three digits

Oracle1-数据类型

char(n)  n=1 to 2000字节 定长字符串,n字节长,如果不指定长度,缺省 为1个字节长(一个汉字为2字节) varchar2(n)  n=1 to 4000字节 可变长的字符串,具体定义时指明最大长度n, 这种数据类型可以放数字、字母以及ASCII码字符集(或者EBCDIC等数据库系统接受的字符集标准)中的所有符号。 如果数据长度没有达到最大

java数据类型相互转换工具类

package com.rest.ful.utils;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/*** 数据类型转换工具类* @author zlzhaoe* @version [版本号, 2017年5月8日]* @see [相关类/方法]

Java之运算符,位运算(源码反码补码)和基本数据类型

文章目录 1 java运算符1.1 各个运算符一览1.2 部分运算符说明1.3 java基本位操作1.3.1 位操作符号1.3.2 原码反码补码1.3.2.1 相关定义1.3.2.2 为何要使用原码, 反码和补码1.3.2.3 负数运算1.3.2.4 转换16进制为什么需要 &0xff 1.3.3 常用的位运算符运算1.3.3.1 左右位移 2 基本数据类型 1 java运算符