/**
 * 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.spark;

import java.util.concurrent.TimeUnit;

import org.apache.pulsar.client.api.ClientConfiguration;
import org.apache.pulsar.client.api.ConsumerConfiguration;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.spark.storage.StorageLevel;
import org.apache.spark.streaming.receiver.Receiver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SparkStreamingPulsarReceiver extends Receiver<byte[]> {

    private ClientConfiguration clientConfiguration;
    private ConsumerConfiguration consumerConfiguration;
    private PulsarClient pulsarClient;
    private String url;
    private String topic;
    private String subscription;

    public SparkStreamingPulsarReceiver(ClientConfiguration clientConfiguration,
            ConsumerConfiguration consumerConfiguration, String url, String topic, String subscription) {
        this(StorageLevel.MEMORY_AND_DISK_2(), clientConfiguration, consumerConfiguration, url, topic, subscription);
    }

    public SparkStreamingPulsarReceiver(StorageLevel storageLevel, ClientConfiguration clientConfiguration,
            ConsumerConfiguration consumerConfiguration, String url, String topic, String subscription) {
        super(storageLevel);
        this.clientConfiguration = clientConfiguration;
        this.url = url;
        this.topic = topic;
        this.subscription = subscription;
        if (consumerConfiguration.getAckTimeoutMillis() == 0) {
            consumerConfiguration.setAckTimeout(60, TimeUnit.SECONDS);
        }
        consumerConfiguration.setMessageListener((consumer, msg) -> {
            try {
                store(msg.getData());
                consumer.acknowledgeAsync(msg);
            } catch (Exception e) {
                log.error("Failed to store a message : {}", e.getMessage());
            }
        });
        this.consumerConfiguration = consumerConfiguration;
    }

    public void onStart() {
        try {
            pulsarClient = PulsarClient.create(url, clientConfiguration);
            pulsarClient.subscribe(topic, subscription, consumerConfiguration);
        } catch (PulsarClientException e) {
            log.error("Failed to start subscription : {}", e.getMessage());
            restart("Restart a consumer");
        }
    }

    public void onStop() {
        try {
            if (pulsarClient != null) {
                pulsarClient.close();
            }
        } catch (PulsarClientException e) {
            log.error("Failed to close client : {}", e.getMessage());
        }
    }

    private static final Logger log = LoggerFactory.getLogger(SparkStreamingPulsarReceiver.class);
}
@@ -836,16 +836,45 @@
 spark;%0A%0A
+import java.io.Serializable;%0A
 import j
@@ -903,16 +903,16 @@
 meUnit;%0A
-
 %0Aimport 
@@ -1012,32 +1012,85 @@
 rConfiguration;%0A
+import org.apache.pulsar.client.api.MessageListener;%0A
 import org.apach
@@ -1339,16 +1339,82 @@
 ctory;%0A%0A
+import static com.google.common.base.Preconditions.checkNotNull;%0A%0A
 public c
@@ -2263,16 +2263,186 @@
 Level);%0A
+        checkNotNull(clientConfiguration, %22ClientConfiguration must not be null%22);%0A        checkNotNull(consumerConfiguration, %22ConsumerConfiguration must not be null%22);%0A
         
@@ -2720,32 +2720,32 @@
 NDS);%0A        %7D%0A
-
         consumer
@@ -2777,16 +2777,49 @@
 istener(
+(MessageListener & Serializable) 
 (consume

Make spark consumer-listener lambda Serializable (#866)