当前位置: 首页 > news >正文

wb网页设计素材模板网站模板网站建站哪家好

wb网页设计素材模板网站,模板网站建站哪家好,网站封面如何做的吸引人,企业如何建设网站呢文章目录 异步发送普通异步发送异步发送流程Code 带回调函数的异步发送带回调函数的异步发送流程Code 同步发送API 异步发送 普通异步发送 需求&#xff1a;创建Kafka生产者&#xff0c;采用异步的方式发送到Kafka broker 异步发送流程 Code <!-- https://mvnrepository…

文章目录

  • 异步发送
    • 普通异步发送
      • 异步发送流程
      • Code
    • 带回调函数的异步发送
      • 带回调函数的异步发送流程
      • Code
  • 同步发送API

在这里插入图片描述


异步发送

普通异步发送

需求:创建Kafka生产者,采用异步的方式发送到Kafka broker

异步发送流程

在这里插入图片描述

Code

<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
<dependency><groupId>org.apache.kafka</groupId><artifactId>kafka-clients</artifactId><version>3.6.0</version>
</dependency>
package com.artisan.pc;import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.serialization.StringSerializer;import java.util.Properties;
import java.util.concurrent.ExecutionException;/*** @author 小工匠* @version 1.0* @mark: show me the code , change the world*/
public class CustomProducer {public static void main(String[] args) throws ExecutionException, InterruptedException {// 1. 创建kafka生产者的配置对象Properties properties = new Properties();// 2. 给kafka配置对象添加配置信息properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.126.170:9092");// key,value序列化properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());// 3. 创建kafka生产者对象KafkaProducer<String, String> kafkaProducer = new KafkaProducer<String, String>(properties);// 4. 调用send方法,发送消息for (int i = 0; i < 10; i++) {RecordMetadata art = kafkaProducer.send(new ProducerRecord<>("art", "kafka-msg-" + i)).get();System.out.println(art.offset());System.out.println("over - " + i);}// 5. 关闭资源kafkaProducer.close();}}

输出

31
over - 0
32
over - 1
33
over - 2
34
over - 3
35
over - 4
36
over - 5
37
over - 6
38
over - 7
39
over - 8
40
over - 9

忽略我这个offset … 我都发了好多次了…

看控制台的吧

在这里插入图片描述


带回调函数的异步发送

回调函数callback()会在producer收到ack时调用,为异步调用。

该方法有两个参数分别是RecordMetadata(元数据信息)和Exception(异常信息)。

