Programmatically set PubSub filter on subscription

I create a GCP PubSub subscription like that

    TopicName topic = TopicName.ofProjectTopicName(projectId, this.topic);
    try {
        client.createSubscription(subscriptionId, topic, PushConfig.getDefaultInstance(), 10);
    } catch (AlreadyExistsException e) {
        // sub is already there. nothing to do
    }

Is it programmatically possible to add filters to the subscription? I know its possible in the google console but I would prefer to have it bootstrapped in the code.


Solution 1:

Found it out:

        Subscription subscription = Subscription.newBuilder()
                .setName("projects/xyz/subscriptions/foo")
                .setTopic(topic.toString())
                .setPushConfig(PushConfig.getDefaultInstance())
                .setAckDeadlineSeconds(10)
                .setFilter("attributes.A = \"a\"")
                .build();
        client.createSubscription(subscription);