codecamp

Spring Cloud 入站通道适配器

PubSubInboundChannelAdapter是GCP发布/订阅的入站通道适配器,它侦听GCP发布/订阅的新消息。它将新消息转换为内部Spring Message,然后将其发送到绑定的输出通道。

Google Pub / Sub将消息有效负载视为字节数组。因此,默认情况下,入站通道适配器将使用byte[]作为有效载荷来构造Spring Message但是,可以通过设置PubSubInboundChannelAdapterpayloadType属性来更改所需的有效负载类型。 PubSubInboundChannelAdapter将对所需有效负载类型的转换委派给在PubSubTemplate中配置的PubSubMessageConverter

要使用入站通道适配器,必须在用户应用程序端提供PubSubInboundChannelAdapter并对其进行配置。

@Bean
public MessageChannel pubsubInputChannel() {
    return new PublishSubscribeChannel();
}

@Bean
public PubSubInboundChannelAdapter messageChannelAdapter(
    @Qualifier("pubsubInputChannel") MessageChannel inputChannel,
    SubscriberFactory subscriberFactory) {
    PubSubInboundChannelAdapter adapter =
        new PubSubInboundChannelAdapter(subscriberFactory, "subscriptionName");
    adapter.setOutputChannel(inputChannel);
    adapter.setAckMode(AckMode.MANUAL);

    return adapter;
}

在示例中,我们首先指定适配器将向其写入传入消息的MessageChannelMessageChannel的实现在这里并不重要。根据您的用例,您可能需要使用MessageChannel而非PublishSubscribeChannel

然后,我们声明PubSubInboundChannelAdapter bean。它需要我们刚创建的通道和一个SubscriberFactory,该SubscriberFactory从Google Cloud Java Client for Pub / Sub创建Subscriber对象。 GCP Pub / Sub的Spring Boot入门程序提供了已配置的SubscriberFactory

PubSubInboundChannelAdapter支持三种确认模式,其中AckMode.AUTO是默认值。

自动确认(AckMode.AUTO

如果适配器将消息发送到通道,并且未引发任何异常,则消息将被GCP发布/订阅确认。如果在处理邮件时抛出RuntimeException,则该邮件将被否定。

自动确认确认(AckMode.AUTO_ACK

如果适配器将消息发送到通道,并且未引发任何异常,则消息将被GCP发布/订阅确认。如果在处理消息时抛出RuntimeException,则消息既不会被确认也不会被拒绝。

当使用订阅的确认截止时间超时作为重试传递回退机制时,此功能很有用。

手动确认(AckMode.MANUAL

适配器将BasicAcknowledgeablePubsubMessage对象附加到Message标头。用户可以使用GcpPubSubHeaders.ORIGINAL_MESSAGE键提取BasicAcknowledgeablePubsubMessage,并将其用于(n)确认消息。

@Bean
@ServiceActivator(inputChannel = "pubsubInputChannel")
public MessageHandler messageReceiver() {
    return message -> {
        LOGGER.info("Message arrived! Payload: " + new String((byte[]) message.getPayload()));
        BasicAcknowledgeablePubsubMessage originalMessage =
              message.getHeaders().get(GcpPubSubHeaders.ORIGINAL_MESSAGE, BasicAcknowledgeablePubsubMessage.class);
        originalMessage.ack();
    };
}
Spring Cloud Spring Integration
Spring Cloud 出站通道适配器
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

三、Spring Cloud Netflix

SpringCloud Hystrix超时和Ribbon客户

SpringCloud 重试失败的请求

五、Spring Cloud Stream

六、SpringCloud Binder实现

SpringCloud 重试RabbitMQ Binder

SpringCloud Dead-Letter队列处理

八、Spring Cloud Sleuth

SpringCloud 当前Span

十二、Spring Cloud for Cloud Foundry

十三、Spring Cloud Contract

Spring Cloud Contract验证程序设置

SrpingCloud Gradle项目

十五、Spring Cloud网关

Spring Cloud 配置路由谓词工厂和网关过滤工厂

Spring Cloud TLS / SSL

Spring Cloud网关配置

SpringCloud 故障排除

十八、Spring Cloud GCP

Spring Cloud GCP Spring资源

Spring Cloud Spring JDBC

Spring Cloud Redis的Cloud Memorystore

Spring Cloud 云身份识别代理(IAP)身份验证

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }