I installed the PubSub dependency in my project.
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-pubsub</artifactId>
</dependency>
I implemented a TopicChannel
and 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;
}
}
The topic channel
@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);
}
}
When run the app I get this error
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'messageChannelAdapter' defined in class path resource [com/payfree/bftConfiguration/pubsub/SubscriberChannel.class]: Unsatisfied dependency expressed through method 'messageChannelAdapter' parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pubSubTemplate' defined in class path resource [com/google/cloud/spring/autoconfigure/pubsub/GcpPubSubAutoConfiguration.class]: Unsatisfied dependency expressed through method 'pubSubTemplate' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pubSubPublisherTemplate' defined in class path resource [com/google/cloud/spring/autoconfigure/pubsub/GcpPubSubAutoConfiguration.class]: Unsatisfied dependency expressed through method 'pubSubPublisherTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultPublisherFactory' defined in class path resource [com/google/cloud/spring/autoconfigure/pubsub/GcpPubSubAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.google.cloud.spring.pubsub.support.PublisherFactory]: Factory method 'defaultPublisherFactory' threw exception; nested exception is java.lang.IllegalArgumentException: The project ID can't be null or empty.
How can I fix this error?
Set the projectId using gcloud-cli
gcloud config set project <your-project-id>
reference: https://cloud.google.com/sdk/gcloud/reference/config/set
Install gcloud-cli if not already on your system. Link: https://cloud.google.com/sdk/docs/install