连接mongodb,kafka异步处理代码
1. mongodb异步处理
依赖:
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-async</artifactId>
<version>3.0.4</version>
</dependency>
</dependencies>
代码
public static void main(String[] args) {
List<ServerAddress> address=new ArrayList<>();
address.add(new ServerAddress("172.16.4.90",3000));
address.add(new ServerAddress("172.16.4.91",3000));
address.add(new ServerAddress("172.16.4.92",3000));
ClusterSettings clusterSettings = ClusterSettings.builder().hosts(address).build();
MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings).build();
MongoClient mongoClient = MongoClients.create(settings);
MongoDatabase database = mongoClient.getDatabase("shardb");
MongoCollection<Document> collection = database.getCollection("shardtable");
Document doc = new Document("name", "MongoDB")
.append("type", "database")
.append("count", 1)
.append("info", new Document("x", 203).append("y", 102));
Long start=System.currentTimeMillis();
collection.insertOne(doc, new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable t) {
System.out.println("Inserted cosume="+(System.currentTimeMillis()-start));
}
});
System.out.println("response cosume="+(System.currentTimeMillis()-start));
}
2.kafka异步处理
依赖:
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.9.0.0</version>
</dependency>
代码
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "172.16.4.93:9092,172.16.4.94:9092,172.16.4.95:9092");
props.put("acks", "all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("linger.ms", 1);
props.put("buffer.memory", 33554432);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer(props);
Long start=System.currentTimeMillis();
for(int i = 0; i < 100; i++){
//Future<RecordMetadata> response=
producer.send(new ProducerRecord<String, String>("davidwang456", Integer.toString(i), Integer.toString(i)),
new Callback() {
public void onCompletion(RecordMetadata metadata, Exception e) {
if(e != null){
e.printStackTrace();
System.out.println("The offset of the record we just sent is: " + metadata.offset());
}
}});
/* if(response.isDone()){
System.out.println("send message to david1 message key="+i+",value="+i);
} */
}
System.out.println(System.currentTimeMillis()-start);
producer.close();
}
结果
1. kafka的异步处理结果可以打印出来。
2. mongodb的异步处理结果没有打印出来。
连接mongodb,kafka异步处理代码的更多相关文章
- Kafka Producer相关代码分析【转】
来源:https://www.zybuluo.com/jewes/note/63925 @jewes 2015-01-17 20:36 字数 1967 阅读 1093 Kafka Producer相关 ...
- Kafka 异步消息也会阻塞?记一次 Dubbo 频繁超时排查过程
线上某服务 A 调用服务 B 接口完成一次交易,一次晚上的生产变更之后,系统监控发现服务 B 接口频繁超时,后续甚至返回线程池耗尽错误 Thread pool is EXHAUSTED.因为服务 B ...
- Nodejs开发(2.连接MongoDB)
一.先配置MongoDB Win10下下载那个安装版,zip版的会报却各种DLL,安装在你希望的路径,实在安装错了,就剪切过来也行(本例E:\mongodb). 然后是配置启动脚本,就是写一个bat文 ...
- java连接mongodb的一个奇葩问题及奇葩解决方式
昨天在eclipse中编写代码,本来连接mongodb进行各项操作都是正常的,但是有一会儿突然之间就没法连接了,还一直抱错,错误如下: 信息: Cluster created with setting ...
- NodeJS连接MongoDB数据库时报错
今天第一次尝试连接MongoDB数据库,具体步骤也很简单. 首先,通过NodeJS运行环境安装MongoDB包,进入要安装的目录,执行语句 npm install mongodb 安装成功后,通过如下 ...
- .net Core连接MongoDB
前两天在学习MongoDB相关的知识,做了个小Demo,大概是省份里面有多少所学校 连接MongoDB首先要通过Nuget添加一个MongoDB的包,下载此包 安装完毕后开始写代码了,创建一个省份实体 ...
- 【Node.js】二、基于Express框架 + 连接MongoDB + 写后端接口
在上节,我们讲了如何搭建express环境,现在我们说说如何通过node.js写服务接口给前端调用 1. 首先通过MongoDB建好数据库与表格 例如,我的数据库名字为db_demo,数据库表格为go ...
- Python 连接MongoDB并比较两个字符串相似度的简单示例
本文介绍一个示例:使用 pymongo 连接 MongoDB,查询MongoDB中的 字符串 记录,并比较字符串之间的相似度. 一,Python连接MongoDB 大致步骤:创建MongoClient ...
- (原创) 使用pymongo 3.6.0连接MongoDB的正确姿势
0.疑惑 前两天使用pymongo连接MongoDB的时候发现了一个奇怪的现象:我本机MongoDB并没有打开,但是使用pymong.MongoClient()进行连接时,并没有异常,我的服务端也正常 ...
随机推荐
- IOS 以随意点为圆心 旋转UIView
环绕底边中点旋转 UIView本身是支持旋转的,能够用UIView.transform属性实现旋转. The origin of the transform i ...
- 1>LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
解决方法如下:项目\属性\配置属性\清单工具\输入和输出\嵌入清单:原来是“是”,改成“否”. 终极解决方案:VS2010在经历一些更新后,建立Win32 Console Project时会出“err ...
- tomcat:web容器
Web容器和应用服务器除了你所说的request response之外还有很多其他重要的功能. 1. 多线程和并发处理,估计你写的socket程序支持不了几个并发访问吧 2. 安全性 3. 生命周期管 ...
- Python day字符串所有使用
字符串所有的操作name = "dio"names = "my\t name is {Name} and i am a {job}"print(name.cap ...
- 前阿里云CTO章文嵩:怎样做开源才有意义?
阿里云CTO章文嵩已于昨日离职,据传加盟滴滴.可靠消息透露,章文嵩在滴滴出行担任的是技术高级副总裁的职位.这样一个身价上亿的技术大牛,是怎么看待开源项目的?InfoQ:关于淘宝-阿里系的开源进程,我们 ...
- iOS -读书笔记-网络请求
知道"3次握手"吗?突然想起这个词 什么是3次握手? TCP三次握手/四次挥手详解 这里是3次握手的详解 3次握手就是为了可靠的传送数据,TCP(什么是TCP呢?TCP就是一种可靠 ...
- 怎样利用ash监控会话
ash是很有效的监控工具之中的一个.1秒抓一次 select max(sample_time)over(),min(sample_time)over() from dba_hist_active_se ...
- POJ 3461 Oulipo KMP算法题解
本题就是给出非常多对字符串,然后问一个字符串在另外一个字符串出现的次数. 就是所谓的Strstr函数啦. Leetcode有这道差点儿一模一样的题目. 使用KMP算法加速.算法高手必会的算法了. 另外 ...
- ubuntu-虚拟机跟主机资源共享的实现方法
之前自己的虚拟机跟主机资源共享的时候,使用的是非常笨的方法,就是通过创建两个飞鸽,然后在两个飞鸽之间进行文件的传输,今天工作相对轻松一些,就趁机完善一下自己的工作环境,在网上搜索了关于资源共享的方法, ...
- MockServer jar包安装
github地址: https://github.com/jamesdbloom/mockserver 1. org.apache.maven.plugin-tools:maven-plugin-an ...