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

自己做网站可行吗直接在原备案号下增加新网站

自己做网站可行吗,直接在原备案号下增加新网站,毕节网站网站建设,高水平的大连网站建设rabbitmq的stream: 4、对比 autoTrackingstrategy方式:始终监听Stream中的新消息(狗狗看家,忠于职守)指定偏移量方式:针对指定偏移量的消息消费之后就停止(狗狗叼飞盘,回来就完) 这两种分别怎么写?java 在 RabbitMQ 中&#xff0c…

rabbitmq的stream:
4、对比
autoTrackingstrategy方式:始终监听Stream中的新消息(狗狗看家,忠于职守)指定偏移量方式:针对指定偏移量的消息消费之后就停止(狗狗叼飞盘,回来就完)
这两种分别怎么写?java


在 RabbitMQ 中,Stream 是一种新的消息传递机制,允许高吞吐量和低延迟的消息传递。你提到的两种消费方式分别是自动跟踪策略(Auto Tracking Strategy)和指定偏移量(Offset)的消费方式。下面我们将分别演示这两种方式的 Java 实现。

### 1. 自动跟踪策略(Auto Tracking Strategy)

这种方式下,消费者会一直监听 Stream 中的新消息,并不断处理这些消息。

#### 示例代码

```java
import com.rabbitmq.stream.Consumer;
import com.rabbitmq.stream.Environment;
import com.rabbitmq.stream.Message;
import com.rabbitmq.stream.OffsetSpecification;
import com.rabbitmq.stream.ConsumerBuilder;

public class AutoTrackingConsumer {
    public static void main(String[] args) {
        Environment environment = Environment.builder().host("localhost").build();

        String streamName = "test_stream";

        // 创建消费者
        Consumer consumer = environment.consumerBuilder()
            .stream(streamName)
            .offset(OffsetSpecification.first()) // 从第一个消息开始
            .messageHandler((context, message) -> {
                String body = new String(message.getBodyAsBinary());
                System.out.println("Received message: " + body);
            })
            .build();

        // 消费者会一直运行,监听新的消息
        System.out.println("Consumer is listening for new messages...");
    }
}
```

### 2. 指定偏移量(Offset)的消费方式

这种方式下,消费者会从指定的偏移量开始消费消息,并在消费完这些消息后停止。

#### 示例代码

```java
import com.rabbitmq.stream.Consumer;
import com.rabbitmq.stream.Environment;
import com.rabbitmq.stream.Message;
import com.rabbitmq.stream.OffsetSpecification;
import com.rabbitmq.stream.ConsumerBuilder;

public class OffsetConsumer {
    public static void main(String[] args) {
        Environment environment = Environment.builder().host("localhost").build();

        String streamName = "test_stream";
        long offset = 10; // 指定偏移量

        // 创建消费者
        Consumer consumer = environment.consumerBuilder()
            .stream(streamName)
            .offset(OffsetSpecification.offset(offset)) // 从指定偏移量开始
            .messageHandler((context, message


============
为什么没写.autoTrackingStrategy()?

你提到的 `.autoTrackingStrategy()` 是 RabbitMQ Stream API 中的一个重要方法,它用于指定消费者的自动跟踪策略。让我们详细解释一下如何在代码中使用这个方法。

### 自动跟踪策略(Auto Tracking Strategy)

自动跟踪策略允许消费者始终监听 Stream 中的新消息,并自动跟踪消息的偏移量。这种方式下,消费者会持续消费新消息,而不会停止。

#### 示例代码

```java
import com.rabbitmq.stream.Consumer;
import com.rabbitmq.stream.Environment;
import com.rabbitmq.stream.Message;
import com.rabbitmq.stream.OffsetSpecification;
import com.rabbitmq.stream.ConsumerBuilder;