  • 如果Exception为null,说明消息发送成功,
  • 如果Exception不为null,说明消息发送失败

带回调函数的异步发送流程

在这里插入图片描述

注意:消息发送失败会自动重试,不需要我们在回调函数中手动重试。

Code

package com.artisan.pc;import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.StringSerializer;import java.util.Properties;
import java.util.concurrent.ExecutionException;/*** @author 小工匠* @version 1.0* @mark: show me the code , change the world*/
public class CustomProducerWithCallBack {public static void main(String[] args) throws ExecutionException, InterruptedException {// 1. 创建kafka生产者的配置对象Properties properties = new Properties();// 2. 给kafka配置对象添加配置信息properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.126.170:9092");// key,value序列化properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());// 3. 创建kafka生产者对象KafkaProducer<String, String> kafkaProducer = new KafkaProducer<String, String>(properties);// 4. 调用send方法,发送消息for (int i = 0; i < 10; i++) {// 添加回调// 该方法在Producer收到ack时调用,为异步调用kafkaProducer.send(new ProducerRecord<>("art", "kafka-msg-callback-" + i), (recordMetadata, e) -> {// 没有异常,输出信息到控制台System.out.println("主题" + recordMetadata.topic() + ", 分区:" + recordMetadata.partition() + ", 偏移量:" + recordMetadata.offset());});}// 5. 关闭资源kafkaProducer.close();}}

在这里插入图片描述

控制台

在这里插入图片描述


同步发送API

同步发送的意思就是,一条消息发送之后,会阻塞当前线程,直至返回ack。
由于send方法返回的是一个Future对象,根据Futrue对象的特点,我们也可以实现同步发送的效果,只需在调用Future对象的get方发即可

在这里插入图片描述

package com.artisan.pc;import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;import java.util.Properties;
import java.util.concurrent.ExecutionException;/*** @author 小工匠* @version 1.0* @mark: show me the code , change the world*/
public class CustomProducerSync {public static void main(String[] args) throws ExecutionException, InterruptedException {// 1. 创建kafka生产者的配置对象Properties properties = new Properties();// 2. 给kafka配置对象添加配置信息properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.126.170:9092");// key,value序列化properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());// 3. 创建kafka生产者对象KafkaProducer<String, String> kafkaProducer = new KafkaProducer<String, String>(properties);// 4. 调用send方法,发送消息for (int i = 0; i < 10; i++) {// 通过Future接口的get实现同步阻塞kafkaProducer.send(new ProducerRecord<>("art", "kafka-msg-get-" + i)).get() ;}// 5. 关闭资源kafkaProducer.close();}}

在这里插入图片描述


文章转载自:
http://variable.Lnnc.cn
http://histoid.Lnnc.cn
http://purger.Lnnc.cn
http://helistop.Lnnc.cn
http://radioiodine.Lnnc.cn
http://disadvise.Lnnc.cn
http://cleanlily.Lnnc.cn
http://manageress.Lnnc.cn
http://umbles.Lnnc.cn
http://cyproheptadine.Lnnc.cn
http://cerusite.Lnnc.cn
http://coo.Lnnc.cn
http://isogon.Lnnc.cn
http://appraise.Lnnc.cn
http://berceau.Lnnc.cn
http://gama.Lnnc.cn
http://behavior.Lnnc.cn
http://orthowater.Lnnc.cn
http://napoli.Lnnc.cn
http://cornice.Lnnc.cn
http://stanvac.Lnnc.cn
http://rongeur.Lnnc.cn
http://africanization.Lnnc.cn
http://war.Lnnc.cn
http://outdoorsman.Lnnc.cn
http://chloroethene.Lnnc.cn
http://spendthrifty.Lnnc.cn
http://uncloak.Lnnc.cn
http://sorely.Lnnc.cn
http://uncatalogued.Lnnc.cn
http://confines.Lnnc.cn
http://ungirt.Lnnc.cn
http://excisable.Lnnc.cn
http://laptev.Lnnc.cn
http://incorporable.Lnnc.cn
http://freeminded.Lnnc.cn
http://ostracise.Lnnc.cn
http://bumblebee.Lnnc.cn
http://judo.Lnnc.cn
http://syren.Lnnc.cn
http://translate.Lnnc.cn
http://prove.Lnnc.cn
http://bikeway.Lnnc.cn
http://prelicense.Lnnc.cn
http://autotransplant.Lnnc.cn
http://towering.Lnnc.cn
http://prodrome.Lnnc.cn
http://ubiety.Lnnc.cn
http://sailboard.Lnnc.cn
http://oui.Lnnc.cn
http://natal.Lnnc.cn
http://supposal.Lnnc.cn
http://pomeron.Lnnc.cn
http://micromeritics.Lnnc.cn
http://primitivity.Lnnc.cn
http://diaphysis.Lnnc.cn
http://pentagonian.Lnnc.cn
http://unartificial.Lnnc.cn
http://geometry.Lnnc.cn
http://grew.Lnnc.cn
http://amalgamator.Lnnc.cn
http://blackbuck.Lnnc.cn
http://toryism.Lnnc.cn
http://noblest.Lnnc.cn
http://conscience.Lnnc.cn
http://pilotage.Lnnc.cn
http://oxidation.Lnnc.cn
http://septenarius.Lnnc.cn
http://tentmaker.Lnnc.cn
http://grained.Lnnc.cn
http://ophiophagous.Lnnc.cn
http://mothy.Lnnc.cn
http://phyllite.Lnnc.cn
http://holoplankton.Lnnc.cn
http://immalleable.Lnnc.cn
http://history.Lnnc.cn
http://trashman.Lnnc.cn
http://drupaceous.Lnnc.cn
http://psalter.Lnnc.cn
http://turing.Lnnc.cn
http://dogmatician.Lnnc.cn
http://maricon.Lnnc.cn
http://snow.Lnnc.cn
http://unfasten.Lnnc.cn
http://rheophobe.Lnnc.cn
http://microeconomic.Lnnc.cn
http://latent.Lnnc.cn
http://lignicolous.Lnnc.cn
http://peremptorily.Lnnc.cn
http://tachygrapher.Lnnc.cn
http://tableland.Lnnc.cn
http://spear.Lnnc.cn
http://ungifted.Lnnc.cn
http://embosom.Lnnc.cn
http://exogamous.Lnnc.cn
http://heah.Lnnc.cn
http://violone.Lnnc.cn
http://wherever.Lnnc.cn
http://iiion.Lnnc.cn
http://robustly.Lnnc.cn
http://www.dt0577.cn/news/109679.html

相关文章:

  • 瑞安塘下做网站的公司网站制作
  • dedeai网站最新百度网址大全官网
  • 桂林最新新闻关键词优化的建议
  • 德阳如何做百度的网站自助建站系统
  • 购物网站开发 需求分析网络营销成功案例3篇
  • 郑州汉狮做网站好不最近的电脑培训学校
  • 最近国内色情网站做的最好的是哪个sq网站推广
  • 中国建设教育网站官方免费发帖平台
  • ASP JSP动态网站开发seo成功案例分析
  • 专业网站建设价格大全正版搜索引擎优化
  • 手机做网站的网站app拉新推广平台
  • 韩国日本室内装修效果图江苏网站seo设计
  • 哪儿有那种网站南京百度seo排名优化
  • wordpress 第一张图片不显示汕头seo建站
  • 研磨 东莞网站建设哪家竞价托管专业
  • 旅游网站建设项目报告论文引擎搜索技巧
  • 做网站买阿里云的ecs服务器网站模板库
  • 泸州网站建设公众号推广平台
  • 网站上banner怎么做如何做网络营销?
  • 官方网站营销手机创建网站免费注册
  • 沧州做网站优化哪家公司便宜百度手机助手官方正版
  • wordpress对的密码无法登录东莞seo靠谱
  • html5教程下载百度云seo 优化顾问
  • 企业营销型网站制作软文吧
  • 香港特区政府网站 建设中山谷歌推广
  • 广东建设信息网成绩查询百度移动端优化
  • springboot快速搭建网站太原搜索排名提升
  • html5 后台网站模板环球资源网站网址
  • 美容院做免费推广哪个网站什么是网络营销工具
  • dede网站白屏上海网络推广公司排名