0

My aim is to get notifications from google container registry in code whenever any image is updated/inserted/deleted from the registry.

I am following tutorial - https://cloud.google.com/container-registry/docs/configuring-notifications

I am able to pull notification messages from the registry using the google console using command - gcloud alpha pubsub subscriptions pull SUBSCRIPTION

But I want these notification messages to be delivered in code (in java).

If someone can give me any reference to any article or tutorial that will help.

After comment from dsesto i have added following code. This code gave me some messages when i run first. But after that i kept application running and tried to delete/insert images from container registry but it did not gave any message. Any suggestions.

package com.avaya.ipoffice.mcm.googleconnect;

import org.springframework.stereotype.Service;

import com.google.cloud.pubsub.v1.AckReplyConsumer;
import com.google.cloud.pubsub.v1.MessageReceiver;
import com.google.cloud.pubsub.v1.Subscriber;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.PubsubMessage;

@Service
public class RecieveMessagesUtil {      
    public static void main(String... args) throws Exception {

        String projectId = "xxxxx";
        String subscriptionId = "prashantsub";

        ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
        // Instantiate an asynchronous message receiver
        MessageReceiver receiver = new MessageReceiver() {
            @Override
            public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
                // handle incoming message, then ack/nack the received message
                System.out.println("Id : " + message.getMessageId());
                System.out.println("Data : " + message.getData().toStringUtf8());
                consumer.ack();
            }
        };

        Subscriber subscriber = null;
        try {
            // Create a subscriber for "my-subscription-id" bound to the message receiver
            subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
            subscriber.startAsync();
            // ...
        } catch (Exception e) {
            System.out.println("Exception while subscribing" + e);
        } finally {
            // stop receiving messages
            if (subscriber != null) {
                subscriber.stopAsync();
            }
        }
    }
}
questp
  • 133
  • 1
  • 3
  • 11
  • There is no need to share Project IDs, topic/subscription names or any other type of information belonging to your specific use case. Please consider obfuscating them with a placeholder like `PROJECT_ID`. – dsesto Jul 09 '18 at 13:51
  • By having a look at your code, I see that you do not keep listening, so I suspect you may only be listening to the first bunch of msgs, and then do not retrieve more. Why don't you try using the [example provided in the official repository](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/pubsub/cloud-client/src/main/java/com/example/pubsub/SubscriberExample.java)? Have you checked the comments I posted in my answer? Also, you have posted a new question in this post, being the first one already answered. Would you consider posting a different one for the new issues? – dsesto Jul 09 '18 at 14:21

2 Answers2

0

I would recommend looking at the Cloud Pub/Sub documentation, particularly, the subscriber guide, which gives you the code necessary to receive messages in Java.

Kamal Aboul-Hosn
  • 15,111
  • 1
  • 34
  • 46
0

From your question I understand that you have already successfully setup the notifications system of Container Registry using Pub/Sub topics and subscriptions, given that you said that you are already able to retrieve the messages from your Pub/Sub subscription using command gcloud pubsub subscriptions pull. Therefore, it looks like your concern is mostly related to pulling messages from a Subscription programatically.

First of all, I would recommend you to have a look at this documentation page about Subscribers in Pub/Sub. Especially, have a look at the Pull vs. Push comparison, where you will have a better idea of the possibilities available, and you should first decide whether to work with a Pull Subscription (such as the one you are using when calling the gcloud command, where the application initiates requests) or with a Push Subscription (where Pub/Sub initiates the requests to a subscriber application, such as an App Engine application).

Once that is clear (and assuming that you go for the Pull Subscription, which you are already using with gcloud), you can have a look at the documentation on how to perform Asynchronous Pull operations, with a Java-based example. Additionally, you can have a look at the complete subscriber example available in GitHub.

Finally, you should have a look at the Client Libraries documentation, more specifically the Pub/Sub Java reference, where you will find the complete documentation for the Pub/Sub Client Libraries used to work with Pub/Sub programatically.

dsesto
  • 7,864
  • 2
  • 33
  • 50
  • Thank you very much for your response. I tried following code it worked once and gave me multiple messages but after that even though i have deleted/inserted container registry image it is not giving me any message. Please note i have kept application running. – questp Jul 09 '18 at 13:25
  • I am glad I could help! If the answer was useful for you, please consider [accepting and upvoting](https://stackoverflow.com/help/someone-answers) my answer so that the community sees it solved your issue. – dsesto Jul 09 '18 at 13:47
  • Regarding your followup question, can you check if messages are being published into your topic by stopping the Java application and using the `gcloud` command to pull them directly from the subscription? Additionally, you can work with [Stackdriver Monitoring](https://app.google.stackdriver.com/metrics-explorer) to see the message publications in the topic and the state of your subscriptions. If the issue about pulling programatically is solved, you may consider creating a new question for this other issue, if you do not manage to identify the issue. – dsesto Jul 09 '18 at 13:48