Search in sources :

Example 1 with State

use of utilities.StateProto.State in project java-pubsub by googleapis.

the class PublishProtobufMessagesExample method publishProtobufMessagesExample.

public static void publishProtobufMessagesExample(String projectId, String topicId) throws IOException, ExecutionException, InterruptedException {
    Encoding encoding = null;
    TopicName topicName = TopicName.of(projectId, topicId);
    // Get the topic encoding type.
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        encoding = topicAdminClient.getTopic(topicName).getSchemaSettings().getEncoding();
    }
    Publisher publisher = null;
    // Instantiate a protoc-generated class defined in `us-states.proto`.
    State state = State.newBuilder().setName("Alaska").setPostAbbr("AK").build();
    block: try {
        publisher = Publisher.newBuilder(topicName).build();
        PubsubMessage.Builder message = PubsubMessage.newBuilder();
        // Prepare an appropriately formatted message based on topic encoding.
        switch(encoding) {
            case BINARY:
                message.setData(state.toByteString());
                System.out.println("Publishing a BINARY-formatted message:\n" + message);
                break;
            case JSON:
                String jsonString = JsonFormat.printer().omittingInsignificantWhitespace().print(state);
                message.setData(ByteString.copyFromUtf8(jsonString));
                System.out.println("Publishing a JSON-formatted message:\n" + message);
                break;
            default:
                break block;
        }
        // Publish the message.
        ApiFuture<String> future = publisher.publish(message.build());
        System.out.println("Published message ID: " + future.get());
    } finally {
        if (publisher != null) {
            publisher.shutdown();
            publisher.awaitTermination(1, TimeUnit.MINUTES);
        }
    }
}
Also used : ApiFuture(com.google.api.core.ApiFuture) TopicAdminClient(com.google.cloud.pubsub.v1.TopicAdminClient) State(utilities.StateProto.State) Encoding(com.google.pubsub.v1.Encoding) Publisher(com.google.cloud.pubsub.v1.Publisher) ByteString(com.google.protobuf.ByteString) TopicName(com.google.pubsub.v1.TopicName)

Example 2 with State

use of utilities.StateProto.State in project java-pubsub by googleapis.

the class SubscribeWithProtoSchemaExample method subscribeWithProtoSchemaExample.

public static void subscribeWithProtoSchemaExample(String projectId, String subscriptionId) {
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
    MessageReceiver receiver = (PubsubMessage message, AckReplyConsumer consumer) -> {
        ByteString data = message.getData();
        // Get the schema encoding type.
        String encoding = message.getAttributesMap().get("googclient_schemaencoding");
        block: try {
            switch(encoding) {
                case "BINARY":
                    // Obtain an object of the generated proto class.
                    State state = State.parseFrom(data);
                    System.out.println("Received a BINARY-formatted message: " + state);
                    break;
                case "JSON":
                    State.Builder stateBuilder = State.newBuilder();
                    JsonFormat.parser().merge(data.toStringUtf8(), stateBuilder);
                    System.out.println("Received a JSON-formatted message:" + stateBuilder.build());
                    break;
                default:
                    break block;
            }
        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        }
        consumer.ack();
        System.out.println("Ack'ed the message");
    };
    // Create subscriber client.
    Subscriber subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
    try {
        subscriber.startAsync().awaitRunning();
        System.out.printf("Listening for messages on %s:\n", subscriptionName);
        subscriber.awaitTerminated(30, TimeUnit.SECONDS);
    } catch (TimeoutException timeoutException) {
        subscriber.stopAsync();
    }
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) MessageReceiver(com.google.cloud.pubsub.v1.MessageReceiver) Subscriber(com.google.cloud.pubsub.v1.Subscriber) ByteString(com.google.protobuf.ByteString) State(utilities.StateProto.State) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteString(com.google.protobuf.ByteString) AckReplyConsumer(com.google.cloud.pubsub.v1.AckReplyConsumer) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

ByteString (com.google.protobuf.ByteString)2 State (utilities.StateProto.State)2 ApiFuture (com.google.api.core.ApiFuture)1 AckReplyConsumer (com.google.cloud.pubsub.v1.AckReplyConsumer)1 MessageReceiver (com.google.cloud.pubsub.v1.MessageReceiver)1 Publisher (com.google.cloud.pubsub.v1.Publisher)1 Subscriber (com.google.cloud.pubsub.v1.Subscriber)1 TopicAdminClient (com.google.cloud.pubsub.v1.TopicAdminClient)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Encoding (com.google.pubsub.v1.Encoding)1 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)1 PubsubMessage (com.google.pubsub.v1.PubsubMessage)1 TopicName (com.google.pubsub.v1.TopicName)1 TimeoutException (java.util.concurrent.TimeoutException)1