Search in sources :

Example 6 with Encoding

use of com.google.pubsub.v1.Encoding 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 7 with Encoding

use of com.google.pubsub.v1.Encoding in project java-pubsub by googleapis.

the class CreateTopicWithSchemaExample method createTopicWithSchemaExample.

public static void createTopicWithSchemaExample(String projectId, String topicId, String schemaId, Encoding encoding) throws IOException {
    TopicName topicName = TopicName.of(projectId, topicId);
    SchemaName schemaName = SchemaName.of(projectId, schemaId);
    SchemaSettings schemaSettings = SchemaSettings.newBuilder().setSchema(schemaName.toString()).setEncoding(encoding).build();
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        Topic topic = topicAdminClient.createTopic(Topic.newBuilder().setName(topicName.toString()).setSchemaSettings(schemaSettings).build());
        System.out.println("Created topic with schema: " + topic.getName());
    } catch (AlreadyExistsException e) {
        System.out.println(schemaName + "already exists.");
    }
}
Also used : AlreadyExistsException(com.google.api.gax.rpc.AlreadyExistsException) TopicAdminClient(com.google.cloud.pubsub.v1.TopicAdminClient) Topic(com.google.pubsub.v1.Topic) SchemaSettings(com.google.pubsub.v1.SchemaSettings) SchemaName(com.google.pubsub.v1.SchemaName) TopicName(com.google.pubsub.v1.TopicName)

Example 8 with Encoding

use of com.google.pubsub.v1.Encoding 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)6 PubsubMessage (com.google.pubsub.v1.PubsubMessage)5 TopicAdminClient (com.google.cloud.pubsub.v1.TopicAdminClient)4 Encoding (com.google.pubsub.v1.Encoding)4 TopicName (com.google.pubsub.v1.TopicName)4 ApiFuture (com.google.api.core.ApiFuture)3 Publisher (com.google.cloud.pubsub.v1.Publisher)3 AckReplyConsumer (com.google.cloud.pubsub.v1.AckReplyConsumer)2 MessageReceiver (com.google.cloud.pubsub.v1.MessageReceiver)2 Subscriber (com.google.cloud.pubsub.v1.Subscriber)2 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 TimeoutException (java.util.concurrent.TimeoutException)2 Encoder (org.apache.avro.io.Encoder)2 State (utilities.State)2 State (utilities.StateProto.State)2 Customer (com.avro.Customer)1 AlreadyExistsException (com.google.api.gax.rpc.AlreadyExistsException)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 SchemaName (com.google.pubsub.v1.SchemaName)1