【发布时间】:2022-08-24 05:31:45
【问题描述】:
我在我的项目中安装了 PubSub 依赖项。
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-pubsub</artifactId>
</dependency>
我实现了TopicChannel 和SubscriptionChannel:
@Configuration
@Slf4j
public class SubscriberChannel {
private final String subscription = \"test-sub\";
@Autowired
private DeviceRepository deviceRepository;
@Autowired
private RfidReaderRepository rfidReaderRepository;
@Autowired
private BagDetectionRepository bagDetectionRepository;
@Autowired
private PersonDetectionRepository personDetectionRepository;
@Bean
@ServiceActivator(inputChannel = \"pubsubInputChannel\")
public MessageHandler messageReceiver() {
return message -> {
String payload = (String) message.getPayload();
long start = System.currentTimeMillis();
log.info(\"Message arrived! Payload: \" + payload);
BasicAcknowledgeablePubsubMessage originalMessage =
message.getHeaders().get(GcpPubSubHeaders.ORIGINAL_MESSAGE, BasicAcknowledgeablePubsubMessage.class);
originalMessage.ack();
};
}
@Bean
public PubSubInboundChannelAdapter messageChannelAdapter(
@Qualifier(\"pubsubInputChannel\") MessageChannel inputChannel,
PubSubTemplate pubSubTemplate) {
PubSubInboundChannelAdapter adapter =
new PubSubInboundChannelAdapter(pubSubTemplate, subscription);
adapter.setErrorChannelName(\"pubsubErrors\");
adapter.setOutputChannel(inputChannel);
adapter.setAckMode(AckMode.MANUAL);
return adapter;
}
@ServiceActivator(inputChannel = \"pubsubErrors\")
public void pubsubErrorHandler(Message<MessagingException> exceptionMessage) {
BasicAcknowledgeablePubsubMessage originalMessage =
(BasicAcknowledgeablePubsubMessage) exceptionMessage.getPayload().getFailedMessage()
.getHeaders().get(GcpPubSubHeaders.ORIGINAL_MESSAGE);
originalMessage.nack();
}
@Bean
public MessageChannel pubsubInputChannel() {
return new DirectChannel();
}
@Bean
@InboundChannelAdapter(channel = \"pubsubInputChannel\", poller = @Poller(fixedDelay = \"100\"))
public MessageSource<Object> pubsubAdapter(PubSubTemplate pubSubTemplate) {
PubSubMessageSource messageSource = new PubSubMessageSource(pubSubTemplate, subscription);
messageSource.setAckMode(AckMode.MANUAL);
messageSource.setPayloadType(String.class);
messageSource.setBlockOnPull(true);
messageSource.setMaxFetchSize(20 * 1024 * 1024);
return messageSource;
}
}
话题频道
@Configuration
public class TopicChannel {
private final String topic = \"testt\";
@Bean
@ServiceActivator(inputChannel = \"pubsubOutputChannel\")
public MessageHandler messageSender(PubSubTemplate pubsubTemplate) {
return new PubSubMessageHandler(pubsubTemplate, topic);
}
@MessagingGateway(defaultRequestChannel = \"pubsubOutputChannel\")
public interface PubsubOutboundGateway {
void sendToPubsub(String text);
}
}
运行应用程序时出现此错误
org.springframework.beans.factory.UnsatisfiedDependencyException: 创建类中定义的名称为 \'messageChannelAdapter\' 的 bean 时出错 路径资源 [com/payfree/bftConfiguration/pubsub/SubscriberChannel.class]: 通过方法表达的不满足的依赖关系 \'messageChannelAdapter\' 参数1;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建类路径中定义的名称为 \'pubSubTemplate\' 的 bean 时出错 资源 [com/google/cloud/spring/autoconfigure/pubsub/GcpPubSubAutoConfiguration.class]: 通过方法 \'pubSubTemplate\' 表达的不满足的依赖关系 参数0;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名称为 \'pubSubPublisherTemplate\' 中定义的 bean 时出错 类路径资源 [com/google/cloud/spring/autoconfigure/pubsub/GcpPubSubAutoConfiguration.class]: 通过方法表达的不满足的依赖关系 \'pubSubPublisherTemplate\' 参数 0;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建类中定义的名称为 \'defaultPublisherFactory\' 的 bean 路径资源 [com/google/cloud/spring/autoconfigure/pubsub/GcpPubSubAutoConfiguration.class]: 通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化 [com.google.cloud.spring.pubsub.support.PublisherFactory]: 工厂方法\'defaultPublisherFactory\'抛出异常;嵌套的 异常是 java.lang.IllegalArgumentException: 项目 ID 不能 为空或为空。
我该如何解决这个错误?
-
你可以试试那个订阅格式:
projects/<projectID>/subscriptions/test-sub而不是test-sub吗? (用您的项目 ID 替换<projectID>) -
您在类路径上有任何其他 spring-cloud-gcp 依赖项吗?
标签: spring-boot google-cloud-platform publish-subscribe google-cloud-pubsub spring-cloud-gcp