Search in sources :

Example 1 with State

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

the class SubscribeWithAvroSchemaExample method subscribeWithAvroSchemaExample.

public static void subscribeWithAvroSchemaExample(String projectId, String subscriptionId) {
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
    // Prepare a reader for the encoded Avro records.
    SpecificDatumReader<State> reader = new SpecificDatumReader<>(State.getClassSchema());
    // Instantiate an asynchronous message receiver.
    MessageReceiver receiver = (PubsubMessage message, AckReplyConsumer consumer) -> {
        ByteString data = message.getData();
        // Get the schema encoding type.
        String encoding = message.getAttributesMap().get("googclient_schemaencoding");
        // Send the message data to a byte[] input stream.
        InputStream inputStream = new ByteArrayInputStream(data.toByteArray());
        Decoder decoder = null;
        // based on the schema encoding type.
        block: try {
            switch(encoding) {
                case "BINARY":
                    decoder = DecoderFactory.get().directBinaryDecoder(inputStream, /*reuse=*/
                    null);
                    System.out.println("Receiving a binary-encoded message:");
                    break;
                case "JSON":
                    decoder = DecoderFactory.get().jsonDecoder(State.getClassSchema(), inputStream);
                    System.out.println("Receiving a JSON-encoded message:");
                    break;
                default:
                    break block;
            }
            // Obtain an object of the generated Avro class using the decoder.
            State state = reader.read(null, decoder);
            System.out.println(state.getName() + " is abbreviated as " + state.getPostAbbr());
        } catch (IOException e) {
            System.err.println(e);
        }
        // Ack the message.
        consumer.ack();
    };
    Subscriber subscriber = null;
    try {
        subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
        subscriber.startAsync().awaitRunning();
        System.out.printf("Listening for messages on %s:\n", subscriptionName.toString());
        subscriber.awaitTerminated(30, TimeUnit.SECONDS);
    } catch (TimeoutException timeoutException) {
        subscriber.stopAsync();
    }
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) ByteString(com.google.protobuf.ByteString) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) Decoder(org.apache.avro.io.Decoder) PubsubMessage(com.google.pubsub.v1.PubsubMessage) MessageReceiver(com.google.cloud.pubsub.v1.MessageReceiver) ByteArrayInputStream(java.io.ByteArrayInputStream) Subscriber(com.google.cloud.pubsub.v1.Subscriber) State(utilities.State) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) AckReplyConsumer(com.google.cloud.pubsub.v1.AckReplyConsumer) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with State

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

the class PublishAvroRecordsExample method publishAvroRecordsExample.

public static void publishAvroRecordsExample(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();
    }
    // Instantiate an avro-tools-generated class defined in `us-states.avsc`.
    State state = State.newBuilder().setName("Alaska").setPostAbbr("AK").build();
    Publisher publisher = null;
    block: try {
        publisher = Publisher.newBuilder(topicName).build();
        // Prepare to serialize the object to the output stream.
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        Encoder encoder = null;
        // Prepare an appropriate encoder for publishing to the topic.
        switch(encoding) {
            case BINARY:
                System.out.println("Preparing a BINARY encoder...");
                encoder = EncoderFactory.get().directBinaryEncoder(byteStream, /*reuse=*/
                null);
                break;
            case JSON:
                System.out.println("Preparing a JSON encoder...");
                encoder = EncoderFactory.get().jsonEncoder(State.getClassSchema(), byteStream);
                break;
            default:
                break block;
        }
        // Encode the object and write it to the output stream.
        state.customEncode(encoder);
        encoder.flush();
        // Publish the encoded object as a Pub/Sub message.
        ByteString data = ByteString.copyFrom(byteStream.toByteArray());
        PubsubMessage message = PubsubMessage.newBuilder().setData(data).build();
        System.out.println("Publishing message: " + message);
        ApiFuture<String> future = publisher.publish(message);
        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.State) Encoder(org.apache.avro.io.Encoder) ByteString(com.google.protobuf.ByteString) Encoding(com.google.pubsub.v1.Encoding) Publisher(com.google.cloud.pubsub.v1.Publisher) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TopicName(com.google.pubsub.v1.TopicName)

Aggregations

ByteString (com.google.protobuf.ByteString)2 PubsubMessage (com.google.pubsub.v1.PubsubMessage)2 State (utilities.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 Encoding (com.google.pubsub.v1.Encoding)1 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)1 TopicName (com.google.pubsub.v1.TopicName)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 TimeoutException (java.util.concurrent.TimeoutException)1 Decoder (org.apache.avro.io.Decoder)1 Encoder (org.apache.avro.io.Encoder)1 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)1