-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Hey there,
I stumbled on this repo looking for some examples of Google Subscription API implementation and noticed how you parse a message in PaymentController. Instead, I would suggest you too look at spring integration where you get parsing out of the box and you can bypass decoding instead, you can just deserialze from json string.
Additionally, when you registered AndroidPublisher bean, you can potentially simplify authentication part. The way I did it, is I created a service account with managed permissions and store the credentials file in AWS SM
Sharing you example how I did below in case you want to do:
@Configuration
class GoogleConfig(
@Value("\${spring.cloud.gcp.pubsub.billing-subscription}")
private val billingSubscription: String,
private val awsSecretsManagerService: AwsSecretsManagerService
) {
private val jsonFactory = GsonFactory.getDefaultInstance()
private val httpTransport = GoogleNetHttpTransport.newTrustedTransport()
@Bean
fun androidPublisher(
credentialsProvider: CredentialsProvider
): AndroidPublisher {
return AndroidPublisher.Builder(httpTransport, jsonFactory, HttpCredentialsAdapter(credentialsProvider.credentials))
.setApplicationName("project-name")
.build()
}
@Bean
fun pubsubInputChannel(): MessageChannel {
return DirectChannel()
}
@Bean
fun messageChannelAdapter(
//Declaring a qualifier for a message handling defined in BillingSubscriber
@Qualifier("pubsubInputChannel") inputChannel: MessageChannel?,
pubSubTemplate: PubSubTemplate?
): PubSubInboundChannelAdapter {
val adapter = PubSubInboundChannelAdapter(pubSubTemplate, billingSubscription)
adapter.outputChannel = inputChannel
adapter.setAckMode(AckMode.MANUAL)
return adapter
}
@Bean
fun credentialsProvider(): CredentialsProvider {
val credentialsStream = awsSecretsManagerService.getSecrets().gcpCredentials.byteInputStream()
val credentials = GoogleCredentials.fromStream(credentialsStream)
return CredentialsProvider { credentials }
}
}
BillinSubscriber.kt
@Component
class BillingSubscriber(
private val objectMapper: ObjectMapper
) {
@Bean
@ServiceActivator(inputChannel = "pubsubInputChannel")
fun messageReceiver(): MessageHandler {
return MessageHandler { message ->
val messageString = String((message.payload as ByteArray))
val originalMessage = GcpPubSubHeaders.getOriginalMessage(message)
if (originalMessage.isPresent) {
val notification = objectMapper.readValue(messageString, DeveloperNotification::class.java)
// Handle published message
originalMessage.get().ack()
} else {
logger.error { "Message can not be acknowledged" }
}
}
}
}
Cheers
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels