srkukarni commented on a change in pull request #1649: refactoring functions to use source interface URL: https://github.com/apache/incubator-pulsar/pull/1649#discussion_r184476490
########## File path: pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/PulsarSource.java ########## @@ -0,0 +1,117 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pulsar.functions.instance; + +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.apache.pulsar.client.api.PulsarClient; +import org.apache.pulsar.client.impl.MessageIdImpl; +import org.apache.pulsar.client.impl.TopicMessageIdImpl; +import org.apache.pulsar.client.impl.TopicMessageImpl; +import org.apache.pulsar.connect.core.Record; +import org.apache.pulsar.connect.core.Source; +import org.apache.pulsar.functions.proto.Function; + +import java.util.ArrayList; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +@Slf4j +public class PulsarSource<T> implements Source<T> { + + private PulsarClient pulsarClient; + private PulsarConfig pulsarConfig; + + @Getter + private org.apache.pulsar.client.api.Consumer inputConsumer; + + public PulsarSource(PulsarClient pulsarClient, PulsarConfig pulsarConfig) { + this.pulsarClient = pulsarClient; + this.pulsarConfig = pulsarConfig; + } + + @Override + public void open(Map<String, Object> config) throws Exception { + this.inputConsumer = this.pulsarClient.newConsumer() + .topics(new ArrayList<>(this.pulsarConfig.getTopicToSerdeMap().keySet())) + .subscriptionName(this.pulsarConfig.getSubscription()) + .subscriptionType(this.pulsarConfig.getSubscriptionType()) + .ackTimeout(1, TimeUnit.MINUTES) + .subscribe(); + } + + @Override + public Record<T> read() throws Exception { + org.apache.pulsar.client.api.Message<T> message = this.inputConsumer.receive(); + + String topicName; + String partitionId; + if (message instanceof TopicMessageImpl) { Review comment: Can you comment why you are doing the if loop? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