public class AutoTrackingConsumer {
    public static void main(String[] args) {
        Environment environment = Environment.builder().host("localhost").build();

        String streamName = "test_stream";

        // 创建消费者
        Consumer consumer = environment.consumerBuilder()
            .stream(streamName)
            .offset(OffsetSpecification.first()) // 从第一个消息开始
            .autoTrackingStrategy() // 启用自动跟踪策略
            .messageHandler((context, message) -> {
                String body = new String(message.getBodyAsBinary());
                System.out.println("Received message: " + body);
            })
            .build();

        // 消费者会一直运行,监听新的消息
        System.out.println("Consumer is listening for new messages...");
    }
}
```

### 指定偏移量(Offset)的消费方式

这种方式下,消费者会从指定的偏移量开始消费消息,并在消费完这些消息后停止。

#### 示例代码

```java
import com.rabbitmq.stream.Consumer;
import com.rabbitmq.stream.Environment;
import com.rabbitmq.stream.Message;
import com.rabbitmq.stream.OffsetSpecification;
import com.rabbitmq.stream.ConsumerBuilder;

public class OffsetConsumer {
    public static void main(String[] args) {
        Environment environment = Environment.builder().host("localhost").build();

        String streamName = "test_stream";
        long offset = 10; // 指定偏移量

        // 创建消费者
        Consumer consumer = environment.consumerBuilder()
            .stream(streamName)
            .offset(OffsetSpecification.offset(offset)) // 从指定偏移量开始
            .messageHandler((context, message) -> {


============
            .offset(OffsetSpecification.first()) // 从第一个消息开始
            .autoTrackingStrategy() /
-----这里指定了 .autoTrackingStrategy() /就不用指定.offset(OffsetSpecification.first())了吧?


确实,如果你使用 `.autoTrackingStrategy()`,那么你不需要再指定 `.offset(OffsetSpecification.first())`,因为自动跟踪策略会自动从最新的消息开始消费并持续监听新的消息。让我们分别看看两种方式的完整代码示例。

### 自动跟踪策略(Auto Tracking Strategy)

这种方式下,消费者会自动从最新的消息开始消费,并持续监听新的消息。

#### 示例代码

```java
import com.rabbitmq.stream.Consumer;
import com.rabbitmq.stream.Environment;
import com.rabbitmq.stream.Message;
import com.rabbitmq.stream.ConsumerBuilder;

public class AutoTrackingConsumer {
    public static void main(String[] args) {
        Environment environment = Environment.builder().host("localhost").build();

        String streamName = "test_stream";

        // 创建消费者
        Consumer consumer = environment.consumerBuilder()
            .stream(streamName)
            .autoTrackingStrategy() // 启用自动跟踪策略
            .messageHandler((context, message) -> {
                String body = new String(message.getBodyAsBinary());
                System.out.println("Received message: " + body);
            })
            .build();

        // 消费者会一直运行,监听新的消息
        System.out.println("Consumer is listening for new messages...");
    }
}
```

### 指定偏移量(Offset)的消费方式

这种方式下,消费者会从指定的偏移量开始消费消息,并在消费完这些消息后停止。

#### 示例代码

```java
import com.rabbitmq.stream.Consumer;
import com.rabbitmq.stream.Environment;
import com.rabbitmq.stream.Message;
import com.rabbitmq.stream.OffsetSpecification;
import com.rabbitmq.stream.ConsumerBuilder;

public class OffsetConsumer {
    public static void main(String[] args) {
        Environment environment = Environment.builder().host("localhost").build();

        String streamName = "test_stream";
        long offset = 10; // 指定偏移量

        // 创建消费者
        Consumer consumer = environment.consumerBuilder()
            .stream(streamName)
            .offset(OffsetSpecification.offset(offset)) // 从指定偏移量开始
            .messageHandler((context, message) -> {
                String body = new String(message.getBodyAsBinary());
                System.out.println("Received message: " + body);
           


============
那里之前为什么要写:            .offset(OffsetSpecification.first()) // 从第一个消息开始
            .autoTrackingStrategy() /

你说得对,如果你使用 `.autoTrackingStrategy()`,就不需要再指定 `.offset(OffsetSpecification.first())`。这是一个错误的示例。使用 `.autoTrackingStrategy()` 时,消费者会自动从最新的消息开始消费,并持续监听新的消息。

让我们修正并简化这两种方式的示例代码:

### 自动跟踪策略(Auto Tracking Strategy)

这种方式下,消费者会自动从最新的消息开始消费,并持续监听新的消息。

#### 示例代码

```java
import com.rabbitmq.stream.Consumer;
import com.rabbitmq.stream.Environment;
import com.rabbitmq.stream.Message;
import com.rabbitmq.stream.ConsumerBuilder;

public class AutoTrackingConsumer {
    public static void main(String[] args) {
        Environment environment = Environment.builder().host("localhost").build();

        String streamName = "test_stream";

        // 创建消费者
        Consumer consumer = environment.consumerBuilder()
            .stream(streamName)
            .autoTrackingStrategy() // 启用自动跟踪策略
            .messageHandler((context, message) -> {
                String body = new String(message.getBodyAsBinary());
                System.out.println("Received message: " + body);
            })
            .build();

        // 消费者会一直运行,监听新的消息
        System.out.println("Consumer is listening for new messages...");
    }
}
```

### 指定偏移量(Offset)的消费方式

这种方式下,消费者会从指定的偏移量开始消费消息,并在消费完这些消息后停止。

#### 示例代码

```java
import com.rabbitmq.stream.Consumer;
import com.rabbitmq.stream.Environment;
import com.rabbitmq.stream.Message;
import com.rabbitmq.stream.OffsetSpecification;
import com.rabbitmq.stream.ConsumerBuilder;

public class OffsetConsumer {
    public static void main(String[] args) {
        Environment environment = Environment.builder().host("localhost").build();

        String streamName = "test_stream";
        long offset = 10; // 指定偏移量

        // 创建消费者
        Consumer consumer = environment.consumerBuilder()
            .stream(streamName)
            .offset(OffsetSpecification.offset(offset)) // 从指定偏移量开始
            .messageHandler((context, message) -> {
                String body = new String(message.getBodyAs

http://www.yayakq.cn/news/894383/

相关文章:

  • 河北建设集团有限公司 信息化网站吉林省城乡建设官方网站
  • 郎溪县建设局网站上海哪家优化公司好
  • 企业备案网站可以做论坛吗网站开发 图片储存
  • 网站建设框架图安徽六安特产有哪些
  • 上海平台网站建设哪家有提升学历的意义
  • 用python怎么做网站网站页面seo
  • intitle 郑州网站建设wordpress layer 主题
  • 赣县区建设局网站手机网站源文件
  • 制作网站免费wordpress 图片剪裁
  • 韩国展厅设计网站互联网创业项目整合网站
  • 海南省住房和城乡建设厅官网网站首页比较好的做网站
  • 怎么建一个小说网站wordpress栏目
  • 什么对网站建设起到计划和指导作用企业网站的建设论文
  • Wordpress 源码 商城南京seo关键词优化预订
  • 音乐网站开发文档源码分享
  • 手机网站建站软件wordpress 社交插件
  • 做论坛网站需要多大空间建设银行江苏省行网站
  • 摄影网站开题报告企业网站建设不足
  • 对方把我的网站他网站内页友情链接 站长工具检测到是无反链推推蛙seo
  • html5风格网站特色怎么做网站推广知乎
  • 东圃做网站常州商城网站制作公司
  • 北京网站建站推广2003系统做网站
  • 网站布局设计怎么写个人网站经营 合法么
  • 上传网站到二级域名百度seo点击排名优化
  • 医院网站设计模板苏州网站设计选哪家
  • 江苏网站建设公司h5编辑软件
  • 广东微信网站建设哪家专业浙江建设工程信息网高工评选
  • 学校响应式网站模板网页制作怎么下载
  • 电影网站制作教程销售管理系统实验报告
  • 网站维护中页面模板什么是网络营销促销